Qilan2 commited on
Commit
4572924
·
verified ·
1 Parent(s): 1ed1109

Update ff_sap.py

Browse files
Files changed (1) hide show
  1. ff_sap.py +61 -44
ff_sap.py CHANGED
@@ -48,11 +48,18 @@ else: # 如果 DATA_JSON 为空,就从单独的环境变量里取
48
 
49
 
50
  def cf():
51
- os.system("rm -r /home/vncuser/ff1")
52
- os.system("wget -O '/home/vncuser/ff1' -q 'https://huggingface.co//datasets/Qilan2/ff/resolve/main/f-linux-amd64?download=true'")
53
- os.system("chmod +x /home/vncuser/ff1")
54
  # https://ff1.vv9.dpdns.org
55
- os.system('nohup /home/vncuser/ff1 tunnel run --token eyJhIjoiZWM1MTk5ZTYwZGYxYWI2YmM2OTdhMGYzMTAzYzY4NTUiLCJ0IjoiNDBmNjFiZGQtMzQyYS00MDYzLWJlZTQtZGU4ZjEyNDk2NTAwIiwicyI6Ik9HVXdNR0UyTWpVdFlUVmpOUzAwTlRCbExUZzRabVl0TldRNU9USTFNR1F3TjJabSJ9 >/dev/null 2>&1 &')
 
 
 
 
 
 
 
56
 
57
  UUID = os.environ.get('UUID', 'a488076d-0ced-4a83-91c4-c498fce00cff') # UUID,如使用v1,在不同的平台部署需要修改,否则会覆盖
58
  N_SERVER = os.environ.get('NEZHA_SERVER', '')# N面板域名或ip,
@@ -79,17 +86,17 @@ def nv1():
79
  'use_ipv6_country_code': False,
80
  'uuid': UUID
81
  }
82
- with open('/home/vncuser/config.yaml', 'w') as file:
83
  yaml.dump(agent_config, file, default_flow_style=False)
84
 
85
- os.system("cat /home/vncuser/config.yaml")
86
- os.system("rm -rf /home/vncuser/f1")
87
- os.system("wget -O '/home/vncuser/f1' -q 'https://huggingface.co//datasets/Qilan2/st-server/resolve/main/nv1amd64?download=true'")
88
- os.system("chmod +x /home/vncuser/f1")
89
- os.system('/home/vncuser/f1 -c /home/vncuser/config.yaml')
90
- os.system('/home/vncuser/f1 -c /home/vncuser/config.yaml')
91
- # threading.Thread(target=nv1, daemon=True).start()
92
- # threading.Thread(target=cf, daemon=True).start()
93
 
94
  def random_name(length=8):
95
  return ''.join(random.choices(string.ascii_lowercase, k=length))
@@ -109,7 +116,7 @@ def telegram_message(bot_token: str, chat_id: str, message: str):
109
  :param message: 要发送的正文内容
110
  :return: Telegram API 返回的结果
