Skip to content

Commit

Permalink
Update logging
Browse files Browse the repository at this point in the history
Add syslog for daemonize. This allows to log not only to the standard
output but also in system log
  • Loading branch information
Waujito committed Nov 26, 2024
1 parent d408a7b commit 4189d0f
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 130 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,13 @@ Available flags:

- `--threads=<threads number>` Specifies the amount of threads you want to be running for your program. This defaults to **1** and shouldn't be edited for normal use. But if you really want multiple queue instances of youtubeUnblock, note that you should change --queue-num to --queue balance. For example, with 4 threads, use `--queue-balance 537:540` on iptables and `queue num 537-540` on nftables.

- `--daemonize` Daemonizes the youtubeUnblock (forks and detaches it from the shell). Terminate the program with `killall youtubeUnblock`.
- `--daemonize` Daemonizes the youtubeUnblock (forks and detaches it from the shell). Terminate the program with `killall youtubeUnblock`. If you want to track the logs of youtubeUnblock in logread or journalctl, use **--syslog** flag.

- `--syslog` Redirects logs to the system log.

- `--noclose` Usable only with `--daemonize`. Will not redirect io streams to /dev/null.


- `--packet-mark=<mark>` Use this option if youtubeUnblock conflicts with other systems rely on packet mark. Note that you may want to change accept rule for iptables to follow the mark.

- `--fbegin` and `--fend` flags: youtubeUnblock supports multiple sets of strategies for specific filters. You may want to initiate a new set after the default one, like: `--sni-domains=googlevideo.com --faking-strategy=md5sum --fbegin --sni-domains=youtube.com --faking-strategy=tcp_check --fend --fbegin --sni-domains=l.google.com --faking-strategy=pastseq --fend`. Note, that the priority of these sets goes backwards: last is first, default (one that does not start with --fbegin) is last. If you start the new section, the default settings are implemented just like youtubeUnblock without any parameters. Note that the config above is just an example and won't work for you.
Expand Down
69 changes: 40 additions & 29 deletions args.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string.h>
#include "types.h"
#include "args.h"
#include "logging.h"

static char custom_fake_buf[MAX_FAKE_SIZE];

Expand All @@ -24,7 +25,8 @@ struct config_t config = {
.custom_configs_len = 0,

.daemonize = 0,
.noclose = 0
.noclose = 0,
.syslog = 0,
};

