基于前缀树的一种多规则高性能匹配方法

背景

snort 的匹配过程 是规则逐条逐条匹配, 这种模式极大的浪费了性能,最好的情况下,至少要执行N次运行,才能走完规则集合。

研究一种将规则局部复用,合并规则之间相同的运算,快速收敛,提高执行效率。

启蒙

启蒙来自AC算法。
但AC算法不适用于Snort情景:Snort是将网络数据结构化,然后执行规则匹配。
这是一种主动运算。

AC算法是 输入字符 驱动AC自动机达到多模匹配功能。
这是一种被动运算。

那能不能在AC的基础之上,将Snort规则华为AC上的各个节点。主动遍历这个前缀树呢?

代码

基于前缀树的多模规则匹配

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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
root@localhost:~# cat trie.cpp
#include <stdio.h>
#include <unordered_map>
#include <map>
#include <vector>
#include <iostream>

#include <iostream>
#include <iomanip>
#include <functional>
#include <string>
#include <unordered_set>

#define RuleOpcode "=="

struct http_recoder
{
const char* http_method;
const char* http_uri;
const char* http_version;
const char* http_cookie;
const char* http_paylaod;
const char* http_paylaod_len;
const char* http_host;
const char* http_connect;
const char* http_accept;
const char* http_useragent;
const char* http_origin;
const char* http_referer;
const char* http_acceptencoding;
const char* http_acceptlanguage;

const char* http_code;
const char* http_contenttype;
const char* http_contentlength;
const char* http_contentencode;
const char* http_content;
const char* http_server;
const char* http_date;
const char* http_location;
};



// 规则结构
class RuleUnit
{
public:
RuleUnit(){}
~RuleUnit(){}

public:
std::string field; //字段名
std::string value; //匹配值
int weight; //权重
};

//前缀树结构
class TrieNode
{
public:
TrieNode(){}
~TrieNode(){}

public:
std::string field; //字段名
std::string opcode; //操作码
std::string value; //匹配值
int is_end; //终结符
int ruleid; //规则ID号
int index; //索引

TrieNode *fail; //回溯
TrieNode *next; //前进
std::map<std::string, TrieNode*> child;
};


int RuleTrieBuildFail(TrieNode *TrieRoot)
{
for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
if(TrieRoot->child.size() > 1)
{
it++;
if(it != TrieRoot->child.end())
{
Node->fail = it->second;
}
else
{
Node->fail = TrieRoot->fail;
}
it--;
}
else
{
Node->fail = TrieRoot->fail;
}

RuleTrieBuildFail(Node);
}
return 0;
}

int RuleTrieBuildNext(TrieNode *TrieRoot)
{
for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;

if(it == TrieRoot->child.begin())
{
TrieRoot->next = Node;
}

RuleTrieBuildNext(Node);
}

if(0 == TrieRoot->child.size())
{
TrieRoot->next = TrieRoot->fail;
}

return 0;
}


int RuleTrieBuildIndex(TrieNode *TrieRoot)
{
static int index = 0;
for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
Node->index = index++;
RuleTrieBuildIndex(Node);
}
return index;
}

int RuleTrieBuildShow(TrieNode *TrieRoot)
{
for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
printf("索引 %02d 当前节点:%p 父节点:%p 前进节点:%p 失败节点:%p ", Node->index, Node, TrieRoot, Node->next, Node->fail);
std::cout << Node->field << Node->opcode << Node->value << "\n";
RuleTrieBuildShow(Node);
}

return 0;
}


int RuleTrieDraw_init_terminal(TrieNode *TrieRoot, FILE *FileTrie)
{
if(1 == TrieRoot->is_end)
{
std::string label = TrieRoot->field + TrieRoot->opcode + TrieRoot->value ;
fprintf(FileTrie," \"%p\" [label=\"id=%d \\n %s\"]\n", TrieRoot, TrieRoot->ruleid, label.c_str());
}

for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
RuleTrieDraw_init_terminal(Node, FileTrie);
}

