明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

完成具有動態(tài)主機IP的域名解析

[摘要]作者: wenhuah來源:www.chinaaid.com.cn目的利用一臺INTERNET DNS HOST 將域名定向到家中(個人)的主機上. 目前CABLEMODEM已經(jīng)很快了,而且有動態(tài)IP,所以就有了將域名解析到個人機器的上可能. 只要通過很簡單的設置就可以搞定.例如你有一個域名叫 ...
作者: wenhuah

來源:www.chinaaid.com.cn

目的利用一臺INTERNET DNS HOST 將域名定向到家中(個人)的主機上.
目前CABLEMODEM已經(jīng)很快了,而且有動態(tài)IP,所以就有了將域名解析到個人機器的上可能.
只要通過很簡單的設置就可以搞定.

例如你有一個域名叫 yourdomain.com ,你可以將此域名解析任意的主機.


1.服務器 (IP: A.A.A.A)  啟動DNS動態(tài)解析的一個小進程.

   yourdomain.com的域名文件為 /var/named/yourdomain.com
   再建個/var/named/yourdomain.com.static (模板文件可以用來生成yourdomain.com的)

   (JDK1.3 , linux ,找個網(wǎng)管幫一下吧:)
=================================================
#  原代碼如下: 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);


       InetAddress localHostAddress =InetAddress.getLocalHost() ;  //get LocalHost Ip
       out.println("LAguest");
       out.println("LByourdomain.com");
       out.println("LC" + localHostAddress.getHostAddress()  );

       out.close();
       s.close();

  }
       catch (IOException e)
   { System.out.println(e);
   }
   }
}

=========================================================
dns 樣本文件: ( yourdomain.com.static)
@ ns.dnsserver.com  xxxxx
                   xxxx
......
host       IN A 127.0.0.1           // 此處就是要更改的地方
www        IN CNAME host
...