-
Hi, Developer I am trying to create a simple VPN connection on Android and use your native program to forward the traffic, The following code inherits the Android VpnService. I call the native programs in Am I missing something? Need your help, Thank you! private static final int VPN_MTU = 1500;
private static final String PRIVATE_VLAN4_CLIENT = "172.19.0.1";
private static final String PRIVATE_VLAN4_ROUTER = "172.19.0.2";
private static final String PRIVATE_VLAN6_CLIENT = "fdfe:dcba:9876::1";
private static final String PRIVATE_VLAN6_ROUTER = "fdfe:dcba:9876::2";
int portLocalDns = 5450;
int portProxy = 1080;
// start shadowsocks
Process startShadowsocks() {
try {
String cmd = new File(getApplicationInfo().nativeLibraryDir, "libsslocal.so").getAbsolutePath()
+ " --local-addr 127.0.0.1:1080"
+ " --udp-bind-addr 127.0.0.1:1080"
+ " --server-addr 123.123.123.123:1234" // server address and port
+ " -k password" // server password
+ " -m rc4-md5"
+ " --dns-addr 127.0.0.1:" + portLocalDns
+ " --local-dns-addr local_dns_path"
+ " --remote-dns-addr 8.8.8.8:53"
+ " --vpn"
+ " -U";
return Runtime.getRuntime().exec(cmd, null, getApplicationContext().getNoBackupFilesDir());
} catch (Exception exception) {
}
return null;
}
ParcelFileDescriptor startVpn() {
try {
Builder builder = new Builder()
.setSession("session")
.setMtu(VPN_MTU)
.addAddress(PRIVATE_VLAN4_CLIENT, 30)
.addDnsServer(PRIVATE_VLAN4_ROUTER)
.addRoute(PRIVATE_VLAN4_ROUTER, 32);
ParcelFileDescriptor pfd = builder.establish();
setUnderlyingNetworks(null);
// start tun2socks
String cmd = new File(getApplicationInfo().nativeLibraryDir, "libtun2socks.so").getAbsolutePath()
+ " --netif-ipaddr " + PRIVATE_VLAN4_ROUTER
+ " --socks-server-addr 127.0.0.1:" + portProxy
+ " --tunmtu " + VPN_MTU
+ " --sock-path sock_path"
+ " --dnsgw 127.0.0.1:" + portLocalDns
+ " --loglevel warning"
+ " --enable-udprelay";
Runtime.getRuntime().exec(cmd, null, getApplicationContext().getNoBackupFilesDir());
return pfd;
} catch (Exception exception) {
}
return null;
}
private boolean sendFd(FileDescriptor fd) {
int tries = 0;
String path = new File(getApplicationContext().getNoBackupFilesDir(), "sock_path").getAbsolutePath();
while (true) {
try {
Thread.sleep(100);
LocalSocket localSocket = new LocalSocket();
localSocket.connect(new LocalSocketAddress(path, LocalSocketAddress.Namespace.FILESYSTEM));
localSocket.setFileDescriptorsForSend(new FileDescriptor[]{fd});
localSocket.getOutputStream().write(42);
return true;
} catch (Exception exception) {
if (tries > 5) {
return false;
}
tries += 1;
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (startShadowsocks() == null) {
stopSelf();
return START_NOT_STICKY;
}
ParcelFileDescriptor vpnInterface = startVpn();
if (vpnInterface == null) {
stopSelf();
return START_NOT_STICKY;
}
if (!sendFd(vpnInterface.getFileDescriptor())) {
stopSelf();
return START_NOT_STICKY;
}
// protect ss traffic
new Thread(() -> {
try {
File socketFile = new File(getApplicationContext().getNoBackupFilesDir(), "protect_path");
LocalSocket localSocket = new LocalSocket();
localSocket.bind(new LocalSocketAddress(socketFile.getAbsolutePath(), LocalSocketAddress.Namespace.FILESYSTEM));
LocalServerSocket serverSocket = new LocalServerSocket(localSocket.getFileDescriptor());
LocalSocket socket = serverSocket.accept();
if (socket.getInputStream().read() == -1) {
return;
}
FileDescriptor[] fileDescriptors = socket.getAncillaryFileDescriptors();
FileDescriptor fd = fileDescriptors[0];
@SuppressLint("DiscouragedPrivateApi")
Method getInt = FileDescriptor.class.getDeclaredMethod("getInt$");
int fdsslocal = (Integer) getInt.invoke(fd);
boolean protected = protect(fdsslocal); // returns true
} catch (Exception exception) {
}
}).start();
return START_STICKY;
} |
Beta Was this translation helpful? Give feedback.
Answered by
xinlake
Feb 8, 2021
Replies: 1 comment 1 reply
-
This problem has been resolved and it is related to DNS. Thank you. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
xinlake
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
This problem has been resolved and it is related to DNS. Thank you.