Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bitmold committed Feb 16, 2022
1 parent 4231a8f commit e79b3b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 234 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package org.torproject.android.service.vpn;

import static org.torproject.android.service.vpn.OrbotVpnManager.FAKE_DNS;
import static org.torproject.android.service.vpn.OrbotVpnManager.FAKE_DNS_HEX;

import android.net.VpnService;
import android.util.Log;

import org.pcap4j.packet.IllegalRawDataException;
import org.pcap4j.packet.IpPacket;
import org.pcap4j.packet.IpV4Packet;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.UdpPacket;
import org.pcap4j.packet.namednumber.IpNumber;
import org.pcap4j.packet.namednumber.UdpPort;
import org.xbill.DNS.Message;
import org.xbill.DNS.SimpleResolver;

Expand All @@ -21,8 +10,6 @@
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;

public class DNSProxy {

Expand All @@ -31,13 +18,11 @@ public class DNSProxy {
private final SimpleResolver mResolver;
private DatagramSocket serverSocket;
private Thread mThread;
private VpnService mVpnService;
private boolean keepRunning = false;

public DNSProxy (String localDns, int localPort, VpnService service) throws UnknownHostException, IOException {
public DNSProxy (String localDns, int localPort) throws IOException {
mResolver = new SimpleResolver(localDns);
mResolver.setPort(localPort);
mVpnService = service;
}

public void startProxy (String serverHost, int serverPort) {
Expand Down Expand Up @@ -68,24 +53,20 @@ public void stopProxy() {
}

public byte[] processDNS (byte[] payload) throws IOException {

Message msgRequest = new Message(payload);

if (msgRequest.getQuestion() != null) {
Message queryMessage = Message.newQuery(msgRequest.getQuestion());
Message answer = mResolver.send(queryMessage);
byte[] respData = answer.toWire();
return respData;

return answer.toWire();
}
else
return null;

return null;
}

private void startProxyImpl (String serverHost, int serverPort) {
try {
serverSocket = new DatagramSocket(serverPort, InetAddress.getByName(serverHost));
// mVpnService.protect(serverSocket);

byte[] receive_data = new byte[1024];

Expand All @@ -98,7 +79,6 @@ private void startProxyImpl (String serverHost, int serverPort) {
serverSocket.receive(receive_packet);

Message msgRequest = new Message(receive_data);
String given_hostname = msgRequest.getQuestion().getName().toString();
Message queryMessage = Message.newQuery(msgRequest.getQuestion());

Message answer = mResolver.send(queryMessage);
Expand All @@ -114,9 +94,6 @@ private void startProxyImpl (String serverHost, int serverPort) {
int client_port = receive_packet.getPort();
DatagramPacket send_packet = new DatagramPacket(send_data, send_data.length, client_address, client_port);
serverSocket.send(send_packet);

// byte[] pData = send_packet.getData();

}
catch (SocketException e) {
if (e.toString().contains("Socket closed")) {
Expand All @@ -129,22 +106,4 @@ private void startProxyImpl (String serverHost, int serverPort) {
Log.e("DNSProxy","error running DNSProxy server",e);
}
}



public boolean isDNS (IpPacket p)
{

if (p.getHeader().getProtocol()== IpNumber.UDP) {
UdpPacket up = (UdpPacket) p.getPayload();
if (up.getHeader().getDstPort() == UdpPort.DOMAIN)
return true;
}

return false;

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.torproject.android.service.vpn;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
Expand All @@ -27,28 +26,19 @@
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.system.OsConstants;
import android.util.Log;
import android.widget.Toast;

import com.runjva.sourceforge.jsocks.protocol.ProxyServer;
import com.runjva.sourceforge.jsocks.server.ServerAuthenticatorNone;

import org.pcap4j.packet.DnsPacket;
import org.pcap4j.packet.IllegalRawDataException;
import org.pcap4j.packet.IpPacket;
import org.pcap4j.packet.IpSelector;
import org.pcap4j.packet.IpV4Packet;
import org.pcap4j.packet.IpV4Rfc791Tos;
import org.pcap4j.packet.IpV6Packet;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.TcpPacket;
import org.pcap4j.packet.UdpPacket;
import org.pcap4j.packet.UnknownPacket;
import org.pcap4j.packet.namednumber.IpNumber;
import org.pcap4j.packet.namednumber.IpVersion;
import org.pcap4j.packet.namednumber.UdpPort;
import org.pcap4j.util.Inet4NetworkAddress;
import org.torproject.android.service.OrbotConstants;
import org.torproject.android.service.OrbotService;
import org.torproject.android.service.TorServiceConstants;
Expand All @@ -58,14 +48,9 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -75,7 +60,6 @@
import static org.torproject.android.service.TorServiceConstants.ACTION_STOP_VPN;

import androidx.annotation.ChecksSdkIntAtLeast;
import androidx.annotation.Nullable;

import IPtProxy.IPtProxy;
import IPtProxy.PacketFlow;
Expand All @@ -97,7 +81,7 @@ public class OrbotVpnManager implements Handler.Callback {
private final SharedPreferences prefs;
private DNSProxy mDnsProxy;

private ExecutorService mExec = Executors.newFixedThreadPool(10);
private final ExecutorService mExec = Executors.newFixedThreadPool(10);
private Thread mThreadPacket;
private boolean keepRunningPacket = false;

Expand Down Expand Up @@ -231,12 +215,9 @@ private void stopVPN() {
mDnsProxy = null;
}

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


}

@Override
Expand Down Expand Up @@ -290,7 +271,7 @@ private synchronized void setupTun2Socks(final VpnService.Builder builder) {
// Allow applications to bypass the VPN
builder.allowBypass();
// Explictly allow both families, so we do not block
// Explicitly allow both families, so we do not block
// traffic for ones without DNS servers (issue 129).
builder.allowFamily(OsConstants.AF_INET);
builder.allowFamily(OsConstants.AF_INET6);
Expand All @@ -303,8 +284,7 @@ private synchronized void setupTun2Socks(final VpnService.Builder builder) {
.setBlocking(true)
.establish();
}
else
{
else {
mInterface = builder.setSession(mSessionName)
.setConfigureIntent(null) // previously this was set to a null member variable
.establish();
Expand All @@ -314,29 +294,22 @@ private synchronized void setupTun2Socks(final VpnService.Builder builder) {
FileInputStream fis = new FileInputStream(mInterface.getFileDescriptor());
DataOutputStream fos = new DataOutputStream(new FileOutputStream(mInterface.getFileDescriptor()));

mDnsProxy = new DNSProxy(localhost, mTorDns, mService);
// mDnsProxy.startProxy(localhost, dnsProxyPort);
mDnsProxy = new DNSProxy(localhost, mTorDns);

//write packets back out to TUN
PacketFlow pFlow = new PacketFlow() {
@Override
public void writePacket(byte[] packet) {
try {
fos.write(packet);
} catch (IOException e) {
Log.e(TAG, "error writing to VPN fd", e);

}
PacketFlow pFlow = (PacketFlow) packet -> {
try {
fos.write(packet);
} catch (IOException e) {
Log.e(TAG, "error writing to VPN fd", e);
}
};

IPtProxy.startSocks(pFlow,localhost,mTorSocks);

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

// Allocate the buffer for a single packet.
ByteBuffer buffer = ByteBuffer.allocate(32767);
Expand All @@ -351,22 +324,13 @@ public void run ()
{
buffer.limit(pLen);
byte[] pdata = buffer.array();
Packet packet = null;
Packet packet;
try {
packet = (Packet) IpSelector.newPacket(pdata,0,pdata.length);

if (packet instanceof IpPacket) {
boolean isDNS = false;

IpPacket ipPacket = (IpPacket) packet;

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

if (isDNS)
if (isPacketDNS(ipPacket))
mExec.execute(new RequestPacketHandler(ipPacket, pFlow, mDnsProxy));
else
IPtProxy.inputPacket(pdata);
Expand All @@ -382,7 +346,6 @@ public void run ()
Log.d(TAG, "error reading from VPN fd: " + e.getLocalizedMessage());
}
}

}
};
mThreadPacket.start();
Expand All @@ -392,6 +355,14 @@ public void run ()
}
}

private static boolean isPacketDNS(IpPacket p) {
if (p.getHeader().getProtocol()== IpNumber.UDP) {
UdpPacket up = (UdpPacket) p.getPayload();
return up.getHeader().getDstPort() == UdpPort.DOMAIN;
}
return false;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void doLollipopAppRouting(VpnService.Builder builder) throws NameNotFoundException {
ArrayList<TorifiedApp> apps = TorifiedApp.getApps(mService, prefs);
Expand Down Expand Up @@ -419,5 +390,4 @@ private void doLollipopAppRouting(VpnService.Builder builder) throws NameNotFoun
public boolean isStarted() {
return isStarted;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void run() {
.dontFragmentFlag(ipPacket.getHeader().getDontFragmentFlag())
.reservedFlag(ipPacket.getHeader().getReservedFlag())
.moreFragmentFlag(ipPacket.getHeader().getMoreFragmentFlag())
.ttl(new Integer(64).byteValue())
.ttl(Integer.valueOf(64).byteValue())
.payloadBuilder(udpBuilder);

respPacket = ipv4Builder.build();
Expand Down
Loading

0 comments on commit e79b3b3

Please sign in to comment.