Commit 9217aee2 authored by Max Lv's avatar Max Lv

dnsgw works now

parent c361dacf
...@@ -184,7 +184,7 @@ LOCAL_CFLAGS := -std=gnu99 ...@@ -184,7 +184,7 @@ LOCAL_CFLAGS := -std=gnu99
LOCAL_CFLAGS += -DBADVPN_THREADWORK_USE_PTHREAD -DBADVPN_LINUX -DBADVPN_BREACTOR_BADVPN -D_GNU_SOURCE LOCAL_CFLAGS += -DBADVPN_THREADWORK_USE_PTHREAD -DBADVPN_LINUX -DBADVPN_BREACTOR_BADVPN -D_GNU_SOURCE
LOCAL_CFLAGS += -DBADVPN_USE_SELFPIPE -DBADVPN_USE_EPOLL LOCAL_CFLAGS += -DBADVPN_USE_SELFPIPE -DBADVPN_USE_EPOLL
LOCAL_CFLAGS += -DBADVPN_LITTLE_ENDIAN -DBADVPN_THREAD_SAFE LOCAL_CFLAGS += -DBADVPN_LITTLE_ENDIAN -DBADVPN_THREAD_SAFE
LOCAL_CFLAGS += -DANDROID LOCAL_CFLAGS += -DNDEBUG -DANDROID
LOCAL_C_INCLUDES:= \ LOCAL_C_INCLUDES:= \
$(LOCAL_PATH)/badvpn/lwip/src/include/ipv4 \ $(LOCAL_PATH)/badvpn/lwip/src/include/ipv4 \
...@@ -255,6 +255,8 @@ TUN2SOCKS_SOURCES := \ ...@@ -255,6 +255,8 @@ TUN2SOCKS_SOURCES := \
LOCAL_MODULE := tun2socks LOCAL_MODULE := tun2socks
LOCAL_LDLIBS := -ldl -llog
LOCAL_SRC_FILES := $(addprefix badvpn/, $(TUN2SOCKS_SOURCES)) LOCAL_SRC_FILES := $(addprefix badvpn/, $(TUN2SOCKS_SOURCES))
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)
......
...@@ -32,6 +32,10 @@ ...@@ -32,6 +32,10 @@
#include "BLog.h" #include "BLog.h"
#ifdef ANDROID
#include <android/log.h>
#endif
#ifndef BADVPN_PLUGIN #ifndef BADVPN_PLUGIN
struct _BLog_channel blog_channel_list[] = { struct _BLog_channel blog_channel_list[] = {
...@@ -51,12 +55,22 @@ static char *level_names[] = { NULL, "ERROR", "WARNING", "NOTICE", "INFO", "DEBU ...@@ -51,12 +55,22 @@ static char *level_names[] = { NULL, "ERROR", "WARNING", "NOTICE", "INFO", "DEBU
static void stdout_log (int channel, int level, const char *msg) static void stdout_log (int channel, int level, const char *msg)
{ {
#ifndef ANDROID
fprintf(stdout, "%s(%s): %s\n", level_names[level], blog_global.channels[channel].name, msg); fprintf(stdout, "%s(%s): %s\n", level_names[level], blog_global.channels[channel].name, msg);
#else
__android_log_print(ANDROID_LOG_DEBUG, "tun2socks",
"%s(%s): %s\n", level_names[level], blog_global.channels[channel].name, msg);
#endif
} }
static void stderr_log (int channel, int level, const char *msg) static void stderr_log (int channel, int level, const char *msg)
{ {
#ifndef ANDROID
fprintf(stderr, "%s(%s): %s\n", level_names[level], blog_global.channels[channel].name, msg); fprintf(stderr, "%s(%s): %s\n", level_names[level], blog_global.channels[channel].name, msg);
#else
__android_log_print(ANDROID_LOG_ERROR, "tun2socks",
"%s(%s): %s\n", level_names[level], blog_global.channels[channel].name, msg);
#endif
} }
static void stdout_stderr_free (void) static void stdout_stderr_free (void)
......
...@@ -40,14 +40,6 @@ ...@@ -40,14 +40,6 @@
#include <misc/debug.h> #include <misc/debug.h>
#include <base/BMutex.h> #include <base/BMutex.h>
#ifdef ANDROID
#include <android/log.h>
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "tun2socks", __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "tun2socks", __VA_ARGS__))
#endif
// auto-generated channel numbers and number of channels // auto-generated channel numbers and number of channels
#include <generated/blog_channels_defines.h> #include <generated/blog_channels_defines.h>
......
...@@ -965,7 +965,7 @@ int process_arguments (void) ...@@ -965,7 +965,7 @@ int process_arguments (void)
} }
#ifdef ANDROID #ifdef ANDROID
// resolve dnsgw ipaddr // resolve dnsgw addr
if (options.dnsgw) { if (options.dnsgw) {
if (!BAddr_Parse2(&dnsgw, options.dnsgw, NULL, 0, 0)) { if (!BAddr_Parse2(&dnsgw, options.dnsgw, NULL, 0, 0)) {
BLog(BLOG_ERROR, "dnsgw addr: BAddr_Parse2 failed"); BLog(BLOG_ERROR, "dnsgw addr: BAddr_Parse2 failed");
...@@ -1179,9 +1179,12 @@ int process_device_dns_packet (uint8_t *data, int data_len) ...@@ -1179,9 +1179,12 @@ int process_device_dns_packet (uint8_t *data, int data_len)
goto fail; goto fail;
} }
BAddr local_addr; static BAddr local_addr;
BAddr remote_addr; static BAddr remote_addr;
int is_dns; static int init = 0;
int to_dns;
int from_dns;
int packet_length = 0; int packet_length = 0;
uint8_t ip_version = 0; uint8_t ip_version = 0;
...@@ -1215,28 +1218,63 @@ int process_device_dns_packet (uint8_t *data, int data_len) ...@@ -1215,28 +1218,63 @@ int process_device_dns_packet (uint8_t *data, int data_len)
if (checksum_in_packet != checksum_computed) { if (checksum_in_packet != checksum_computed) {
goto fail; goto fail;
} }
BLog(BLOG_INFO, "UDP: from device %d bytes", data_len);
// to port 53 is considered a DNS packet // to port 53 is considered a DNS packet
is_dns = udp_header.dest_port == hton16(53); to_dns = udp_header.dest_port == hton16(53);
// from port 8153 is considered a DNS packet
from_dns = udp_header.source_port == dnsgw.ipv4.port;
// if not DNS packet, just bypass it. // if not DNS packet, just bypass it.
if (!is_dns) { if (!to_dns && !from_dns) {
goto fail; goto fail;
} }
// build IP header // modify DNS packet
ipv4_header.destination_address = dnsgw.ipv4.ip; if (to_dns) {
BLog(BLOG_INFO, "UDP: to DNS %d bytes", data_len);
// construct addresses
if (!init) {
init = 1;
BAddr_InitIPv4(&local_addr, ipv4_header.source_address, udp_header.source_port);
BAddr_InitIPv4(&remote_addr, ipv4_header.destination_address, udp_header.dest_port);
}
// build IP header
ipv4_header.destination_address = dnsgw.ipv4.ip;
ipv4_header.source_address = netif_ipaddr.ipv4;
// build UDP header
udp_header.dest_port = dnsgw.ipv4.port;
} else if (from_dns) {
// if not initialized
if (!init) {
goto fail;
}
BLog(BLOG_INFO, "UDP: from DNS %d bytes", data_len);
// build IP header
ipv4_header.source_address = remote_addr.ipv4.ip;
ipv4_header.destination_address = local_addr.ipv4.ip;
// build UDP header
udp_header.source_port = remote_addr.ipv4.port;
}
// update IPv4 header's checksum
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 // update UDP header's checksum
udp_header.dest_port = 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);
// write packet // write packet
memcpy(device_write_buf, &ipv4_header, sizeof(ipv4_header)); memcpy(device_write_buf, &ipv4_header, sizeof(ipv4_header));
memcpy(device_write_buf + sizeof(ipv4_header), &udp_header, sizeof(udp_header)); memcpy(device_write_buf + sizeof(ipv4_header), &udp_header, sizeof(udp_header));
......
...@@ -75,7 +75,6 @@ class ShadowVpnService extends VpnService { ...@@ -75,7 +75,6 @@ class ShadowVpnService extends VpnService {
val PRIVATE_VLAN_172 = "172.30.254.%d" val PRIVATE_VLAN_172 = "172.30.254.%d"
var conn: ParcelFileDescriptor = null var conn: ParcelFileDescriptor = null
var udpgw: String = null
var notificationManager: NotificationManager = null var notificationManager: NotificationManager = null
var receiver: BroadcastReceiver = null var receiver: BroadcastReceiver = null
var apps: Array[ProxiedApp] = null var apps: Array[ProxiedApp] = null
...@@ -203,14 +202,6 @@ class ShadowVpnService extends VpnService { ...@@ -203,14 +202,6 @@ class ShadowVpnService extends VpnService {
resolved = true resolved = true
} }
// Resolve UDP gateway
if (resolved) {
Utils.resolve("u.maxcdn.info", enableIPv6 = false) match {
case Some(host) => udpgw = host
case None => resolved = false
}
}
if (resolved && handleConnection) { if (resolved && handleConnection) {
handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 300) handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 300)
} else { } else {
...@@ -259,7 +250,6 @@ class ShadowVpnService extends VpnService { ...@@ -259,7 +250,6 @@ class ShadowVpnService extends VpnService {
.setMtu(VPN_MTU) .setMtu(VPN_MTU)
.addAddress(localAddress.format(1), 24) .addAddress(localAddress.format(1), 24)
.addDnsServer("8.8.8.8") .addDnsServer("8.8.8.8")
.addDnsServer("8.8.4.4")
if (InetAddressUtils.isIPv6Address(config.proxy)) { if (InetAddressUtils.isIPv6Address(config.proxy)) {
builder.addRoute("0.0.0.0", 0) builder.addRoute("0.0.0.0", 0)
...@@ -312,13 +302,13 @@ class ShadowVpnService extends VpnService { ...@@ -312,13 +302,13 @@ 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 " + "--dnsgw %s: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), config.localPort, fd, VPN_MTU, BASE) .format(localAddress.format(2), localAddress.format(1), config.localPort, fd, VPN_MTU, BASE)
Log.d(TAG, cmd) Log.d(TAG, cmd)
System.exec(cmd) System.exec(cmd)
} }
......
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