Skip to content

Commit

Permalink
ensure threads are cleaned up, handle "UnknownPacket" types
Browse files Browse the repository at this point in the history
- this may help resolve onBoot issues with VPN
  • Loading branch information
n8fr8 committed Feb 16, 2022
1 parent 79aee40 commit 9507e70
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class DNSProxy {
private DatagramSocket serverSocket;
private Thread mThread;
private VpnService mVpnService;
private boolean keepRunning = false;

public DNSProxy (String localDns, int localPort, VpnService service) throws UnknownHostException, IOException {
mResolver = new SimpleResolver(localDns);
mResolver.setPort(localPort);
Expand All @@ -58,6 +60,8 @@ public void interrupt() {
}

public void stopProxy() {
keepRunning = false;

if (mThread != null) {
mThread.interrupt();
}
Expand Down Expand Up @@ -85,7 +89,9 @@ private void startProxyImpl (String serverHost, int serverPort) {

byte[] receive_data = new byte[1024];

while (true) { //waiting for a client request...
keepRunning = true;

while (keepRunning) { //waiting for a client request...
try {
//receiving the udp packet of data from client and turning them to string to print them
DatagramPacket receive_packet = new DatagramPacket(receive_data, receive_data.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public class OrbotVpnManager implements Handler.Callback {
private ParcelFileDescriptor mInterface;
private int mTorSocks = -1;
private int mTorDns = -1;
private int mTorHttp = -1;
private ProxyServer mSocksProxyServer;
private final VpnService mService;
private final SharedPreferences prefs;
private DNSProxy mDnsProxy;

private ExecutorService mExec = Executors.newFixedThreadPool(10);

private Thread mThreadPacket;
private boolean keepRunningPacket = false;

public OrbotVpnManager(OrbotService service) {
mService = service;
Expand Down Expand Up @@ -133,11 +133,11 @@ public int handleIntent(VpnService.Builder builder, Intent intent) {
int torDns = intent.getIntExtra(OrbotService.EXTRA_DNS_PORT, -1);

//if running, we need to restart
if ((torSocks != mTorSocks && torDns != mTorDns)) {
if ((torSocks != -1 && torSocks != mTorSocks
&& torDns != -1 && torDns != mTorDns)) {

mTorSocks = torSocks;
mTorDns = torDns;
mTorHttp = torHttp;

if (!mIsLollipop) {
// stopSocksBypass();
Expand Down Expand Up @@ -210,6 +210,8 @@ private void stopVPN() {
if (!mIsLollipop)
stopSocksBypass();

keepRunningPacket = false;

if (mInterface != null) {
try {
Log.d(TAG, "closing interface, destroying VPN interface");
Expand All @@ -224,8 +226,17 @@ private void stopVPN() {
}
}

if (mDnsProxy != null)
if (mDnsProxy != null) {
mDnsProxy.stopProxy();
mDnsProxy = null;
}

if (mThreadPacket != null && mThreadPacket.isAlive())
{
mThreadPacket.interrupt();
}


}

@Override
Expand Down Expand Up @@ -254,15 +265,6 @@ private synchronized void setupTun2Socks(final VpnService.Builder builder) {
builder.addRoute(defaultRoute,0);


/**
* // can't use this since Tor's HTTP port is CONNECT only and not a full PROXY for http traffic
if (mTorHttp != -1) {
//extra capability to set local http proxy
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.setHttpProxy(ProxyInfo.buildDirectProxy("127.0.0.1", 8118));
}
}**/

builder.setSession(vpnName);


Expand Down Expand Up @@ -330,26 +332,17 @@ public void writePacket(byte[] packet) {

IPtProxy.startSocks(pFlow,localhost,mTorSocks);

/**
Inet4Address loopback;
try {
loopback = (Inet4Address)Inet4Address.getByAddress(new byte[]{127,0,0,1});
} catch (UnknownHostException e) {
throw new RuntimeException();
}**/



//read packets from TUN and send to go-tun2socks
new Thread ()
mThreadPacket = new Thread ()
{
public void run ()
{

// Allocate the buffer for a single packet.
ByteBuffer buffer = ByteBuffer.allocate(32767);

while (true)
keepRunningPacket = true;
while (keepRunningPacket)
{
try {

Expand All @@ -358,22 +351,26 @@ public void run ()
{
buffer.limit(pLen);
byte[] pdata = buffer.array();
IpPacket packet = null;
Packet packet = null;
try {
packet = (IpPacket) IpSelector.newPacket(pdata,0,pdata.length);
boolean isDNS = false;
packet = (Packet) IpSelector.newPacket(pdata,0,pdata.length);

if (packet.getHeader().getProtocol()== IpNumber.UDP) {
UdpPacket up = (UdpPacket) packet.getPayload();
if (up.getHeader().getDstPort() == UdpPort.DOMAIN)
isDNS = true;
}
if (packet instanceof IpPacket) {
boolean isDNS = false;

IpPacket ipPacket = (IpPacket) packet;

if (isDNS)
mExec.execute(new RequestPacketHandler(packet, pFlow, mDnsProxy));
else
IPtProxy.inputPacket(pdata);
if (ipPacket.getHeader().getProtocol() == IpNumber.UDP) {
UdpPacket up = (UdpPacket) packet.getPayload();
if (up.getHeader().getDstPort() == UdpPort.DOMAIN)
isDNS = true;
}

if (isDNS)
mExec.execute(new RequestPacketHandler(ipPacket, pFlow, mDnsProxy));
else
IPtProxy.inputPacket(pdata);
}

} catch (IllegalRawDataException e) {
return;
Expand All @@ -387,7 +384,8 @@ public void run ()
}

}
}.start();
};
mThreadPacket.start();

} catch (Exception e) {
Log.d(TAG, "tun2Socks has stopped", e);
Expand Down

0 comments on commit 9507e70

Please sign in to comment.