最后活跃于 1727274127

update-certificates.py 原始文件
1# Update Certificate from Server NginxProxyManager
2import ctypes
3import os
4import sys
5import time
6import zipfile
7
8import requests
9
10server_url = 'https://yourdomain.com'
11certificate_index = 1
12npm_username = ''
13npm_password = ''
14login_url = f'{server_url}/api/tokens'
15download_url = f'{server_url}/api/nginx/certificates/{certificate_index}/download'
16
17
18def login():
19 r = requests.post(url=login_url, json={"identity": npm_username, "secret": npm_password})
20 print(r.status_code, r.text)
21 if r.status_code == 200:
22 print('登录成功,准备下载证书文件')
23 download_certificates(r.json()['token'])
24 else:
25 print('登录失败')
26
27
28def download_certificates(token):
29 r = requests.get(url=download_url, headers={'Authorization': 'Bearer ' + token})
30 print(r.url, r.status_code)
31 if r.status_code == 200:
32 content = r.content
33 with open('certificates.zip', 'wb') as file:
34 file.write(content)
35 file.close()
36 print('证书下载成功!')
37 unzip_certificates('certificates.zip')
38 else:
39 print('证书下载失败!', r.text)
40
41
42def unzip_certificates(file):
43 zip_file = zipfile.ZipFile(file)
44 zip_file.extractall(path='./letsencrypt/live/')
45 print('证书文件已解压,请手动更新配置文件以使用新证书,并重启Nginx')
46 # restart_nginx()
47
48
49def restart_nginx():
50 a = os.system('sc stop Nginx')
51 print(a)
52 time.sleep(1)
53 a = os.system('sc start Nginx')
54 print(a)
55 print('Nginx 服务重启成功')
56
57
58# https://www.jinky.top/posts/request-uac-using-python/
59def request_uac():
60 if not is_admin():
61 # Re-run the program with admin rights
62 ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
63
64
65def is_admin():
66 try:
67 return ctypes.windll.shell32.IsUserAnAdmin()
68 except:
69 return False
70
71
72# Press the green button in the gutter to run the script.
73if __name__ == '__main__':
74 request_uac()
75 login()
76
77# See PyCharm help at https://www.jetbrains.com/help/pycharm/
78