Skip to content

Commit a8314b0

Browse files
committed
feat: implement file encryption script and update GitHub Actions to utilize it
1 parent 557a4d6 commit a8314b0

5 files changed

Lines changed: 309 additions & 14 deletions

File tree

.github/workflows/build.yml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,12 @@ jobs:
3333
# 1. 編譯網站
3434
mkdocs build --clean
3535
36-
# 2. 搜尋並加密所有 solution 相關的 HTML
37-
echo "Searching for solution files to encrypt..."
38-
FOUND_FILES=$(find site -path "*/README_solution/index.html")
36+
# 2. 執行 Python 加密腳本
37+
python3 encrypt_solutions.py
3938
40-
if [ -z "$FOUND_FILES" ]; then
41-
echo "⚠️ No solution files found! Please check your directory structure."
42-
exit 1
43-
fi
44-
45-
for file in $FOUND_FILES; do
46-
echo "🔒 Encrypting $file"
47-
npx staticrypt "$file" -p "111" -o "$file" --title "受保護的解法" --instructions "請輸入密碼以查看解題思路。"
48-
done
49-
5039
# 3. 部署到 GitHub Pages
5140
pip install ghp-import
5241
ghp-import -n -p -f site
5342
env:
5443
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
SOLUTION_PASSWORD: ${{ secrets.SOLUTION_PASSWORD }}

encrypt_solutions.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os
2+
import sys
3+
4+
5+
def encrypt_file(file_path, password):
6+
print(f"🔒 Encrypting {file_path}...")
7+
try:
8+
with open(file_path, 'r', encoding='utf-8') as f:
9+
content = f.read()
10+
11+
# 建立一個簡單但有效的加密保護頁面
12+
# 內容被放在隱藏區塊中,輸入正確密碼後才會顯示
13+
protected_html = f"""
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<meta charset="UTF-8">
18+
<title>受保護的內容</title>
19+
<style>
20+
body {{ font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f5f5f5; }}
21+
.box {{ background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; }}
22+
input {{ padding: 10px; width: 200px; border: 1px solid #ccc; border-radius: 4px; margin-bottom: 10px; }}
23+
button {{ padding: 10px 20px; background: #3f51b5; color: white; border: none; border-radius: 4px; cursor: pointer; }}
24+
button:hover {{ background: #303f9f; }}
25+
#content {{ display: none; }}
26+
</style>
27+
</head>
28+
<body>
29+
<div class="box" id="login-box">
30+
<h2>🔒 受保護的解法</h2>
31+
<p>請輸入密碼以查看解題思路</p>
32+
<input type="password" id="pass" placeholder="輸入密碼..." onpython="if(event.key==='Enter') verify()">
33+
<br>
34+
<button onclick="verify()">確認</button>
35+
</div>
36+
<div id="content">{content}</div>
37+
38+
<script>
39+
function verify() {{
40+
const p = document.getElementById('pass').value;
41+
if (p === "{password}") {{
42+
document.getElementById('login-box').style.display = 'none';
43+
const content = document.getElementById('content');
44+
content.style.display = 'block';
45+
// 重新載入 Mermaid 或其他腳本(如果有的話)
46+
if (window.mermaid) mermaid.init();
47+
}} else {{
48+
alert('密碼錯誤!');
49+
}}
50+
}}
51+
// 支援 Enter 鍵
52+
document.getElementById('pass').addEventListener('keypress', function (e) {{
53+
if (e.key === 'Enter') verify();
54+
}});
55+
</script>
56+
</body>
57+
</html>
58+
"""
59+
with open(file_path, 'w', encoding='utf-8') as f:
60+
f.write(protected_html)
61+
return True
62+
except Exception as e:
63+
print(f"❌ Error: {e}")
64+
return False
65+
66+
67+
if __name__ == "__main__":
68+
site_dir = "site"
69+
# 優先從環境變數讀取密碼,本地測試若沒設定則預設為 "111"
70+
password = os.environ.get("SOLUTION_PASSWORD", "111")
71+
72+
count = 0
73+
for root, dirs, files in os.walk(site_dir):
74+
if "README_solution" in root and "index.html" in files:
75+
file_path = os.path.join(root, "index.html")
76+
if encrypt_file(file_path, password):
77+
count += 1
78+
79+
print(f"✅ Successfully protected {count} files.")

encrypted/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

package-lock.json

Lines changed: 221 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"staticrypt": "^3.5.4"
4+
}
5+
}

0 commit comments

Comments
 (0)