forked from louisalflame/WebTraceCollector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.py
385 lines (306 loc) · 14.8 KB
/
algorithm.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys, json, codecs, logging, random
from abc import ABCMeta, abstractmethod
from dom_analyzer import DomAnalyzer
from executor import SeleniumExecutor
class AlgoCrawler:
__metaclass__ = ABCMeta
@abstractmethod
def set_utility(self, crawler, configuration, executor, automata):
pass
@abstractmethod
def prepare(self):
pass
@abstractmethod
def add_new_events(self, state, prev_state, depth):
pass
@abstractmethod
def get_next_action(self, action_events):
pass
@abstractmethod
def trigger_action(self, state, new_edge, action, depth):
pass
@abstractmethod
def update_with_same_state(self, current_state, new_edge, action, depth, dom_list, url):
pass
@abstractmethod
def update_with_out_of_domain(self, current_state, new_edge, action, depth, dom_list, url):
pass
@abstractmethod
def update_with_new_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
pass
@abstractmethod
def update_with_old_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
pass
@abstractmethod
def save_traces(self):
pass
@abstractmethod
def end(self):
pass
class DFScrawler(AlgoCrawler):
def __init__(self):
pass
def set_utility(self, crawler, configuration, executor, automata):
self.crawler = crawler
self.configuration = configuration
self.executor = executor
self.automata = automata
def prepare(self):
#executor
self.executor.start()
self.executor.goto_url()
#initial state
initial_state = self.crawler.get_initail_state()
self.crawler.run_script_before_crawl(initial_state)
def add_new_events(self, state, prev_state, depth):
for clickables, iframe_key in DomAnalyzer.get_clickables(state, prev_state if prev_state else None):
for clickable in clickables:
self.crawler.action_events.append( {
'state' : state,
'action' : { 'clickable':clickable, 'iframe_key':iframe_key },
'depth' : depth,
} )
def get_next_action(self, action_events):
event = action_events.pop()
return event
def change_state(self, state, action, depth):
logging.info('==========< BACKTRACK START >==========')
logging.info('==<BACKTRACK> depth %s -> backtrack to state %s',depth ,state.get_id() )
self.crawler.executor_backtrack(state, self.executor)
logging.info('==========< BACKTRACK END >==========')
def trigger_action(self, state, new_edge, action, depth):
logging.info(' |depth:%s state:%s| fire element in iframe(%s)', depth, state.get_id(), action['iframe_key'])
self.crawler.make_value(new_edge)
self.executor.click_event_by_edge(new_edge)
def update_with_same_state(self, current_state, new_edge, action, depth, dom_list, url):
# Do Nothing when same state
return None
def update_with_out_of_domain(self, current_state, new_edge, action, depth, dom_list, url):
# back if state out of domain
logging.info(' |depth:%s state:%s| out of domain: %s', depth, current_state.get_id(), url)
logging.info('==========< BACKTRACK START >==========')
logging.info('==<BACKTRACK> depth %s -> backtrack to state %s',depth ,current_state.get_id() )
self.crawler.executor_backtrack(current_state, self.executor)
logging.info('==========< BACKTRACK END >==========')
def update_with_new_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
# automata save new state
logging.info(' |depth:%s state:%s| add new state %s of : %s', depth, current_state.get_id(), new_state.get_id(), url )
self.automata.save_state(self.executor, new_state, depth)
self.automata.save_state_shot(self.executor, new_state)
if depth < self.configuration.get_max_depth():
self.crawler.add_new_events(new_state, current_state, depth)
def update_with_old_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
#check if old state have a shorter depth
if depth < new_state.get_depth():
new_state.set_depth(depth)
self.crawler.add_new_events(new_state, current_state, depth)
def save_traces(self):
self.automata.save_simple_traces()
def end(self):
pass
class MonkeyCrawler(AlgoCrawler):
def __init__(self):
self.trace_length_count = 0
self.traces = []
self.trace_history = {}
def set_utility(self, crawler, configuration, executor, automata):
self.crawler = crawler
self.configuration = configuration
self.executor = executor
self.automata = automata
def prepare(self):
#executor
self.executor.start()
self.executor.goto_url()
#initial state
initial_state = self.crawler.get_initail_state()
#save trace
self.trace_length_count = 0
self.trace_history = { 'states': [ initial_state ], 'edges': [] }
self.crawler.run_script_before_crawl(initial_state)
def add_new_events(self, state, prev_state, depth):
candidate_clickables = []
for clickables, iframe_key in DomAnalyzer.get_clickables(state, prev_state if prev_state else None):
for clickable in clickables:
candidate_clickables.append( (clickable, iframe_key) )
if not candidate_clickables:
return
clickable, iframe_key = random.choice( candidate_clickables )
self.crawler.action_events.append( {
'state' : state,
'action' : { 'clickable':clickable, 'iframe_key':iframe_key },
'depth' : depth,
} )
print(state.get_id(),clickable.get_id(), clickable.get_xpath())
def get_next_action(self, action_events):
event = action_events.pop()
return event
def change_state(self, state, action, depth):
#logging.info('==========< BACKTRACK START >==========')
#logging.info('==<BACKTRACK> depth %s -> backtrack to state %s',depth ,state.get_id() )
#self.crawler.executor_backtrack(state, self.executor)
#logging.info('==========< BACKTRACK END >==========')
logging.info('==========< MONKEY IGNORE BACKTRACK >==========')
def trigger_action(self, state, new_edge, action, depth):
logging.info(' |depth:%s state:%s| fire element in iframe(%s)', depth, state.get_id(), action['iframe_key'])
self.trace_length_count += 1
self.crawler.make_value(new_edge)
self.executor.click_event_by_edge(new_edge)
def update_with_same_state(self, current_state, new_edge, action, depth, dom_list, url):
#save trace
self.trace_history['states'].append(current_state)
self.trace_history['edges'].append(new_edge)
#get new event again
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(current_state, None, depth)
def update_with_out_of_domain(self, current_state, new_edge, action, depth, dom_list, url):
# back if state out of domain
logging.info(' |depth:%s state:%s| out of domain: %s', depth, current_state.get_id(), url)
logging.info('==========< BACKTRACK START >==========')
logging.info('==<BACKTRACK> depth %s -> backtrack to state %s',depth ,current_state.get_id() )
self.crawler.executor_backtrack(current_state, self.executor)
logging.info('==========< BACKTRACK END >==========')
#get new event again
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(current_state, None, depth)
def update_with_new_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
#save trace
self.trace_history['states'].append(new_state)
self.trace_history['edges'].append(new_edge)
# automata save new state
logging.info(' |depth:%s state:%s| add new state %s of : %s', depth, current_state.get_id(), new_state.get_id(), url )
self.automata.save_state(self.executor, new_state, depth)
self.automata.save_state_shot(self.executor, new_state)
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(new_state, None, depth)
def update_with_old_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
#save trace
self.trace_history['states'].append(new_state)
self.trace_history['edges'].append(new_edge)
#check if old state have a shorter depth
if depth < new_state.get_depth():
new_state.set_depth(depth)
if depth > new_state.get_depth():
depth = new_state.get_depth()
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(new_state, None, depth)
def save_traces(self):
self.automata.save_traces(self.traces)
def end(self):
self.traces.append( self.trace_history )
self.trace_length_count = 0
self.trace_history = {}
class CBTMonkeyCrawler(AlgoCrawler):
def __init__(self):
self.trace_length_count = 0
self.traces = []
self.trace_history = {}
self.other_executor = None
def add_executor(self, argv):
self.other_executor = SeleniumExecutor(browserID, config.get_url())
def set_utility(self, crawler, configuration, executor, automata):
self.crawler = crawler
self.configuration = configuration
self.executor = executor
self.automata = automata
def prepare(self):
#executor
self.executor.start()
self.executor.goto_url()
self.other_executor.start()
self.other_executor.goto_url()
#initial state
initial_state = self.crawler.get_initail_state()
#save trace
self.trace_length_count = 0
self.trace_history = { 'states': [ initial_state ], 'edges': [] }
self.crawler.run_script_before_crawl(initial_state)
def add_new_events(self, state, prev_state, depth):
candidate_clickables = []
for clickables, iframe_key in DomAnalyzer.get_clickables(state, prev_state if prev_state else None):
for clickable in clickables:
candidate_clickables.append( (clickable, iframe_key) )
if not candidate_clickables:
return
clickable, iframe_key = random.choice( candidate_clickables )
print(state.get_id(),clickable.get_id(), clickable.get_xpath())
self.crawler.action_events.append( {
'state' : state,
'action' : { 'clickable':clickable, 'iframe_key':iframe_key },
'depth' : depth,
} )
def get_next_action(self, action_events):
event = action_events.pop()
return event
def change_state(self, state, action, depth):
logging.info('==========< BACKTRACK START >==========')
logging.info('==<BACKTRACK> depth %s -> backtrack to state %s',depth ,state.get_id() )
self.crawler.executor_backtrack(state, self.executor, self.other_executor)
logging.info('==========< BACKTRACK END >==========')
def trigger_action(self, state, new_edge, action, depth):
logging.info(' |depth:%s state:%s| fire element in iframe(%s)', depth, state.get_id(), action['iframe_key'])
self.trace_length_count += 1
self.crawler.make_value(new_edge)
self.executor.click_event_by_edge(new_edge)
self.other_executor.click_event_by_edge(new_edge)
def update_with_same_state(self, current_state, new_edge, action, depth, dom_list, url):
#save trace
self.trace_history['states'].append(current_state)
self.trace_history['edges'].append(new_edge)
#get new event again
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(current_state, None, depth)
def update_with_out_of_domain(self, current_state, new_edge, action, depth, dom_list, url):
#save trace
self.trace_history['states'].append(current_state)
self.trace_history['edges'].append(new_edge)
# back if state out of domain
logging.info(' |depth:%s state:%s| out of domain: %s', depth, current_state.get_id(), url)
logging.info('==========< BACKTRACK START >==========')
logging.info('==<BACKTRACK> depth %s -> backtrack to state %s',depth ,current_state.get_id() )
self.crawler.executor_backtrack(current_state, self.executor, self.other_executor)
logging.info('==========< BACKTRACK END >==========')
#get new event again
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(current_state, None, depth)
def update_with_new_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
#save trace
self.trace_history['states'].append(new_state)
self.trace_history['edges'].append(new_edge)
# automata save new state
logging.info(' |depth:%s state:%s| add new state %s of : %s', depth, current_state.get_id(), new_state.get_id(), url )
self.automata.save_state(self.executor, new_state, depth)
self.automata.save_state_shot(self.executor, new_state)
self.check_diff_browser()
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(new_state, None, depth)
def update_with_old_state(self, current_state, new_state, new_edge, action, depth, dom_list, url):
#save trace
self.trace_history['states'].append(new_state)
self.trace_history['edges'].append(new_edge)
#check if old state have a shorter depth
if depth < new_state.get_depth():
new_state.set_depth(depth)
if depth > new_state.get_depth():
depth = new_state.get_depth()
if self.trace_length_count < self.configuration.get_max_length():
self.crawler.add_new_events(new_state, None, depth)
def save_traces(self):
self.automata.save_traces(self.traces)
def end(self):
self.traces.append( self.trace_history )
self.trace_length_count = 0
self.trace_history = {}
self.other_executor.close()
def check_diff_browser(self):
# 1. check executor other_executor is same state
# 2. if same , analsis
analysis_elements( self.executor )
analysis_elements( self.other_executor )
analysis_with_other_browser()
def analysis_elements(self, executor):
pass
def analysis_with_other_browser(self):
pass