return 0;
}

int RuleTrieDraw_init_unit(TrieNode *TrieRoot, FILE *FileTrie)
{
for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
if(Node->child.size() && 0 == Node->is_end)
{
std::string label = Node->field + Node->opcode + Node->value;
fprintf(FileTrie," \"%p\" [label=\"%s\"]\n", Node, label.c_str());
}
RuleTrieDraw_init_unit(Node, FileTrie);
}
return 0;
}

int RuleTrieDraw_init(TrieNode *TrieRoot, FILE *FileTrie)
{
fprintf(FileTrie, "//画图命令: dot -Tpdf trie.dot -o trie.pdf\n");
fprintf(FileTrie, "digraph G {\n");
fprintf(FileTrie, "charset=\"UTF-8\"\n");
fprintf(FileTrie, "fontname=\"Helvetica,Arial,sans-serif\"\n");
fprintf(FileTrie, "node [fontname=\"Helvetica,Arial,sans-serif\"]\n");
fprintf(FileTrie, "edge [fontname=\"Helvetica,Arial,sans-serif\"]\n");
fprintf(FileTrie, "node [shape=\"box\"]\n");

fprintf(FileTrie, "{ node [shape=circle style=filled color=\"#FF0000\"]\n");
fprintf(FileTrie, " \"%p\" [label=\"\"]\n", TrieRoot);
fprintf(FileTrie, "}\n");

fprintf(FileTrie, "{ node [style=filled color=\"#000000\" fillcolor=\"#00FF00\"]\n");
RuleTrieDraw_init_terminal(TrieRoot, FileTrie);
fprintf(FileTrie, "}\n");

fprintf(FileTrie, "{ node []\n");
RuleTrieDraw_init_unit(TrieRoot, FileTrie);
fprintf(FileTrie, "}\n");

return 0;
}


int RuleTrieDraw_Fail(TrieNode *TrieRoot, FILE *FileTrie)
{
fprintf(FileTrie, " \"%p\" -> \"%p\" [color=\"#FF0000\" style=dotted]\n", TrieRoot, TrieRoot->fail);

for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
RuleTrieDraw_Fail(Node, FileTrie);
}

return 0;
}

int RuleTrieDraw_Next(TrieNode *TrieRoot, FILE *FileTrie)
{
if(TrieRoot->next != TrieRoot->fail)
{
fprintf(FileTrie, " \"%p\" -> \"%p\" [color=\"#00FF00\"]\n", TrieRoot, TrieRoot->next);
}

for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
RuleTrieDraw_Next(Node, FileTrie);
}

return 0;
}

int RuleTrieDraw_Fork(TrieNode *TrieRoot, FILE *FileTrie)
{
for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
if(it != TrieRoot->child.begin())
{
fprintf(FileTrie, " \"%p\" -> \"%p\" [color=\"#00FF00\" style=dashed]\n", TrieRoot, Node);
}
RuleTrieDraw_Fork(Node, FileTrie);
}

return 0;
}


int RuleTrieDraw_Refe_(TrieNode *TrieRoot, std::map<std::string, std::vector<TrieNode *> > &ref)
{
for(auto it = TrieRoot->child.begin(); it != TrieRoot->child.end(); it++)
{
TrieNode *Node = it->second;
std::string node_function = Node->field + Node->opcode + Node->value;
ref[node_function].push_back(Node);
RuleTrieDraw_Refe_(Node, ref);
}

return 0;
}


int RuleTrieDraw_Refe(TrieNode *TrieRoot, FILE *FileTrie)
{
std::map<std::string, std::vector<TrieNode *> > ref;
RuleTrieDraw_Refe_(TrieRoot, ref);

for(auto it = ref.begin(); it != ref.end(); it++)
{
if(it->second.size() > 1)
{
auto &value_ref = it->second;
TrieNode *prev = value_ref[0];
for(size_t i = 1; i < value_ref.size(); i++)
{
TrieNode *pos = value_ref[i];
fprintf(FileTrie, " ");
fprintf(FileTrie, "\"%p\" -> \"%p\" [color=\"#0000FF\" style=dotted dir=both]\n", prev, pos);
prev = pos;
}
}
}

return 0;
}

