-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsmasq_route.c
271 lines (264 loc) · 9.48 KB
/
dnsmasq_route.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#ifndef GIT_VER
#define GIT_VER "test"
#endif
#define ipv4_u32(a, b, c, d) ((uint32_t)(a<<24) | (b<<16) | (c<<8) | d)
uint32_t localnet[]={ /* 内网地址表 */
ipv4_u32(0, 0, 0, 0), ipv4_u32(0, 255, 255, 255), /* 0.0.0.0/8 */
ipv4_u32(10, 0, 0, 0), ipv4_u32(10, 255, 255, 255), /* 10.0.0.0/8 */
ipv4_u32(100, 64, 0, 0), ipv4_u32(100, 127, 255, 255), /* 100.64.0.0/10 */
ipv4_u32(127, 0, 0, 0), ipv4_u32(127, 255, 255, 255), /* 127.0.0.0/8 */
ipv4_u32(169, 254, 0, 0), ipv4_u32(169, 254, 255, 255), /* 169.254.0.0/16 */
ipv4_u32(172, 16, 0, 0), ipv4_u32(172, 32, 255, 255), /* 172.16.0.0/12 */
ipv4_u32(192, 0, 0, 0), ipv4_u32(192, 0, 0, 255), /* 192.0.0.0/24 */
ipv4_u32(192, 0, 2, 0), ipv4_u32(192, 0, 2, 255), /* 192.0.2.0/24 */
ipv4_u32(192, 88, 99, 0), ipv4_u32(192, 88, 99, 255), /* 192.88.99.0/24 */
ipv4_u32(192, 168, 0, 0), ipv4_u32(192, 168, 255, 255), /* 192.168.0.0/16 */
ipv4_u32(198, 18, 0, 0), ipv4_u32(198, 19, 255, 255), /* 198.18.0.0/15 */
ipv4_u32(198, 51, 100, 0), ipv4_u32(198, 51, 100, 255), /* 198.51.100.0/24 */
ipv4_u32(203, 0, 113, 0), ipv4_u32(203, 0, 113, 255), /* 203.0.113.0/24 */
ipv4_u32(224, 0, 0, 0), ipv4_u32(239, 255, 255, 255), /* 224.0.0.0/4 */
ipv4_u32(233, 252, 0, 0), ipv4_u32(233, 252, 0, 255), /* 233.252.0.0/24 */
/* ipv4_u32(240, 0, 0, 0), ipv4_u32(255, 255, 255, 255), // 240.0.0.0/4 */
ipv4_u32(255, 255, 255, 255), ipv4_u32(255, 255, 255, 255), /* 255.255.255.255/32 */
};
void log_scan();
void ip_rule(const char * dip);
#define PID_SIZE 20
uint32_t pids[PID_SIZE];
extern char * optarg;
char * buf0, * remote_ip, * dns_server, skip[100], *table, from_net[sizeof("from 192.168.123.123/255.255.255.255 ")];
#define RULES_SIZE 4096
struct rules {
uint8_t hour;
uint32_t ip;
} rules[RULES_SIZE];
bool v = false;
bool route_clean = false;
void add_key(const uint32_t pid) { //将转发到8.8.4.4的请求id 记录下来,
uint32_t dat;
for(uint8_t i = 0; i < PID_SIZE; i++) {
if(pids[i] == 0){
pids[i] = pid;
return;
}
if(i != 0 && pids[0] > pids[i]){
dat = pids[0];
pids[0] = pids[i];
pids[i] = dat;
}else if(i != PID_SIZE - 1 && pids[PID_SIZE - 1] < pids[i]) {
dat = pids[PID_SIZE - 1];
pids[PID_SIZE - 1] = pids[i];
pids[i] = dat;
}
}
if(pid > pids[PID_SIZE - 1]) //如果没有溢出就覆盖最小的pid
pids[0] = pid;
else //如果溢出,就覆盖最大的pid, 这里有极小的概率(10的11次方分之一),会丢一次回应,可以忽略。
pids[PID_SIZE - 1] = pid;
}
bool in_key(const uint32_t pid) { //看回应的pid,是否在转发到8.8.4.4的记录中,
for(uint8_t i = 0; i < PID_SIZE; i++)
if(pids[i] == pid)
return true;
return false;
}
void update_rule_list() {
FILE *dfp;
char buf[1024];
uint16_t count = 0;
uint32_t metric;
uint8_t ip[4];
snprintf(buf, sizeof(buf),"ip rule list %s |grep ^290 |tr -d ':' >/tmp/dnsmasq_rule.list", from_net); //初始化已经存在的清单
system(buf);
dfp = fopen("/tmp/dnsmasq_rule.list", "r");
if(!dfp) return;
for(count = 0; count < RULES_SIZE; count++){
if(rules[count].ip == 0)
break;
}
while(!feof(dfp)) {
/*
29010: from all to 120.121.121.140 lookup 107
29010: from all to 120.121.121.141 lookup 107
*/
int rc = fscanf(dfp,"%d %99s %99s %99s %hhd.%hhd.%hhd.%hhd", &metric, skip, skip, skip, &ip[0], &ip[1], &ip[2], &ip[3]);
fgets(skip, sizeof(skip), dfp);
if(rc != 8) continue;
rules[count].hour = metric % 100;
rules[count].ip = (uint32_t) (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) || ip[3];
count ++;
}
fclose(dfp);
if(v)
printf("\r\nload ip rule %d\r\n",count);
remove("/tmp/dnsmasq_rule.list");
}
int main(int argc, char * argv[])
{
int opt = 0;
bool h = false;
uint8_t ip[8];
uint8_t count = 0;
memset(from_net, 0, sizeof(from_net));
while((opt = getopt(argc, argv, "cChHvVd:r:n:s:t:")) != -1) {
switch(opt) {
case 'c':
case 'C':
route_clean = true;
break;
case 'h':
case 'H': //帮助信息
h = true;
break;
case 'v':
v = true;
break;
case 'V':
printf("Version:%s\r\n",GIT_VER);
break;
case 'd':
dns_server = optarg;
break;
case 'r':
remote_ip = optarg;
break;
case 'n':
if(sscanf(optarg, "%hhd.%hhd.%hhd.%hhd", &ip[0], &ip[1], &ip[2], &ip[3]) == 4) {
rules[count].ip = ipv4_u32(ip[0], ip[1], ip[2], 0); //跳过这些目标地址
if(rules[count].ip != 0) {
if(v) printf("skip ip:%d.%d.%d.0/24\r\n", ip[0], ip[1], ip[2]);
rules[count].hour = 25; //不会被清理
count++;
}
}
break;
case 's':
if(sscanf(optarg, "%hhd.%hhd.%hhd.%hhd/%hhd", &ip[0], &ip[1], &ip[2], &ip[3], &ip[4]) == 5 && ip[4] <= 32) {
snprintf(from_net, sizeof(from_net), "from %d.%d.%d.%d/%d",ip[0], ip[1], ip[2], ip[3], ip[4]);
} else if(sscanf(optarg, "%hhd.%hhd.%hhd.%hhd/%hhd.%hhd.%hhd.%hhd", &ip[0], &ip[1], &ip[2], &ip[3], &ip[4], &ip[5], &ip[6], &ip[7]) == 8) {
snprintf(from_net, sizeof(from_net), "from %d.%d.%d.%d/%d.%d.%d.%d",ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]);
}
break;
case 't':
table = optarg;
break;
}
}
if( h || table == NULL || (remote_ip == NULL && dns_server == NULL)) {
printf("\r\nUsage:\r\nlogread -f -S 128000 |\\\r\n%s -d dns_server -s 192.168.0.0/24 -r remote_ip -t 107\r\n\r\n -c 23 hours clean route\r\n -d remote dns server\r\n -s sourceip/netmask\r\n -r remote route ip\r\n -n skip target ip\r\n -t route table name\r\n -v verbose mode\r\n -V display version\r\n\r\n",argv[0]);
return 1;
}
if(remote_ip == NULL && dns_server != NULL)
remote_ip = dns_server;
if(remote_ip != NULL && dns_server == NULL)
dns_server = remote_ip;
if(table != NULL) {//rule方式
snprintf(skip, sizeof(skip), "ip ro add default via %s table %s metric 100 >/dev/null 2>/dev/null", remote_ip, table);
if(v)
printf(skip);
system(skip);
update_rule_list();
}
memset(pids,0,sizeof(pids));
while(1){
log_scan();
}
return 0;
}
void log_scan() {
char buf[300],proc[31],sip[100],domain[100],to[100],dip[100];
uint32_t pid;
int rc;
while(1) {
/*
Sun Dec 26 15:29:33 2021 daemon.info dnsmasq[11997]:xxxx
*/
for( uint8_t i = 0; i < 10; i++) {
rc = scanf("%99s",buf);
if(rc != 1)break;
buf[7]=0;
if(strcmp(buf,"dnsmasq") == 0){
break;
}else
continue;
}
if(rc <= 0) continue;
/*
dnsmasq[12670]: 177526 192.168.12.13/36330 query[A] www.google.com from 192.168.12.13
dnsmasq[12670]: 177526 192.168.12.13/36330 forwarded www.google.com to 8.8.4.4
dnsmasq[12670]: 177526 192.168.12.13/36330 reply www.google.com is 142.250.81.228
dnsmasq[12670]: 177527 192.168.12.13/40934 query[AAAA] www.google.com from 192.168.12.13
dnsmasq[12670]: 177527 192.168.12.13/40934 forwarded www.google.com to 8.8.4.4
dnsmasq[12670]: 177527 192.168.12.13/40934 reply www.google.com is 2607:f8b0:4006:817::2004
dnsmasq[2302]: 12173 192.168.12.1/55747 cached google.com is 173.194.219.101 */
rc = scanf("%d %99s %30s %99s %99s %99s",
&pid, sip, proc, domain, to, dip);
buf0 = strstr(dip,"\n");
if(buf0)
buf0[0] = 0;
buf0=strstr(dip,"\r");
if(buf0)
buf0[0]=0;
if(strcmp(proc, "forwarded") == 0 && strncmp(dip,dns_server,strlen(dns_server)) == 0) { //找出转发到8.8.4.4的请求
add_key(pid); //保存请求id
}else{
if(strcmp(to, "is") == 0) {
if(in_key(pid) //如果是记录的请求id的回应, 就处理
&& !index(dip, ':')){ //去掉ipv6回应
ip_rule(dip); //路由名称不为空,就是修改rule路由规则
}
}
}
}
}
void ip_rule(const char * dip) {
char buf[2048], dest[10];
uint8_t ip[4];
uint32_t dip32;
time_t time0;
struct tm tm;
if(sscanf(dip, "%hhd.%hhd.%hhd.%hhd",&ip[0],&ip[1],&ip[2],&ip[3]) != 4) return;
ip[3] = 0; //目标使用c段
time(&time0);
localtime_r(&time0, &tm);
uint16_t count = 0;
if(route_clean) { //命令行-c 需要删除
for(count = 0; count < RULES_SIZE; count++) {
if(rules[count].ip == 0) break;
if(rules[count].hour == ((tm.tm_hour + 1) % 24)) { //清理23小时前的rule
for(uint16_t i = count; i < RULES_SIZE; i ++) {
if(rules[i + 1].ip == 0) break;
rules[count] = rules[i + 1];
}
//删除路由
snprintf(buf,sizeof(buf), "ip rule del %s to %s/32", from_net, dest);
if(v)
printf("%s\r\n",buf);
system(buf);
}
}
}
dip32 = (uint32_t) (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];
for(count = 0; count < RULES_SIZE; count++) {
if(rules[count].ip == 0) break;
if(rules[count].ip == dip32) return; //已经存在, 不需要添加
}
for(uint16_t i = 0; i < sizeof(localnet) / sizeof(uint32_t); i = i + 2) {
if(localnet[i] <= dip32 && localnet[i + 1] >= dip32) return; /* 忽略内网地址 */
}
snprintf(buf,sizeof(buf),"ip rule add %s to %d.%d.%d.0/24 lookup %s pref %d", from_net, ip[0], ip[1], ip[2], table, 29000 + tm.tm_hour); //用metric 来区分每个小时添加的路由,方便定期清理
if(v)
printf("%s\r\n",buf);
system(buf);
if(count < RULES_SIZE && rules[count].ip == 0) {
rules[count].hour = tm.tm_hour;
rules[count].ip = dip32;
}
}