-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
executable file
·1260 lines (986 loc) · 34.3 KB
/
scanner.py
File metadata and controls
executable file
·1260 lines (986 loc) · 34.3 KB
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
################################################################################
# Link Scanner #
# #
# Copyright (C) 2020 J.C. Fields (jcfields@jcfields.dev). #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to #
# deal in the Software without restriction, including without limitation the #
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or #
# sell copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS #
# IN THE SOFTWARE. #
################################################################################
# from standard library
import json
import os
import re
import time
import urllib
import webbrowser
# threading
from contextlib import closing
from queue import Queue
import threading
# GUI toolkit
import wx
from wx.adv import AboutBox
# HTML parser
from bs4 import BeautifulSoup, SoupStrainer
# HTTP
import requests
################################################################################
# Global variables #
################################################################################
# about
TITLE = 'Link Scanner'
COPYRIGHT = '© 2020 J.C. Fields <jcfields@jcfields.dev>'
WEB_SITE_URL = 'https://github.com/jcfieldsdev/link-scanner'
VERSION = '1.0.0'
MAIN_FRAME_SIZE = (1000, 600)
SUB_FRAME_SIZE = (600, 400)
# scan settings
HTML_TAGS = ('a', 'img', 'link')
CONTENT_TYPES = ('text/html', 'application/xhtml+xml')
ACCEPT_SCHEMES = ('http', 'https')
IGNORE_SCHEMES = ('mailto', 'javascript') # not reported under "Skipped" filter
# link follow modes
IGNORE = 0
CHECK = 1
FOLLOW = 2
# scanner result event statuses
TIMEOUT = 0
SKIPPED = 1
COMPLETED = 2
# scanner result event server types
ANY = 0
INTERNAL = 1
EXTERNAL = 2
# rule types
INCLUDE = False
EXCLUDE = True
# unique IDs for worker thread
EVT_RESULT_ID = wx.NewIdRef(count=1)
ID_START = wx.NewIdRef(count=1)
ID_STOP = wx.NewIdRef(count=1)
# HTTP status codes
STATUS_CODES = {
'200': 'OK',
'201': 'Created',
'202': 'Accepted',
'203': 'Non-Authoritative Information',
'204': 'No Content',
'205': 'Reset Content',
'206': 'Partial Content',
'300': 'Multiple Choices',
'301': 'Moved Permanently',
'302': 'Found',
'303': 'See Other',
'304': 'Not Modified',
'305': 'Use Proxy',
'306': 'Switch Proxy',
'307': 'Temporary Redirect',
'308': 'Permanent Redirect',
'400': 'Bad Request',
'401': 'Unauthorized',
'403': 'Forbidden',
'404': 'Not Found',
'405': 'Method Not Allowed',
'406': 'Not Accepted',
'407': 'Proxy Authentication Required',
'408': 'Request Timed Out',
'409': 'Conflict',
'410': 'Gone',
'411': 'Length Required',
'412': 'Precondition Failed',
'413': 'Payload Too Large',
'414': 'URI Too Large',
'415': 'Unsupported Media Type',
'416': 'Range Not Satisfiable',
'417': 'Expectation Failed',
'421': 'Misdirected Request',
'425': 'Too Early',
'426': 'Upgrade Required',
'428': 'Precondition Required',
'429': 'Too Many Requests',
'431': 'Request Header Fields Too Large',
'500': 'Internal Server Error',
'501': 'Not Implemented',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway Timeout',
'505': 'HTTP Version Not Supported',
'506': 'Variant Also Negotiates',
'510': 'Not Extended',
'511': 'Network Authentication Required'
}
################################################################################
# MainFrame class #
################################################################################
class MainFrame(wx.Frame):
def __init__(self):
self.config = wx.Config(TITLE)
self.options = {
'url': 'https://',
'redirect': True,
'query': True,
'external': [False, True, False],
'internal': [False, False, True],
'depth': 1,
'threads': 1,
'delay': 0,
'timeout': 10,
'local': True,
'remote': True,
'status': [True, False, True, True, True, True]
}
self.rules = []
self.read_config()
size = self.options.get('size', MAIN_FRAME_SIZE)
wx.Frame.__init__(self, None, title=TITLE, size=size)
self.Bind(wx.EVT_CLOSE, self.close)
self.status = self.CreateStatusBar(2)
self.panel = MainPanel(self, self.options, self.rules)
self.create_menu()
def create_menu(self):
menu_bar = wx.MenuBar()
menu_file = wx.Menu()
self.item_start = menu_file.Append(ID_START, '&Start', 'Start scan')
self.item_stop = menu_file.Append(ID_STOP, 'S&top', 'Stop scan')
menu_file.AppendSeparator()
item_close = menu_file.Append(wx.ID_EXIT, '&Close', 'Close the program')
menu_help = wx.Menu()
item_web_site = menu_help.Append(
wx.ID_ANY, 'Visit &Web Site',
'Visit the program web site'
)
item_about = menu_help.Append(
wx.ID_ABOUT, '&About {}'.format(TITLE),
'About this program'
)
self.item_stop.Enable(False)
self.Bind(wx.EVT_MENU, self.panel.enter, self.item_start)
self.Bind(wx.EVT_MENU, self.panel.stop, self.item_stop)
self.Bind(wx.EVT_MENU, self.close, item_close)
self.Bind(wx.EVT_MENU, self.open_web_site, item_web_site)
self.Bind(wx.EVT_MENU, self.about, item_about)
menu_bar.Append(menu_file, '&File')
menu_bar.Append(menu_help, '&Help')
self.SetMenuBar(menu_bar)
def read_config(self):
try:
options = self.config.Read('options')
rules = self.config.Read('rules')
if options != '':
self.options.update(json.loads(options))
if rules != '':
self.rules = json.loads(rules)
except:
wx.MessageBox(
'Could not read configuration file.',
'Error', wx.OK | wx.ICON_ERROR
)
def write_config(self):
size = self.GetSize().Get() # converts size object to tuple
if size != MAIN_FRAME_SIZE:
self.options['size'] = size
try:
self.config.Write('options', json.dumps(self.options))
self.config.Write('rules', json.dumps(self.rules))
except:
wx.MessageBox(
'Could not write configuration file.',
'Error', wx.OK | wx.ICON_ERROR
)
def open_web_site(self, event=None):
webbrowser.open(WEB_SITE_URL)
def about(self, event=None):
dialog = wx.adv.AboutDialogInfo()
dialog.SetName(TITLE)
dialog.SetCopyright(COPYRIGHT)
dialog.SetVersion(VERSION)
wx.adv.AboutBox(dialog)
def close(self, event=None):
self.panel.stop()
self.write_config()
self.Destroy()
################################################################################
# MainPanel class #
################################################################################
class MainPanel(wx.Panel):
def __init__(self, parent, options, rules):
wx.Panel.__init__(self, parent)
self.parent = parent
self.options = options
self.rules = rules
self.paused = False
self.done = False
self.scanner = None
self.results = []
self.rows = 0
self.q = ''
self.list_ctrl = self.create_list_ctrl()
self.info_sizer = self.create_info_sizer()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.AddMany((
(self.create_url_sizer(), 0, wx.ALL | wx.EXPAND, 5),
(self.create_option_sizer(), 0, wx.ALL | wx.EXPAND, 5),
(self.list_ctrl, 1, wx.ALL | wx.EXPAND, 5),
(self.info_sizer, 0, wx.EXPAND | wx.RESERVE_SPACE_EVEN_IF_HIDDEN, 5)
))
self.SetSizer(self.sizer)
self.load_options()
self.Connect(-1, -1, EVT_RESULT_ID, self.update)
self.update_status_action()
self.update_status_items()
def create_url_sizer(self):
self.url = wx.TextCtrl(self, ID_START, style=wx.TE_PROCESS_ENTER)
self.url.Bind(wx.EVT_TEXT, self.save_options)
self.url.Bind(wx.EVT_TEXT_ENTER, self.enter, id=ID_START)
self.button_start = wx.Button(self, ID_START, label='&Start')
self.button_start.Bind(wx.EVT_BUTTON, self.enter, id=ID_START)
self.button_stop = wx.Button(self, ID_STOP, label='S&top')
self.button_stop.Disable()
self.button_stop.Bind(wx.EVT_BUTTON, self.stop, id=ID_STOP)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.AddMany((
(wx.StaticText(self, label='URL:'), 0, wx.CENTER),
(self.url, 1, wx.ALL, 5),
(self.button_start, 0, wx.ALL, 5),
(self.button_stop, 0, wx.TOP | wx.RIGHT | wx.BOTTOM, 5)
))
return sizer
def create_option_sizer(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.AddMany((
(self.create_connections_box(), 1, wx.ALL | wx.EXPAND),
(self.create_links_box(), 1, wx.ALL | wx.EXPAND),
(self.create_filter_box(), 1, wx.ALL | wx.EXPAND)
))
return sizer
def create_filter_box(self):
self.search = wx.SearchCtrl(self)
self.search.Bind(wx.EVT_TEXT, self.filter)
self.status = (
wx.CheckBox(self, label='Timeouts'),
wx.CheckBox(self, label='Skipped'),
wx.CheckBox(self, label='200 (Success)'),
wx.CheckBox(self, label='300 (Redirects)'),
wx.CheckBox(self, label='400 (Client Errors)'),
wx.CheckBox(self, label='500 (Server Errors)')
)
self.local = wx.CheckBox(self, label='Internal')
self.remote = wx.CheckBox(self, label='External')
self.local.Bind(wx.EVT_CHECKBOX, self.filter)
self.remote.Bind(wx.EVT_CHECKBOX, self.filter)
sizer = wx.FlexGridSizer(cols=2, gap=(5, 5))
sizer.AddMany((self.local, self.remote))
for element in self.status:
sizer.Add(element)
element.Bind(wx.EVT_CHECKBOX, self.filter)
box = wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Filter'), wx.VERTICAL)
box.AddMany(((self.search, 1, wx.BOTTOM | wx.EXPAND, 10), (sizer, 0)))
return box
def create_links_box(self):
self.external = (
wx.RadioButton(self, label='Ignore', style=wx.RB_GROUP),
wx.RadioButton(self, label='Check'),
wx.RadioButton(self, label='Follow')
)
external_sizer = wx.BoxSizer(wx.HORIZONTAL)
external_sizer.AddMany(self.external)
self.internal = (
wx.RadioButton(self, label='Ignore', style=wx.RB_GROUP),
wx.RadioButton(self, label='Check'),
wx.RadioButton(self, label='Follow')
)
internal_sizer = wx.BoxSizer(wx.HORIZONTAL)
internal_sizer.AddMany(self.internal)
self.depth = wx.SpinCtrl(self, initial=1, min=1, size=(50, -1))
self.depth.Bind(wx.EVT_SPINCTRL, self.save_options)
button_rules = wx.Button(self, label='Edit &Rules')
button_rules.Bind(wx.EVT_BUTTON, self.open_rules_editor)
sizer = wx.FlexGridSizer(cols=2, gap=(5, 5))
sizer.AddMany((
wx.StaticText(self, label='Internal links:'), internal_sizer,
wx.StaticText(self, label='External links:'), external_sizer,
wx.StaticText(self, label='Recursion depth:'), self.depth,
wx.StaticText(self, label='Matching rules:'), button_rules
))
for element in self.external:
element.Bind(wx.EVT_RADIOBUTTON, self.save_options)
for element in self.internal:
element.Bind(wx.EVT_RADIOBUTTON, self.save_options)
box = wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Links'), wx.VERTICAL)
box.Add(sizer)
return box
def create_connections_box(self):
size = (50, -1)
self.threads = wx.SpinCtrl(self, initial=1, min=1, size=size)
self.delay = wx.SpinCtrl(self, initial=0, min=0, size=size)
self.timeout = wx.SpinCtrl(self, initial=10, min=1, size=size)
self.threads.Bind(wx.EVT_SPINCTRL, self.save_options)
self.delay.Bind(wx.EVT_SPINCTRL, self.save_options)
self.timeout.Bind(wx.EVT_SPINCTRL, self.save_options)
sizer = wx.FlexGridSizer(cols=2, gap=(5, 5))
sizer.AddMany((
wx.StaticText(self, label='Number of threads:'), self.threads,
wx.StaticText(self, label='Delay:'), self.delay,
wx.StaticText(self, label='Timeout:'), self.timeout
))
self.redirect = wx.CheckBox(self, label='Follow redirects.')
self.query = wx.CheckBox(self, label='Follow query strings.')
self.redirect.Bind(wx.EVT_CHECKBOX, self.save_options)
self.query.Bind(wx.EVT_CHECKBOX, self.save_options)
box = wx.StaticBoxSizer(
wx.StaticBox(self, -1, 'Connections'),
wx.VERTICAL
)
box.AddMany((
sizer,
(self.redirect, 0, wx.TOP, 5),
(self.query, 0, wx.TOP, 5)
))
return box
def create_list_ctrl(self):
element = wx.ListCtrl(self, style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
element.InsertColumn(0, 'Status', width=75)
element.InsertColumn(1, 'Link to', width=445)
element.InsertColumn(2, 'Appears on', width=445)
element.Bind(wx.EVT_LIST_ITEM_SELECTED, self.list_selected)
element.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.list_deselected)
return element
def create_info_sizer(self):
size = (80, -1) # text label size
self.text_status = wx.StaticText(self, label='')
self.text_link = wx.TextCtrl(self)
self.text_source = wx.TextCtrl(self)
self.text_status.SetFont(self.text_status.GetFont().MakeBold())
status_sizer = wx.BoxSizer(wx.HORIZONTAL)
status_sizer.AddMany((
(wx.StaticText(self, label='Status:', size=size)),
self.text_status
))
self.button_link = wx.Button(self, label='&Open')
self.button_link.Bind(wx.EVT_BUTTON, self.open_link)
self.button_link.Disable()
link_sizer = wx.BoxSizer(wx.HORIZONTAL)
link_sizer.AddMany((
(wx.StaticText(self, label='Link to:', size=size), 0, wx.CENTER),
(self.text_link, 1),
(self.button_link, 0, wx.LEFT, 5)
))
self.button_source = wx.Button(self, label='Op&en')
self.button_source.Bind(wx.EVT_BUTTON, self.open_source)
self.button_source.Disable()
source_sizer = wx.BoxSizer(wx.HORIZONTAL)
source_sizer.AddMany((
(wx.StaticText(self, label='Appears on:', size=size), 0, wx.CENTER),
(self.text_source, 1),
(self.button_source, 0, wx.LEFT, 5)
))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.AddMany((
(status_sizer, 0, wx.ALL | wx.EXPAND, 5),
(link_sizer, 0, wx.ALL | wx.EXPAND, 5),
(source_sizer, 0, wx.ALL | wx.EXPAND, 5)
))
return sizer
def load_options(self):
self.url.ChangeValue(self.options['url']) # does not fire event
self.depth.SetValue(self.options['depth'])
self.threads.SetValue(self.options['threads'])
self.delay.SetValue(self.options['delay'])
self.timeout.SetValue(self.options['timeout'])
self.redirect.SetValue(self.options['redirect'])
self.query.SetValue(self.options['query'])
for i, element in enumerate(self.external):
element.SetValue(self.options['external'][i])
for i, element in enumerate(self.internal):
element.SetValue(self.options['internal'][i])
self.local.SetValue(self.options['local'])
self.remote.SetValue(self.options['remote'])
for i, element in enumerate(self.status):
element.SetValue(self.options['status'][i])
def save_options(self, event=None):
self.options['url'] = self.url.GetValue()
self.options['depth'] = self.depth.GetValue()
self.options['threads'] = self.threads.GetValue()
self.options['delay'] = self.delay.GetValue()
self.options['timeout'] = self.timeout.GetValue()
self.options['redirect'] = self.redirect.IsChecked()
self.options['query'] = self.query.IsChecked()
self.options['local'] = self.local.IsChecked()
self.options['remote'] = self.remote.IsChecked()
self.q = self.search.GetValue()
for i, element in enumerate(self.external):
self.options['external'][i] = element.GetValue()
for i, element in enumerate(self.internal):
self.options['internal'][i] = element.GetValue()
for i, element in enumerate(self.status):
self.options['status'][i] = element.GetValue()
def read_item(self, m, n):
return self.list_ctrl.GetItem(m, n).GetText()
def list_selected(self, event=None):
self.button_link.Enable()
self.button_source.Enable()
self.sizer.Show(self.info_sizer)
self.update_info()
def list_deselected(self, event=None):
self.button_link.Disable()
self.button_source.Disable()
self.sizer.Hide(self.info_sizer)
def update_info(self):
selected = self.list_ctrl.GetNextSelected(-1)
if selected < 0:
return
status = self.read_item(selected, 0)
if status in STATUS_CODES:
status = '{} ({})'.format(status, STATUS_CODES[status])
self.text_status.SetLabel(status)
self.text_link.SetLabel(self.read_item(selected, 1))
self.text_source.SetValue(self.read_item(selected, 2))
def update_status_action(self):
if self.scanner is not None:
if self.paused:
status = 'Paused'
else:
status = 'Scanning...'
else:
if self.done:
status = 'Done'
else:
status = 'Stopped'
self.parent.status.SetStatusText(status, 0)
def update_status_items(self):
self.parent.status.SetStatusText('{:d} items'.format(self.rows), 1)
def open_browser(self, link):
if link != '':
webbrowser.open(link)
def open_link(self, event=None):
self.open_browser(self.text_link.GetValue())
def open_source(self, event=None):
webbrowser.open(self.text_source.GetValue())
def open_rules_editor(self, event=None):
frame = RulesFrame(self, self.rules)
frame.Show()
frame.Center()
def get_radio_value(self, element, default):
return next((k for k, v in enumerate(element) if v), default)
def enter(self, event=None):
if self.scanner is None:
self.start()
else:
self.pause()
def start(self, event=None):
self.done = False
self.results = []
self.rows = 0
self.button_stop.Enable()
self.button_start.SetLabel('Pau&se')
self.parent.item_stop.Enable(True)
self.parent.item_start.SetItemLabel('Pau&se')
self.list_ctrl.DeleteAllItems()
self.update_status_items()
self.scanner = Scanner(self, (
self.url.GetValue(),
self.options['depth'],
self.options['threads'],
self.options['delay'],
self.options['timeout'],
self.options['redirect'],
self.options['query'],
self.get_radio_value(self.options['external'], CHECK),
self.get_radio_value(self.options['internal'], FOLLOW)
), self.rules)
self.scanner.start()
self.update_status_action()
def pause(self):
if self.paused:
self.button_start.SetLabel('Pau&se')
self.parent.item_start.SetItemLabel('Pau&se')
else:
self.button_start.SetLabel('Re&sume')
self.parent.item_start.SetItemLabel('Re&sume')
self.paused = not self.paused
self.scanner.pause()
self.update_status_action()
def stop(self, event=None):
self.button_start.Enable()
self.button_stop.Disable()
self.button_start.SetLabel('&Start')
self.parent.item_start.Enable(True)
self.parent.item_stop.Enable(False)
self.parent.item_start.SetItemLabel('&Start')
if self.paused:
self.scanner.pause() # resolves unresolved pause event
self.paused = False
if self.scanner is not None:
self.scanner.stop()
self.scanner = None
self.update_status_action()
def filter(self, event=None):
self.save_options()
self.list_ctrl.DeleteAllItems()
self.rows = 0
for row in self.results:
self.insert_row(row)
self.update_status_items()
def update(self, event):
if event.status == COMPLETED:
self.done = True
self.stop()
else:
row = (event.status, event.link, event.source, event.server)
self.results.append(row)
self.insert_row(row)
def insert_row(self, row):
status, link, source, server = row
# checks visibility of row
if status == TIMEOUT:
n = TIMEOUT
text = 'Timeout'
elif status == SKIPPED:
n = SKIPPED
text = 'Skipped'
else:
n = status // 100
text = str(status)
if server == INTERNAL and not self.options['local']:
return
if server == EXTERNAL and not self.options['remote']:
return
if not self.options['status'][n]:
return
if self.q != '' and link.find(self.q) < 0 and source.find(self.q) < 0:
return
self.list_ctrl.InsertItem(self.rows, text)
self.list_ctrl.SetItem(self.rows, 1, link)
self.list_ctrl.SetItem(self.rows, 2, source)
# scrolls to bottom
self.list_ctrl.EnsureVisible(self.list_ctrl.GetItemCount() - 1)
self.update_status_items()
self.rows += 1
################################################################################
# RulesFrame class #
################################################################################
class RulesFrame(wx.Frame):
def __init__(self, parent, rules):
wx.Frame.__init__(self, parent, title='Link Rules', size=SUB_FRAME_SIZE)
self.panel = RulesPanel(self, rules)
def close(self, event=None):
self.Destroy()
################################################################################
# RulesPanel class #
################################################################################
class RulesPanel(wx.Panel):
def __init__(self, parent, rules):
wx.Panel.__init__(self, parent)
self.rules = rules
self.list_ctrl = self.create_list_ctrl()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.AddMany((
(self.list_ctrl, 3, wx.ALL | wx.EXPAND, 5),
(self.create_edit_sizer(), 0, wx.EXPAND, 5),
(self.create_button_sizer(parent), 0, wx.EXPAND, 5)
))
self.SetSizer(self.sizer)
self.reload()
def create_list_ctrl(self):
element = wx.ListCtrl(self, style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
element.InsertColumn(0, 'Condition', width=75)
element.InsertColumn(1, 'Scope', width=75)
element.InsertColumn(2, 'Rule', width=410)
element.Bind(wx.EVT_LIST_ITEM_SELECTED, self.list_selected)
element.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.list_deselected)
return element
def create_edit_sizer(self):
size = (80, -1) # text label size
self.choices_condition = ('Include', 'Exclude')
self.choices_scope = ('Any', 'Internal', 'External')
self.select_condition = wx.Choice(self, choices=self.choices_condition)
self.select_scope = wx.Choice(self, choices=self.choices_scope)
self.text_match = wx.TextCtrl(self)
self.text_match.Bind(wx.EVT_TEXT, self.toggle_add_button)
condition_sizer = wx.BoxSizer(wx.HORIZONTAL)
condition_sizer.AddMany((
(wx.StaticText(self, label='Condition:', size=size)),
self.select_condition
))
scope_sizer = wx.BoxSizer(wx.HORIZONTAL)
scope_sizer.AddMany((
(wx.StaticText(self, label='Scope:', size=size)),
self.select_scope
))
match_sizer = wx.BoxSizer(wx.HORIZONTAL)
match_sizer.AddMany((
(wx.StaticText(self, label='Match:', size=size), 0),
(self.text_match, 1)
))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.AddMany((
(condition_sizer, 0, wx.ALL | wx.EXPAND, 5),
(scope_sizer, 0, wx.ALL | wx.EXPAND, 5),
(match_sizer, 0, wx.ALL | wx.EXPAND, 5)
))
# selects default choices
self.select_condition.Select(0)
self.select_scope.Select(0)
return sizer
def create_button_sizer(self, parent):
button_close = wx.Button(self, label='&Close')
self.button_add = wx.Button(self, label='&Add')
self.button_modify = wx.Button(self, label='&Modify')
self.button_remove = wx.Button(self, label='&Remove')
button_close.Bind(wx.EVT_BUTTON, parent.close)
self.button_add.Bind(wx.EVT_BUTTON, self.add)
self.button_modify.Bind(wx.EVT_BUTTON, self.modify)
self.button_remove.Bind(wx.EVT_BUTTON, self.remove)
self.button_add.Disable()
self.button_modify.Disable()
self.button_remove.Disable()
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.AddMany((
(self.button_add, 0, wx.ALL, 5),
(self.button_modify, 0, wx.TOP | wx.RIGHT | wx.BOTTOM, 5),
(self.button_remove, 0, wx.TOP | wx.RIGHT | wx.BOTTOM, 5)
))
sizer.AddStretchSpacer()
sizer.Add(button_close, 0, wx.ALL, 5)
return sizer
def toggle_add_button(self, event=None):
self.button_add.Enable(self.text_match.GetValue() != '')
def list_selected(self, event=None):
self.button_modify.Enable()
self.button_remove.Enable()
selected = self.list_ctrl.GetNextSelected(-1)
condition, scope, match = self.rules[selected]
self.select_condition.SetSelection(condition)
self.select_scope.SetSelection(scope)
self.text_match.SetValue(match)
def list_deselected(self, event=None):
self.button_modify.Disable()
self.button_remove.Disable()
self.select_condition.SetSelection(INCLUDE)
self.select_scope.SetSelection(INTERNAL)
self.text_match.SetValue('')
def add(self, event=None):
condition = self.select_condition.GetSelection()
scope = self.select_scope.GetSelection()
match = self.text_match.GetValue()
if match != '':
self.rules.append((condition, scope, match))
self.reload()
self.list_ctrl.Select(len(self.rules) - 1)
def modify(self, event=None):
condition = self.select_condition.GetSelection()
scope = self.select_scope.GetSelection()
match = self.text_match.GetValue()
selected = self.list_ctrl.GetNextSelected(-1)
self.rules[selected] = (condition, scope, match)
self.reload()
self.list_ctrl.Select(selected)
def remove(self, event=None):
selected = self.list_ctrl.GetNextSelected(-1)
del self.rules[selected]
self.list_deselected()
self.reload()
def reload(self):
self.list_ctrl.DeleteAllItems()
for i, rule in enumerate(self.rules):
condition, scope, match = rule
self.list_ctrl.InsertItem(i, self.choices_condition[condition])
self.list_ctrl.SetItem(i, 1, self.choices_scope[scope])
self.list_ctrl.SetItem(i, 2, match)
################################################################################
# Scanner class #
################################################################################
class Scanner(threading.Thread):
def __init__(self, parent, options, rules):
threading.Thread.__init__(self)
self.parent = parent
self.paused = None
self.stopped = False
self.url = options[0]
self.depth = options[1]
self.threads = options[2]
self.delay = options[3]
self.timeout = options[4]
self.redirect = options[5]
self.query = options[6]
self.external = options[7]
self.internal = options[8]
self.rules = list(map(lambda r: (r[0], r[1], re.compile(r[2])), rules))
self.links = set([self.url]) # link cache to avoid repeating links
self.domain = urllib.parse.urlparse(self.url).netloc
self.pool = ThreadPool(self.threads)
self.pool.add(Task(
link=self.url,
source='',
depth=0,
timeout=self.timeout,
redirect=self.redirect,
server=INTERNAL,
follow=True
))
def run(self):
self.pool.start()
for task in self.pool.poll_completed_tasks():
if self.paused is not None: # paused by user
self.paused.wait()
if self.stopped: # stopped by user
return
if task.server == INTERNAL and task.redirected: # redirected
parsed = urllib.parse.urlparse(task.link)
# changes server type if domain has changed
if parsed.netloc != self.domain:
task.server = EXTERNAL
if task.error: # error encountered
self.error(task)
continue
self.tell(task.status, task.link, task.source, task.server)
# domain has changed, so check follow option again
# before processing page links
if task.server == EXTERNAL and self.external != FOLLOW:
continue
# adds links found on page to tasks
for link in self.scan_links(task):
self.pool.add(link)
time.sleep(self.delay)
self.done()
def scan_links(self, task):
for link in task.links:
depth = task.depth
# ignores URL fragments
link, fragment = urllib.parse.urldefrag(link)
# checks if already scanned
if link in self.links:
continue
self.links.add(link)
parsed = urllib.parse.urlparse(link)
# checks for query string
if parsed.query != '':
link += '?' + parsed.query
if not self.query:
self.skip(link, task)
continue
# checks if http/s
if not parsed.scheme in ACCEPT_SCHEMES:
if not parsed.scheme in IGNORE_SCHEMES:
self.skip(link, task)
continue
if parsed.netloc == self.domain: # internal link
if self.internal == IGNORE:
self.skip(link, task)