int RuleTrieDraw_fini(TrieNode *TrieRoot, FILE *FileTrie)
{
fprintf(FileTrie, "}\n");
return 0;
}


TrieNode* RuleTrieBuild(std::vector<std::map<std::string, std::string>* > *RuleManeger)
{
// 计算 HASH, 累计
std::unordered_map<std::string, int> RuleNodeHash; //存储节点统计
for(size_t i = 0 ; i < RuleManeger->size(); i++)
{
printf("规则 %zd ", i);
for(auto it = RuleManeger->at(i)->begin(); it != RuleManeger->at(i)->end(); it++)
{
std::cout << " "<<it->first << RuleOpcode << it->second;
RuleNodeHash[it->first + RuleOpcode + it->second]++; //HASH累计
}
printf("\n");
}
printf("\n");

//序列结构转换
std::vector< std::vector<RuleUnit>* > RuleRoot;
for(size_t i = 0 ; i < RuleManeger->size(); i++)
{
std::vector<RuleUnit> *newRuleList = new std::vector<RuleUnit>;
for(auto it = RuleManeger->at(i)->begin(); it != RuleManeger->at(i)->end(); it++)
{
RuleUnit newRuleUnit;
newRuleUnit.field = it->first;
newRuleUnit.value = it->second;
newRuleUnit.weight = RuleNodeHash[it->first + RuleOpcode + it->second];
newRuleList->push_back(newRuleUnit);
}
RuleRoot.push_back(newRuleList);
}

//基于序列 内部排序
for(size_t i = 0 ; i < RuleRoot.size(); i++)
{
std::vector<RuleUnit>* list = RuleRoot.at(i);
for(size_t j = 0; j < list->size(); j++)
{
for(size_t k = 0; k < list->size(); k++)
{
// 根据权重移动
if(list->at(k).weight < list->at(j).weight)
{
RuleUnit Unit = list->at(k);
list->at(k) = list->at(j);
list->at(j) = Unit;
}

// 如果权重一样大
std::hash<std::string> str_hash;
if(list->at(k).weight == list->at(j).weight &&
str_hash(list->at(k).field + list->at(k).value) > str_hash(list->at(j).field + list->at(j).value))
{
RuleUnit Unit = list->at(k);
list->at(k) = list->at(j);
list->at(j) = Unit;
}
}
}
}

//打印输出 内部排序
printf("规则内排序\n");
for(size_t i = 0 ; i < RuleRoot.size(); i++)
{
std::vector<RuleUnit>* list = RuleRoot.at(i);
for(size_t j = 0; j < list->size(); j++)
{
std::cout << list->at(j).weight <<" "<< list->at(j).field << RuleOpcode << list->at(j).value << " ";
}
printf("\n");
}
printf("\n");


//构造前缀树
printf("构造前缀树\n");
TrieNode *TrieRoot = new TrieNode;
TrieRoot->fail = TrieRoot;

for(size_t i = 0 ; i < RuleRoot.size(); i++)
{
TrieNode *TrieUnit = TrieRoot;
std::vector<RuleUnit>* list = RuleRoot.at(i);
for(size_t j = 0; j < list->size(); j++)
{
std::string TrieNodeKey = list->at(j).field + RuleOpcode + list->at(j).value;
if(TrieUnit->child.find(TrieNodeKey) != TrieUnit->child.end())
{
TrieUnit = TrieUnit->child.at(TrieNodeKey);
std::cout << "复用!!"<< TrieNodeKey << std::endl;
continue;
}

std::cout << "构造: "<< TrieNodeKey << std::endl;
TrieNode *NewTrieNode = new TrieNode;
NewTrieNode->field = list->at(j).field;
NewTrieNode->opcode = RuleOpcode;
NewTrieNode->value = list->at(j).value;

TrieUnit->child[TrieNodeKey] = NewTrieNode;
TrieUnit = NewTrieNode;
}

if(TrieUnit != TrieRoot)
{
TrieUnit->is_end = 1;
TrieUnit->ruleid = i+1;
}
printf("\n");
}


//构造索引
int indexMax = RuleTrieBuildIndex(TrieRoot);
printf("有效节点 %d\n", indexMax);

//构造回溯指针
RuleTrieBuildFail(TrieRoot);

//构造前进指针
RuleTrieBuildNext(TrieRoot);

//打印显示前缀树
printf("打印前缀树 Root %p\n", TrieRoot);
RuleTrieBuildShow(TrieRoot);

FILE *FileTrie = fopen("trie.dot", "w+");
if(FileTrie)
{
RuleTrieDraw_init(TrieRoot, FileTrie);
RuleTrieDraw_Fail(TrieRoot, FileTrie);
RuleTrieDraw_Next(TrieRoot, FileTrie);
RuleTrieDraw_Fork(TrieRoot, FileTrie);
RuleTrieDraw_Refe(TrieRoot, FileTrie);
RuleTrieDraw_fini(TrieRoot, FileTrie);
fclose(FileTrie);
}

return TrieRoot;
}

