Commit 90b75009 authored by Max Lv's avatar Max Lv

Apply udpgw patch from linusyang

parent eb046e49
......@@ -39,6 +39,16 @@
#define UDPGW_CLIENT_FLAG_DNS (1 << 2)
#define UDPGW_CLIENT_FLAG_IPV6 (1 << 3)
#ifdef BADVPN_SOCKS_UDP_RELAY
B_START_PACKED
struct socks_udp_header {
uint16_t rsv;
uint8_t frag;
uint8_t atyp;
} B_PACKED;
B_END_PACKED
#endif
B_START_PACKED
struct udpgw_header {
uint8_t flags;
......@@ -63,7 +73,11 @@ B_END_PACKED
static int udpgw_compute_mtu (int dgram_mtu)
{
bsize_t bs = bsize_add(
#ifdef BADVPN_SOCKS_UDP_RELAY
bsize_fromsize(sizeof(struct socks_udp_header)),
#else
bsize_fromsize(sizeof(struct udpgw_header)),
#endif
bsize_add(
bsize_max(
bsize_fromsize(sizeof(struct udpgw_addr_ipv4)),
......
......@@ -32,8 +32,20 @@
#include <misc/debug.h>
#include <base/DebugObject.h>
#include <system/BReactor.h>
#ifdef BADVPN_SOCKS_UDP_RELAY
#include <protocol/udpgw_proto.h>
#include <protocol/packetproto.h>
#include <system/BDatagram.h>
#include <flow/PacketBuffer.h>
#include <flow/SinglePacketBuffer.h>
#include <flow/BufferWriter.h>
#include <structure/BAVL.h>
#include <structure/LinkedList1.h>
#include <misc/offset.h>
#else
#include <udpgw_client/UdpGwClient.h>
#include <socksclient/BSocksClient.h>
#endif
typedef void (*SocksUdpGwClient_handler_received) (void *user, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
......@@ -46,14 +58,44 @@ typedef struct {
BReactor *reactor;
void *user;
SocksUdpGwClient_handler_received handler_received;
#ifdef BADVPN_SOCKS_UDP_RELAY
int udpgw_mtu;
int num_connections;
int max_connections;
BAVL connections_tree;
LinkedList1 connections_list;
#else
UdpGwClient udpgw_client;
BTimer reconnect_timer;
int have_socks;
BSocksClient socks_client;
int socks_up;
#endif
DebugObject d_obj;
} SocksUdpGwClient;
#ifdef BADVPN_SOCKS_UDP_RELAY
typedef struct {
BAddr local_addr;
BAddr remote_addr;
} SocksUdpGwClient_conaddr;
typedef struct {
SocksUdpGwClient *client;
SocksUdpGwClient_conaddr conaddr;
BPending first_job;
const uint8_t *first_data;
int first_data_len;
BDatagram udp_dgram;
BufferWriter udp_send_writer;
PacketBuffer udp_send_buffer;
SinglePacketBuffer udp_recv_buffer;
PacketPassInterface udp_recv_if;
BAVLNode connections_tree_node;
LinkedList1Node connections_list_node;
} SocksUdpGwClient_connection;
#endif
int SocksUdpGwClient_Init (SocksUdpGwClient *o, int udp_mtu, int max_connections, int send_buffer_size, btime_t keepalive_time,
BAddr socks_server_addr, const struct BSocksClient_auth_info *auth_info, size_t num_auth_info,
BAddr remote_udpgw_addr, btime_t reconnect_time, BReactor *reactor, void *user,
......
......@@ -585,10 +585,15 @@ void print_help (const char *name)
" [--password <password>]\n"
" [--password-file <file>]\n"
" [--append-source-to-username]\n"
#ifdef ANDROID
" [--enable-udprelay]\n"
" [--udprelay-max-connections <number>]\n"
#else
" [--udpgw-remote-server-addr <addr>]\n"
" [--udpgw-max-connections <number>]\n"
" [--udpgw-connection-buffer-size <number>]\n"
" [--udpgw-transparent-dns]\n"
#endif
"Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
name
);
......@@ -820,6 +825,10 @@ int parse_arguments (int argc, char *argv[])
else if (!strcmp(arg, "--append-source-to-username")) {
options.append_source_to_username = 1;
}
#ifdef ANDROID
else if (!strcmp(arg, "--enable-udprelay")) {
options.udpgw_remote_server_addr = "0.0.0.0:0";
#else
else if (!strcmp(arg, "--udpgw-remote-server-addr")) {
if (1 >= argc - i) {
fprintf(stderr, "%s: requires an argument\n", arg);
......@@ -827,8 +836,13 @@ int parse_arguments (int argc, char *argv[])
}
options.udpgw_remote_server_addr = argv[i + 1];
i++;
#endif
}
#ifdef ANDROID
else if (!strcmp(arg, "--udprelay-max-connections")) {
#else
else if (!strcmp(arg, "--udpgw-max-connections")) {
#endif
if (1 >= argc - i) {
fprintf(stderr, "%s: requires an argument\n", arg);
return 0;
......@@ -839,6 +853,7 @@ int parse_arguments (int argc, char *argv[])
}
i++;
}
#ifndef ANDROID
else if (!strcmp(arg, "--udpgw-connection-buffer-size")) {
if (1 >= argc - i) {
fprintf(stderr, "%s: requires an argument\n", arg);
......@@ -853,6 +868,7 @@ int parse_arguments (int argc, char *argv[])
else if (!strcmp(arg, "--udpgw-transparent-dns")) {
options.udpgw_transparent_dns = 1;
}
#endif
else {
fprintf(stderr, "unknown option: %s\n", arg);
return 0;
......@@ -959,7 +975,11 @@ int process_arguments (void)
// resolve remote udpgw server address
if (options.udpgw_remote_server_addr) {
if (!BAddr_Parse2(&udpgw_remote_server_addr, options.udpgw_remote_server_addr, NULL, 0, 0)) {
#ifdef ANDROID
BLog(BLOG_ERROR, "udprelay server addr: BAddr_Parse2 failed");
#else
BLog(BLOG_ERROR, "remote udpgw server addr: BAddr_Parse2 failed");
#endif
return 0;
}
}
......@@ -2080,7 +2100,11 @@ void udpgw_client_handler_received (void *unused, BAddr local_addr, BAddr remote
switch (local_addr.type) {
case BADDR_TYPE_IPV4: {
#ifdef ANDROID
BLog(BLOG_INFO, "UDP: from udprelay %d bytes", data_len);
#else
BLog(BLOG_INFO, "UDP: from udpgw %d bytes", data_len);
#endif
if (data_len > UINT16_MAX - (sizeof(struct ipv4_header) + sizeof(struct udp_header)) ||
data_len > BTap_GetMTU(&device) - (int)(sizeof(struct ipv4_header) + sizeof(struct udp_header))
......@@ -2119,10 +2143,18 @@ void udpgw_client_handler_received (void *unused, BAddr local_addr, BAddr remote
} break;
case BADDR_TYPE_IPV6: {
#ifdef ANDROID
BLog(BLOG_INFO, "UDP/IPv6: from udprelay %d bytes", data_len);
#else
BLog(BLOG_INFO, "UDP/IPv6: from udpgw %d bytes", data_len);
#endif
if (!options.netif_ip6addr) {
#ifdef ANDROID
BLog(BLOG_ERROR, "got IPv6 packet from udprelay but IPv6 is disabled");
#else
BLog(BLOG_ERROR, "got IPv6 packet from udpgw but IPv6 is disabled");
#endif
return;
}
......
......@@ -77,7 +77,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
def startShadowsocksDaemon() {
val cmd: String = (Path.BASE +
"ss-local -b 127.0.0.1 -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -f " +
"ss-local -b 127.0.0.1 -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -u -f " +
Path.BASE + "ss-local.pid")
.format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod)
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
......@@ -186,18 +186,24 @@ class ShadowsocksVpnService extends VpnService with BaseService {
val fd = conn.getFd
val cmd = (Path.BASE +
var cmd = (Path.BASE +
"tun2socks --netif-ipaddr %s "
+ "--dnsgw %s:8153 "
+ "--netif-netmask 255.255.255.0 "
+ "--socks-server-addr 127.0.0.1:%d "
+ "--tunfd %d "
+ "--tunmtu %d "
+ "--loglevel 3 "
+ "--loglevel 5 "
+ "--pid %stun2socks.pid")
.format(PRIVATE_VLAN.format("2"), PRIVATE_VLAN.format("1"), config.localPort, fd, VPN_MTU,
Path.BASE)
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
.format(PRIVATE_VLAN.format("2"), config.localPort, fd, VPN_MTU, Path.BASE)
if (config.isUdpDns)
cmd += " --enable-udprelay"
else
cmd += " --dnsgw %s:8153".format(PRIVATE_VLAN.format("1"))
if (BuildConfig.DEBUG)
Log.d(TAG, cmd)
System.exec(cmd)
}
......@@ -205,7 +211,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
def handleConnection: Boolean = {
startVpn()
startShadowsocksDaemon()
startDnsDaemon()
if (!config.isUdpDns) startDnsDaemon()
true
}
......
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