-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,529 additions
and
2,670 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
app/src/main/java/io/github/xsocks/service/XsocksVpnThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package io.github.xsocks.service; | ||
|
||
|
||
import android.net.LocalServerSocket; | ||
import android.net.LocalSocket; | ||
import android.net.LocalSocketAddress; | ||
import android.util.Log; | ||
|
||
import java.io.File; | ||
import java.io.FileDescriptor; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class XsocksVpnThread extends Thread { | ||
private String TAG = "XsocksVpnService"; | ||
|
||
private volatile boolean isRunning = true; | ||
private volatile LocalServerSocket serverSocket = null; | ||
|
||
private XsocksVpnService vpnService; | ||
|
||
public XsocksVpnThread(XsocksVpnService vpnService) { | ||
this.vpnService = vpnService; | ||
} | ||
|
||
private void closeServerSocket() { | ||
if (serverSocket != null) { | ||
try { | ||
serverSocket.close(); | ||
} catch (IOException e) { | ||
// ignore | ||
} | ||
serverSocket = null; | ||
} | ||
} | ||
|
||
public void stopThread() { | ||
isRunning = false; | ||
closeServerSocket(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
String PATH = "/data/data/io.github.xsocks/protect_path"; | ||
|
||
try { | ||
new File(PATH).delete(); | ||
} catch (Exception e) { | ||
} | ||
|
||
try { | ||
LocalSocket localSocket = new LocalSocket(); | ||
localSocket.bind(new LocalSocketAddress(PATH, LocalSocketAddress.Namespace.FILESYSTEM)); | ||
serverSocket = new LocalServerSocket(localSocket.getFileDescriptor()); | ||
|
||
} catch (IOException e) { | ||
Log.e(TAG, "unable to bind", e); | ||
return; | ||
} | ||
|
||
ExecutorService pool = Executors.newFixedThreadPool(4); | ||
|
||
while (isRunning) { | ||
try { | ||
LocalSocket socket = serverSocket.accept(); | ||
|
||
pool.execute(() -> { | ||
try { | ||
InputStream input = socket.getInputStream(); | ||
OutputStream output = socket.getOutputStream(); | ||
|
||
input.read(); | ||
|
||
FileDescriptor[] fds = socket.getAncillaryFileDescriptors(); | ||
|
||
if (fds != null && fds.length > 0) { | ||
Method getInt = FileDescriptor.class.getDeclaredMethod("getInt$"); | ||
int fd = (int) getInt.invoke(fds[0]); | ||
boolean ret = vpnService.protect(fd); | ||
|
||
io.github.xsocks.System.jniclose(fd); | ||
|
||
output.write(ret ? 0 : 1); | ||
|
||
input.close(); | ||
output.close(); | ||
} | ||
|
||
} catch (Exception e) { | ||
Log.e(TAG, "Error when protect socket", e); | ||
} | ||
|
||
// close socket | ||
try { | ||
socket.close(); | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
}); | ||
|
||
} catch (IOException e) { | ||
Log.e(TAG, "Error when accept socket", e); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.