int RuleTrieMatch(TrieNode* TrieRoot, std::map<std::string, std::string> &recoder)
{
printf("开始执行\n");
int cnt = 0;
TrieNode *Node = TrieRoot->next;
while(Node != TrieRoot)
{
cnt++;
printf("路过 %p %s %s ", Node, Node->field.c_str(), Node->value.c_str());
if(recoder[Node->field] == Node->value)
{
if(Node->is_end)
{
printf("[命中 id=%d]", Node->ruleid);
}
else
{
printf("[真]");
}
Node = Node->next;
}
else
{
printf("[假]");
Node = Node->fail;
}
printf("\n");
}

printf("计算次数累计: %d\n", cnt);
return 0;
}


int main()
{
std::map<std::string, std::string> recoder;
recoder["http_method"] = "GET";
recoder["http_uri"] = "/fast-cgi";
recoder["http_version"] = "HTTP/1.1";
recoder["http_cookie"] = "uuid is 1234abcd";
recoder["http_paylaod"] = "success";
recoder["http_paylaod_len"] = "7";
recoder["http_useragent"] = "Windows Chrome 7.0";
recoder["http_host"] = "www.baidu.com";
recoder["http_accept"] = "text, png, jpeg";
recoder["http_referer"] = "video.hao123.com";
recoder["http_acceptencoding"] = "gzip";
recoder["http_acceptlanguage"] = "gbk,utf8";
recoder["http_code"] = "200 OK";
recoder["http_contenttype"] = "html/text";
recoder["http_contentlength"] = "20";
recoder["http_content"] = "message send success";
recoder["http_server"] = "Nginx 1.2";
recoder["http_date"] = "2022-02-18 12:08";
recoder["http_location"] = "http://www.baidu.com/";

std::map<std::string, std::string> rule1;
rule1["http_host"] = "music.baidu.com";
rule1["http_method"] = "get";
rule1["http_uri"] = "/upload";
rule1["http_code"] = "200";

std::map<std::string, std::string> rule2;
rule2["http_host"] = "music.baidu.com";
rule2["http_method"] = "post";
rule2["http_uri"] = "/upload";

std::map<std::string, std::string> rule3;
rule3["http_uri"] = "/upload";
rule3["http_referer"] = "music.hao123.com";

std::map<std::string, std::string> rule4;
rule4["http_uri"] = "/fast-cgi";
rule4["http_referer"] = "video.hao123.com";

std::map<std::string, std::string> rule5;
rule5["http_accept"] = "text, png, jpeg";
rule5["http_cookie"] = "uuid is 1234abcd";
rule5["http_referer"] = "video.hao123.com";

std::map<std::string, std::string> rule6;
rule6["http_accept"] = "text, png, jpeg";
rule6["http_referer"] = "hao123.com";

std::map<std::string, std::string> rule7;
rule7["http_uri"] = "/img/2022/view/123.png";
rule7["http_referer"] = "i.qiyi.com";

std::map<std::string, std::string> rule8;
rule8["http_host"] = "www.baidu.com";
rule8["http_cookie"] = "uuid is 1234abcd";

std::vector<std::map<std::string, std::string>* > RuleManager;
RuleManager.push_back(&rule1);
RuleManager.push_back(&rule2);
RuleManager.push_back(&rule3);
RuleManager.push_back(&rule4);
RuleManager.push_back(&rule5);
RuleManager.push_back(&rule6);
RuleManager.push_back(&rule7);
RuleManager.push_back(&rule8);

TrieNode * TrieRoot = RuleTrieBuild(&RuleManager);
RuleTrieMatch(TrieRoot, recoder);

return 0;
}