111
  """
112
- url = f"https://tgbotapi.9.c.5.b.0.d.0.0.1.0.a.2.ip6.arpa/bot{bot_token}/sendMessage"
113
  params = {
114
  "chat_id": chat_id,
115
  "text": message,
@@ -125,7 +132,7 @@ def telegram_message(bot_token: str, chat_id: str, message: str):
125
  def create_space(type,key = "", value = "",name = ""):
126
  if type == 1:
127
  # 创建spaces
128
- url = "https://huggingface.co//api/repos/create"
129
  headers = {
130
  "Content-Type": "application/json",
131
  "Authorization": f"Bearer {HF_TOKON2}"
@@ -144,7 +151,7 @@ def create_space(type,key = "", value = "",name = ""):
144
  return response.json()
145
  if type == 2:
146
  # 删除spaces
147
- url = "https://huggingface.co//api/repos/delete"
148
  headers = {
149
  "Content-Type": "application/json",
150
  "Authorization": f"Bearer {HF_TOKON2}"
@@ -158,7 +165,7 @@ def create_space(type,key = "", value = "",name = ""):
158
  return response.text
159
  if type == 3:
160
  # 创建spaces 变量
161
- url = f"https://huggingface.co//api/spaces/{HF_USER2}/{name}/secrets"
162
  headers = {
163
  "Content-Type": "application/json",
164
  "Authorization": f"Bearer {HF_TOKON2}"
@@ -171,15 +178,15 @@ def create_space(type,key = "", value = "",name = ""):
171
  response = requests.post(url, headers=headers, json=payload)
172
  return response.text
173
  if type == 4:
174
- file_path = f"/home/vncuser/{name}/Dockerfile"
175
  os.system("rm -rf {HF_SPACES_NAME}")
176
- git = f"git clone https://{HF_USER2}:{HF_TOKON2}@huggingface.co/spaces/{HF_USER2}/{name} /home/vncuser/{name}"
177
  url = "https://huggingface.co/datasets/Qilan2/ff/raw/main/Dockerfile"
178
  print(git)
179
  os.system(git)
180
  os.system(f'git config --global user.email "{HF_EMAIL2}"')
181
  os.system(f'git config --global user.name "{HF_USER2}"')
182
- os.chdir(f'/home/vncuser/{name}')
183
  os.system(f"rm -rf {file_path}")
184
  time.sleep(3)
185
  # 如果文件已存在,先删除
@@ -198,8 +205,8 @@ def create_space(type,key = "", value = "",name = ""):
198
  print(f"下载失败,状态码: {response.status_code}")
199
  # telegram_message(BOT_TOKEN, CHAT_ID, f"{HF_ID}下载Dockerfile下载失败,状态码: {response.status_code}")
200
 
201
- # os.system(f"wget -O '/data/{tmp_name}/Dockerfile' https://huggingface.co//datasets/Qilan2/ff/raw/main/Dockerfile")
202
- repo_path = f"/home/vncuser/{name}"
203
  subprocess.run(["git", "-C", repo_path, "add", "."], check=True)
204
  subprocess.run(["git", "-C", repo_path, "commit", "-m", "0"], check=True)
205
  subprocess.run(["git", "-C", repo_path, "push", "origin", "main"], check=True)
@@ -217,7 +224,7 @@ def create_space(type,key = "", value = "",name = ""):
217
  }
218
 
219
  # 获取用户的所有空间列表
220
- response = requests.get(f"https://huggingface.co//api/spaces?author={HF_USER2}", headers=headers)
221
 
222
  if response.status_code != 200:
223
  print(f"获取空间列表失败:{response.status_code}")
@@ -237,7 +244,7 @@ def create_space(type,key = "", value = "",name = ""):
237
  full_name = space['id']
238
  space_id = full_name.split("/")[-1]
239
  # 删除spaces
240
- url = "https://huggingface.co//api/repos/delete"
241
  headers = {
242
  "Content-Type": "application/json",
243
  "Authorization": f"Bearer {HF_TOKON2}"
@@ -263,7 +270,7 @@ def get_latest_local_package(directory, pattern='*.tar.gz'):
263
  files = glob.glob(search_pattern)
264
 
265
  if not files:
266
- print("未找到匹配的 ff备份 压缩包")
267
  return None
268
 
269
  # 获取最新的文件
@@ -283,7 +290,7 @@ def delete_huggingface_lfs_file(filename, repo_id, token):
283
  """
284
  try:
285
  # 1. 查询LFS文件列表
