doracoin a révisé ce gist . Aller à la révision
1 file changed, 77 insertions
update-certificates.py(fichier créé)
| @@ -0,0 +1,77 @@ | |||
| 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/ | |
Plus récent
Plus ancien