root@localhost:~#

编译

1
root@localhost:~# g++ -std=c++11 trie.cpp

运行

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
root@localhost:~# ./a.out
规则 0 http_code==200 http_host==music.baidu.com http_method==get http_uri==/upload
规则 1 http_host==music.baidu.com http_method==post http_uri==/upload
规则 2 http_referer==music.hao123.com http_uri==/upload
规则 3 http_referer==video.hao123.com http_uri==/fast-cgi
规则 4 http_accept==text, png, jpeg http_cookie==uuid is 1234abcd http_referer==video.hao123.com
规则 5 http_accept==text, png, jpeg http_referer==hao123.com
规则 6 http_referer==i.qiyi.com http_uri==/img/2022/view/123.png
规则 7 http_cookie==uuid is 1234abcd http_host==www.baidu.com

规则内排序
3 http_uri==/upload 2 http_host==music.baidu.com 1 http_code==200 1 http_method==get
3 http_uri==/upload 2 http_host==music.baidu.com 1 http_method==post
3 http_uri==/upload 1 http_referer==music.hao123.com
2 http_referer==video.hao123.com 1 http_uri==/fast-cgi
2 http_referer==video.hao123.com 2 http_accept==text, png, jpeg 2 http_cookie==uuid is 1234abcd
2 http_accept==text, png, jpeg 1 http_referer==hao123.com
1 http_uri==/img/2022/view/123.png 1 http_referer==i.qiyi.com
2 http_cookie==uuid is 1234abcd 1 http_host==www.baidu.com

构造前缀树
构造: http_uri==/upload
构造: http_host==music.baidu.com
构造: http_code==200
构造: http_method==get

复用!!http_uri==/upload
复用!!http_host==music.baidu.com
构造: http_method==post

复用!!http_uri==/upload
构造: http_referer==music.hao123.com

构造: http_referer==video.hao123.com
构造: http_uri==/fast-cgi

复用!!http_referer==video.hao123.com
构造: http_accept==text, png, jpeg
构造: http_cookie==uuid is 1234abcd

构造: http_accept==text, png, jpeg
构造: http_referer==hao123.com

构造: http_uri==/img/2022/view/123.png
构造: http_referer==i.qiyi.com

构造: http_cookie==uuid is 1234abcd
构造: http_host==www.baidu.com