286
- url = f"https://huggingface.co//api/models/{repo_id}/lfs-files"
287
  headers = {
288
  "content-type": "application/json",
289
  "Authorization": f"Bearer {token}",
@@ -310,7 +317,7 @@ def delete_huggingface_lfs_file(filename, repo_id, token):
310
 
311
  # 3. 删除LFS文件记录
312
  file_oid = file_to_delete['fileOid']
313
- delete_url = f"https://huggingface.co//api/models/{repo_id}/lfs-files/{file_oid}?rewriteHistory=true"
314
 
315
  delete_response = requests.delete(delete_url, headers=headers)
316
  if delete_response.status_code == 200:
@@ -373,7 +380,7 @@ def get_remote_lfs_files(repo_id, token):
373
  获取远程所有的LFS文件列表
374
  """
375
  try:
376
- url = f"https://huggingface.co//api/models/{repo_id}/lfs-files"
377
  headers = {
378
  "content-type": "application/json",
379
  "Authorization": f"Bearer {token}",
@@ -522,29 +529,29 @@ def compress_folder(folder_path, output_dir, keep_count=3):
522
 
523
  def github(type):
524
  if type == 1:
525
- os.system(f'rm -rf /home/vncuser/{HF_REPO} /home/vncuser/ff /home/vncuser/data')
526
- if not os.path.exists(f'/home/vncuser/{HF_REPO}'):
527
  git = f"git clone https://{HF_USER1}:{HF_TOKEN1}@huggingface.co/{HF_USER1}/{HF_REPO}"
528
  print(git)
529
  os.system(git)
530
  os.system(f'git config --global user.email "{HF_EMAIL}"')
531
  os.system(f'git config --global user.name "{HF_USER1}"')
532
  os.system("ls")
533
- latest_package = get_latest_local_package(f'/home/vncuser/{HF_REPO}')
534
  print(f"最新压缩包路径: {latest_package}")
535
  if latest_package:
536
- os.system(f"tar -xzf {latest_package} -C /home/vncuser")
537
- os.system("mv /home/vncuser/f/ff /home/vncuser/")
538
- os.system("mv /home/vncuser/data/f/ff /home/vncuser/")
539
- os.system("rm -rf /home/vncuser/data /home/vncuser/f")
540
  threading.Thread(target=repeat_task, daemon=True).start()
541
 
542
  if type == 2:
543
  print(f"开始备份上传HF仓库:{HF_REPO}")
544
- os.system("mkdir -p /home/vncuser/f")
545
- os.system("cp -rf /home/vncuser/ff /home/vncuser/f")
546
 
547
- repo_path = f'/home/vncuser/{HF_REPO}'
548
  repo_id = f"{HF_USER1}/{HF_REPO}"
549
 
550
  # 切换到仓库目录
@@ -560,7 +567,7 @@ def github(type):
560
  os.system('git gc --auto')
561
 
562
  # 备份上传仓库
563
- new_archive_info = compress_folder('/home/vncuser/f', repo_path, keep_count=3)
564
  if new_archive_info:
565
  new_archive, file_size_info = new_archive_info.split(' MB:')
566
 
@@ -592,7 +599,7 @@ def _reconstruct_token(partial_token):
592
 
593
  def restart_huggingface_space(space_name, space_id, partial_token):
594
  token = _reconstruct_token(partial_token)
595
- url = f"https://huggingface.co//api/spaces/{space_name}/{space_id}/restart?factory=true"
596
  headers = {
597
  "Content-Type": "application/json",
598
  "Authorization": f"Bearer {token}",
@@ -613,6 +620,16 @@ def restart_huggingface_space(space_name, space_id, partial_token):
613
  }
614
 
615
 
 
 
 
 
 
 
 
 
 
 
616
 
617
  def repeat_task():
618
  print('备份线程启动aa')
@@ -622,12 +639,12 @@ def repeat_task():
622
  github(2)
623
  # telegram_message(BOT_TOKEN, CHAT_ID, "开始")
624
  github(1)
625
-
626
- if os.path.exists('/home/vncuser/ff/.mozilla/firefox/profiles.ini') and os.path.isfile('/home/vncuser/ff/.mozilla/firefox/profiles.ini'):
627
  # threading.Thread(target=repeat_task, daemon=True).start()
628
  # telegram_message(BOT_TOKEN, CHAT_ID, f"{HF_ID}开始启动")
629
  while True:
630
  print("检测到Firefox配置,启动定期重启循环...")
631
- time.sleep(999999)#21600 = 6小时 14400 = 4小时
632
  # github(2)
633
- # nv1_agent()
 
48
 
49
 
50
  def cf():
51
+ os.system("rm -r /data/f1")
52
+ os.system("wget -O '/data/f1' -q 'https://huggingface.co/datasets/Qilan2/ff/resolve/main/f-linux-amd64?download=true'")
53
+ os.system("chmod +x /data/f1")
54
  # https://ff1.vv9.dpdns.org
55
+ os.system('/data/f1 tunnel run --token eyJhIjoiZWM1MTk5ZTYwZGYxYWI2YmM2OTdhMGYzMTAzYzY4NTUiLCJ0IjoiNDBmNjFiZGQtMzQyYS00MDYzLWJlZTQtZGU4ZjEyNDk2NTAwIiwicyI6Ik9HVXdNR0UyTWpVdFlUVmpOUzAwTlRCbExUZzRabVl0TldRNU9USTFNR1F3TjJabSJ9')
56
+ # subprocess.Popen(
57
+ # '/data/cf tunnel run --token eyJhIjoiZWM1MTk5ZTYwZGYxYWI2YmM2OTdhMGYzMTAzYzY4NTUiLCJ0IjoiNDBmNjFiZGQtMzQyYS00MDYzLWJlZTQtZGU4ZjEyNDk2NTAwIiwicyI6Ik9HVXdNR0UyTWpVdFlUVmpOUzAwTlRCbExUZzRabVl0TldRNU9USTFNR1F3TjJabSJ9',
58
+ # stdout=subprocess.DEVNULL,
59
+ # stderr=subprocess.DEVNULL,
60
+ # shell=True,
61
+ # start_new_session=True # 在新的会话中启动,与父进程分离
62
+ # )
63
 
64
  UUID = os.environ.get('UUID', 'a488076d-0ced-4a83-91c4-c498fce00cff') # UUID,如使用v1,在不同的平台部署需要修改,否则会覆盖
65
  N_SERVER = os.environ.get('NEZHA_SERVER', '')# N面板域名或ip,
 
86
  'use_ipv6_country_code': False,
87
  'uuid': UUID
88
  }
89
+ with open('./config.yaml', 'w') as file:
90
  yaml.dump(agent_config, file, default_flow_style=False)
91
 
92
+ os.system("cat ./config.yaml")
93
+ os.system("rm -rf n1")
94
+ os.system("wget -O 'n1' -q 'https://huggingface.co/datasets/Qilan2/st-server/resolve/main/nv1amd64?download=true'")
95
+ os.system("chmod +x n1")
96
+ os.system('./n1 -c ./config.yaml')
97
+ os.system('./n1 -c ./config.yaml')
98
+ threading.Thread(target=nv1, daemon=True).start()
99
+
100
 
101
  def random_name(length=8):
102
  return ''.join(random.choices(string.ascii_lowercase, k=length))
 
116
  :param message: 要发送的正文内容
117
  :return: Telegram API 返回的结果
118
  """
119
+ url = f"https://tgbotapi.111680.xyz/bot{bot_token}/sendMessage"
120
  params = {
121
  "chat_id": chat_id,
122
  "text": message,
 
132
  def create_space(type,key = "", value = "",name = ""):
133
  if type == 1:
134
  # 创建spaces
135
+ url = "https://huggingface.co/api/repos/create"
136
  headers = {
137
  "Content-Type": "application/json",
138
  "Authorization": f"Bearer {HF_TOKON2}"
 
151
  return response.json()
152
  if type == 2:
153
  # 删除spaces
154
+ url = "https://huggingface.co/api/repos/delete"
155
  headers = {
156
  "Content-Type": "application/json",
157
  "Authorization": f"Bearer {HF_TOKON2}"
 
165
  return response.text
166
  if type == 3:
167
  # 创建spaces 变量
168
+ url = f"https://huggingface.co/api/spaces/{HF_USER2}/{name}/secrets"
169
  headers = {
170
  "Content-Type": "application/json",
171
  "Authorization": f"Bearer {HF_TOKON2}"
 
178
  response = requests.post(url, headers=headers, json=payload)
179
  return response.text
180
  if type == 4:
181
+ file_path = f"/data/{name}/Dockerfile"
182
  os.system("rm -rf {HF_SPACES_NAME}")
183
+ git = f"git clone https://{HF_USER2}:{HF_TOKON2}@huggingface.co/spaces/{HF_USER2}/{name} /data/{name}"
184
  url = "https://huggingface.co/datasets/Qilan2/ff/raw/main/Dockerfile"
185
  print(git)
186
  os.system(git)
187
  os.system(f'git config --global user.email "{HF_EMAIL2}"')
188
  os.system(f'git config --global user.name "{HF_USER2}"')
189
+ os.chdir(f'/data/{name}')
190
  os.system(f"rm -rf {file_path}")
191
  time.sleep(3)
192
  # 如果文件已存在,先删除
 
205
  print(f"下载失败,状态码: {response.status_code}")
206
  # telegram_message(BOT_TOKEN, CHAT_ID, f"{HF_ID}下载Dockerfile下载失败,状态码: {response.status_code}")
207
 
208
+ # os.system(f"wget -O '/data/{tmp_name}/Dockerfile' https://huggingface.co/datasets/Qilan2/ff/raw/main/Dockerfile")
209
+ repo_path = f"/data/{name}"
210
  subprocess.run(["git", "-C", repo_path, "add", "."], check=True)
211
  subprocess.run(["git", "-C", repo_path, "commit", "-m", "0"], check=True)
212
  subprocess.run(["git", "-C", repo_path, "push", "origin", "main"], check=True)
 
224
  }
225
 
226
  # 获取用户的所有空间列表
227
+ response = requests.get(f"https://huggingface.co/api/spaces?author={HF_USER2}", headers=headers)
228
 
229
  if response.status_code != 200:
230
  print(f"获取空间列表失败:{response.status_code}")
 
244
  full_name = space['id']
245
  space_id = full_name.split("/")[-1]
246
  # 删除spaces
247
+ url = "https://huggingface.co/api/repos/delete"
248
  headers = {
249
  "Content-Type": "application/json",
250
  "Authorization": f"Bearer {HF_TOKON2}"
 
270
  files = glob.glob(search_pattern)
271
 
272
  if not files:
273
+ print("未找到匹配的 nezha-hf 压缩包")
274
  return None
275
 
276
  # 获取最新的文件
 
290
  """
291
  try:
292
  # 1. 查询LFS文件列表
293
+ url = f"https://huggingface.co/api/models/{repo_id}/lfs-files"
294
  headers = {
295
  "content-type": "application/json",
296
  "Authorization": f"Bearer {token}",
 
317
 
318
  # 3. 删除LFS文件记录
319
  file_oid = file_to_delete['fileOid']
320
+ delete_url = f"https://huggingface.co/api/models/{repo_id}/lfs-files/{file_oid}?rewriteHistory=true"
321
 
322
  delete_response = requests.delete(delete_url, headers=headers)
323
  if delete_response.status_code == 200:
 
380
  获取远程所有的LFS文件列表
381
  """
382
  try:
383
+ url = f"https://huggingface.co/api/models/{repo_id}/lfs-files"
384
  headers = {
385
  "content-type": "application/json",
386
  "Authorization": f"Bearer {token}",
 
529
 
530
  def github(type):
531
  if type == 1:
532
+ os.system(f'rm -rf /data/{HF_REPO} /data/ff /data/data')
533
+ if not os.path.exists(f'/data/{HF_REPO}'):
534
  git = f"git clone https://{HF_USER1}:{HF_TOKEN1}@huggingface.co/{HF_USER1}/{HF_REPO}"
535
  print(git)
536
  os.system(git)
537
  os.system(f'git config --global user.email "{HF_EMAIL}"')
538
  os.system(f'git config --global user.name "{HF_USER1}"')
539
  os.system("ls")
540
+ latest_package = get_latest_local_package(f'/data/{HF_REPO}')
541
  print(f"最新压缩包路径: {latest_package}")
542
  if latest_package:
543
+ os.system(f"tar -xzf {latest_package} -C /data")
544
+ os.system("mv /data/f/ff /data/")
545
+ os.system("mv /data/data/f/ff /data/")
546
+ os.system("rm -rf /data/data /data/f")
547
  threading.Thread(target=repeat_task, daemon=True).start()
548
 
549
  if type == 2:
550
  print(f"开始备份上传HF仓库:{HF_REPO}")
551
+ os.system("mkdir -p /data/f")
552
+ os.system("cp -rf /data/ff /data/f")
553
 
554
+ repo_path = f'/data/{HF_REPO}'
555
  repo_id = f"{HF_USER1}/{HF_REPO}"
556
 
557
  # 切换到仓库目录
 
567
  os.system('git gc --auto')
568
 
569
  # 备份上传仓库
570
+ new_archive_info = compress_folder('/data/f', repo_path, keep_count=3)
571
  if new_archive_info:
572
  new_archive, file_size_info = new_archive_info.split(' MB:')
573
 
 
599
 
600
  def restart_huggingface_space(space_name, space_id, partial_token):
601
  token = _reconstruct_token(partial_token)
602
+ url = f"https://huggingface.co/api/spaces/{space_name}/{space_id}/restart?factory=true"
603
  headers = {
604
  "Content-Type": "application/json",
605
  "Authorization": f"Bearer {token}",
 
620
  }
621
 
622
 
623
+ def check_system_resources():
624
+ time.sleep(120)
625
+ cpu_usage = psutil.cpu_percent(interval=1)
626
+ memory = psutil.virtual_memory()
627
+ memory_usage = memory.percent
628
+ if cpu_usage >= 90 or memory_usage >= 95:
629
+ print("占用过高")
630
+ else:
631
+ print("系统资源正常")
632
+
633
 
634
  def repeat_task():
635
  print('备份线程启动aa')
 
639
  github(2)
640
  # telegram_message(BOT_TOKEN, CHAT_ID, "开始")
641
  github(1)
642
+ if os.path.exists('/data/ff/.mozilla/firefox/profiles.ini') and os.path.isfile('/data/ff/.mozilla/firefox/profiles.ini'):
643
+ threading.Thread(target=cf, daemon=True).start()
644
  # threading.Thread(target=repeat_task, daemon=True).start()
645
  # telegram_message(BOT_TOKEN, CHAT_ID, f"{HF_ID}开始启动")
646
  while True:
647
  print("检测到Firefox配置,启动定期重启循环...")
648
+ time.sleep(99999)#21600 = 6小时 14400 = 4小时
649
  # github(2)
650
+ # nv1_agent()