Add youtubeUnblock statistics

The statistis will be printed on exit in userspace version. In kernel
space version, use `cat /proc/kyoutubeUnblock`.

The feature was proposed by @IceCat74 in #220
This commit is contained in:
Vadim Vetrov
2025-02-01 20:36:13 +03:00
parent a03d05ca19
commit 42917a75fc
5 changed files with 69 additions and 0 deletions

View File

@@ -435,6 +435,12 @@ cat /sys/module/kyoutubeUnblock/parameters/parameters
and check all the parameters configured.
You can check up the statistics of youtubeUnblock with
```sh
sudo cat /proc/kyoutubeUnblock
```
### Building kernel module
#### Building on host system

View File

@@ -26,6 +26,7 @@
#include "getopt.h"
#include "raw_replacements.h"
struct statistics_data global_stats;
/**
* Logging definitions

View File

@@ -339,4 +339,13 @@ struct packet_data {
struct ytb_conntrack yct;
};
struct statistics_data {
unsigned long all_packet_counter;
unsigned long packet_counter;
unsigned long target_counter;
unsigned long sent_counter;
};
extern struct statistics_data global_stats;
#endif /* YTB_CONFIG_H */

View File

@@ -30,6 +30,7 @@
#include <linux/net.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/proc_fs.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
@@ -329,6 +330,8 @@ erret_lc:
return ret;
}
++global_stats.sent_counter;
int ipvx = netproto_version(pkt, pktlen);
if (ipvx == IP4VERSION) {
@@ -483,6 +486,8 @@ static NF_CALLBACK(ykb_nf_hook, skb) {
struct config_t *config = cur_config;
kref_get(&config->refcount);
++global_stats.all_packet_counter;
if ((skb->mark & config->mark) == config->mark) {
goto send_verdict;
}
@@ -523,12 +528,14 @@ static NF_CALLBACK(ykb_nf_hook, skb) {
pd.payload_len = skb->len;
int vrd = process_packet(config, &pd);
++global_stats.packet_counter;
switch(vrd) {
case PKT_ACCEPT:
nf_verdict = NF_ACCEPT;
break;
case PKT_DROP:
++global_stats.target_counter;
nf_verdict = NF_STOLEN;
kfree_skb(skb);
break;
@@ -580,6 +587,22 @@ static struct pernet_operations ykb_pernet_ops = {
};
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0) */
#ifdef CONFIG_PROC_FS
static int stats_show(struct seq_file *s, void *v) {
seq_printf(s, "youtubeUnblock stats: \n"
"\tCatched: %ld packets\n"
"\tProcessed: %ld packets\n"
"\tTargetted: %ld packets\n"
"\tSent over socket %ld packets\n",
global_stats.all_packet_counter, global_stats.packet_counter,
global_stats.target_counter, global_stats.sent_counter);
return 0;
}
#endif
static int __init ykb_init(void) {
int ret;
@@ -615,6 +638,13 @@ static int __init ykb_init(void) {
}
#endif /* NO_IPV6 */
#ifdef CONFIG_PROC_FS
if (!proc_create_single_data("kyoutubeUnblock", 0440, NULL,
stats_show, NULL)) {
lgwarning("kyoutubeUnblock procfs entry creation failed");
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
ret = register_pernet_subsys(&ykb_pernet_ops);
#else
@@ -651,6 +681,10 @@ static void __exit ykb_destroy(void) {
close_raw6_socket();
#endif
#ifdef CONFIG_PROC_FS
remove_proc_entry("kyoutubeUnblock", NULL);
#endif
close_raw_socket();
kref_put(&cur_config->refcount, config_release);
lginfo("youtubeUnblock kernel module destroyed.\n");

View File

@@ -46,6 +46,7 @@
#include <linux/netfilter.h>
#include <pthread.h>
#include <sys/socket.h>
#include <signal.h>
#include "config.h"
#include "mangle.h"
@@ -546,6 +547,7 @@ erret_lc:
return ret;
}
++global_stats.sent_counter;
int ipvx = netproto_version(pkt, pktlen);
if (ipvx == IP4VERSION) {
@@ -640,6 +642,8 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) {
uint16_t l3num;
uint32_t id;
++global_stats.all_packet_counter;
if (nfq_nlmsg_parse(nlh, attr) < 0) {
lgerror(-errno, "Attr parse");
return MNL_CB_ERROR;
@@ -694,8 +698,11 @@ ct_out:
ret = process_packet(cur_config, &packet);
++global_stats.packet_counter;
switch (ret) {
case PKT_DROP:
++global_stats.target_counter;
nfq_nlmsg_verdict_put(verdnlh, id, NF_DROP);
break;
default:
@@ -875,6 +882,16 @@ struct instance_config_t instance_config = {
.send_delayed_packet = delay_packet_send,
};
void sigint_handler(int s) {
lginfo("youtubeUnblock stats: catched %ld packets, "
"processed %ld packets, "
"targetted %ld packets, sent over socket %ld packets",
global_stats.all_packet_counter, global_stats.packet_counter,
global_stats.target_counter, global_stats.sent_counter);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
int ret;
struct config_t config;
@@ -893,6 +910,8 @@ int main(int argc, char *argv[]) {
parse_global_lgconf(&config);
cur_config = &config;
signal(SIGINT, sigint_handler);
signal(SIGTERM, sigint_handler);
if (open_raw_socket() < 0) {
lgerror(-errno, "Unable to open raw socket");