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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import subprocess
|
| 3 |
+
import threading
|
| 4 |
+
import shutil
|
| 5 |
+
import zipfile
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
import json # 导入 json 模块
|
| 9 |
+
from requests.adapters import HTTPAdapter
|
| 10 |
+
from requests.packages.urllib3.util.retry import Retry
|
| 11 |
+
from IPython.display import HTML, display
|
| 12 |
+
import configparser
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
huggiingface_repo_id = 'Qi28/SD_QZ' # Hugging Face 数据集 ID
|
| 16 |
+
huggiingface_path = 'frp'
|
| 17 |
+
download_path = '/tmp/code/frp'
|
| 18 |
+
frp_status = 0 #是否启用frp, 0 否 1 是
|
| 19 |
+
nps_status = 1 #是否启用nps, 0 否 1 是
|
| 20 |
+
sd_url = 'http://119.8.118.149:8000'
|
| 21 |
+
NPS_key = 'q9wj1cnlek4j3hg5'
|
| 22 |
+
NPS_server = '119.8.118.149:7001'
|
| 23 |
+
nezha_agent_key = 'L3USpU8bbpHRV90uuC'
|
| 24 |
+
nezha_agent_url = 'nzzz.nwb.lol:5555'
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
#!export CUDA_VISIBLE_DEVICES=0,1
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def Frpc():
|
| 31 |
+
thread = threading.Thread(target=run_commands)
|
| 32 |
+
thread.start()
|
| 33 |
+
os.system('rm /tmp/code/frp/*.ini')
|
| 34 |
+
# 定义 Hugging Face 数据集 ID
|
| 35 |
+
api_url = f'https://hf-mirror.com/api/datasets/{huggiingface_repo_id}/tree/main/{huggiingface_path}'
|
| 36 |
+
|
| 37 |
+
# 获取文件列表
|
| 38 |
+
response = requests.get(api_url)
|
| 39 |
+
if response.status_code != 200:
|
| 40 |
+
print(f'Error: Failed to fetch file list from {api_url}')
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
|
| 43 |
+
file_list = response.json()
|
| 44 |
+
yun_files = [file['path'] for file in file_list if file['type'] == 'file']
|
| 45 |
+
|
| 46 |
+
# 下载文件
|
| 47 |
+
for yun_file in yun_files:
|
| 48 |
+
url = f'https://hf-mirror.com/datasets/{huggiingface_repo_id}/resolve/main/{yun_file}'
|
| 49 |
+
response = requests.head(url)
|
| 50 |
+
if response.status_code == 200:
|
| 51 |
+
result = subprocess.run(['wget', '-O', os.path.join(download_path, os.path.basename(yun_file)), url, '-q'], capture_output=True)
|
| 52 |
+
if result.returncode != 0:
|
| 53 |
+
print(f'Error: Failed to download {yun_file} from {url}')
|
| 54 |
+
else:
|
| 55 |
+
print(f'Error: Invalid URL {url}')
|
| 56 |
+
|
| 57 |
+
frpc_executable = '/tmp/code/frp/frpc'
|
| 58 |
+
subprocess.run(['chmod', '+x', frpc_executable], check=True)
|
| 59 |
+
|
| 60 |
+
# 启动 frpc 实例
|
| 61 |
+
for yun_file in yun_files:
|
| 62 |
+
frpini_path = os.path.join(download_path, os.path.basename(yun_file))
|
| 63 |
+
print(f'运行FRP配置: {frpini_path}')
|
| 64 |
+
subprocess.Popen([frpc_executable, '-c', frpini_path])
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def NPS():
|
| 68 |
+
os.system("/tmp/code/nps/npc -server={NPS_server} -vkey={NPS_key} -type=tcp")
|
| 69 |
+
|
| 70 |
+
def run_commands():
|
| 71 |
+
run_command("apt update")
|
| 72 |
+
run_command("apt install openssh-server -y")
|
| 73 |
+
run_command("apt-get install vim -y")
|
| 74 |
+
config_client_cmd = "sed -i '/PasswordAuthentication/s/^#//g' /etc/ssh/ssh_config"
|
| 75 |
+
run_command(config_client_cmd)
|
| 76 |
+
config_server_cmd = "sed -i '/PermitRootLogin prohibit-password/s/prohibit-password/yes/' /etc/ssh/sshd_config"
|
| 77 |
+
run_command(config_server_cmd)
|
| 78 |
+
run_command("systemctl restart ssh")
|
| 79 |
+
os.system("echo 'root:qilan' | chpasswd")
|
| 80 |
+
os.system("echo 'PermitRootLogin yes' | tee -a /etc/ssh/sshd_config")
|
| 81 |
+
os.system("service ssh restart")
|
| 82 |
+
os.system("export LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | tr ':' '\n' | grep -v '/opt/conda/lib' | paste -sd ':') ")
|
| 83 |
+
|
| 84 |
+
def run_command(command):
|
| 85 |
+
try:
|
| 86 |
+
subprocess.run(command, check=True, shell=True)
|
| 87 |
+
except subprocess.CalledProcessError as e:
|
| 88 |
+
print(f"An error occurred: {e}")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def run_script():
|
| 93 |
+
os.system('rm -r /tmp/code/nezha-agent')
|
| 94 |
+
# GitHub项目名和API URL
|
| 95 |
+
project_name = "nezhahq/agent"
|
| 96 |
+
releases_api_url = f"https://api.github.com/repos/{project_name}/releases"
|
| 97 |
+
# 获取最新版本的信息
|
| 98 |
+
response = requests.get(releases_api_url)
|
| 99 |
+
if response.status_code == 200:
|
| 100 |
+
releases = response.json()
|
| 101 |
+
latest_version = releases[0]['tag_name'] # 假设最新版本是列表中的第一个
|
| 102 |
+
print(f"Latest version: {latest_version}")
|
| 103 |
+
else:
|
| 104 |
+
print("Failed to fetch releases information.")
|
| 105 |
+
exit()
|
| 106 |
+
# 构造下载链接
|
| 107 |
+
download_url = f"https://slink.ltd/https://github.com/{project_name}/releases/download/{latest_version}/nezha-agent_linux_amd64.zip"
|
| 108 |
+
# 指定下载文件的保存路径
|
| 109 |
+
zip_file_path = "/tmp/code/nezha-agent.zip" # 临时ZIP文件路径
|
| 110 |
+
# 确保下载目录存在
|
| 111 |
+
os.makedirs(os.path.dirname(zip_file_path), exist_ok=True)
|
| 112 |
+
# 发送HTTP GET请求下载文件
|
| 113 |
+
response = requests.get(download_url, stream=True)
|
| 114 |
+
# 检查请求是否成功
|
| 115 |
+
if response.status_code == 200:
|
| 116 |
+
# 打开文件进行写入
|
| 117 |
+
with open(zip_file_path, 'wb') as file:
|
| 118 |
+
for chunk in response.iter_content(chunk_size=128):
|
| 119 |
+
file.write(chunk)
|
| 120 |
+
print(f"Download completed. File saved to {zip_file_path}")
|
| 121 |
+
# 解压ZIP文件
|
| 122 |
+
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
| 123 |
+
zip_ref.extractall("/tmp/code/")
|
| 124 |
+
print("Extraction completed.")
|
| 125 |
+
os.remove(zip_file_path)
|
| 126 |
+
print(f"Removed temporary zip file: {zip_file_path}")
|
| 127 |
+
else:
|
| 128 |
+
print(f"Failed to download the file. Status code: {response.status_code}")
|
| 129 |
+
print('启动哪吒面板')
|
| 130 |
+
try:
|
| 131 |
+
subprocess.run(["chmod", "+x", "/tmp/code/nezha-agent"], check=True)
|
| 132 |
+
subprocess.run(["/tmp/code/nezha-agent", "-s", "nzzz.nwb.lol:5555", "-p", nezha_agent_key, "-d"], check=True)
|
| 133 |
+
except subprocess.CalledProcessError as e:
|
| 134 |
+
print(f"启动哪吒面板失败,错误内容: {e}")
|
| 135 |
+
|
| 136 |
+
# 等待启动穿透
|
| 137 |
+
def requests_retry_session(retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None):
|
| 138 |
+
session = session or requests.Session()
|
| 139 |
+
retry = Retry(total=retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist)
|
| 140 |
+
adapter = HTTPAdapter(max_retries=retry)
|
| 141 |
+
session.mount('http://', adapter)
|
| 142 |
+
session.mount('https://', adapter)
|
| 143 |
+
return session
|
| 144 |
+
def check_api1():
|
| 145 |
+
while True:
|
| 146 |
+
try:
|
| 147 |
+
url = "http://127.0.0.1:7860/sdapi/v1/txt2img"
|
| 148 |
+
response = requests.get(url, timeout=30)
|
| 149 |
+
try:
|
| 150 |
+
response_json = response.json()
|
| 151 |
+
if response_json.get("detail") in ("Method Not Allowed", "Not Found"):
|
| 152 |
+
print("SD已经启动")
|
| 153 |
+
break # 接口正常,退出循环
|
| 154 |
+
else:
|
| 155 |
+
print("SD未启动" )
|
| 156 |
+
except json.JSONDecodeError:
|
| 157 |
+
# 如果响应内容不是有效的 JSON,打印错误信息
|
| 158 |
+
print("SD未启动2")
|
| 159 |
+
except requests.exceptions.RequestException as e:
|
| 160 |
+
print("SD未启动3")
|
| 161 |
+
time.sleep(5)
|
| 162 |
+
print('开始检测接口状态')
|
| 163 |
+
while True:
|
| 164 |
+
url = f"{sd_url}/sdapi/v1/txt2img"
|
| 165 |
+
try:
|
| 166 |
+
response = requests_retry_session().get(url, timeout=30)
|
| 167 |
+
response.raise_for_status()
|
| 168 |
+
response_json = response.json()
|
| 169 |
+
if "detail" in response_json and response_json["detail"] in ("Method Not Allowed", "Not Found"):
|
| 170 |
+
print("接口正常")
|
| 171 |
+
else:
|
| 172 |
+
print("接口返回信息:", response_json)
|
| 173 |
+
except requests.exceptions.HTTPError as e:
|
| 174 |
+
print("接口正常2")
|
| 175 |
+
except requests.exceptions.ConnectionError as e:
|
| 176 |
+
print(f"接口异常1: {e}")
|
| 177 |
+
if frp_status == 1:
|
| 178 |
+
print("启动FRP")
|
| 179 |
+
threading.Thread(target=Frpc, daemon=True).start()
|
| 180 |
+
if nps_status == 1:
|
| 181 |
+
print("启动NPS")
|
| 182 |
+
threading.Thread(target=NPS, daemon=True).start()
|
| 183 |
+
# threading.Thread(target=run_script).start()
|
| 184 |
+
#threading.Thread(target=run_commands).start()
|
| 185 |
+
break
|
| 186 |
+
except requests.exceptions.Timeout as e:
|
| 187 |
+
print(f"接口异常2: {e}")
|
| 188 |
+
if frp_status == 1:
|
| 189 |
+
print("启动FRP")
|
| 190 |
+
threading.Thread(target=Frpc, daemon=True).start()
|
| 191 |
+
if nps_status == 1:
|
| 192 |
+
print("启动NPS")
|
| 193 |
+
threading.Thread(target=NPS, daemon=True).start()
|
| 194 |
+
# threading.Thread(target=run_script).start()
|
| 195 |
+
#threading.Thread(target=run_commands).start()
|
| 196 |
+
break
|
| 197 |
+
|
| 198 |
+
except requests.exceptions.RequestException as e:
|
| 199 |
+
print(f"接口异常3: {e}")
|
| 200 |
+
if frp_status == 1:
|
| 201 |
+
print("启动FRP")
|
| 202 |
+
threading.Thread(target=Frpc, daemon=True).start()
|
| 203 |
+
if nps_status == 1:
|
| 204 |
+
print("启动NPS")
|
| 205 |
+
threading.Thread(target=NPS, daemon=True).start()
|
| 206 |
+
# threading.Thread(target=run_script).start()
|
| 207 |
+
#threading.Thread(target=run_commands).start()
|
| 208 |
+
break
|
| 209 |
+
time.sleep(5)
|
| 210 |
+
threading.Thread(target=check_api1, daemon=True).start()
|
| 211 |
+
|
| 212 |
+
def comfyui():
|
| 213 |
+
subprocess.run(["python", "/tmp/code/sd/ComfyUI/main.py"], check=True)
|
| 214 |
+
def server():
|
| 215 |
+
os.system('mr -r /tmp/code/main.py')
|
| 216 |
+
os.system("wget -O /tmp/code/main.py 'https://hf-mirror.com/datasets/Qi28/SD_QZ/resolve/main/main.py'")
|
| 217 |
+
subprocess.run(["python", "/tmp/code/main.py"], check=True)
|
| 218 |
+
threading.Thread(target=run_commands).start()
|
| 219 |
+
threading.Thread(target=server, daemon=True).start()
|
| 220 |
+
threading.Thread(target=comfyui, daemon=True).start()
|
| 221 |
+
os.system('rm /tmp/code/sd/stable-diffusion-webui/config.json /tmp/code/*.log')
|
| 222 |
+
os.system("wget -O /tmp/code/sd/stable-diffusion-webui/config.json 'https://hf-mirror.com/datasets/Qilan28/hh_20240622/resolve/main/sd/config.json'")
|
| 223 |
+
# python /tmp/code/sd/stable-diffusion-webui/launch.py --api --api-auth QiLan:1415361185 --no-hashing --enable-insecure-extension-access --no-gradio-queue --no-half-vae --port 7860 --xformers
|
| 224 |
+
os.system("python /tmp/code/sd/stable-diffusion-webui/launch.py")
|