update-certificates.py
· 2.0 KiB · Python
Orginalformat
# Update Certificate from Server NginxProxyManager
import ctypes
import os
import sys
import time
import zipfile
import requests
server_url = 'https://yourdomain.com'
certificate_index = 1
npm_username = ''
npm_password = ''
login_url = f'{server_url}/api/tokens'
download_url = f'{server_url}/api/nginx/certificates/{certificate_index}/download'
def login():
r = requests.post(url=login_url, json={"identity": npm_username, "secret": npm_password})
print(r.status_code, r.text)
if r.status_code == 200:
print('登录成功,准备下载证书文件')
download_certificates(r.json()['token'])
else:
print('登录失败')
def download_certificates(token):
r = requests.get(url=download_url, headers={'Authorization': 'Bearer ' + token})
print(r.url, r.status_code)
if r.status_code == 200:
content = r.content
with open('certificates.zip', 'wb') as file:
file.write(content)
file.close()
print('证书下载成功!')
unzip_certificates('certificates.zip')
else:
print('证书下载失败!', r.text)
def unzip_certificates(file):
zip_file = zipfile.ZipFile(file)
zip_file.extractall(path='./letsencrypt/live/')
print('证书文件已解压,请手动更新配置文件以使用新证书,并重启Nginx')
# restart_nginx()
def restart_nginx():
a = os.system('sc stop Nginx')
print(a)
time.sleep(1)
a = os.system('sc start Nginx')
print(a)
print('Nginx 服务重启成功')
# https://www.jinky.top/posts/request-uac-using-python/
def request_uac():
if not is_admin():
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
request_uac()
login()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
| 1 | # Update Certificate from Server NginxProxyManager |
| 2 | import ctypes |
| 3 | import os |
| 4 | import sys |
| 5 | import time |
| 6 | import zipfile |
| 7 | |
| 8 | import requests |
| 9 | |
| 10 | server_url = 'https://yourdomain.com' |
| 11 | certificate_index = 1 |
| 12 | npm_username = '' |
| 13 | npm_password = '' |
| 14 | login_url = f'{server_url}/api/tokens' |
| 15 | download_url = f'{server_url}/api/nginx/certificates/{certificate_index}/download' |
| 16 | |
| 17 | |
| 18 | def 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 | |
| 28 | def 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 | |
| 42 | def unzip_certificates(file): |
| 43 | zip_file = zipfile.ZipFile(file) |
| 44 | zip_file.extractall(path='./letsencrypt/live/') |
| 45 | print('证书文件已解压,请手动更新配置文件以使用新证书,并重启Nginx') |
| 46 | # restart_nginx() |
| 47 | |
| 48 | |
| 49 | def 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/ |
| 59 | def 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 | |
| 65 | def 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. |
| 73 | if __name__ == '__main__': |
| 74 | request_uac() |
| 75 | login() |
| 76 | |
| 77 | # See PyCharm help at https://www.jetbrains.com/help/pycharm/ |
| 78 |