xjasonlyu / tun2socks

tun2socks - powered by gVisor TCP/IP stack
https://github.com/xjasonlyu/tun2socks/wiki
GNU General Public License v3.0
3.14k stars 433 forks source link

Support read from FileDescriptor #71

Closed kaisar945 closed 2 years ago

kaisar945 commented 3 years ago

I don't know how to use this project in android please help me thanks

xjasonlyu commented 3 years ago

I'm not familiar with Android, but try to use tun instead of fd.

nicolas-yangli commented 2 years ago

I'm not familiar with Android, but try to use tun instead of fd.

tun cannot be open(2) directly in android without root privilege, but an fd to tun can be obtained from android framework.

xjasonlyu commented 2 years ago

@kaisar945 FD based device has been added experimentally but not fully tested, see f6ba31f121405b3b746a6980f6b3723c908f71b9 for details.

Keep me updated if you encounter any problems.

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days

marco20240618 commented 2 years ago

@kaisar945 FD based device has been added experimentally but not fully tested, see f6ba31f for details.

Keep me updated if you encounter any problems.

But how to use it?

In badvpn([https://github.com/ambrop72/badvpn]()), I see this command tun2socks --netif-ipaddr 26.26.26.2 --netif-netmask 255.255.255.0 --socks-server-addr 127.0.0.1:1080 --tunmtu 1500 --loglevel notice --enable-udprelay --sock-path /tmp/sock_path

and then send the file desriptor of the tun0 device to --sock-path

To be more specific, this is how badvpn-tun2socks works in Android.

  1. create a tun device
    android.net.VpnService.Builder builder = new android.net.VpnService.Builder()
                    .setMtu(1500)
                    .addAddress("26.26.26.1", 24)
                    .addDnsServer("8.8.8.8")
                    .addRoute("0.0.0.0", 0);
    mInterface = builder.establish();
  2. run tun2socks
    String tun2socks_command = String.format(java.util.Locale.US, "%s/libtun2socks.so --netif-ipaddr 26.26.26.2 --netif-netmask 255.255.255.0 --socks-server-addr 127.0.0.1:1080 --tunmtu 1500 --loglevel notice --enable-udprelay --sock-path %s/sock_path", this.getApplicationInfo().nativeLibraryDir, this.getApplicationInfo().dataDir);
    Runtime.getRuntime().exec(tun2socks_command);
  3. send file descriptor of tun0 to sock-path
            int tries = 0;
            while (true) {
                try {
                    Thread.sleep(1000L * tries);
                    android.net.LocalSocket ls = new android.net.LocalSocket();
                    ls.connect(new android.net.LocalSocketAddress(getApplicationInfo().dataDir + "/sock_path", android.net.LocalSocketAddress.Namespace.FILESYSTEM));
                    ls.setFileDescriptorsForSend(new java.io.FileDescriptor[]{mInterface.getFileDescriptor()});
                    ls.getOutputStream().write(42);
                    android.widget.Toast.makeText(getApplicationContext(), "Start Service", android.widget.Toast.LENGTH_SHORT).show();
                    break;
                } catch (Exception e) {
                    if (tries > 5) {
                        break;
                    }
                    ++tries;
                    Util.myLog(this, "fd send error, tries: " + tries);
                }
            }
kaisar945 commented 2 years ago

Thanks friend😄

huqiyu commented 1 year ago

Engine.stop() will report an error when restarted after disconnection GoLog: fdsan: attempted to close file descriptor 160, expected to be unowned, actually owned by ParcelFileDescriptor 0xe4afcec