Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

sockets - Cannot Get DNS Requests via Java Code in Windows 10 and DLINK DIR-615 router

So I am working on a software that will monitor(and may alter by acting as a Forrowder) all the DNS requests made by my router.

What I did?

So for first I wrote a Java code that can listens to a specific port and prints all the requests to the console[For now I just want to test with the requests].

The code is:

import java.net.*;
import java.io.*;

public class PortLogger{
    public static void main(String[] args) {
        LoggerServer loggerServer = new LoggerServer(53);
        loggerServer.start();
    }
}

class LoggerServer extends Thread{
    private int port;

    public LoggerServer(int port){
        this.port = port;
    }

    @Override
    public void run(){
        try{
            int id = 1;
            ServerSocket server = new ServerSocket(port);
            System.out.println("Server Listening at port " + port);
            Socket client;
            while(true){
                client = server.accept();
                ClientHandler clientHandler = new ClientHandler(client, id++);
                clientHandler.start();
            }
        }catch(Exception ex){
            System.out.println("Exception at Server : 1 :: EX = " + ex);
        }
    }
}

class ClientHandler extends Thread{
    private Socket client;
    private int id;

    public ClientHandler(Socket client, int id){
        this.client = client;
        this.id = id;
    }

    @Override
    public void run(){
        try {
            String data = "";
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            while(true){
                data = reader.readLine();
                if(data.length() > 0){
                    System.out.println("Client : " + id + " :: " + data);
                }
            }
        }catch(Exception ex){
            System.out.println("Exception at Client : " + id + " :: EX = " + ex);
        }
    }
}

The sole propose of this code for now is to Show me all the requests made to the server.

I know that I also have to change the DNS Server in my router for this.

So, for that I first tried by going to internet setup and put the local IP of my computer as DNS server.

But it was showing :

DNS IP and LAN IP must be on different networks!

IMAGE OF ROUTER NOT ALLOWING

But I found another way to do it.

It is as follows:

I went to the setup wizard of the router and the set the DNS Server to the same IP.

Surprisingly this worked!

[I have no idea whether this is a bug in the D-Link Firmware or not.

IP SUCESSFULLY ADDED

I have also added an exception to allow all request both inbound and outbound to port 53.

What is the problem?

So now the problem is that even after successfully changing the DNS to my servers. There seemed to be no requests at all to the console. I tried a lot but nothing.

I checked that the program was working fine by voluntarily sending request to it using telnet?

Now am I doing anything wrong or there is some bug with the router(its is a old one).

NOTE: The black lines on the images are just to hide my public IP address nothing special.

EDIT: I tried a few more times then found that websites were not opening when I changed the DNS in my router but still nothing in the console!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

While it is difficult to give you a complete answer why your application doesn't work I can suggest some ways to investigate:

  1. Port 53 is a privileged port. This means on Linux binding to that port requires root privileges and the application will throw an exception due to 'permission denied' if executed as a 'normal' user. As you are using Windows I don't know what it does if you try to bind as a 'normal' user, or you might be executing as an Admin user (or whatever the equivalent of 'root' is in Windows) and you don't know it. It might even just silently fail i.e. appear to bind when in fact it hasn't and no data is passed through you your application. As an aside, defaulting to 'root' as the default execution user in Linux is not the norm because it's insecure and most Linux distributions if not all do not allow this by default i.e. you can have this but you have to tell the distribution this is what you intend during installation. I'll let you come to your own conclusions what stance Windows takes for making users 'admin'...
  2. In a scenario such as this if it were me I would immediately go to some networking tools to see what is happening. On Linux this is tcpdump or Wireshark. You can also get Wireshark for Windows as it's a GUI application. This will let you monitor and filter network traffic and so will be independent of your application. You can filter by source or destination address and/or port number.
  3. I would leave the DNS setting alone in the router and change the DNS settings in one machine first, call it the test client, and set its DNS address to the machine where your application is running. Using tcpdump or Wireshark you can then make requests on your test_client e.g. browser requests and see the resulting network traffic.
  4. You never mentioned if after changing your router's DNS settings all browser requests from clients fail. This is what I would expect to see if your router can no longer get a name resolution. However there maybe some DNS caching going on in your clients so you may appear to get successful DNS requests on your test_client. Again look at network traffic or use a Linux client which will provide you with much better networking tools.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...