实现具有动态主机IP的域名解析[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 ju_feng 的 blog


[ 作者: wenhuah   添加时间: 2001-5-2 12:24:33 ]


来源:www.chinaaid.com.cn

目的利用一台internet dns host 将域名定向到家中(个人)的主机上.
 目前cablemodem已经很快了,而且有动态ip,所以就有了将域名解析到个人机器的上可能.
 只要通过很简单的设置就可以搞定.

例如你有一个域名叫 yourdomain.com ,你可以将此域名解析任意的主机.


 1.服务器 (ip: a.a.a.a)  启动dns动态解析的一个小进程.

   yourdomain.com的域名文件为 /var/named/yourdomain.com
   再建个/var/named/yourdomain.com.static (模板文件可以用来生成yourdomain.com的)

   (jdk1.3 , linux ,找个网管帮一下吧:)
=================================================
#  原代码如下: threaddnsreloadserver.java
import java.io.*;
import java.net.*;

public class threaddnsreloadserver
{   public static void main(string[] args)
{ int i=1;
 try
 {  serversocket s=new serversocket(8189);
for (;;)
{  socket incoming =s.accept();
  system.out.println("accept new client: " + i);
  new threaddnsreloadhandle(incoming,i).start();
  i++ ;
}
  }
  catch (exception e)
  { system.out.println(e);
  }
}
}

class threaddnsreloadhandle extends thread
{ public threaddnsreloadhandle(socket i,int c)
 { incoming= i; counter=c; }

 public void run()
 { try
{ bufferedreader in=new bufferedreader(new inputstreamreader(incoming.getinputstream()));

     string user="unkown";
     string dnsfilepath="/var/named/";
     string dnsfile="yourdomain.com";
     string hostip="127.0.0.1";
     boolean user_validated=true ;
     boolean done=false;

 while ( !done )
 {   string str=in.readline();
             //validate user;
             if ( str ==null ) done=true;
             else
 {
 str=str.trim();
         if ( str.substring(0,2).equals("la") )
 { if (str.length() >2)  user=str.substring(2);
 }
 if ( str.substring(0,2).equals("lb") )
 { if (str.length() >2) dnsfile=str.substring(2);
 }
 if ( str.substring(0,2).equals("lc") )
 { if (str.length() >2) hostip=str.substring(2);
 }

 if ( str.trim().equals("bye")) done = true;
         }

 }
 incoming.close();
       //加入用户验证.
       //将服务器中的yourdomain.com.cn.static文件写入yourdomain.com.cn中
      string dnstmp =dnsfilepath + dnsfile +".static";
      bufferedreader sin=new bufferedreader(new filereader(dnstmp));
      printwriter sout=new printwriter(new filewriter(dnsfilepath + dnsfile) , true);
      string s;
      while ((s=sin.readline()) != null)
         {
           if (s.startswith("host"))
           { s="host in a " + hostip;
             sout.println(s);
             s="@ in a " + hostip;
            }
             sout.println(s);
         }
         //system.out.println("user:" + user );
         //system.out.println("dnsfile:" + dnsfile );
         //system.out.println("hostip:" + hostip );

         //ndc reload
         string command="ndc reload " + dnsfile ;
         java.lang.runtime.getruntime().exec(command);

}
catch ( exception e)
{ system.out.println(e);
}
}

private socket incoming;
private int counter;

}

2.客户机(windows / linux)
==============================================================
# 客户端每次启动时加入到开机启动(或进程中): socktest.java
import java.io.*;
import java.net.*;

public class sockettest
{  public static void main(string[] args)
  { try
      { socket s=new socket("a.a.a.a",8189);
        bufferedreader in=new bufferedreader(new inputstreamreader(s.getinputstream()));
        printwriter out=new printwriter(s.getoutputstream(),true);


本文关键:IP
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top