#define OPT_SNI_DOMAINS 1
Expand All @@ -39,6 +41,7 @@ struct config_t config = {
#define OPT_END_SECTION 30
#define OPT_DAEMONIZE 31
#define OPT_NOCLOSE 32
#define OPT_SYSLOG 33
#define OPT_FRAG 4
#define OPT_FRAG_SNI_REVERSE 12
#define OPT_FRAG_SNI_FAKED 13
Expand All @@ -59,7 +62,7 @@ struct config_t config = {
#define OPT_NO_GSO 8
#define OPT_QUEUE_NUM 9

#define OPT_MAX OPT_NOCLOSE
#define OPT_MAX OPT_SYSLOG

static struct option long_opt[] = {
{"help", 0, 0, 'h'},
Expand Down Expand Up @@ -91,6 +94,7 @@ static struct option long_opt[] = {
{"no-ipv6", 0, 0, OPT_NO_IPV6},
{"daemonize", 0, 0, OPT_DAEMONIZE},
{"noclose", 0, 0, OPT_NOCLOSE},
{"syslog", 0, 0, OPT_SYSLOG},
{"queue-num", 1, 0, OPT_QUEUE_NUM},
{"packet-mark", 1, 0, OPT_PACKET_MARK},
{"fbegin", 0, 0, OPT_START_SECTION},
Expand Down Expand Up @@ -161,6 +165,7 @@ void print_usage(const char *argv0) {
printf("\t--no-ipv6\n");
printf("\t--daemonize\n");
printf("\t--noclose\n");
printf("\t--syslog\n");
printf("\t--fbegin\n");
printf("\t--fend\n");
printf("\n");
Expand Down Expand Up @@ -217,6 +222,9 @@ int parse_args(int argc, char *argv[]) {
case OPT_NOCLOSE:
config.noclose = 1;
break;
case OPT_SYSLOG:
config.syslog = 1;
break;
case OPT_THREADS:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
Expand Down Expand Up @@ -486,96 +494,99 @@ int parse_args(int argc, char *argv[]) {
}

void print_welcome() {
if (config.syslog) {
printf("Logging to system log\n");
}
if (config.use_gso) {
printf("GSO is enabled\n");
lginfo("GSO is enabled\n");
}

if (config.use_ipv6) {
printf("IPv6 is enabled\n");
lginfo("IPv6 is enabled\n");
} else {
printf("IPv6 is disabled\n");
lginfo("IPv6 is disabled\n");
}

printf("Detected %d config sections\n", config.custom_configs_len + 1);
printf("The sections will be processed in order they goes in this output\n");
lginfo("Detected %d config sections\n", config.custom_configs_len + 1);
lginfo("The sections will be processed in order they goes in this output\n");

ITER_CONFIG_SECTIONS(section) {
int section_number = CONFIG_SECTION_NUMBER(section);
printf("Section #%d\n", section_number);
lginfo("Section #%d\n", section_number);

switch (section->fragmentation_strategy) {
case FRAG_STRAT_TCP:
printf("Using TCP segmentation\n");
lginfo("Using TCP segmentation\n");
break;
case FRAG_STRAT_IP:
printf("Using IP fragmentation\n");
lginfo("Using IP fragmentation\n");
break;
default:
printf("SNI fragmentation is disabled\n");
lginfo("SNI fragmentation is disabled\n");
break;
}

if (section->seg2_delay) {
printf("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", section->seg2_delay);
lginfo("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", section->seg2_delay);
}

if (section->fake_sni) {
printf("Fake SNI will be sent before each target client hello\n");
lginfo("Fake SNI will be sent before each target client hello\n");
} else {
printf("Fake SNI is disabled\n");
lginfo("Fake SNI is disabled\n");
}

if (section->frag_sni_reverse) {
printf("Fragmentation Client Hello will be reversed\n");
lginfo("Fragmentation Client Hello will be reversed\n");
}

if (section->frag_sni_faked) {
printf("Fooling packets will be sent near the original Client Hello\n");
lginfo("Fooling packets will be sent near the original Client Hello\n");
}

if (section->fake_sni_seq_len > 1) {
printf("Faking sequence of length %d will be built as fake sni\n", section->fake_sni_seq_len);
lginfo("Faking sequence of length %d will be built as fake sni\n", section->fake_sni_seq_len);
}

switch (section->faking_strategy) {
case FAKE_STRAT_TTL:
printf("TTL faking strategy will be used with TTL %d\n", section->faking_ttl);
lginfo("TTL faking strategy will be used with TTL %d\n", section->faking_ttl);
break;
case FAKE_STRAT_RAND_SEQ:
printf("Random seq faking strategy will be used\n");
printf("Fake seq offset set to %u\n", section->fakeseq_offset);
lginfo("Random seq faking strategy will be used\n");
lginfo("Fake seq offset set to %u\n", section->fakeseq_offset);
break;
case FAKE_STRAT_TCP_CHECK:
printf("TCP checksum faking strategy will be used\n");
lginfo("TCP checksum faking strategy will be used\n");
break;
case FAKE_STRAT_PAST_SEQ:
printf("Past seq faking strategy will be used\n");
lginfo("Past seq faking strategy will be used\n");
break;
case FAKE_STRAT_TCP_MD5SUM:
printf("md5sum faking strategy will be used\n");
lginfo("md5sum faking strategy will be used\n");
break;
}

if (section->fk_winsize) {
printf("Response TCP window will be set to %d with the appropriate scale\n", section->fk_winsize);
lginfo("Response TCP window will be set to %d with the appropriate scale\n", section->fk_winsize);
}

if (section->synfake) {
printf("Fake SYN payload will be sent with each TCP request SYN packet\n");
lginfo("Fake SYN payload will be sent with each TCP request SYN packet\n");
}

if (section->quic_drop) {
printf("All QUIC packets will be dropped\n");
lginfo("All QUIC packets will be dropped\n");
}

if (section->sni_detection == SNI_DETECTION_BRUTE) {
printf("Server Name Extension will be parsed in the bruteforce mode\n");
lginfo("Server Name Extension will be parsed in the bruteforce mode\n");
}

if (section->all_domains) {
printf("All Client Hello will be targeted by youtubeUnblock!\n");
lginfo("All Client Hello will be targeted by youtubeUnblock!\n");
} else {
printf("Target sni domains: %s\n", section->domains_str);
lginfo("Target sni domains: %s\n", section->domains_str);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct config_t {
int daemonize;
// Same as daemon() noclose
int noclose;
int syslog;

#define VERBOSE_INFO 0
#define VERBOSE_DEBUG 1
Expand Down
10 changes: 5 additions & 5 deletions kytunblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {

NETBUF_ALLOC(buff1, MAX_PACKET_SIZE);
if (!NETBUF_CHECK(buff1)) {
lgerror("Allocation error", -ENOMEM);
lgerror(-ENOMEM, "Allocation error");
return -ENOMEM;
}
NETBUF_ALLOC(buff2, MAX_PACKET_SIZE);
if (!NETBUF_CHECK(buff2)) {
lgerror("Allocation error", -ENOMEM);
lgerror(-ENOMEM, "Allocation error");
NETBUF_FREE(buff2);
return -ENOMEM;
}
Expand Down Expand Up @@ -309,7 +309,7 @@ static NF_CALLBACK(ykb_nf_hook, skb) {

ret = skb_linearize(skb);
if (ret < 0) {
lgerror("Cannot linearize", ret);
lgerror(ret, "Cannot linearize");
goto accept;
}

Expand Down Expand Up @@ -360,7 +360,7 @@ static int __init ykb_init(void) {
for_each_net(n) {
ret = nf_register_net_hook(n, &ykb6_nf_reg);
if (ret < 0)
lgerror("bad rat",ret);
lgerror(ret, "bad rat");
}
#else
nf_register_hook(&ykb6_nf_reg);
Expand All @@ -374,7 +374,7 @@ static int __init ykb_init(void) {
for_each_net(n) {
ret = nf_register_net_hook(n, &ykb_nf_reg);
if (ret < 0)
lgerror("bad rat",ret);
lgerror(ret, "bad rat");
}
#else
nf_register_hook(&ykb_nf_reg);
Expand Down
39 changes: 31 additions & 8 deletions logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,65 @@
#include "config.h"

#define LOG_LEVEL (config.verbose)
#define USE_SYSLOG (config.syslog)

#ifdef KERNEL_SPACE
#include <linux/kernel.h>
#include <linux/module.h>
#define printf pr_info
#define perror pr_err
#define lgerror(msg, ret, ...) __extension__ ({ \

#define log_message(level, msg, ...) \
(printf(msg, ##__VA_ARGS__))

#define lgerror(ret, msg, ...) __extension__ ({ \
pr_err(msg ": %d\n", ##__VA_ARGS__, ret); \
})
#else
#include <stdio.h> // IWYU pragma: export
#include <errno.h>
#define lgerror(msg, ret, ...) __extension__ ({ \
#include <syslog.h>

#define log_message(level, msg, ...) \
(config.syslog ? (void)(syslog((level), msg, ##__VA_ARGS__)) : (void)(printf(msg, ##__VA_ARGS__)))

#define lgerror(ret, msg, ...) __extension__ ({ \
errno = -(ret); \
printf(msg ": %s\n", ##__VA_ARGS__, strerror(errno)); \
log_message(LOG_ERR, msg ": %s\n", ##__VA_ARGS__, strerror(errno)); \
})
#endif /* PROGRAM_SPACE */

#define lgerr(msg, ...) \
(log_message(LOG_ERR, msg, ##__VA_ARGS__))

#define lgwarning(msg, ...) \
(log_message(LOG_WARN, msg, ##__VA_ARGS__))


#define lginfo(msg, ...) \
(log_message(LOG_INFO, msg, ##__VA_ARGS__))

#define print_message(...) \
(lginfo(__VA_ARGS__))

#define lgdebug(msg, ...) \
(LOG_LEVEL >= VERBOSE_DEBUG ? printf(msg, ##__VA_ARGS__) : 0)
(LOG_LEVEL >= VERBOSE_DEBUG ? log_message(LOG_INFO, msg, ##__VA_ARGS__) : (void)0)

#define lgdebugmsg(msg, ...) lgdebug(msg "\n", ##__VA_ARGS__)


#define lgtrace(msg, ...) \
(LOG_LEVEL >= VERBOSE_TRACE ? printf(msg, ##__VA_ARGS__) : 0)
(LOG_LEVEL >= VERBOSE_TRACE ? log_message(LOG_INFO, msg, ##__VA_ARGS__) : (void)0)

#define lgtracemsg(msg, ...) lgtrace(msg "\n", __VA_ARGS__)

#define lgtrace_start(msg, ...) \
(LOG_LEVEL >= VERBOSE_TRACE ? printf("[TRACE] " msg " ( ", ##__VA_ARGS__) : 0)
(LOG_LEVEL >= VERBOSE_TRACE ? log_message(LOG_INFO, "[TRACE] " msg " ( ", ##__VA_ARGS__) : (void)0)

#define lgtrace_addp(msg, ...) \
(LOG_LEVEL >= VERBOSE_TRACE ? printf(msg", ", ##__VA_ARGS__) : 0)
(LOG_LEVEL >= VERBOSE_TRACE ? log_message(LOG_INFO, msg", ", ##__VA_ARGS__) : (void)0)

#define lgtrace_end() \
(LOG_LEVEL >= VERBOSE_TRACE ? printf(") \n") : 0)
(LOG_LEVEL >= VERBOSE_TRACE ? log_message(LOG_INFO, ") \n") : (void)0)

#endif /* LOGGING_H */
Loading

0 comments on commit 4189d0f

Please sign in to comment.