fromnowoncoding commited on
Commit
89b9168
·
verified ·
1 Parent(s): 5844ed7

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +59 -7
  2. app.py +154 -0
  3. get_group_id.sh +20 -0
  4. gitattributes +35 -0
  5. requirements.txt +6 -0
README.md CHANGED
@@ -1,13 +1,65 @@
1
  ---
2
- title: 2fa Mutiple
3
- emoji: ⚡
4
- colorFrom: blue
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 5.12.0
8
  app_file: app.py
9
  pinned: false
10
- short_description: 2FA 的多種取得方法
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Gmail 2FA to LINE
3
+ emoji: 🔐
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 4.44.1
8
  app_file: app.py
9
  pinned: false
10
+ short_description: Gmail 2FA to LINE
11
  ---
12
 
13
+ 這是一個基於 Gradio 的兩步驗證應用,使用者可以透過此應用生成 Gmail 2FA 驗證碼,並將其發送到 LINE。此應用保證了驗證碼的隱私,並且操作簡單。
14
+
15
+ 工作夥伴需要共同管理登入 gmail 帳號,登入時會需要使用 Google Authenticator 應用程式取得兩階段驗證碼。這個小工具可以讓團隊成員在需要時自行取得驗證碼,提高工作效率。
16
+
17
+ # 功能特點
18
+ - 自動生成 Gmail 兩步驗證碼
19
+ - 自動發送到 LINE Bot
20
+ - 使用環境變數保護敏感資訊
21
+ - 簡單的網頁介面
22
+ - 驗證碼只在 LINE 中顯示,確保安全性
23
+
24
+ ℹ️ 感謝 Ronny 的支援,原始 php 程式碼[https://github.com/ronnywang/jothon-donate-api/blob/master/otp.php](https://github.com/ronnywang/jothon-donate-api/blob/master/otp.php)
25
+
26
+ # 設置步驟
27
+ 1. 建立 LINE Bot
28
+ - 到 [LINE Developers Console](https://developers.line.biz/console/) 建立一個新的 Channel
29
+ - 取得 Channel Access Token
30
+ - 將 Bot 加入為好友
31
+
32
+ 2. 準備 Gmail 2FA 密鑰
33
+ - 在設置 Gmail 兩步驗證時,選擇「無法掃描 QR Code」選項
34
+ - 將顯示的密鑰記下來(記得移除空格並轉為大寫)
35
+
36
+ 3. 在 Hugging Face 設置
37
+ - 建立新的 Space,選擇 Gradio SDK
38
+ - 上傳專案檔案
39
+ - 在 Settings > Repository Secrets 中設置以下密鑰:
40
+ - `LINE_CHANNEL_ACCESS_TOKEN`:LINE Bot 的 Channel Access Token
41
+ - `gmail_2fa`:Gmail 的兩步驗證密鑰(去除空格,全大寫)
42
+
43
+ # 注意事項
44
+ 1. Gmail 2FA 密鑰必須是無空格的大寫格式
45
+ 2. 主程式檔案必須命名為 `app.py`
46
+ 3. 記得選擇 Gradio SDK 以獲得網頁介面
47
+ 4. 專案必須設為公開才能分享網址給同事
48
+ 5. 請確保已將 LINE Bot 加為好友,否則無法收到訊息
49
+
50
+ # 技術細節
51
+ - 使用 Gradio 建立網頁介面
52
+ - 使用 LINE Messaging API 發送訊息
53
+ - 使用標準的 TOTP 演算法生成驗證碼
54
+ - 透過 LINE Broadcast 訊息發送驗證碼
55
+
56
+ # 環境變數
57
+ - `LINE_CHANNEL_ACCESS_TOKEN`:LINE Bot 的 Channel Access Token
58
+ - `gmail_2fa`:Gmail 的兩步驗證密鑰
59
+
60
+ # 使用方式
61
+ 1. 開啟網頁介面
62
+ 2. 輸入使用者名稱
63
+ 3. 選擇服務類型(例如:Gmail)
64
+ 4. 點擊送出
65
+ 5. 驗證碼會直接發送到你的 LINE,請查看 LINE Bot 的訊息
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import hmac
3
+ import hashlib
4
+ import os
5
+ import time
6
+ import requests
7
+ import json
8
+ import binascii
9
+ import gradio as gr
10
+ from linebot import LineBotApi
11
+ from linebot.models import TextSendMessage
12
+ from linebot.exceptions import LineBotApiError
13
+ from dotenv import load_dotenv
14
+
15
+ # Base32 解碼 (類似於 PHP 中 Base32::decode)
16
+ def base32_decode(encoded_secret):
17
+ missing_padding = len(encoded_secret) % 8
18
+ if missing_padding != 0:
19
+ encoded_secret += '=' * (8 - missing_padding)
20
+ try:
21
+ return base64.b32decode(encoded_secret)
22
+ except binascii.Error as e:
23
+ raise ValueError(f"Base32 解碼失敗: {e}")
24
+
25
+ # HOTP 驗證碼生成
26
+ def generate_hotp(secret, counter):
27
+ decoded_secret = base32_decode(secret)
28
+ counter_bytes = counter.to_bytes(8, byteorder="big")
29
+ hmac_hash = hmac.new(decoded_secret, counter_bytes, hashlib.sha1).digest()
30
+ return dynamic_truncation(hmac_hash) % (10 ** 6)
31
+
32
+ # 動態截斷算法
33
+ def dynamic_truncation(hmac_value):
34
+ offset = hmac_value[-1] & 0x0F
35
+ code = (
36
+ ((hmac_value[offset] & 0x7f) << 24)
37
+ | ((hmac_value[offset + 1] & 0xff) << 16)
38
+ | ((hmac_value[offset + 2] & 0xff) << 8)
39
+ | (hmac_value[offset + 3] & 0xff)
40
+ )
41
+ return code
42
+
43
+ # 驗證碼生成 (基於時間的計數器,每 30 秒更新一次)
44
+ def generate_otp(secret):
45
+ counter = int(time.time() // 30)
46
+ otp_code = generate_hotp(secret, counter)
47
+ return f"{otp_code:06d}"
48
+
49
+ # 發送訊息到 LINE
50
+ def send_message_to_line(message):
51
+ line_channel_access_token = os.getenv("LINE_CHANNEL_ACCESS_TOKEN")
52
+ if not line_channel_access_token:
53
+ raise ValueError("LINE_CHANNEL_ACCESS_TOKEN is not set in environment variables.")
54
+
55
+ # 使用 LINE API 發送訊息
56
+ url = 'https://api.line.me/v2/bot/message/broadcast'
57
+ headers = {
58
+ 'Content-Type': 'application/json',
59
+ 'Authorization': f'Bearer {line_channel_access_token}'
60
+ }
61
+
62
+ # 先嘗試廣播
63
+ broadcast_payload = {
64
+ 'messages': [{
65
+ 'type': 'text',
66
+ 'text': message
67
+ }]
68
+ }
69
+
70
+ try:
71
+ response = requests.post(url, headers=headers, json=broadcast_payload)
72
+ print(f"Broadcast response: {response.status_code}")
73
+ except Exception as e:
74
+ print(f"Broadcast failed: {str(e)}")
75
+
76
+ # 再使用 push message 發送到指定群組
77
+ push_url = 'https://api.line.me/v2/bot/message/push'
78
+ group_ids = os.getenv("LINE_GROUP_ID", "").split(',')
79
+
80
+ for group_id in group_ids:
81
+ group_id = group_id.strip()
82
+ if not group_id:
83
+ continue
84
+
85
+ push_payload = {
86
+ 'to': group_id,
87
+ 'messages': [{
88
+ 'type': 'text',
89
+ 'text': message
90
+ }]
91
+ }
92
+
93
+ try:
94
+ response = requests.post(push_url, headers=headers, json=push_payload)
95
+ if response.status_code == 200:
96
+ print(f"Successfully sent message to group {group_id}")
97
+ else:
98
+ print(f"Failed to send message to group {group_id}: {response.text}")
99
+ except Exception as e:
100
+ print(f"Error sending to group {group_id}: {str(e)}")
101
+
102
+ # Gradio 前端邏輯
103
+ def two_fa_interface(name, service_type, display_method, passphrase=""):
104
+ if not name or not service_type:
105
+ return "請填寫名稱和選擇服務類型"
106
+
107
+ try:
108
+ # 生成 2FA 驗證碼
109
+ secret = os.getenv('gmail_2fa')
110
+ otp_code = generate_otp(secret)
111
+ message = f"{name} 的驗證碼是:{otp_code}"
112
+
113
+ if display_method == "發送到LINE":
114
+ send_message_to_line(message)
115
+ return "驗證碼已發送到 LINE"
116
+ else: # 網頁顯示
117
+ stored_passphrase = os.getenv("PASSPHRASE", "") # 從環境變數獲取密碼
118
+ if passphrase == stored_passphrase:
119
+ return message
120
+ else:
121
+ return "通關密語錯誤,請重試"
122
+ except Exception as e:
123
+ return f"發生錯誤:{str(e)}"
124
+
125
+ # 初始化邏輯
126
+ def initialize():
127
+ load_dotenv()
128
+
129
+ # Gradio 介面
130
+ service_options = ["ai@washinmura.jp"]
131
+ display_methods = ["發送到LINE", "網頁顯示"]
132
+
133
+ with gr.Blocks() as demo:
134
+ gr.Markdown("# 和心村 2FA 驗證碼生成器")
135
+
136
+ with gr.Row():
137
+ name = gr.Textbox(label="名稱")
138
+ service_type = gr.Dropdown(choices=service_options, label="服務類型")
139
+
140
+ with gr.Row():
141
+ display_method = gr.Radio(choices=display_methods, label="顯示方式", value="發送到LINE")
142
+ passphrase = gr.Textbox(label="通關密語(僅網頁顯示需要)", type="password")
143
+
144
+ submit_btn = gr.Button("生成驗證碼")
145
+ output = gr.Textbox(label="結果")
146
+
147
+ submit_btn.click(
148
+ fn=two_fa_interface,
149
+ inputs=[name, service_type, display_method, passphrase],
150
+ outputs=output
151
+ )
152
+
153
+ # 啟動應用
154
+ demo.launch(share=True)
get_group_id.sh ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ if [ -z "$1" ]; then
4
+ echo "請提供 LINE Channel Access Token"
5
+ echo "使用方式: ./get_group_id.sh YOUR_CHANNEL_ACCESS_TOKEN"
6
+ exit 1
7
+ fi
8
+
9
+ TOKEN=$1
10
+
11
+ # 使用 curl 呼叫 LINE API
12
+ response=$(curl -s -X GET https://api.line.me/v2/bot/info \
13
+ -H "Authorization: Bearer $TOKEN")
14
+
15
+ echo "Bot 資訊:"
16
+ echo "$response" | python3 -m json.tool
17
+
18
+ echo -e "\n要取得特定群組的資訊,請使用以下指令:"
19
+ echo "curl -X GET https://api.line.me/v2/bot/group/{GROUP_ID}/summary \\"
20
+ echo " -H 'Authorization: Bearer $TOKEN'"
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ pyotp
2
+ gradio>=3.50.0
3
+ requests>=2.31.0
4
+ python-dotenv>=1.0.0
5
+ line-bot-sdk>=2.4.2
6
+ flask>=2.0.1