Commit c486fd75 authored by Max Lv's avatar Max Lv

update tun2socks

parent b3e3cb07
global { global {
perm_cache = 2048; perm_cache = 2048;
cache_dir = "/data/data/com.github.shadowsocks"; cache_dir = "/data/data/com.github.shadowsocks";
server_ip = 127.0.0.1; server_ip = 0.0.0.0;
server_port = 8153; server_port = 8153;
query_method = tcp_only; query_method = tcp_only;
run_ipv4 = on; run_ipv4 = on;
......
...@@ -209,8 +209,8 @@ LinkedList1 tcp_clients; ...@@ -209,8 +209,8 @@ LinkedList1 tcp_clients;
int num_clients; int num_clients;
#ifdef ANDROID #ifdef ANDROID
// IP address of dnsgw // Address of dnsgw
BIPAddr dnsgw; BAddr dnsgw;
#endif #endif
static void terminate (void); static void terminate (void);
...@@ -897,20 +897,6 @@ int process_arguments (void) ...@@ -897,20 +897,6 @@ int process_arguments (void)
{ {
ASSERT(!password_file_contents) ASSERT(!password_file_contents)
#ifdef ANDROID
// resolve dnsgw ipaddr
if (options.dnsgw) {
if (!BIPAddr_Resolve(&dnsgw, options.dnsgw, 0)) {
BLog(BLOG_ERROR, "dnsgw ipaddr: BIPAddr_Resolve failed");
return 0;
}
if (dnsgw.type != BADDR_TYPE_IPV4) {
BLog(BLOG_ERROR, "dnsgw ipaddr: must be an IPv4 address");
return 0;
}
}
#endif
// resolve netif ipaddr // resolve netif ipaddr
if (!BIPAddr_Resolve(&netif_ipaddr, options.netif_ipaddr, 0)) { if (!BIPAddr_Resolve(&netif_ipaddr, options.netif_ipaddr, 0)) {
BLog(BLOG_ERROR, "netif ipaddr: BIPAddr_Resolve failed"); BLog(BLOG_ERROR, "netif ipaddr: BIPAddr_Resolve failed");
...@@ -978,6 +964,20 @@ int process_arguments (void) ...@@ -978,6 +964,20 @@ int process_arguments (void)
} }
} }
#ifdef ANDROID
// resolve dnsgw ipaddr
if (options.dnsgw) {
if (!BAddr_Parse2(&dnsgw, options.dnsgw, NULL, 0, 0)) {
BLog(BLOG_ERROR, "dnsgw addr: BAddr_Parse2 failed");
return 0;
}
if (dnsgw.type != BADDR_TYPE_IPV4) {
BLog(BLOG_ERROR, "dnsgw addr: must be an IPv4 address");
return 0;
}
}
#endif
return 1; return 1;
} }
...@@ -1227,11 +1227,12 @@ int process_device_dns_packet (uint8_t *data, int data_len) ...@@ -1227,11 +1227,12 @@ int process_device_dns_packet (uint8_t *data, int data_len)
} }
// build IP header // build IP header
ipv4_header.destination_address = dnsgw.ipv4; ipv4_header.destination_address = dnsgw.ipv4.ip;
ipv4_header.checksum = hton16(0); ipv4_header.checksum = hton16(0);
ipv4_header.checksum = ipv4_checksum(&ipv4_header, NULL, 0); ipv4_header.checksum = ipv4_checksum(&ipv4_header, NULL, 0);
// build UDP header // build UDP header
udp_header.dest_port = hton16(dnsgw.ipv4.port);
udp_header.checksum = hton16(0); udp_header.checksum = hton16(0);
udp_header.checksum = udp_checksum(&udp_header, data, data_len, udp_header.checksum = udp_checksum(&udp_header, data, data_len,
ipv4_header.source_address, ipv4_header.destination_address); ipv4_header.source_address, ipv4_header.destination_address);
......
...@@ -50,6 +50,7 @@ import android.net.VpnService ...@@ -50,6 +50,7 @@ import android.net.VpnService
import org.apache.http.conn.util.InetAddressUtils import org.apache.http.conn.util.InetAddressUtils
import android.os.Message import android.os.Message
import scala.Some import scala.Some
import scala.concurrent.ops._
object ShadowVpnService { object ShadowVpnService {
def isServiceStarted(context: Context): Boolean = { def isServiceStarted(context: Context): Boolean = {
...@@ -136,18 +137,20 @@ class ShadowVpnService extends VpnService { ...@@ -136,18 +137,20 @@ class ShadowVpnService extends VpnService {
} }
def startShadowsocksDaemon() { def startShadowsocksDaemon() {
new Thread { spawn {
override def run() {
val cmd: String = (BASE + val cmd: String = (BASE +
"shadowsocks -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -f " + "shadowsocks -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -f " +
BASE + BASE +
"shadowsocks.pid") "shadowsocks.pid")
.format(config.proxy, config.remotePort, config.localPort, config.sitekey, .format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod)
config.encMethod)
Log.d(TAG, cmd) Log.d(TAG, cmd)
System.exec(cmd) System.exec(cmd)
} }
}.start() }
def startDnsDaemon() {
val cmd: String = BASE + "pdnsd -c " + BASE + "pdnsd.conf"
Utils.runCommand(cmd)
} }
def getVersionName: String = { def getVersionName: String = {
...@@ -182,9 +185,8 @@ class ShadowVpnService extends VpnService { ...@@ -182,9 +185,8 @@ class ShadowVpnService extends VpnService {
config = Extra.get(intent) config = Extra.get(intent)
new Thread(new Runnable {
def run() {
spawn {
killProcesses() killProcesses()
// Resolve server address // Resolve server address
...@@ -218,7 +220,6 @@ class ShadowVpnService extends VpnService { ...@@ -218,7 +220,6 @@ class ShadowVpnService extends VpnService {
} }
handler.sendEmptyMessageDelayed(MSG_CONNECT_FINISH, 300) handler.sendEmptyMessageDelayed(MSG_CONNECT_FINISH, 300)
} }
}).start()
} }
def waitForProcess(name: String): Boolean = { def waitForProcess(name: String): Boolean = {
...@@ -310,19 +311,21 @@ class ShadowVpnService extends VpnService { ...@@ -310,19 +311,21 @@ class ShadowVpnService extends VpnService {
val cmd = (BASE + val cmd = (BASE +
"tun2socks --netif-ipaddr %s " "tun2socks --netif-ipaddr %s "
+ "--udpgw-remote-server-addr %s:7300 " // + "--udpgw-remote-server-addr %s:7300 "
+ "--dnsgw 127.0.0.1:8153 "
+ "--netif-netmask 255.255.255.0 " + "--netif-netmask 255.255.255.0 "
+ "--socks-server-addr 127.0.0.1:%d " + "--socks-server-addr 127.0.0.1:%d "
+ "--tunfd %d " + "--tunfd %d "
+ "--tunmtu %d " + "--tunmtu %d "
+ "--pid %stun2socks.pid") + "--pid %stun2socks.pid")
.format(localAddress.format(2), udpgw, config.localPort, fd, VPN_MTU, BASE) .format(localAddress.format(2), config.localPort, fd, VPN_MTU, BASE)
Log.d(TAG, cmd) Log.d(TAG, cmd)
System.exec(cmd) System.exec(cmd)
} }
/** Called when the activity is first created. */ /** Called when the activity is first created. */
def handleConnection: Boolean = { def handleConnection: Boolean = {
startDnsDaemon()
startShadowsocksDaemon() startShadowsocksDaemon()
startVpn() startVpn()
true true
...@@ -384,11 +387,9 @@ class ShadowVpnService extends VpnService { ...@@ -384,11 +387,9 @@ class ShadowVpnService extends VpnService {
unregisterReceiver(receiver) unregisterReceiver(receiver)
receiver = null receiver = null
} }
new Thread { spawn {
override def run() {
killProcesses() killProcesses()
} }
}.start()
if (conn != null) { if (conn != null) {
conn.close() conn.close()
conn = null conn = null
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment