hyperscan安装见 文章

Basic Knownledge Of Hyperscan

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
import hyperscan

# Building database for hyperscan

from typing import List, Optional, Any

RULES = {
r"^10\.13[01].*\.php": "php",
r"\.woff": "font",
r"\.min\.": "minimized",
r"\.css": "stylesheet",
r"login": "login",
r"bootstrap": "bootstrap",
r"\/vendor\/": "vendored-js"
}

PATTERNS = list(RULES.keys())
TAG_LOOKUP = list(RULES.values())

def load_data():
content = open("xxxx.log", "r").read()
logs = content.split("\n")[1:]
return logs

db = hyperscan.Database()
patterns = [
# expression, id, flags
(pattern.encode("utf-8"), id, hyperscan.HS_FLAG_CASELESS | hyperscan.HS_FLAG_SINGLEMATCH) for id, pattern in enumerate(PATTERNS)
]
expressions, ids, flags = zip(*patterns)
db.compile(
expressions=expressions, ids=ids, elements=len(patterns), flags=flags
)

def hyperscan_match(lines: List[str]) -> List[str]:
result = []
def on_match(id: int, froms: int, to: int, flags: int, context: Optional[Any] = None) -> Optional[bool]:
result.append(TAG_LOOKUP[id])

for line in lines:
db.scan(line.encode("utf-8"), match_event_handler=on_match)

return result

print(hyperscan_match(load_data()))


Result:

参考文章