-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitguard_sync.py
More file actions
1665 lines (1341 loc) · 63 KB
/
gitguard_sync.py
File metadata and controls
1665 lines (1341 loc) · 63 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
"""
GitGuard Sync - Git 倉庫安全同步守護者
功能強大的 Git 倉庫同步工具,具備現代化的 GUI 介面
支援 GitLab 和 GitHub 雙平台同步,並整合 GitGuardian 專業機敏資料掃描
版本: 3.0.1 (修復版)
"""
import os
import sys
import json
import subprocess
import threading
import webbrowser
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass
from datetime import datetime
import re
# GUI 相關
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, scrolledtext
from tkinter.font import Font
# 第三方庫
try:
import requests
import git
except ImportError as e:
print(f"缺少必要套件: {e}")
print("請執行: pip install gitpython requests")
sys.exit(1)
@dataclass
class ScanResult:
"""掃描結果資料結構"""
file_path: str
line_number: int
secret_type: str
content: str
severity: str
@dataclass
class RemoteConfig:
"""遠端倉庫配置"""
name: str
url: str
type: str # 'github' or 'gitlab'
status: str # 'connected', 'error', 'not_configured'
class GitGuardSyncGUI:
"""GitGuard Sync GUI 主應用程式"""
def __init__(self):
# 先初始化基本屬性
self.current_path = Path.cwd()
self.repo = None
self.gitguardian_api_key = os.getenv('GITGUARDIAN_API_KEY', '')
self.security_patterns = self._init_security_patterns()
# 初始化線程控制
self.stop_threads = threading.Event()
# 再設定 GUI
self.root = tk.Tk()
self.setup_window()
self.setup_fonts()
self.setup_colors()
self.setup_variables()
self.setup_ui()
self.center_window()
def setup_window(self):
"""設定主視窗"""
self.root.title("GitGuard Sync v3.0.1 - Git 倉庫安全同步守護者")
self.root.geometry("900x700")
self.root.minsize(800, 600)
# 設定圖示(如果有的話)
try:
if hasattr(sys, '_MEIPASS'):
icon_path = os.path.join(sys._MEIPASS, 'icon.ico')
else:
icon_path = 'icon.ico'
if os.path.exists(icon_path):
self.root.iconbitmap(icon_path)
except:
pass
def setup_fonts(self):
"""設定字型"""
self.font_title = Font(family="Consolas", size=12, weight="bold")
self.font_ascii = Font(family="Courier New", size=8)
self.font_button = Font(family="微軟正黑體", size=10)
self.font_text = Font(family="微軟正黑體", size=9)
def setup_colors(self):
"""設定色彩主題"""
self.colors = {
'bg_primary': '#2c3e50', # 深藍灰
'bg_secondary': '#34495e', # 次要背景
'bg_card': '#ecf0f1', # 卡片背景
'text_primary': '#2c3e50', # 主要文字
'text_secondary': '#7f8c8d', # 次要文字
'accent_blue': '#3498db', # 藍色強調
'accent_green': '#2ecc71', # 綠色強調
'accent_red': '#e74c3c', # 紅色強調
'accent_orange': '#f39c12', # 橙色強調
'button_hover': '#bdc3c7' # 按鈕懸停
}
# 設定主視窗背景
self.root.configure(bg=self.colors['bg_card'])
def setup_variables(self):
"""設定變數"""
self.current_dir_var = tk.StringVar(value=str(self.current_path))
self.api_key_var = tk.StringVar(value=self.gitguardian_api_key)
self.scan_progress_var = tk.DoubleVar()
self.scan_status_var = tk.StringVar(value="就緒")
def setup_ui(self):
"""設定使用者介面"""
# 主容器
main_frame = ttk.Frame(self.root)
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# ASCII 標題
self.create_title_section(main_frame)
# 主要功能區域
content_frame = ttk.Frame(main_frame)
content_frame.pack(fill=tk.BOTH, expand=True, pady=(10, 0))
# 左側功能面板
left_frame = ttk.LabelFrame(content_frame, text="🎛️ 控制面板", padding=10)
left_frame.pack(side=tk.LEFT, fill=tk.Y, padx=(0, 5))
# 右側日誌面板
right_frame = ttk.LabelFrame(content_frame, text="📋 操作日誌", padding=10)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=(5, 0))
self.create_control_panel(left_frame)
self.create_log_panel(right_frame)
# 底部狀態列
self.create_status_bar(main_frame)
def create_title_section(self, parent):
"""創建標題區域"""
title_frame = ttk.Frame(parent)
title_frame.pack(fill=tk.X, pady=(0, 10))
# ASCII 藝術標題 - 中文版,置中顯示
ascii_title = """
╔══════════════════════════════════════════════════════════════════════════════╗
║ ██████╗ ██╗████████╗ ██████╗ ██╗ ██╗ █████╗ ██████╗ ██████╗ ║
║ ██╔════╝ ██║╚══██╔══╝ ██╔════╝ ██║ ██║██╔══██╗██╔══██╗██╔══██╗ ║
║ ██║ ███╗██║ ██║ ██║ ███╗██║ ██║███████║██████╔╝██║ ██║ ║
║ ██║ ██║██║ ██║ ██║ ██║██║ ██║██╔══██║██╔══██╗██║ ██║ ║
║ ╚██████╔╝██║ ██║ ╚██████╔╝╚██████╔╝██║ ██║██║ ██║██████╔╝ ║
║ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ║
║ ║
║ ███████╗██╗ ██╗███╗ ██╗ ██████╗ ██╗ ██╗██████╗ ██╗██████╗ ║
║ ██╔════╝╚██╗ ██╔╝████╗ ██║██╔════╝ ██║ ██║╚════██╗██║██╔══██╗ ║
║ ███████╗ ╚████╔╝ ██╔██╗ ██║██║ ██║ ██║ █████╔╝██║██████╔╝ ║
║ ╚════██║ ╚██╔╝ ██║╚██╗██║██║ ╚██╗ ██╔╝ ╚═══██╗██║██╔══██╗ ║
║ ███████║ ██║ ██║ ╚████║╚██████╗ ╚████╔╝ ██████╔╝██║██████╔╝ ║
║ ╚══════╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═══╝ ╚═════╝ ╚═╝╚═════╝ ║
║ ║
║ 🔐 Git 倉庫安全同步守護者 🔐 ║
╚══════════════════════════════════════════════════════════════════════════════╝
"""
title_text = tk.Text(title_frame,
height=16,
wrap=tk.NONE,
font=self.font_ascii,
bg=self.colors['bg_primary'],
fg='white',
relief=tk.FLAT,
state=tk.DISABLED)
title_text.pack(fill=tk.X)
title_text.config(state=tk.NORMAL)
title_text.insert(tk.END, ascii_title)
# 將文字置中
title_text.tag_add("center", "1.0", "end")
title_text.tag_config("center", justify='center')
title_text.config(state=tk.DISABLED)
# 副標題 - 置中
subtitle_frame = ttk.Frame(title_frame)
subtitle_frame.pack(fill=tk.X, pady=(5, 0))
subtitle = ttk.Label(
subtitle_frame,
text="🔐 Git 倉庫安全同步守護者 | 支援雙平台同步 + GitGuardian 安全掃描 🔐",
font=self.font_title,
foreground=self.colors['text_secondary'],
anchor="center")
subtitle.pack(expand=True)
def create_control_panel(self, parent):
"""創建控制面板"""
# 目錄選擇區域
dir_frame = ttk.LabelFrame(parent, text="📁 工作目錄", padding=5)
dir_frame.pack(fill=tk.X, pady=(0, 10))
ttk.Entry(dir_frame,
textvariable=self.current_dir_var,
font=self.font_text,
state="readonly").pack(fill=tk.X, pady=(0, 5))
dir_buttons_frame = ttk.Frame(dir_frame)
dir_buttons_frame.pack(fill=tk.X)
ttk.Button(dir_buttons_frame,
text="📂 瀏覽目錄",
command=self.select_directory).pack(side=tk.LEFT,
padx=(0, 5))
ttk.Button(dir_buttons_frame, text="🔄 重新載入",
command=self.refresh_repo).pack(side=tk.LEFT)
# 遠端倉庫管理
remote_frame = ttk.LabelFrame(parent, text="🌐 遠端倉庫", padding=5)
remote_frame.pack(fill=tk.X, pady=(0, 10))
remote_buttons_frame = ttk.Frame(remote_frame)
remote_buttons_frame.pack(fill=tk.X)
ttk.Button(remote_buttons_frame,
text="⚙️ 配置遠端",
command=self.configure_remotes).pack(side=tk.LEFT,
padx=(0, 5))
ttk.Button(remote_buttons_frame,
text="🔍 測試連線",
command=self.test_remotes).pack(side=tk.LEFT)
# 安全掃描區域
scan_frame = ttk.LabelFrame(parent, text="🔒 安全掃描", padding=5)
scan_frame.pack(fill=tk.X, pady=(0, 10))
# GitGuardian API 設定
api_frame = ttk.Frame(scan_frame)
api_frame.pack(fill=tk.X, pady=(0, 5))
ttk.Label(api_frame, text="API 金鑰:").pack(anchor=tk.W)
api_entry_frame = ttk.Frame(api_frame)
api_entry_frame.pack(fill=tk.X)
self.api_entry = ttk.Entry(api_entry_frame,
textvariable=self.api_key_var,
show="*",
font=self.font_text)
self.api_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
ttk.Button(api_entry_frame,
text="💾",
command=self.save_api_key,
width=3).pack(side=tk.RIGHT, padx=(5, 0))
# 掃描按鈕
scan_buttons_frame = ttk.Frame(scan_frame)
scan_buttons_frame.pack(fill=tk.X, pady=(5, 0))
ttk.Button(scan_buttons_frame,
text="🔍 本地掃描",
command=lambda: self.start_scan("local")).pack(side=tk.LEFT,
padx=(0, 5))
ttk.Button(
scan_buttons_frame,
text="🛡️ GitGuardian",
command=lambda: self.start_scan("gitguardian")).pack(side=tk.LEFT)
# 進度條
progress_frame = ttk.Frame(scan_frame)
progress_frame.pack(fill=tk.X, pady=(5, 0))
self.progress_bar = ttk.Progressbar(progress_frame,
variable=self.scan_progress_var,
maximum=100)
self.progress_bar.pack(fill=tk.X)
# 同步推送區域
sync_frame = ttk.LabelFrame(parent, text="🚀 同步推送", padding=5)
sync_frame.pack(fill=tk.X, pady=(0, 10))
sync_buttons_frame = ttk.Frame(sync_frame)
sync_buttons_frame.pack(fill=tk.X)
ttk.Button(sync_buttons_frame,
text="📤 提交變更",
command=self.commit_changes).pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(sync_buttons_frame,
text="🌐 推送同步",
command=self.sync_repositories).pack(side=tk.LEFT)
# 工具區域
tools_frame = ttk.LabelFrame(parent, text="🛠️ 工具", padding=5)
tools_frame.pack(fill=tk.X)
tools_buttons_frame = ttk.Frame(tools_frame)
tools_buttons_frame.pack(fill=tk.X)
ttk.Button(tools_buttons_frame,
text="📊 倉庫狀態",
command=self.show_repo_status).pack(side=tk.LEFT,
padx=(0, 5))
ttk.Button(tools_buttons_frame, text="ℹ️ 關於",
command=self.show_about).pack(side=tk.LEFT)
def create_log_panel(self, parent):
"""創建日誌面板"""
# 日誌文字區域
self.log_text = scrolledtext.ScrolledText(parent,
wrap=tk.WORD,
font=self.font_text,
height=20)
self.log_text.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
# 日誌控制按鈕
log_buttons_frame = ttk.Frame(parent)
log_buttons_frame.pack(fill=tk.X)
ttk.Button(log_buttons_frame, text="🗑️ 清除日誌",
command=self.clear_log).pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(log_buttons_frame, text="💾 儲存日誌",
command=self.save_log).pack(side=tk.LEFT)
# 初始化日誌
self.log("GitGuard Sync v3.0.1 啟動成功")
self.log("=" * 50)
def create_status_bar(self, parent):
"""創建狀態列"""
status_frame = ttk.Frame(parent)
status_frame.pack(fill=tk.X, pady=(10, 0))
# 狀態標籤
ttk.Label(status_frame,
textvariable=self.scan_status_var,
relief=tk.SUNKEN,
padding=5).pack(side=tk.LEFT)
# Git 狀態指示器
self.git_status_label = ttk.Label(status_frame,
text="❌ 非 Git 倉庫",
relief=tk.SUNKEN,
padding=5)
self.git_status_label.pack(side=tk.RIGHT)
# 更新初始狀態
self.update_git_status()
def center_window(self):
"""視窗置中"""
self.root.update_idletasks()
x = (self.root.winfo_screenwidth() - self.root.winfo_width()) // 2
y = (self.root.winfo_screenheight() - self.root.winfo_height()) // 2
self.root.geometry(f"+{x}+{y}")
def _init_security_patterns(self) -> Dict[str, str]:
"""初始化安全檢查模式"""
return {
'aws_access_key': r'AKIA[0-9A-Z]{16}',
'github_token': r'gh[pousr]_[A-Za-z0-9]{36}',
'jwt_token':
r'eyJ[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*',
'api_key': r'api[_-]?key["\']?\s*[:=]\s*["\']?[A-Za-z0-9]{20,}',
'password': r'password["\']?\s*[:=]\s*["\'][^"\']{8,}["\']',
'secret_key':
r'secret[_-]?key["\']?\s*[:=]\s*["\']?[A-Za-z0-9]{20,}',
'database_url': r'(mysql|postgresql|mongodb)://[^\s]+',
'email': r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}',
'ssh_private_key': r'-----BEGIN [A-Z ]*PRIVATE KEY-----',
'slack_token': r'xox[baprs]-[A-Za-z0-9-]{10,48}',
}
def log(self, message: str, level: str = "INFO"):
"""記錄日誌"""
timestamp = datetime.now().strftime("%H:%M:%S")
log_message = f"[{timestamp}] {level}: {message}\n"
self.log_text.insert(tk.END, log_message)
self.log_text.see(tk.END)
# 根據等級設定顏色
if level == "ERROR":
# 可以添加顏色標記
pass
def clear_log(self):
"""清除日誌"""
self.log_text.delete(1.0, tk.END)
self.log("日誌已清除")
def save_log(self):
"""儲存日誌"""
try:
filename = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[
("文字檔案", "*.txt"),
("所有檔案", "*.*")
],
title="儲存日誌檔案")
if filename:
with open(filename, 'w', encoding='utf-8') as f:
f.write(self.log_text.get(1.0, tk.END))
self.log(f"日誌已儲存至: {filename}")
except Exception as e:
self.log(f"儲存日誌失敗: {e}", "ERROR")
messagebox.showerror("錯誤", f"儲存日誌失敗: {e}")
def select_directory(self):
"""選擇工作目錄"""
directory = filedialog.askdirectory(title="選擇 Git 倉庫目錄",
initialdir=str(self.current_path))
if directory:
self.current_path = Path(directory)
self.current_dir_var.set(str(self.current_path))
self.log(f"已切換到目錄: {self.current_path}")
self.refresh_repo()
def refresh_repo(self):
"""重新載入倉庫"""
try:
if self._is_git_repo():
self.repo = git.Repo(self.current_path)
self.log("Git 倉庫載入成功")
else:
self.repo = None
self.log("當前目錄不是 Git 倉庫", "WARNING")
self.update_git_status()
except Exception as e:
self.log(f"載入倉庫失敗: {e}", "ERROR")
self.repo = None
self.update_git_status()
def _is_git_repo(self, path: Optional[Path] = None) -> bool:
"""檢查是否為 Git 倉庫"""
check_path = path or self.current_path
return (check_path / '.git').exists()
def update_git_status(self):
"""更新 Git 狀態顯示"""
if self._is_git_repo():
try:
if self.repo:
branch = self.repo.active_branch.name
self.git_status_label.config(text=f"✅ Git 倉庫 ({branch})")
else:
self.git_status_label.config(text="✅ Git 倉庫")
except:
self.git_status_label.config(text="✅ Git 倉庫")
else:
self.git_status_label.config(text="❌ 非 Git 倉庫")
def save_api_key(self):
"""儲存 API 金鑰"""
api_key = self.api_key_var.get().strip()
if api_key:
os.environ['GITGUARDIAN_API_KEY'] = api_key
self.gitguardian_api_key = api_key
self.log("GitGuardian API 金鑰已設定")
messagebox.showinfo("成功", "API 金鑰已儲存")
else:
self.log("API 金鑰為空", "WARNING")
def configure_remotes(self):
"""配置遠端倉庫"""
if not self._is_git_repo():
messagebox.showerror("錯誤", "當前目錄不是 Git 倉庫")
return
RemoteConfigDialog(self.root, self)
def test_remotes(self):
"""測試遠端連線"""
if not self.repo:
messagebox.showerror("錯誤", "請先載入 Git 倉庫")
return
self.log("開始測試遠端連線...")
def test_worker():
try:
remotes = list(self.repo.remotes)
if not remotes:
self.log("沒有配置遠端倉庫", "WARNING")
return
for remote in remotes:
try:
result = subprocess.run(
["git", "ls-remote", "--heads", remote.name],
cwd=self.current_path,
capture_output=True,
timeout=10,
encoding='utf-8')
if result.returncode == 0:
self.log(f"✅ {remote.name}: 連線正常")
else:
self.log(f"❌ {remote.name}: 連線失敗", "ERROR")
except subprocess.TimeoutExpired:
self.log(f"⏰ {remote.name}: 連線逾時", "WARNING")
except Exception as e:
self.log(f"❌ {remote.name}: {e}", "ERROR")
self.log("遠端連線測試完成")
except Exception as e:
self.log(f"測試遠端連線失敗: {e}", "ERROR")
thread = threading.Thread(target=test_worker, daemon=True)
thread.start()
def start_scan(self, scan_type: str):
"""開始安全掃描"""
if not self._is_git_repo():
messagebox.showerror("錯誤", "請先選擇 Git 倉庫目錄")
return
self.scan_progress_var.set(0)
self.scan_status_var.set("掃描中...")
self.log(f"開始 {scan_type} 安全掃描...")
def scan_worker():
try:
results = []
if scan_type == "local":
results = self._local_security_scan()
elif scan_type == "gitguardian":
if not self.gitguardian_api_key:
self.log("請先設定 GitGuardian API 金鑰", "ERROR")
self.scan_status_var.set("就緒")
return
results = self._gitguardian_scan()
self.scan_progress_var.set(100)
self._display_scan_results(results)
self.scan_status_var.set("掃描完成")
except Exception as e:
self.log(f"掃描失敗: {e}", "ERROR")
self.scan_status_var.set("掃描失敗")
thread = threading.Thread(target=scan_worker, daemon=True)
thread.start()
def _local_security_scan(self) -> List[ScanResult]:
"""本地安全掃描"""
results = []
file_count = 0
processed = 0
# 計算總檔案數
for file_path in self.current_path.rglob("*"):
if self._should_scan_file(file_path):
file_count += 1
self.log(f"準備掃描 {file_count} 個檔案...")
for file_path in self.current_path.rglob("*"):
if self._should_scan_file(file_path):
try:
content = file_path.read_text(encoding='utf-8',
errors='ignore')
file_results = self._scan_file_content(
str(file_path), content)
results.extend(file_results)
processed += 1
progress = (processed / file_count) * 100
self.scan_progress_var.set(progress)
if file_results:
self.log(
f"發現 {len(file_results)} 個問題: {file_path.name}")
except Exception as e:
self.log(f"掃描檔案失敗 {file_path}: {e}", "WARNING")
continue
return results
def _should_scan_file(self, file_path: Path) -> bool:
"""判斷是否應該掃描檔案"""
if not file_path.is_file():
return False
# 排除隱藏目錄和檔案
if any(part.startswith('.') for part in file_path.parts):
return False
# 只掃描特定副檔名
scan_extensions = {
'.py', '.js', '.json', '.yaml', '.yml', '.env', '.sh', '.md',
'.txt', '.conf', '.cfg'
}
return file_path.suffix.lower() in scan_extensions
def _scan_file_content(self, file_path: str,
content: str) -> List[ScanResult]:
"""掃描檔案內容"""
results = []
lines = content.split('\n')
for line_num, line in enumerate(lines, 1):
for pattern_name, pattern in self.security_patterns.items():
matches = re.finditer(pattern, line, re.IGNORECASE)
for match in matches:
results.append(
ScanResult(file_path=file_path,
line_number=line_num,
secret_type=pattern_name,
content=match.group(),
severity="high" if pattern_name in [
'aws_access_key', 'github_token'
] else "medium"))
return results
def _gitguardian_scan(self) -> List[ScanResult]:
"""GitGuardian API 掃描"""
results = []
headers = {
'Authorization': f'Token {self.gitguardian_api_key}',
'Content-Type': 'application/json'
}
file_count = 0
processed = 0
# 計算檔案數
for file_path in self.current_path.rglob("*"):
if self._should_scan_file(file_path):
file_count += 1
self.log(f"使用 GitGuardian API 掃描 {file_count} 個檔案...")
for file_path in self.current_path.rglob("*"):
if self._should_scan_file(file_path):
try:
content = file_path.read_text(encoding='utf-8',
errors='ignore')
# 呼叫 GitGuardian API
response = requests.post(
'https://api.gitguardian.com/v1/scan',
headers=headers,
json={
'document': content,
'filename': file_path.name
},
timeout=30)
if response.status_code == 200:
scan_data = response.json()
if scan_data.get('policy_breaks'):
for break_info in scan_data['policy_breaks']:
matches = break_info.get('matches', [{}])
if matches:
match = matches[0]
results.append(
ScanResult(file_path=str(file_path),
line_number=match.get(
'line_start', 0),
secret_type=break_info.get(
'break_type',
'unknown'),
content=match.get(
'match', ''),
severity=break_info.get(
'severity', 'medium')))
self.log(
f"GitGuardian 發現 {len(scan_data['policy_breaks'])} 個問題: {file_path.name}"
)
elif response.status_code == 401:
self.log("GitGuardian API 金鑰無效", "ERROR")
break
processed += 1
progress = (processed / file_count) * 100
self.scan_progress_var.set(progress)
except requests.RequestException as e:
self.log(f"GitGuardian API 請求失敗: {e}", "ERROR")
continue
except Exception as e:
self.log(f"掃描檔案失敗 {file_path}: {e}", "WARNING")
continue
return results
def _display_scan_results(self, results: List[ScanResult]):
"""顯示掃描結果"""
if not results:
self.log("✅ 未發現安全問題")
messagebox.showinfo("掃描完成", "未發現安全問題")
return
# 按嚴重程度分組
high_severity = [r for r in results if r.severity == "high"]
medium_severity = [r for r in results if r.severity == "medium"]
self.log(f"🔍 掃描完成,發現 {len(results)} 個潛在安全問題")
if high_severity:
self.log(f"🚨 高風險問題: {len(high_severity)} 個", "ERROR")
if medium_severity:
self.log(f"⚠️ 中風險問題: {len(medium_severity)} 個", "WARNING")
# 顯示詳細結果視窗
ScanResultDialog(self.root, results, self)
def commit_changes(self):
"""提交變更"""
if not self.repo:
messagebox.showerror("錯誤", "請先載入 Git 倉庫")
return
try:
# 檢查是否有變更
if not self.repo.is_dirty():
self.log("沒有需要提交的變更")
messagebox.showinfo("資訊", "沒有需要提交的變更")
return
# 顯示提交對話框
CommitDialog(self.root, self)
except Exception as e:
self.log(f"檢查變更失敗: {e}", "ERROR")
messagebox.showerror("錯誤", f"檢查變更失敗: {e}")
def sync_repositories(self):
"""同步推送到所有遠端倉庫"""
if not self.repo:
messagebox.showerror("錯誤", "請先載入 Git 倉庫")
return
try:
remotes = list(self.repo.remotes)
if not remotes:
messagebox.showerror("錯誤", "沒有配置遠端倉庫")
return
# 顯示推送對話框
SyncDialog(self.root, self, remotes)
except Exception as e:
self.log(f"準備同步失敗: {e}", "ERROR")
messagebox.showerror("錯誤", f"準備同步失敗: {e}")
def show_repo_status(self):
"""顯示倉庫狀態"""
if not self.repo:
messagebox.showerror("錯誤", "請先載入 Git 倉庫")
return
RepoStatusDialog(self.root, self)
def show_about(self):
"""顯示關於對話框"""
AboutDialog(self.root)
def run(self):
"""執行主程式"""
try:
# 設置窗口關閉處理
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
self.refresh_repo()
self.root.mainloop()
except KeyboardInterrupt:
self.log("程式被使用者中斷")
except Exception as e:
self.log(f"程式執行錯誤: {e}", "ERROR")
messagebox.showerror("錯誤", f"程式執行錯誤: {e}")
finally:
# 確保線程安全結束
if hasattr(self, 'stop_threads'):
self.stop_threads.set()
def on_closing(self):
"""窗口關閉時的處理"""
try:
self.log("程式正在關閉...")
# 設置停止標誌
self.stop_threads.set()
# 銷毀視窗
self.root.destroy()
except Exception as e:
print(f"關閉程式時發生錯誤: {e}")
# 對話框類別
class RemoteConfigDialog:
"""遠端倉庫配置對話框"""
def __init__(self, parent, main_app):
self.main_app = main_app
self.dialog = tk.Toplevel(parent)
self.dialog.title("遠端倉庫配置")
self.dialog.geometry("500x400")
self.dialog.resizable(False, False)
self.dialog.transient(parent)
self.dialog.grab_set()
self.setup_ui()
self.load_remotes()
# 置中顯示
self.dialog.update_idletasks()
x = parent.winfo_x() + (parent.winfo_width() -
self.dialog.winfo_width()) // 2
y = parent.winfo_y() + (parent.winfo_height() -
self.dialog.winfo_height()) // 2
self.dialog.geometry(f"+{x}+{y}")
def setup_ui(self):
"""設定界面"""
main_frame = ttk.Frame(self.dialog)
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# 遠端列表
list_frame = ttk.LabelFrame(main_frame, text="現有遠端倉庫", padding=5)
list_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
# 創建 Treeview
self.tree = ttk.Treeview(list_frame,
columns=("url", "type"),
show="tree headings",
height=8)
self.tree.heading("#0", text="名稱")
self.tree.heading("url", text="URL")
self.tree.heading("type", text="類型")
self.tree.column("#0", width=100)
self.tree.column("url", width=250)
self.tree.column("type", width=80)
scrollbar = ttk.Scrollbar(list_frame,
orient=tk.VERTICAL,
command=self.tree.yview)
self.tree.configure(yscrollcommand=scrollbar.set)
self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 新增遠端區域
add_frame = ttk.LabelFrame(main_frame, text="新增遠端倉庫", padding=5)
add_frame.pack(fill=tk.X, pady=(0, 10))
ttk.Label(add_frame, text="名稱:").grid(row=0,
column=0,
sticky=tk.W,
pady=2)
self.name_var = tk.StringVar()
ttk.Entry(add_frame, textvariable=self.name_var,
width=50).grid(row=0, column=1, sticky=tk.EW, pady=2)
ttk.Label(add_frame, text="URL:").grid(row=1,
column=0,
sticky=tk.W,
pady=2)
self.url_var = tk.StringVar()
ttk.Entry(add_frame, textvariable=self.url_var,
width=50).grid(row=1, column=1, sticky=tk.EW, pady=2)
add_frame.columnconfigure(1, weight=1)
# 按鈕區域
button_frame = ttk.Frame(main_frame)
button_frame.pack(fill=tk.X)
ttk.Button(button_frame, text="新增",
command=self.add_remote).pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(button_frame, text="移除",
command=self.remove_remote).pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(button_frame, text="關閉",
command=self.dialog.destroy).pack(side=tk.RIGHT)
def load_remotes(self):
"""載入遠端倉庫列表"""
# 清除現有項目
for item in self.tree.get_children():
self.tree.delete(item)
if not self.main_app.repo:
return
for remote in self.main_app.repo.remotes:
remote_type = "GitHub" if "github.com" in remote.url else "GitLab"
self.tree.insert("",
tk.END,
text=remote.name,
values=(remote.url, remote_type))
def add_remote(self):
"""新增遠端倉庫"""
name = self.name_var.get().strip()
url = self.url_var.get().strip()
if not name or not url:
messagebox.showerror("錯誤", "請填寫完整的名稱和 URL")
return
try:
self.main_app.repo.create_remote(name, url)
self.main_app.log(f"已新增遠端倉庫: {name}")
self.load_remotes()
# 清除輸入框
self.name_var.set("")
self.url_var.set("")
messagebox.showinfo("成功", f"已新增遠端倉庫: {name}")
except Exception as e:
self.main_app.log(f"新增遠端倉庫失敗: {e}", "ERROR")
messagebox.showerror("錯誤", f"新增失敗: {e}")
def remove_remote(self):
"""移除遠端倉庫"""
selection = self.tree.selection()
if not selection:
messagebox.showwarning("警告", "請選擇要移除的遠端倉庫")
return
item = selection[0]
name = self.tree.item(item, "text")
if messagebox.askyesno("確認", f"確定要移除遠端倉庫 '{name}' 嗎?"):
try:
self.main_app.repo.delete_remote(name)
self.main_app.log(f"已移除遠端倉庫: {name}")
self.load_remotes()
messagebox.showinfo("成功", f"已移除遠端倉庫: {name}")
except Exception as e:
self.main_app.log(f"移除遠端倉庫失敗: {e}", "ERROR")
messagebox.showerror("錯誤", f"移除失敗: {e}")
class ScanResultDialog:
"""掃描結果對話框"""
def __init__(self, parent, results: List[ScanResult], main_app):
self.results = results
self.main_app = main_app
self.dialog = tk.Toplevel(parent)
self.dialog.title("安全掃描結果")
self.dialog.geometry("800x600")
self.dialog.transient(parent)
self.dialog.grab_set()
self.setup_ui()
self.load_results()
# 置中顯示
self.dialog.update_idletasks()
x = parent.winfo_x() + (parent.winfo_width() -
self.dialog.winfo_width()) // 2
y = parent.winfo_y() + (parent.winfo_height() -
self.dialog.winfo_height()) // 2
self.dialog.geometry(f"+{x}+{y}")
def setup_ui(self):
"""設定界面"""
main_frame = ttk.Frame(self.dialog)
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# 標題
title_label = ttk.Label(main_frame,
text=f"發現 {len(self.results)} 個潛在安全問題",
font=("微軟正黑體", 12, "bold"))
title_label.pack(pady=(0, 10))