Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support read from FileDescriptor #71

Closed
kaisar945 opened this issue Sep 30, 2021 · 7 comments
Closed

Support read from FileDescriptor #71

kaisar945 opened this issue Sep 30, 2021 · 7 comments
Labels

Comments

@kaisar945
Copy link

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

@xjasonlyu
Copy link
Owner

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

@nicolas-yangli
Copy link

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
Copy link
Owner

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

Keep me updated if you encounter any problems.

@github-actions
Copy link

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
Copy link

marco20240618 commented Apr 27, 2022

@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();
  1. 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);
  1. 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
Copy link
Author

Thanks friend😄

@huqiyu
Copy link

huqiyu commented May 24, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants