From f3c90cdd0947ea3fdfa86a31d860525769c3d2e4 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Wed, 12 Apr 2023 22:08:34 +0800 Subject: [PATCH] style: clean code --- .gitignore | 10 ++----- CMakeLists.txt | 2 +- Makefile | 4 +-- src/statistics.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ src/statistics.h | 16 ++++++++++++ src/ua2f.c | 60 +++++++----------------------------------- 6 files changed, 98 insertions(+), 62 deletions(-) create mode 100644 src/statistics.c create mode 100644 src/statistics.h diff --git a/.gitignore b/.gitignore index a09353b..d6bd7f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,3 @@ -# Created by .ignore support plugin (hsz.mobi) -### Example user template template -### Example user template - # IntelliJ project files .idea *.iml @@ -77,10 +73,8 @@ Module.symvers Mkfile.old dkms.conf - # Project exclude paths /cmake-build-*/ -test/ - -/build/ \ No newline at end of file +/build/ +/third_party/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fdb829..db288f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ include_directories("/usr/local/include") add_compile_options(-fsanitize=address) add_link_options(-fsanitize=address) -add_executable(ua2f src/ua2f.c) +add_executable(ua2f src/ua2f.c src/statistics.c) target_link_libraries(ua2f mnl netfilter_queue ipset) diff --git a/Makefile b/Makefile index 5256050..7f29857 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ define Package/ua2f TITLE:=Change User-Agent to Fwords on the fly. URL:=https://github.com/Zxilly/UA2F DEPENDS:=+ipset +iptables-mod-conntrack-extra +iptables-mod-nfqueue \ - +libnetfilter-conntrack +libnetfilter-queue + +libnetfilter-conntrack +libnetfilter-queue +iptables-mod-filter +iptables-mod-ipopt endef define Package/ua2f/description @@ -27,7 +27,7 @@ EXTRA_LDFLAGS += -lmnl -lnetfilter_queue -lipset define Build/Compile $(TARGET_CC) $(TARGET_CFLAGS) $(TARGET_LDFLAGS) $(EXTRA_LDFLAGS) \ - $(PKG_BUILD_DIR)/ua2f.c -o $(PKG_BUILD_DIR)/ua2f + $(PKG_BUILD_DIR)/ua2f.c $(PKG_BUILD_DIR)/statistics.c -o $(PKG_BUILD_DIR)/ua2f endef define Package/ua2f/install diff --git a/src/statistics.c b/src/statistics.c new file mode 100644 index 0000000..50f04ad --- /dev/null +++ b/src/statistics.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include "statistics.h" + +static long long UserAgentPacketCount = 0; +static long long TcpPacketCount = 0; +static long long PacketWithUserAgentMark = 0; +static long long PacketWithoutUserAgentMark = 0; +static long long HttpPacketCount = 4; + +static time_t start_t; + +void init_statistics() { + start_t = time(NULL); +} + +void count_user_agent_packet() { + UserAgentPacketCount++; +} + +void count_tcp_packet() { + TcpPacketCount++; +} + +void count_packet_with_user_agent_mark() { + PacketWithUserAgentMark++; +} + +void count_packet_without_user_agent_mark() { + PacketWithoutUserAgentMark++; +} + +void count_http_packet() { + HttpPacketCount++; +} + +static char TimeStringBuffer[60]; + +char *fill_time_string(int sec) { + memset(TimeStringBuffer, 0, sizeof(TimeStringBuffer)); + if (sec <= 60) { + sprintf(TimeStringBuffer, "%d seconds", sec); + } else if (sec <= 3600) { + sprintf(TimeStringBuffer, "%d minutes and %d seconds", sec / 60, sec % 60); + } else if (sec <= 86400) { + sprintf(TimeStringBuffer, "%d hours, %d minutes and %d seconds", sec / 3600, sec % 3600 / 60, sec % 60); + } else { + sprintf(TimeStringBuffer, "%d days, %d hours, %d minutes and %d seconds", sec / 86400, sec % 86400 / 3600, + sec % 3600 / 60, + sec % 60); + } + return TimeStringBuffer; +} + +void try_print_statistics() { + if (UserAgentPacketCount / HttpPacketCount == 2 || UserAgentPacketCount - HttpPacketCount >= 8192) { + HttpPacketCount = UserAgentPacketCount; + time_t current_t = time(NULL); + syslog(LOG_INFO, + "UA2F has handled %lld ua http, %lld tcp. Set %lld mark and %lld noUA mark in %s", + UserAgentPacketCount, TcpPacketCount, PacketWithUserAgentMark, PacketWithoutUserAgentMark, + fill_time_string((int) difftime(current_t, start_t))); + } +} + + diff --git a/src/statistics.h b/src/statistics.h new file mode 100644 index 0000000..cd7e6a4 --- /dev/null +++ b/src/statistics.h @@ -0,0 +1,16 @@ +#ifndef UA2F_STATISTICS_H +#define UA2F_STATISTICS_H + +void count_user_agent_packet(); + +void count_tcp_packet(); + +void count_packet_with_user_agent_mark(); + +void count_packet_without_user_agent_mark(); + +void count_http_packet(); + +void init_statistics(); + +#endif //UA2F_STATISTICS_H diff --git a/src/ua2f.c b/src/ua2f.c index bd3d759..d8dbc8a 100644 --- a/src/ua2f.c +++ b/src/ua2f.c @@ -1,12 +1,5 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#ifndef __USE_GNU -#define __USE_GNU -#endif - #include "ipset_hook.h" +#include "statistics.h" #include #include @@ -36,16 +29,6 @@ int child_status; static struct mnl_socket *nl; static const int queue_number = 10010; -static long long UAcount = 0; -static long long tcpcount = 0; -static long long UAmark = 0; -static long long noUAmark = 0; -static long long httpcount = 4; - -static time_t start_t, current_t; - -static char timestr[60]; - char *UAstr = NULL; static struct ipset *Pipset; @@ -77,22 +60,6 @@ void *memncasemem(const void *l, size_t l_len, const void *s, size_t s_len) { return NULL; } -static char *time2str(int sec) { - memset(timestr, 0, sizeof(timestr)); - if (sec <= 60) { - sprintf(timestr, "%d seconds", sec); - } else if (sec <= 3600) { - sprintf(timestr, "%d minutes and %d seconds", sec / 60, sec % 60); - } else if (sec <= 86400) { - sprintf(timestr, "%d hours, %d minutes and %d seconds", sec / 3600, sec % 3600 / 60, sec % 60); - } else { - sprintf(timestr, "%d days, %d hours, %d minutes and %d seconds", sec / 86400, sec % 86400 / 3600, - sec % 3600 / 60, - sec % 60); - } - return timestr; -} - static int parse_attrs(const struct nlattr *attr, void *data) { const struct nlattr **tb = data; int type = mnl_attr_get_type(attr); @@ -140,14 +107,14 @@ nfq_send_verdict(int queue_num, uint32_t id, struct pkt_buff *pktb, uint32_t mar ipset_parse_line(Pipset, addcmd); //加 ipset 标记 - noUAmark++; + count_packet_without_user_agent_mark(); } } else { if (mark != 44) { nest = mnl_attr_nest_start(nlh, NFQA_CT); mnl_attr_put_u32(nlh, CTA_MARK, htonl(44)); mnl_attr_nest_end(nlh, nest); - UAmark++; + count_packet_with_user_agent_mark(); } } @@ -158,7 +125,7 @@ nfq_send_verdict(int queue_num, uint32_t id, struct pkt_buff *pktb, uint32_t mar exit(EXIT_FAILURE); } - tcpcount++; + count_tcp_packet(); pktb_free(pktb); } @@ -169,7 +136,7 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) { struct nlattr *originattr[CTA_TUPLE_MAX + 1] = {}; struct nlattr *ipattr[CTA_IP_MAX + 1] = {}; struct nlattr *portattr[CTA_PROTO_MAX + 1] = {}; - uint16_t plen; + uint16_t payloadLength; struct pkt_buff *pktb; struct iphdr *ippkhdl; struct tcphdr *tcppkhdl; @@ -235,11 +202,11 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) { ph = mnl_attr_get_payload(attr[NFQA_PACKET_HDR]); - plen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]); + payloadLength = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]); payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); - pktb = pktb_alloc(AF_INET, payload, plen, 0); //IP包 + pktb = pktb_alloc(AF_INET, payload, payloadLength, 0); //IP包 if (!pktb) { syslog(LOG_ERR, "pktb malloc failed"); @@ -283,7 +250,7 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) { if (ualength > 0) { if (nfq_tcp_mangle_ipv4(pktb, uaoffset, ualength, UAstr, ualength) == 1) { - UAcount++; //记录修改包的数量 + count_user_agent_packet(); } else { syslog(LOG_ERR, "Mangle packet failed."); pktb_free(pktb); @@ -297,15 +264,6 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data) { nfq_send_verdict(ntohs(nfg->res_id), ntohl((uint32_t) ph->packet_id), pktb, mark, noUA, addcmd); - if (UAcount / httpcount == 2 || UAcount - httpcount >= 8192) { - httpcount = UAcount; - current_t = time(NULL); - syslog(LOG_INFO, - "UA2F has handled %lld ua http, %lld tcp. Set %lld mark and %lld noUA mark in %s", - UAcount, tcpcount, UAmark, noUAmark, - time2str((int) difftime(current_t, start_t))); - } - return MNL_CB_OK; } @@ -357,7 +315,7 @@ int main(int argc, char *argv[]) { openlog("UA2F", LOG_PID, LOG_SYSLOG); - start_t = time(NULL); + init_statistics(); ipset_load_types(); Pipset = ipset_init();