heiher / sockstun

A simple and lightweight VPN over socks5 proxy (tun2socks) for Android.
MIT License
213 stars 58 forks source link

the ServerSocket run on this app can not accept connection #17

Closed gitcfly closed 8 months ago

gitcfly commented 8 months ago

Thanks for your project. When I want to start ServerSocket locally in this app to receive requests, I can never receive the client connection. Below is the code I added based on this project:

// step1 :start a local serverSocket for accpect tun2socks connection in this app localSocksServer = new ServerSocket(port); Log.e("LocalSocks Server", "Start run on port: " + port); while (TProxyService.shouldRunLocalSocksServer) { Socket client = localSocksServer.accept(); // here can not accept a connection? why ? Log.e("LocalSocks Server", "Accept client connection"); vpnService.protect(client); new Thread(new Runnable() { @Override public void run() { try { OutputStream clientOuts = client.getOutputStream(); int readN = 0; byte[] bytes = new byte[65535]; while ((readN = ins.read(bytes)) != -1) { Log.e("Socks Server", "Received Data=" + new String(bytes, 0, readN)); } } catch (Exception e) { e.printStackTrace(); } } }).start(); }

// step2: begin tun2socks on this app builder.setSession(session); tunFd = builder.establish(); if (tunFd == null) { stopSelf(); return; }

    /* TProxy */
    File tproxy_file = new File(getCacheDir(), "tproxy.conf");
    try {
        tproxy_file.createNewFile();
        FileOutputStream fos = new FileOutputStream(tproxy_file, false);

        String tproxy_conf = "misc:\n" +
                "  task-stack-size: " + prefs.getTaskStackSize() + "\n" +
                "tunnel:\n" +
                "  mtu: " + prefs.getTunnelMtu() + "\n";

        tproxy_conf += "socks5:\n" +
                "  port: " + prefs.getSocksPort() + "\n" +
                "  address: '" + prefs.getSocksAddress() + "'\n" +
                "  udp: '" + (prefs.getUdpInTcp() ? "tcp" : "udp") + "'\n";

        if (!prefs.getSocksUsername().isEmpty() &&
                !prefs.getSocksPassword().isEmpty()) {
            tproxy_conf += "  username: '" + prefs.getSocksUsername() + "'\n";
            tproxy_conf += "  password: '" + prefs.getSocksPassword() + "'\n";
        }
        Log.e("LocalSocks Server", "Get config="+tproxy_conf);
        fos.write(tproxy_conf.getBytes());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    TProxyStartService(tproxy_file.getAbsolutePath(), tunFd.getFd());

    String channelName = "socks5";
heiher commented 8 months ago

Hmm. I'm not familiar with java.