From 255371cbaa45e458e338ee6c795fa773e8973f62 Mon Sep 17 00:00:00 2001 From: zabbius Date: Wed, 7 Aug 2024 02:22:33 +0300 Subject: [PATCH 1/4] start-stop priority in rc script - now service can be enabled --- owrt/youtubeUnblock.owrt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/owrt/youtubeUnblock.owrt b/owrt/youtubeUnblock.owrt index 26382d3..23be195 100755 --- a/owrt/youtubeUnblock.owrt +++ b/owrt/youtubeUnblock.owrt @@ -1,6 +1,9 @@ #!/bin/sh /etc/rc.common USE_PROCD=1 +START=50 +STOP=50 + # Openwrt procd script: https://openwrt.org/docs/guide-developer/procd-init-script-example # The program should be put into /usr/bin/ # This file should be put into /etc/init.d/ From 69955d52378f1a6ea67cd5dbd5887d94b219679a Mon Sep 17 00:00:00 2001 From: zabbius Date: Wed, 7 Aug 2024 03:31:10 +0300 Subject: [PATCH 2/4] commandline options --- config.h | 23 +-- youtubeUnblock.c | 445 +++++++++++++++++++++++++++++++---------------- 2 files changed, 303 insertions(+), 165 deletions(-) diff --git a/config.h b/config.h index 4373364..a8b1b74 100644 --- a/config.h +++ b/config.h @@ -20,31 +20,26 @@ #define FRAGMENTATION_STRATEGY FRAG_STRAT_TCP #endif -#if FRAGMENTATION_STRATEGY == FRAG_STRAT_TCP - #define USE_TCP_SEGMENTATION -#elif FRAGMENTATION_STRATEGY == FRAG_STRAT_IP - #define USE_IP_FRAGMENTATION -#elif FRAGMENTATION_STRATEGY == FRAG_STRAT_NONE - #define USE_NO_FRAGMENTATION -#endif - #define RAWSOCKET_MARK (1 << 15) #ifdef USE_SEG2_DELAY #define SEG2_DELAY 100 #endif -#ifndef NO_FAKE_SNI -#define FAKE_SNI -#endif - #define FAKE_SNI_TTL 8 +// No fake SNI +#define FKSN_STRAT_NONE 0 // Will invalidate fake client hello by out-of-ack_seq out-of-seq request -#define FKSN_STRAT_ACK_SEQ 0 +#define FKSN_STRAT_ACK_SEQ 1 // Will assume that GGC server is located further than FAKE_SNI_TTL // Thus, Fake Client Hello will be eliminated automatically. -#define FKSN_STRAT_TTL 1 +#define FKSN_STRAT_TTL 2 + + +#ifndef NO_FAKE_SNI +#define FAKE_SNI_STRATEGY FKSN_STRAT_NONE +#endif #ifndef FAKE_SNI_STRATEGY #define FAKE_SNI_STRATEGY FKSN_STRAT_ACK_SEQ diff --git a/youtubeUnblock.c b/youtubeUnblock.c index 4eda718..f86ed6a 100644 --- a/youtubeUnblock.c +++ b/youtubeUnblock.c @@ -34,11 +34,134 @@ static struct { int rawsocket; pthread_mutex_t rawsocket_lock; int threads; + bool use_gso; + int fragmentation_strategy; + uint8_t fake_sni_ttl; + int fake_sni_strategy; + bool verbose; + uint32_t seg2_delay; } config = { - .rawsocket = -2, - .threads = THREADS_NUM + .rawsocket = -2, + .threads = THREADS_NUM, + .fragmentation_strategy = FRAGMENTATION_STRATEGY, + .fake_sni_strategy = FAKE_SNI_STRATEGY, + .fake_sni_ttl = FAKE_SNI_TTL, + +#ifdef SEG2_DELAY + .seg2_delay = SEG2_DELAY, +#else + .seg2_delay = 0, +#endif + +#ifdef USE_GSO + .use_gso = true, +#else + .use_gso = false, +#endif + +#ifdef DEBUG + .verbose = true, +#else + .verbose = false, +#endif }; +const char* get_value(const char *option, const char *prefix) +{ + size_t prefix_len = strlen(prefix); + size_t option_len = strlen(option); + + if (option_len < prefix_len || memcmp(prefix, option, prefix_len)) + return 0; + + return option + prefix_len; +} + +bool parse_bool_option(const char *value) { + errno = 0; + if (strcmp(value, "1") == 0) { + return true; + } + else if (strcmp(value, "0") == 0) { + return false; + } + errno = EINVAL; + return false; +} + +uint32_t parse_uint_option(const char* value) { + char* end; + uint32_t result = strtoul(value, &end, 10); + if (*end != '\0') { + errno = EINVAL; + return -1; + } + return result; +} + +bool parse_option(const char* option) { + const char* value; + + if ((value = get_value(option, "--gso=")) != 0) { + config.use_gso = parse_bool_option(value); + return errno == 0; + } + + if ((value = get_value(option, "--verbose=")) != 0) { + config.verbose = parse_bool_option(value); + return errno == 0; + } + + if ((value = get_value(option, "--frag=")) != 0) { + if (strcmp(value, "tcp") == 0) { + config.fragmentation_strategy = FRAG_STRAT_TCP; + } + else if (strcmp(value, "ip") == 0) { + config.fragmentation_strategy = FRAG_STRAT_IP; + } + else if (strcmp(value, "none") == 0) { + config.fragmentation_strategy = FRAG_STRAT_NONE; + } + else { + return false; + } + return true; + } + + if ((value = get_value(option, "--fake-sni=")) != 0) { + if (strcmp(value, "ack") == 0) { + config.fake_sni_strategy = FKSN_STRAT_ACK_SEQ; + } + else if (strcmp(value, "ttl") == 0) { + config.fake_sni_strategy = FKSN_STRAT_TTL; + } + else if (strcmp(value, "none") == 0) { + config.fake_sni_strategy = FKSN_STRAT_NONE; + } + else { + return false; + } + return true; + } + + if ((value = get_value(option, "--seg2delay=")) != 0) { + config.seg2_delay = parse_uint_option(value); + return errno == 0; + } + + if ((value = get_value(option, "--threads=")) != 0) { + config.threads = parse_uint_option(value); + return errno == 0 && config.threads <= MAX_THREADS; + } + + if ((value = get_value(option, "--fake-sni-ttl=")) != 0) { + config.fake_sni_ttl = parse_uint_option(value); + return errno == 0; + } + + return false; +} + static int parse_args(int argc, const char *argv[]) { int err; char *end; @@ -48,15 +171,29 @@ static int parse_args(int argc, const char *argv[]) { goto errormsg_help; } - uint32_t queue_num = strtoul(argv[1], &end, 10); - if (errno != 0 || *end != '\0') goto errormsg_help; + config.queue_start_num = parse_uint_option(argv[1]); + if (errno != 0) goto errormsg_help; + + for (int i = 2; i < argc; i++) { + if (!parse_option(argv[i])) { + printf("Invalid option %s\n", argv[i]); + goto errormsg_help; + } + } - config.queue_start_num = queue_num; return 0; errormsg_help: err = errno; - printf("Usage: %s [queue_num]\n", argv[0]); + printf("Usage: %s [OPTIONS]\n", argv[0]); + printf("Options:\n"); + printf("\t--gso={0,1}\n"); + printf("\t--fake-sni={ack,ttl,none}\n"); + printf("\t--fake-sni-ttl=\n"); + printf("\t--frag={tcp,ip,none}\n"); + printf("\t--seg2delay=\n"); + printf("\t--threads=\n"); + printf("\t--verbose={0,1}\n"); errno = err; if (errno == 0) errno = EINVAL; @@ -154,36 +291,38 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) { int ret; if (pktlen > AVAILABLE_MTU) { -#ifdef DEBUG - printf("Split packet!\n"); -#endif + if (config.verbose) + printf("Split packet!\n"); uint8_t buff1[MNL_SOCKET_BUFFER_SIZE]; uint32_t buff1_size = MNL_SOCKET_BUFFER_SIZE; uint8_t buff2[MNL_SOCKET_BUFFER_SIZE]; uint32_t buff2_size = MNL_SOCKET_BUFFER_SIZE; -#if defined(USE_TCP_SEGMENTATION) || defined(RAWSOCK_TCP_FSTRAT) - if ((ret = tcp4_frag(pkt, pktlen, AVAILABLE_MTU-128, - buff1, &buff1_size, buff2, &buff2_size)) < 0) { + switch (config.fragmentation_strategy) { + case FRAG_STRAT_TCP: + if ((ret = tcp4_frag(pkt, pktlen, AVAILABLE_MTU-128, + buff1, &buff1_size, buff2, &buff2_size)) < 0) { - errno = -ret; - return ret; - } -#elif defined(USE_IP_FRAGMENTATION) || defined(RAWSOCK_IP_FSTRAT) - if ((ret = ip4_frag(pkt, pktlen, AVAILABLE_MTU-128, - buff1, &buff1_size, buff2, &buff2_size)) < 0) { + errno = -ret; + return ret; + } + break; + case FRAG_STRAT_IP: + if ((ret = ip4_frag(pkt, pktlen, AVAILABLE_MTU-128, + buff1, &buff1_size, buff2, &buff2_size)) < 0) { - errno = -ret; - return ret; + errno = -ret; + return ret; + } + break; + default: + errno = EINVAL; + printf("send_raw_socket: Packet is too big but fragmentation is disabled! " + "Pass -DRAWSOCK_TCP_FSTRAT or -DRAWSOCK_IP_FSTRAT as CFLAGS " + "To enable it only for raw socket\n"); + return -EINVAL; } -#else - errno = EINVAL; - printf("send_raw_socket: Packet is too big but fragmentation is disabled! " - "Pass -DRAWSOCK_TCP_FSTRAT or -DRAWSOCK_IP_FSTRAT as CFLAGS " - "To enable it only for raw socket\n"); - return -EINVAL; -#endif int sent = 0; int status = send_raw_socket(buff1, buff1_size); @@ -332,20 +471,16 @@ static int process_packet(const struct packet_data packet, struct queue_data qda nfq_nlmsg_verdict_put(verdnlh, packet.id, NF_ACCEPT); if (vrd.gvideo_hello) { -#ifdef DEBUG - printf("Google video!\n"); -#endif + if (config.verbose) + printf("Google video!\n"); if (dlen > 1480) { -#ifdef DEBUG - fprintf(stderr, "WARNING! Google video packet is too big and may cause issues!\n"); -#endif + if (config.verbose) + fprintf(stderr, "WARNING! Google video packet is too big and may cause issues!\n"); } -#ifdef FAKE_SNI uint8_t fake_sni[MNL_SOCKET_BUFFER_SIZE]; uint32_t fsn_len = MNL_SOCKET_BUFFER_SIZE; -#endif uint8_t frag1[MNL_SOCKET_BUFFER_SIZE]; uint8_t frag2[MNL_SOCKET_BUFFER_SIZE]; @@ -358,54 +493,59 @@ static int process_packet(const struct packet_data packet, struct queue_data qda nfq_tcp_compute_checksum_ipv4( (struct tcphdr *)tcph, (struct iphdr *)iph); -#ifdef FAKE_SNI - ret = gen_fake_sni(iph, tcph, fake_sni, &fsn_len); - if (ret < 0) { - errno = -ret; - perror("gen_fake_sni"); - goto fallback; + if (config.fake_sni_strategy != FKSN_STRAT_NONE) { + ret = gen_fake_sni(iph, tcph, fake_sni, &fsn_len); + if (ret < 0) { + errno = -ret; + perror("gen_fake_sni"); + goto fallback; + } + + ret = send_raw_socket(fake_sni, fsn_len); + if (ret < 0) { + errno = -ret; + perror("send fake sni"); + goto fallback; + } } - ret = send_raw_socket(fake_sni, fsn_len); - if (ret < 0) { - errno = -ret; - perror("send fake sni"); - goto fallback; + size_t ipd_offset; + size_t mid_offset; + + switch (config.fragmentation_strategy) { + case FRAG_STRAT_TCP: + ipd_offset = vrd.sni_offset; + mid_offset = ipd_offset + vrd.sni_len / 2; + + if ((ret = tcp4_frag(raw_payload, raw_payload_len, + mid_offset, frag1, &f1len, frag2, &f2len)) < 0) { + + errno = -ret; + perror("tcp4_frag"); + goto send_verd; + } + break; + case FRAG_STRAT_IP: + ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_offset; + mid_offset = ipd_offset + vrd.sni_len / 2; + mid_offset += 8 - mid_offset % 8; + + if ((ret = ip4_frag(raw_payload, raw_payload_len, + mid_offset, frag1, &f1len, frag2, &f2len)) < 0) { + + errno = -ret; + perror("ip4_frag"); + goto send_verd; + } + break; + default: + ret = send_raw_socket(raw_payload, raw_payload_len); + if (ret < 0) { + errno = -ret; + perror("raw pack send"); + } + goto send_verd; } -#endif - - -#if defined(USE_TCP_SEGMENTATION) - size_t ipd_offset = vrd.sni_offset; - size_t mid_offset = ipd_offset + vrd.sni_len / 2; - - if ((ret = tcp4_frag(raw_payload, raw_payload_len, - mid_offset, frag1, &f1len, frag2, &f2len)) < 0) { - errno = -ret; - perror("tcp4_frag"); - goto send_verd; - } - -#elif defined(USE_IP_FRAGMENTATION) - size_t ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_offset; - size_t mid_offset = ipd_offset + vrd.sni_len / 2; - mid_offset += 8 - mid_offset % 8; - - if ((ret = ip4_frag(raw_payload, raw_payload_len, - mid_offset, frag1, &f1len, frag2, &f2len)) < 0) { - errno = -ret; - perror("ip4_frag"); - goto send_verd; - } - -#else - ret = send_raw_socket(raw_payload, raw_payload_len); - if (ret < 0) { - errno = -ret; - perror("raw pack send"); - } - goto send_verd; -#endif ret = send_raw_socket(frag2, f2len); if (ret < 0) { @@ -414,35 +554,34 @@ static int process_packet(const struct packet_data packet, struct queue_data qda goto send_verd; } - -#ifdef SEG2_DELAY - struct dps_t *dpdt = malloc(sizeof(struct dps_t)); - dpdt->pkt = malloc(f1len); - memcpy(dpdt->pkt, frag1, f1len); - dpdt->pktlen = f1len; - dpdt->timer = SEG2_DELAY; - pthread_t thr; - pthread_create(&thr, NULL, delay_packet_send, dpdt); - pthread_detach(thr); -#else - ret = send_raw_socket(frag1, f1len); - if (ret < 0) { - errno = -ret; - perror("raw frags send: frag1"); - goto send_verd; + if (config.seg2_delay) { + struct dps_t *dpdt = malloc(sizeof(struct dps_t)); + dpdt->pkt = malloc(f1len); + memcpy(dpdt->pkt, frag1, f1len); + dpdt->pktlen = f1len; + dpdt->timer = config.seg2_delay; + pthread_t thr; + pthread_create(&thr, NULL, delay_packet_send, dpdt); + pthread_detach(thr); } + else { + ret = send_raw_socket(frag1, f1len); + if (ret < 0) { + errno = -ret; + perror("raw frags send: frag1"); -#endif - + goto send_verd; + } + } } /* if (pktb_mangled(pktb)) { -#ifdef DEBUG - printf("Mangled!\n"); -#endif + if (config.versose) + printf("Mangled!\n"); + nfq_nlmsg_verdict_put_pkt( verdnlh, pktb_data(pktb), pktb_len(pktb)); } @@ -533,10 +672,10 @@ int init_queue(int queue_num) { nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num); nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff); -#ifdef USE_GSO + if (config.use_gso) { mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO)); mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO)); -#endif + } if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { perror("mnl_socket_send"); @@ -609,69 +748,73 @@ int main(int argc, const char *argv[]) { exit(EXIT_FAILURE); } -#if defined(USE_TCP_SEGMENTATION) - printf("Using TCP segmentation\n"); -#elif defined(USE_IP_FRAGMENTATION) - printf("Using IP fragmentation\n"); -#else - printf("SNI fragmentation is disabled\n"); -#endif + switch (config.fragmentation_strategy) { + case FRAG_STRAT_TCP: + printf("Using TCP segmentation\n"); + break; + case FRAG_STRAT_IP: + printf("Using IP fragmentation\n"); + break; + default: + printf("SNI fragmentation is disabled\n"); + break; + } -#ifdef SEG2_DELAY - printf("Some outgoing googlevideo request segments will be delayed for %d ms as of SEG2_DELAY define\n", SEG2_DELAY); -#endif + if (config.seg2_delay) + printf("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", config.seg2_delay); -#ifdef FAKE_SNI - printf("Fake SNI will be sent before each googlevideo request\n"); + switch (config.fake_sni_strategy) { + case FKSN_STRAT_TTL: + printf("Fake SNI will be sent before each googlevideo request, TTL strategy will be used with TTL %d\n", config.fake_sni_ttl); + break; + case FRAG_STRAT_IP: + printf("Fake SNI will be sent before each googlevideo request, Ack-Seq strategy will be used\n"); + break; + default: + printf("SNI fragmentation is disabled\n"); + break; + } - printf("Fake SNI will use strategy: " -#if FAKE_SNI_STRATEGY == FKSN_STRAT_TTL - "TTL" -#else - "Ack-Seq" -#endif - "\n"); -#endif - -#ifdef USE_GSO - printf("GSO is enabled!\n"); -#endif + if (config.use_gso) + printf("GSO is enabled\n"); if (open_raw_socket() < 0) { perror("Unable to open raw socket"); exit(EXIT_FAILURE); } + struct queue_res *qres; + if (config.threads == 1) { + struct queue_conf tconf = { + .i = 0, + .queue_num = config.queue_start_num + }; -#if THREADS_NUM == 1 - struct queue_conf tconf = { - .i = 0, - .queue_num = config.queue_start_num - }; - - struct queue_res *qres = init_queue_wrapper(&tconf); -#else - struct queue_conf thread_confs[MAX_THREADS]; - pthread_t threads[MAX_THREADS]; - for (int i = 0; i < config.threads; i++) { - struct queue_conf *tconf = thread_confs + i; - pthread_t *thr = threads + i; - - tconf->queue_num = config.queue_start_num + i; - tconf->i = i; - - pthread_create(thr, NULL, init_queue_wrapper, tconf); + qres = init_queue_wrapper(&tconf); } + else { + printf("%d threads wil be used\n", config.threads); - void *res; - struct queue_res *qres - for (int i = 0; i < config.threads; i++) { - pthread_join(threads[i], &res); + struct queue_conf thread_confs[MAX_THREADS]; + pthread_t threads[MAX_THREADS]; + for (int i = 0; i < config.threads; i++) { + struct queue_conf *tconf = thread_confs + i; + pthread_t *thr = threads + i; - qres = res; + tconf->queue_num = config.queue_start_num + i; + tconf->i = i; + + pthread_create(thr, NULL, init_queue_wrapper, tconf); + } + + void *res; + for (int i = 0; i < config.threads; i++) { + pthread_join(threads[i], &res); + + qres = res; + } } -#endif if (close_raw_socket() < 0) { perror("Unable to close raw socket"); From 6eca2cb77c56920d9d679f786e1d5eee54d1310c Mon Sep 17 00:00:00 2001 From: zabbius Date: Wed, 7 Aug 2024 04:00:48 +0300 Subject: [PATCH 3/4] minor --- config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.h b/config.h index a8b1b74..5b3e763 100644 --- a/config.h +++ b/config.h @@ -37,7 +37,7 @@ #define FKSN_STRAT_TTL 2 -#ifndef NO_FAKE_SNI +#ifdef NO_FAKE_SNI #define FAKE_SNI_STRATEGY FKSN_STRAT_NONE #endif From 66906dbe0cc625f29997c176c8d4234686d24804 Mon Sep 17 00:00:00 2001 From: Vadim Vetrov Date: Wed, 7 Aug 2024 13:32:01 +0300 Subject: [PATCH 4/4] Code cleanup --- config.h | 14 ++++ mangle.c | 10 ++- youtubeUnblock.c | 183 +++++++++++++++++++++++++++-------------------- 3 files changed, 123 insertions(+), 84 deletions(-) diff --git a/config.h b/config.h index 5b3e763..483703a 100644 --- a/config.h +++ b/config.h @@ -1,3 +1,17 @@ + +struct config_t { + unsigned int queue_start_num; + int rawsocket; + int threads; + int use_gso; + int fragmentation_strategy; + unsigned char fake_sni_ttl; + int fake_sni_strategy; + int verbose; + unsigned int seg2_delay; +}; +extern struct config_t config; + #define MAX_THREADS 16 #ifndef THREADS_NUM diff --git a/mangle.c b/mangle.c index 8ccf705..d330785 100644 --- a/mangle.c +++ b/mangle.c @@ -190,9 +190,8 @@ int ip4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset, f2_hdr->tot_len = htons(f2_dlen); -#if defined(DEBUG) - printf("Packet split in portion %u %u\n", f1_plen, f2_plen); -#endif + if (config.verbose) + printf("Packet split in portion %u %u\n", f1_plen, f2_plen); ip4_set_checksum(f1_hdr); ip4_set_checksum(f2_hdr); @@ -268,9 +267,8 @@ int tcp4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset, s2_tcph->seq = htonl(ntohl(s2_tcph->seq) + payload_offset); -#if defined(DEBUG) - printf("Packet split in portion %u %u\n", s1_plen, s2_plen); -#endif + if (config.verbose) + printf("Packet split in portion %u %u\n", s1_plen, s2_plen); tcp4_set_checksum(s1_tcph, s1_hdr); tcp4_set_checksum(s2_tcph, s2_hdr); diff --git a/youtubeUnblock.c b/youtubeUnblock.c index f86ed6a..049fc93 100644 --- a/youtubeUnblock.c +++ b/youtubeUnblock.c @@ -29,23 +29,14 @@ #include "config.h" #include "mangle.h" -static struct { - uint32_t queue_start_num; - int rawsocket; - pthread_mutex_t rawsocket_lock; - int threads; - bool use_gso; - int fragmentation_strategy; - uint8_t fake_sni_ttl; - int fake_sni_strategy; - bool verbose; - uint32_t seg2_delay; -} config = { +pthread_mutex_t rawsocket_lock; + +struct config_t config = { .rawsocket = -2, .threads = THREADS_NUM, - .fragmentation_strategy = FRAGMENTATION_STRATEGY, + .fragmentation_strategy = FRAGMENTATION_STRATEGY, .fake_sni_strategy = FAKE_SNI_STRATEGY, - .fake_sni_ttl = FAKE_SNI_TTL, + .fake_sni_ttl = FAKE_SNI_TTL, #ifdef SEG2_DELAY .seg2_delay = SEG2_DELAY, @@ -68,114 +59,148 @@ static struct { const char* get_value(const char *option, const char *prefix) { + errno = 0; + size_t prefix_len = strlen(prefix); size_t option_len = strlen(option); - if (option_len < prefix_len || memcmp(prefix, option, prefix_len)) - return 0; + if (option_len <= prefix_len || strncmp(prefix, option, prefix_len)) { + return NULL; + } return option + prefix_len; } -bool parse_bool_option(const char *value) { +int parse_bool_option(const char *value) { errno = 0; - if (strcmp(value, "1") == 0) { - return true; + if (strcmp(value, "1") == 0) { + return 1; } else if (strcmp(value, "0") == 0) { - return false; + return 0; } - errno = EINVAL; - return false; + + errno = EINVAL; + return -1; } -uint32_t parse_uint_option(const char* value) { +long parse_numeric_option(const char* value) { + errno = 0; + char* end; - uint32_t result = strtoul(value, &end, 10); + long result = strtol(value, &end, 10); if (*end != '\0') { errno = EINVAL; - return -1; + return 0; } + return result; } -bool parse_option(const char* option) { +int parse_option(const char* option) { const char* value; + int ret; - if ((value = get_value(option, "--gso=")) != 0) { - config.use_gso = parse_bool_option(value); - return errno == 0; + if (!strcmp(option, "--no-gso")) { + config.use_gso = 0; + goto out; } - if ((value = get_value(option, "--verbose=")) != 0) { - config.verbose = parse_bool_option(value); - return errno == 0; + if (!strcmp(option, "--silent")) { + config.verbose = 0; + goto out; } if ((value = get_value(option, "--frag=")) != 0) { + if (!value) { + goto err; + } + if (strcmp(value, "tcp") == 0) { config.fragmentation_strategy = FRAG_STRAT_TCP; - } - else if (strcmp(value, "ip") == 0) { + } else if (strcmp(value, "ip") == 0) { config.fragmentation_strategy = FRAG_STRAT_IP; - } - else if (strcmp(value, "none") == 0) { + } else if (strcmp(value, "none") == 0) { config.fragmentation_strategy = FRAG_STRAT_NONE; + } else { + goto err; } - else { - return false; - } - return true; + + goto out; } if ((value = get_value(option, "--fake-sni=")) != 0) { if (strcmp(value, "ack") == 0) { config.fake_sni_strategy = FKSN_STRAT_ACK_SEQ; - } - else if (strcmp(value, "ttl") == 0) { + } else if (strcmp(value, "ttl") == 0) { config.fake_sni_strategy = FKSN_STRAT_TTL; } else if (strcmp(value, "none") == 0) { config.fake_sni_strategy = FKSN_STRAT_NONE; + } else { + goto err; } - else { - return false; - } - return true; + + goto out; } if ((value = get_value(option, "--seg2delay=")) != 0) { - config.seg2_delay = parse_uint_option(value); - return errno == 0; + long num = parse_numeric_option(value); + if (errno != 0 || + num < 0) + goto err; + + config.seg2_delay = num; + goto out; } if ((value = get_value(option, "--threads=")) != 0) { - config.threads = parse_uint_option(value); - return errno == 0 && config.threads <= MAX_THREADS; + long num = parse_numeric_option(value); + if (errno != 0 || + num < 0 || + num > MAX_THREADS) { + errno = EINVAL; + goto err; + } + + config.threads = num; + goto out; } if ((value = get_value(option, "--fake-sni-ttl=")) != 0) { - config.fake_sni_ttl = parse_uint_option(value); - return errno == 0; + long num = parse_numeric_option(value); + if (errno != 0 || + num < 0 || + num > 255) { + goto err; + } + + config.fake_sni_ttl = num; + goto out; } - return false; +err: + errno = EINVAL; + return -1; +out: + errno = 0; + return 0; } static int parse_args(int argc, const char *argv[]) { int err; char *end; - if (argc != 2) { + if (argc < 2) { errno = EINVAL; goto errormsg_help; } - config.queue_start_num = parse_uint_option(argv[1]); + config.queue_start_num = parse_numeric_option(argv[1]); if (errno != 0) goto errormsg_help; for (int i = 2; i < argc; i++) { - if (!parse_option(argv[i])) { + if (parse_option(argv[i])) { printf("Invalid option %s\n", argv[i]); goto errormsg_help; } @@ -187,13 +212,13 @@ errormsg_help: err = errno; printf("Usage: %s [OPTIONS]\n", argv[0]); printf("Options:\n"); - printf("\t--gso={0,1}\n"); printf("\t--fake-sni={ack,ttl,none}\n"); printf("\t--fake-sni-ttl=\n"); printf("\t--frag={tcp,ip,none}\n"); printf("\t--seg2delay=\n"); printf("\t--threads=\n"); - printf("\t--verbose={0,1}\n"); + printf("\t--silent\n"); + printf("\t--no-gso\n"); errno = err; if (errno == 0) errno = EINVAL; @@ -254,7 +279,7 @@ static int open_raw_socket(void) { return -1; } - int mst = pthread_mutex_init(&config.rawsocket_lock, NULL); + int mst = pthread_mutex_init(&rawsocket_lock, NULL); if (mst) { fprintf(stderr, "Mutex err: %d\n", mst); close(config.rawsocket); @@ -276,11 +301,11 @@ static int close_raw_socket(void) { if (close(config.rawsocket)) { perror("Unable to close raw socket"); - pthread_mutex_destroy(&config.rawsocket_lock); + pthread_mutex_destroy(&rawsocket_lock); return -1; } - pthread_mutex_destroy(&config.rawsocket_lock); + pthread_mutex_destroy(&rawsocket_lock); config.rawsocket = -2; return 0; @@ -314,13 +339,11 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) { errno = -ret; return ret; - } + } break; - default: - errno = EINVAL; - printf("send_raw_socket: Packet is too big but fragmentation is disabled! " - "Pass -DRAWSOCK_TCP_FSTRAT or -DRAWSOCK_IP_FSTRAT as CFLAGS " - "To enable it only for raw socket\n"); + default: + errno = EINVAL; + printf("send_raw_socket: Packet is too big but fragmentation is disabled!\n"); return -EINVAL; } @@ -359,13 +382,13 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) { } }; - pthread_mutex_lock(&config.rawsocket_lock); + pthread_mutex_lock(&rawsocket_lock); int sent = sendto(config.rawsocket, pkt, pktlen, 0, (struct sockaddr *)&daddr, sizeof(daddr)); - pthread_mutex_unlock(&config.rawsocket_lock); + pthread_mutex_unlock(&rawsocket_lock); /* The function will return -errno on error as well as errno value set itself */ if (sent < 0) sent = -errno; @@ -524,6 +547,7 @@ static int process_packet(const struct packet_data packet, struct queue_data qda perror("tcp4_frag"); goto send_verd; } + break; case FRAG_STRAT_IP: ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_offset; @@ -537,6 +561,7 @@ static int process_packet(const struct packet_data packet, struct queue_data qda perror("ip4_frag"); goto send_verd; } + break; default: ret = send_raw_socket(raw_payload, raw_payload_len); @@ -555,7 +580,7 @@ static int process_packet(const struct packet_data packet, struct queue_data qda goto send_verd; } - if (config.seg2_delay) { + if (config.seg2_delay) { struct dps_t *dpdt = malloc(sizeof(struct dps_t)); dpdt->pkt = malloc(f1len); memcpy(dpdt->pkt, frag1, f1len); @@ -564,8 +589,7 @@ static int process_packet(const struct packet_data packet, struct queue_data qda pthread_t thr; pthread_create(&thr, NULL, delay_packet_send, dpdt); pthread_detach(thr); - } - else { + } else { ret = send_raw_socket(frag1, f1len); if (ret < 0) { errno = -ret; @@ -672,9 +696,9 @@ int init_queue(int queue_num) { nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num); nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff); - if (config.use_gso) { - mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO)); - mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO)); + if (config.use_gso) { + mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO)); + mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO)); } if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { @@ -728,6 +752,7 @@ struct queue_conf { struct queue_res { int status; }; +static struct queue_res defqres = {0}; static struct queue_res threads_reses[MAX_THREADS]; @@ -760,8 +785,9 @@ int main(int argc, const char *argv[]) { break; } - if (config.seg2_delay) + if (config.seg2_delay) { printf("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", config.seg2_delay); + } switch (config.fake_sni_strategy) { case FKSN_STRAT_TTL: @@ -775,15 +801,16 @@ int main(int argc, const char *argv[]) { break; } - if (config.use_gso) + if (config.use_gso) { printf("GSO is enabled\n"); + } if (open_raw_socket() < 0) { perror("Unable to open raw socket"); exit(EXIT_FAILURE); } - struct queue_res *qres; + struct queue_res *qres = &defqres; if (config.threads == 1) { struct queue_conf tconf = {