Is it possible to restrict NanoHttpd to only the local host?

I am running NanoHttpd on 8080 on my local desktop. I can access the server locally in my browser at http://localhost:8080/. This part works as expected.

However, I do not want my neighbor (or, even worse, the world) to also be able to access him http://my.local.ip.add:8080/.

How can I limit it only to localhost access, so that I am the only one who can see that these pages are served by my local instance of NanoHttpd?

+4
source share
1 answer

NanoHttpd is only one source file: here is the corresponding clip:

   /**
     * Constructs an HTTP server on given hostname andport.
     */
    public NanoHTTPD(String hostname, int port) {
        this.hostname = hostname;
        this.myPort = port;
        setTempFileManagerFactory(new DefaultTempFileManagerFactory());
        setAsyncRunner(new DefaultAsyncRunner());
    }

    /**
     * Start the server.
     * @throws IOException if the socket is in use.
     */
    public void start() throws IOException {
        myServerSocket = new ServerSocket();
        myServerSocket.bind((hostname != null) ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort));

, , , new NanoHTTPD("localhost",8080) new NanoHTTPD("localhost",8080) - ( )

: , - : ^) - , , : localhost (.. "" , , ,) , , tcp NanoHttpd - , " , - . , - http, , " http://localhost: 8080 " . , , , : - , , - localhost .

+7

All Articles