Spaces:
Building
Building
admin
commited on
Commit
·
c71b819
1
Parent(s):
eef51fd
sync ms
Browse files- app.py +4 -4
- modules/cmd.py +88 -0
- modules/github.py +0 -96
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
from modules.data import data_converter
|
3 |
from modules.exif import clexif
|
4 |
from modules.gif import video2gif
|
5 |
-
from modules.
|
6 |
from modules.qr import qrcode
|
7 |
from modules.rct import rct_generator
|
8 |
from modules.smtp import smtp_tester
|
@@ -16,7 +16,7 @@ ZH2EN = {
|
|
16 |
"数据文件转换": "Data Converter",
|
17 |
"图片 EXIF 清理": "Image EXIF Cleaner",
|
18 |
"视频转 GIF 动图": "Video to GIF",
|
19 |
-
"
|
20 |
"二维码生成": "QR Code",
|
21 |
"随机对照试验生成": "RCT Generator",
|
22 |
"SMTP 测试": "SMTP Test",
|
@@ -42,8 +42,8 @@ if __name__ == "__main__":
|
|
42 |
with gr.Tab(_L("视频转 GIF 动图")):
|
43 |
video2gif()
|
44 |
|
45 |
-
with gr.Tab(_L("
|
46 |
-
|
47 |
|
48 |
with gr.Tab(_L("二维码生成")):
|
49 |
qrcode()
|
|
|
2 |
from modules.data import data_converter
|
3 |
from modules.exif import clexif
|
4 |
from modules.gif import video2gif
|
5 |
+
from modules.cmd import cmd_inject
|
6 |
from modules.qr import qrcode
|
7 |
from modules.rct import rct_generator
|
8 |
from modules.smtp import smtp_tester
|
|
|
16 |
"数据文件转换": "Data Converter",
|
17 |
"图片 EXIF 清理": "Image EXIF Cleaner",
|
18 |
"视频转 GIF 动图": "Video to GIF",
|
19 |
+
"命令注入测试": "CMD Injector",
|
20 |
"二维码生成": "QR Code",
|
21 |
"随机对照试验生成": "RCT Generator",
|
22 |
"SMTP 测试": "SMTP Test",
|
|
|
42 |
with gr.Tab(_L("视频转 GIF 动图")):
|
43 |
video2gif()
|
44 |
|
45 |
+
with gr.Tab(_L("命令注入测试")):
|
46 |
+
cmd_inject()
|
47 |
|
48 |
with gr.Tab(_L("二维码生成")):
|
49 |
qrcode()
|
modules/cmd.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import threading
|
4 |
+
import subprocess
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
8 |
+
ZH2EN = {
|
9 |
+
"输入命令": "Input command",
|
10 |
+
"状态栏": "Status",
|
11 |
+
"执行结果": "Command output",
|
12 |
+
"命令执行测试工具": "Command executor",
|
13 |
+
"输入一个命令, 点击查看其执行结果": "Enter a command, and click to see its result",
|
14 |
+
"执行超时时间": "Execution timeout",
|
15 |
+
}
|
16 |
+
|
17 |
+
|
18 |
+
def _L(zh_txt: str):
|
19 |
+
return ZH2EN[zh_txt] if EN_US else zh_txt
|
20 |
+
|
21 |
+
|
22 |
+
class Inject:
|
23 |
+
def __init__(self):
|
24 |
+
self.output = ""
|
25 |
+
self.status = "Success"
|
26 |
+
self.thread: threading.Thread = None
|
27 |
+
self.timeout = 10
|
28 |
+
self.delay = 0.1
|
29 |
+
|
30 |
+
def run(self, cmd: str):
|
31 |
+
try:
|
32 |
+
self.output = subprocess.check_output(
|
33 |
+
cmd,
|
34 |
+
shell=True,
|
35 |
+
stderr=subprocess.STDOUT,
|
36 |
+
text=True,
|
37 |
+
)
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
self.status = f" {e} "
|
41 |
+
self.output = ""
|
42 |
+
|
43 |
+
def infer(self, cmd: str):
|
44 |
+
self.status = "Success"
|
45 |
+
self.output = ""
|
46 |
+
try:
|
47 |
+
if self.thread and self.thread.is_alive():
|
48 |
+
self.thread.join(timeout=0)
|
49 |
+
|
50 |
+
self.thread = threading.Thread(
|
51 |
+
target=self.run,
|
52 |
+
args=(cmd,),
|
53 |
+
daemon=True,
|
54 |
+
)
|
55 |
+
self.thread.start()
|
56 |
+
delay = 0
|
57 |
+
while self.thread and self.thread.is_alive():
|
58 |
+
delay += self.delay
|
59 |
+
time.sleep(self.delay)
|
60 |
+
if delay > self.timeout:
|
61 |
+
self.thread.join(timeout=0)
|
62 |
+
self.status = "Killed"
|
63 |
+
self.output = _L("执行超时时间") + f": {self.timeout}s"
|
64 |
+
break
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
self.status = f" {e} "
|
68 |
+
self.output = ""
|
69 |
+
|
70 |
+
return self.status, self.output
|
71 |
+
|
72 |
+
|
73 |
+
def cmd_inject():
|
74 |
+
inject = Inject()
|
75 |
+
return gr.Interface(
|
76 |
+
fn=inject.infer,
|
77 |
+
inputs=gr.Textbox(
|
78 |
+
label=_L("输入命令"),
|
79 |
+
value="dir" if os.name == "nt" else "ls /",
|
80 |
+
),
|
81 |
+
outputs=[
|
82 |
+
gr.Textbox(label=_L("状态栏"), show_copy_button=True),
|
83 |
+
gr.TextArea(label=_L("执行结果"), show_copy_button=True),
|
84 |
+
],
|
85 |
+
title=_L("命令执行测试工具"),
|
86 |
+
description=_L("输入一个命令, 点击查看其执行结果"),
|
87 |
+
flagging_mode="never",
|
88 |
+
)
|
modules/github.py
DELETED
@@ -1,96 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
import gradio as gr
|
4 |
-
from utils import EN_US
|
5 |
-
|
6 |
-
ZH2EN = {
|
7 |
-
"仓库拥有者": "Repo owner",
|
8 |
-
"输入用户名或组织名": "Username / organization name",
|
9 |
-
"仓库名": "GitHub repo",
|
10 |
-
"输入仓库名": "Repo name",
|
11 |
-
"输入 personal access token": "Personal access token",
|
12 |
-
"发布标签": "Release tag",
|
13 |
-
"发布标题": "Release title",
|
14 |
-
"发布描述": "Describe this release",
|
15 |
-
"上传发布文件(可多选)": "Binary file(s)",
|
16 |
-
"状态栏": "Status",
|
17 |
-
"上传文件创建一个新的 GitHub 发布": "Upload binary file(s) to create a new GitHub release.",
|
18 |
-
}
|
19 |
-
|
20 |
-
|
21 |
-
def _L(zh_txt: str):
|
22 |
-
return ZH2EN[zh_txt] if EN_US else zh_txt
|
23 |
-
|
24 |
-
|
25 |
-
def create_github_release(owner, repo, token, tag, name, description, files):
|
26 |
-
try:
|
27 |
-
# 创建 Release
|
28 |
-
response = requests.post(
|
29 |
-
f"https://api.github.com/repos/{owner}/{repo}/releases",
|
30 |
-
headers={
|
31 |
-
"Authorization": f"token {token}",
|
32 |
-
"Accept": "application/vnd.github.v3+json",
|
33 |
-
},
|
34 |
-
json={
|
35 |
-
"tag_name": tag,
|
36 |
-
"name": name,
|
37 |
-
"body": description,
|
38 |
-
"draft": False,
|
39 |
-
"prerelease": False,
|
40 |
-
},
|
41 |
-
)
|
42 |
-
|
43 |
-
if response.status_code != 201:
|
44 |
-
raise ConnectionError(f"{response.status_code}: {response.json()}")
|
45 |
-
|
46 |
-
# 获取上传 URL
|
47 |
-
release = response.json()
|
48 |
-
upload_url = release["upload_url"].split("{")[0]
|
49 |
-
|
50 |
-
# 上传多个二进制文件
|
51 |
-
results = ""
|
52 |
-
for file_path in files:
|
53 |
-
file_name = os.path.basename(file_path)
|
54 |
-
with open(file_path, "rb") as binary_file:
|
55 |
-
upl_response = requests.post(
|
56 |
-
f"{upload_url}?name={file_name}",
|
57 |
-
headers={
|
58 |
-
"Authorization": f"token {token}",
|
59 |
-
"Content-Type": "application/octet-stream",
|
60 |
-
},
|
61 |
-
data=binary_file,
|
62 |
-
)
|
63 |
-
|
64 |
-
if upl_response.status_code == 201:
|
65 |
-
results += f"Upload '{file_name}' success!"
|
66 |
-
else:
|
67 |
-
results += f"Failed to upload {file_name}: {upl_response.status_code}, {upl_response.json()}"
|
68 |
-
|
69 |
-
return results
|
70 |
-
|
71 |
-
except Exception as e:
|
72 |
-
return f"{e}"
|
73 |
-
|
74 |
-
|
75 |
-
def github_release_creator():
|
76 |
-
return gr.Interface(
|
77 |
-
fn=create_github_release,
|
78 |
-
inputs=[
|
79 |
-
gr.Textbox(label=_L("仓库拥有者"), placeholder=_L("输入用户名或组织名")),
|
80 |
-
gr.Textbox(label=_L("仓库名"), placeholder=_L("输入仓库名")),
|
81 |
-
gr.Textbox(
|
82 |
-
label="GitHub Token",
|
83 |
-
placeholder=_L("输入 personal access token"),
|
84 |
-
type="password",
|
85 |
-
),
|
86 |
-
gr.Textbox(label=_L("发布标签"), placeholder="Release tag"),
|
87 |
-
gr.Textbox(label=_L("发布标题"), placeholder="Release title"),
|
88 |
-
gr.TextArea(
|
89 |
-
label=_L("发布描述"),
|
90 |
-
placeholder=_L("上传文件创建一个新的 GitHub 发布"),
|
91 |
-
),
|
92 |
-
gr.File(label=_L("上传发布文件(可多选)"), file_count="multiple"),
|
93 |
-
],
|
94 |
-
outputs=gr.TextArea(label=_L("状态栏"), show_copy_button=True),
|
95 |
-
flagging_mode="never",
|
96 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|