1
|
|
|
import asyncio |
2
|
|
|
import base64 |
3
|
|
|
from threading import Thread, Lock |
4
|
|
|
|
5
|
|
|
from .image import Image |
6
|
|
|
from ...tools import Tools |
7
|
|
|
|
8
|
|
|
""" |
9
|
|
|
Copyright (c) 2020 Star Inc.(https://starinc.xyz) |
10
|
|
|
|
11
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public |
12
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this |
13
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/. |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class View: |
18
|
|
|
def __init__(self, pbp_handle): |
19
|
|
|
self.data_control = pbp_handle.data_control |
20
|
|
|
self.image_handle = Image(pbp_handle) |
21
|
|
|
|
22
|
|
|
async def analyze(self, target_type, target_url): |
23
|
|
|
""" |
24
|
|
|
Analyze URL |
25
|
|
|
:param target_type: integer |
26
|
|
|
:param target_url: URL |
27
|
|
|
:return: URLs similar to in trustlist |
28
|
|
|
""" |
29
|
|
|
(view_signature, view_data) = await self.image_handle.capture(target_url) |
30
|
|
|
|
31
|
|
|
signature_query = await self.image_handle.signature(view_signature) |
32
|
|
|
yield signature_query |
33
|
|
|
|
34
|
|
|
if signature_query: |
35
|
|
|
return |
36
|
|
|
|
37
|
|
|
query = {} |
38
|
|
|
async for url, score in self.image_handle.rank(target_type, view_data): |
39
|
|
|
query[url] = score |
40
|
|
|
|
41
|
|
|
for url in query: |
42
|
|
|
if query[url] > 0.9 and query[url] == max(query.values()): |
43
|
|
|
yield url |
44
|
|
|
|
45
|
|
|
async def generate(self): |
46
|
|
|
""" |
47
|
|
|
Generate samples |
48
|
|
|
:return: |
49
|
|
|
""" |
50
|
|
|
thread = None |
51
|
|
|
lock = Lock() |
52
|
|
|
|
53
|
|
|
async def _upload(url: str): |
54
|
|
|
""" |
55
|
|
|
Child function, to upload data to database |
56
|
|
|
:param url: URL |
57
|
|
|
:return: |
58
|
|
|
""" |
59
|
|
|
lock.acquire() |
60
|
|
|
try: |
61
|
|
|
(view_signature, view_data) = await self.image_handle.capture(url) |
62
|
|
|
b64_view_data = base64.b64encode(view_data.dumps()) |
63
|
|
|
self.data_control.upload_view_sample( |
64
|
|
|
url, |
65
|
|
|
view_signature, |
66
|
|
|
b64_view_data, |
67
|
|
|
) |
68
|
|
|
except: |
69
|
|
|
error_report = Tools.error_report() |
70
|
|
|
raise ViewException("generate._upload", url, error_report) |
71
|
|
|
lock.release() |
72
|
|
|
|
73
|
|
|
for origin_url in self.data_control.get_urls_from_trustlist(): |
74
|
|
|
thread = Thread( |
75
|
|
|
target=lambda url: asyncio.run(_upload(url)), |
76
|
|
|
args=( |
77
|
|
|
origin_url, |
78
|
|
|
) |
79
|
|
|
) |
80
|
|
|
thread.start() |
81
|
|
|
|
82
|
|
|
if thread: |
83
|
|
|
thread.join() |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
class ViewException(Exception): |
87
|
|
|
def __init__(self, cause, url, message): |
88
|
|
|
self.err_msg = { |
89
|
|
|
"cause": cause, |
90
|
|
|
"url": url, |
91
|
|
|
"message": message |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
def __str__(self): |
95
|
|
|
return "{cause}[{url}]: {message}".format(**self.err_msg) |
96
|
|
|
|