1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
import os import random import subprocess import time from datetime import datetime from urllib.parse import urlparse
def task_log(message): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(f"{timestamp} - {message}")
def extract_main_domain(url): parsed_url = urlparse(url) domains = parsed_url.netloc if parsed_url.netloc else parsed_url.path main_domain = domains.split('.')[0] return main_domain
def run_shell_command(commands): try: result = subprocess.run(commands, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) return result.stdout, result.stderr except subprocess.CalledProcessError as e: return e.stdout, e.stderr
def format_nginx_https(base_domain): ret_str = """ server { server_name base_domain; #将locaihost修改为您证书绑定的域名,例如:www.example.com。 listen 443 ssl; #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。 access_log /data/wwwlogs/access_nginx.log combined; root /data/wwwroot/base_domain; index index.html index.htm collection.html; ssl_certificate /usr/local/nginx/ssl/base_domain/fullchain.cer; #将domain name.pem替换成您证书的文件名。 ssl_certificate_key /usr/local/nginx/ssl/base_domain/base_domain.key; #将domain name.key替换成您证书的密钥文件名。 ssl_session_timeout 1m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。 ssl_prefer_server_ciphers on; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 1d; access_log off; } location ~ .*\.(js|css)?$ { expires 1d; access_log off; } location /.well-known { allow all; } }
server { listen 80; server_name base_domain; access_log /data/wwwlogs/access_nginx.log combined; #return 301 https://$server_name$request_uri; root /data/wwwroot/base_domain; index index.html index.htm collection.html; #error_page 404 /404.html; #error_page 502 /502.html; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 1d; access_log off; } location ~ .*\.(js|css)?$ { expires 1d; access_log off; } location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) { deny all; } location /.well-known { allow all; } } """ ret_str = ret_str.replace("base_domain", base_domain) return ret_str
def format_nginx_http(base_domain): ret_str = """ server { listen 80; server_name base_domain; access_log /data/wwwlogs/access_nginx.log combined; #return 301 https://$server_name$request_uri; root /data/wwwroot/base_domain; index index.html index.htm; #error_page 404 /404.html; #error_page 502 /502.html; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 7d; access_log off; } location /.well-known { allow all; } } """ ret_str = ret_str.replace("base_domain", base_domain) return ret_str
if __name__ == '__main__':
domain_list = [] file_name = "domain.txt" with open(file_name, 'r') as file: for line in file: doms = line.strip() domain_list.append(doms) print(len(domain_list)) try: for base_domain in domain_list: root_dir = "/data/wwwroot" + "/" + base_domain file_name = "index.html" if not os.path.exists(root_dir): os.mkdir(root_dir) task_log(f"【1】wwwroot : \t create dir is : {root_dir}") else: task_log(f"【1】wwwroot :\t {root_dir} exist,skip it")
file_path = os.path.join(root_dir, file_name)
if not os.path.exists(file_path): with open(file_path, 'w') as file: content = "<h1>{} hello world</>".format(base_domain) file.write(content) task_log(f"【2】{base_domain} index.html:\t create file:{file_path}") else: task_log(f"【2】{base_domain} index.html: \t '{file_path}' is exist")
domain_conf = "/usr/local/nginx/conf/vhost" + "/" + base_domain + ".conf"
if not os.path.exists(domain_conf): with open(domain_conf, 'w') as file: content_n = format_nginx_http(base_domain) file.write(content_n) task_log(f"【3】vhost domain_config:\t create file is: {domain_conf}") else: task_log(f"【3】vhost domain_config: \t {domain_conf} is exist") ssl_path = "/usr/local/nginx/ssl" + "/" + base_domain if not os.path.exists(ssl_path): os.mkdir(ssl_path) task_log(f"【4】ssl dir is: \t create {root_dir}") else: task_log(f"【4】ssl dir is:\t {root_dir} exist,skip it") command = ["/usr/local/nginx/sbin/nginx", "-s", "reload"] stdout, stderr = run_shell_command(command) task_log("【5】http nginx test: \n" + str(stdout).rstrip("\n") + "stderr:" + str(stderr).rstrip("\n")) rand_time = random.randint(1, 4)
command = ["/root/.acme.sh/acme.sh", "--set-default-ca", "--server", "letsencrypt"] stdout, stderr = run_shell_command(command) task_log( "【6】set-default-ca letsencrypt: \n" + str(stdout).rstrip("\n") + "stderr:" + str(stderr).rstrip("\n"))
print(f" please waiting {rand_time}s") time.sleep(rand_time) command = ["/root/.acme.sh/acme.sh", "--issue", "--domain", base_domain, "--domain", base_domain, "--webroot", root_dir] stdout, stderr = run_shell_command(command) task_log("【7】application ssl: \n" + str(stdout).rstrip("\n") + "stderr:" + str(stderr).rstrip("\n"))
print(f" please waiting 5s") time.sleep(5) ssl_key_name = os.path.join(ssl_path, base_domain + ".key") ssl_full_name = os.path.join(ssl_path, "fullchain.cer") command = ["/root/.acme.sh/acme.sh", "--install-cert", "-d", base_domain, "--key-file", ssl_key_name, "--fullchain-file", ssl_full_name] stdout, stderr = run_shell_command(command) task_log("【8】ssl move target stdout: \n" + str(stdout).rstrip("\n") + "stderr:" + str(stderr).rstrip("\n"))
with open(domain_conf, 'w') as file: content_n = format_nginx_https(base_domain) file.write(content_n) task_log(f"【9】vhost https config:\t create file is: {domain_conf}")
task_log( "【10】^^^^^^^^^domain 【{}】 is complete, go to the next one ,sleep {}s ^^^^^^^^^ \n".format(base_domain, rand_time)) time.sleep(rand_time) command_end = ["/usr/local/nginx/sbin/nginx", "-s", "reload"] stdout, stderr = run_shell_command(command_end) except Exception as e: print(e) task_log("【11】All Domain Application SSL success")
|