有效节点 16
打印前缀树 Root 0x1406720
索引 00 当前节点:0x1407290 父节点:0x1406720 前进节点:0x1407370 失败节点:0x1407730 http_accept==text, png, jpeg
索引 01 当前节点:0x1407370 父节点:0x1407290 前进节点:0x1407730 失败节点:0x1407730 http_referer==hao123.com
索引 02 当前节点:0x1407730 父节点:0x1406720 前进节点:0x1407860 失败节点:0x1406dc0 http_cookie==uuid is 1234abcd
索引 03 当前节点:0x1407860 父节点:0x1407730 前进节点:0x1406dc0 失败节点:0x1406dc0 http_host==www.baidu.com
索引 04 当前节点:0x1406dc0 父节点:0x1406720 前进节点:0x1407030 失败节点:0x14074e0 http_referer==video.hao123.com
索引 05 当前节点:0x1407030 父节点:0x1406dc0 前进节点:0x1407160 失败节点:0x1406ea0 http_accept==text, png, jpeg
索引 06 当前节点:0x1407160 父节点:0x1407030 前进节点:0x1406ea0 失败节点:0x1406ea0 http_cookie==uuid is 1234abcd
索引 07 当前节点:0x1406ea0 父节点:0x1406dc0 前进节点:0x14074e0 失败节点:0x14074e0 http_uri==/fast-cgi
索引 08 当前节点:0x14074e0 父节点:0x1406720 前进节点:0x14075c0 失败节点:0x1406790 http_uri==/img/2022/view/123.png
索引 09 当前节点:0x14075c0 父节点:0x14074e0 前进节点:0x1406790 失败节点:0x1406790 http_referer==i.qiyi.com
索引 10 当前节点:0x1406790 父节点:0x1406720 前进节点:0x1406850 失败节点:0x1406720 http_uri==/upload
索引 11 当前节点:0x1406850 父节点:0x1406790 前进节点:0x1406930 失败节点:0x1406ce0 http_host==music.baidu.com
索引 12 当前节点:0x1406930 父节点:0x1406850 前进节点:0x1406a50 失败节点:0x1406bc0 http_code==200
索引 13 当前节点:0x1406a50 父节点:0x1406930 前进节点:0x1406bc0 失败节点:0x1406bc0 http_method==get
索引 14 当前节点:0x1406bc0 父节点:0x1406850 前进节点:0x1406ce0 失败节点:0x1406ce0 http_method==post
索引 15 当前节点:0x1406ce0 父节点:0x1406790 前进节点:0x1406720 失败节点:0x1406720 http_referer==music.hao123.com
开始执行
路过 0x1407290 http_accept text, png, jpeg [真]
路过 0x1407370 http_referer hao123.com [假]
路过 0x1407730 http_cookie uuid is 1234abcd [真]
路过 0x1407860 http_host www.baidu.com [命中 id=8]
路过 0x1406dc0 http_referer video.hao123.com [真]
路过 0x1407030 http_accept text, png, jpeg [真]
路过 0x1407160 http_cookie uuid is 1234abcd [命中 id=5]
路过 0x1406ea0 http_uri /fast-cgi [命中 id=4]
路过 0x14074e0 http_uri /img/2022/view/123.png [假]
路过 0x1406790 http_uri /upload [假]
计算次数累计: 10
root@localhost:~#

规则关系图

程序运行会自动生成一张规则关系图 trie.dot,dot语言描述。

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
root@localhost:~# cat trie.dot
//画图命令: dot -Tpdf trie.dot -o trie.pdf
digraph G {
charset="UTF-8"
fontname="Helvetica,Arial,sans-serif"
node [fontname="Helvetica,Arial,sans-serif"]
edge [fontname="Helvetica,Arial,sans-serif"]
node [shape="box"]
{ node [shape=circle style=filled color="#FF0000"]
"0x1406720" [label=""]
}
{ node [style=filled color="#000000" fillcolor="#00FF00"]
"0x1407370" [label="id=6 \n http_referer==hao123.com"]
"0x1407860" [label="id=8 \n http_host==www.baidu.com"]
"0x1407160" [label="id=5 \n http_cookie==uuid is 1234abcd"]
"0x1406ea0" [label="id=4 \n http_uri==/fast-cgi"]
"0x14075c0" [label="id=7 \n http_referer==i.qiyi.com"]
"0x1406a50" [label="id=1 \n http_method==get"]
"0x1406bc0" [label="id=2 \n http_method==post"]
"0x1406ce0" [label="id=3 \n http_referer==music.hao123.com"]
}
{ node []
"0x1407290" [label="http_accept==text, png, jpeg"]
"0x1407730" [label="http_cookie==uuid is 1234abcd"]
"0x1406dc0" [label="http_referer==video.hao123.com"]
"0x1407030" [label="http_accept==text, png, jpeg"]
"0x14074e0" [label="http_uri==/img/2022/view/123.png"]
"0x1406790" [label="http_uri==/upload"]
"0x1406850" [label="http_host==music.baidu.com"]
"0x1406930" [label="http_code==200"]
}
"0x1406720" -> "0x1406720" [color="#FF0000" style=dotted]
"0x1407290" -> "0x1407730" [color="#FF0000" style=dotted]
"0x1407370" -> "0x1407730" [color="#FF0000" style=dotted]
"0x1407730" -> "0x1406dc0" [color="#FF0000" style=dotted]
"0x1407860" -> "0x1406dc0" [color="#FF0000" style=dotted]
"0x1406dc0" -> "0x14074e0" [color="#FF0000" style=dotted]
"0x1407030" -> "0x1406ea0" [color="#FF0000" style=dotted]
"0x1407160" -> "0x1406ea0" [color="#FF0000" style=dotted]
"0x1406ea0" -> "0x14074e0" [color="#FF0000" style=dotted]
"0x14074e0" -> "0x1406790" [color="#FF0000" style=dotted]
"0x14075c0" -> "0x1406790" [color="#FF0000" style=dotted]
"0x1406790" -> "0x1406720" [color="#FF0000" style=dotted]
"0x1406850" -> "0x1406ce0" [color="#FF0000" style=dotted]
"0x1406930" -> "0x1406bc0" [color="#FF0000" style=dotted]
"0x1406a50" -> "0x1406bc0" [color="#FF0000" style=dotted]
"0x1406bc0" -> "0x1406ce0" [color="#FF0000" style=dotted]
"0x1406ce0" -> "0x1406720" [color="#FF0000" style=dotted]
"0x1406720" -> "0x1407290" [color="#00FF00"]
"0x1407290" -> "0x1407370" [color="#00FF00"]
"0x1407730" -> "0x1407860" [color="#00FF00"]
"0x1406dc0" -> "0x1407030" [color="#00FF00"]
"0x1407030" -> "0x1407160" [color="#00FF00"]
"0x14074e0" -> "0x14075c0" [color="#00FF00"]
"0x1406790" -> "0x1406850" [color="#00FF00"]
"0x1406850" -> "0x1406930" [color="#00FF00"]
"0x1406930" -> "0x1406a50" [color="#00FF00"]
"0x1406720" -> "0x1407730" [color="#00FF00" style=dashed]
"0x1406720" -> "0x1406dc0" [color="#00FF00" style=dashed]
"0x1406dc0" -> "0x1406ea0" [color="#00FF00" style=dashed]
"0x1406720" -> "0x14074e0" [color="#00FF00" style=dashed]
"0x1406720" -> "0x1406790" [color="#00FF00" style=dashed]
"0x1406850" -> "0x1406bc0" [color="#00FF00" style=dashed]
"0x1406790" -> "0x1406ce0" [color="#00FF00" style=dashed]
"0x1407290" -> "0x1407030" [color="#0000FF" style=dotted dir=both]
"0x1407730" -> "0x1407160" [color="#0000FF" style=dotted dir=both]
}
root@localhost:~#

绘制

1
2
dot工具 需要自行安装
root@localhost:~# dot -Tpdf trie.dot -o trie.pdf

效果图

说明:
红色虚线: 失败指针
绿色实现: 前进指针
绿色虚线: 分支示意,不参与路径运算
蓝色虚线: 节点值引用,一次相同的节点,只执行一次,运算的结果被多次引用

每个节点都有失败指针 与 前进指针。
节点运算成功时, 接着执行前进指针。
节点运算失败时, 接着执行失败指针。

性能:

  1. match算法就一个普通循环,简洁高效。无需普通前缀树的 递归、队列的加持。

trie.pdf下载

加强

类似 http_referer这种多次出现, 可以用hyperscan 将结果全部输出,一次http_referer运算, 所有http_referer相关节点值全部生成。