File size: 52,797 Bytes
a757bd3 | 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 | import json
import os
import sys
import shutil
import time
# import OpenSSL
import re
from hashlib import md5
from datetime import datetime, timedelta
from typing import Optional, Tuple, List, Dict, Union, Callable
from mod.base import json_response
from .util import webserver, check_server_config, write_file, read_file, GET_CLASS, service_reload
if "/www/server/panel/class" not in sys.path:
sys.path.insert(0, "/www/server/panel/class")
import public
import db
from panelAes import AesCryptPy3
SSL_SAVE_PATH = "{}/vhost/ssl_saved".format(public.get_panel_path())
class _SSLDatabase:
def __init__(self):
db_path = "{}/data/db".format(public.get_panel_path())
if not os.path.exists(db_path):
os.makedirs(db_path, 0o600)
self.db_file = '{}/data/db/ssl_data.db'.format(public.get_panel_path())
if not os.path.exists(self.db_file):
self.init_db()
if not os.path.exists(SSL_SAVE_PATH):
os.makedirs(SSL_SAVE_PATH, 0o600)
def init_db(self):
tmp_db = db.Sql()
setattr(tmp_db, "_Sql__DB_FILE", self.db_file)
create_sql_str = (
"CREATE TABLE IF NOT EXISTS 'ssl_info' ("
"'id' INTEGER PRIMARY KEY AUTOINCREMENT, "
"'hash' TEXT NOT NULL UNIQUE, "
"'path' TEXT NOT NULL, "
"'dns' TEXT NOT NULL, "
"'subject' TEXT NOT NULL, "
"'info' TEXT NOT NULL DEFAULT '', "
"'cloud_id' INTEGER NOT NULL DEFAULT -1, "
"'not_after' TEXT NOT NULL, "
"'use_for_panel' INTEGER NOT NULL DEFAULT 0, "
"'use_for_site' TEXT NOT NULL DEFAULT '[]', "
"'auth_info' TEXT NOT NULL DEFAULT '{}', "
"'create_time' INTEGER NOT NULL DEFAULT (strftime('%s'))"
");"
)
res = tmp_db.execute(create_sql_str)
if isinstance(res, str) and res.startswith("error"):
public.WriteLog("SSL管理", "建表ssl_info失败")
return
index_sql_str = "CREATE INDEX IF NOT EXISTS 'hash_index' ON 'ssl_info' ('hash');"
res = tmp_db.execute(index_sql_str)
if isinstance(res, str) and res.startswith("error"):
public.WriteLog("SSL管理", "为ssl_info建立索引hash_index失败")
return
tmp_db.close()
def connection(self):
tmp_db = db.Sql()
setattr(tmp_db, "_Sql__DB_FILE", self.db_file)
tmp_db.table("ssl_info")
return tmp_db
ssl_db = _SSLDatabase()
class _LocalSSLInfoTool:
def __init__(self):
self._letsencrypt = self.get_letsencrypt_conf()
@staticmethod
def get_letsencrypt_conf():
conf_file = "{}/config/letsencrypt_v2.json".format(public.get_panel_path())
if not os.path.exists(conf_file):
conf_file = "{}/config/letsencrypt.json".format(public.get_panel_path())
if not os.path.exists(conf_file):
return None
tmp_config = public.readFile(conf_file)
try:
orders = json.loads(tmp_config)["orders"]
except (json.JSONDecodeError, KeyError):
return None
return orders
def get_auth(self, domains):
if self._letsencrypt is None:
return None
for _, data in self._letsencrypt.items():
if 'save_path' not in data:
continue
for d in data['domains']:
if d in domains:
return {
"auth_type": data.get('auth_type'),
"auth_to": data.get('auth_to')
}
class RealSSLManger:
_REFRESH_TIP = "{}/data/ssl_cloud_refresh.tip".format(public.get_panel_path())
_OTHER_DATA_NAME = ("use_for_panel", "use_for_site",)
def __init__(self, conf_prefix=""):
self._local_ssl_info_tool = None
self._vhost_path = "/www/server/panel/vhost"
self.conf_prefix = conf_prefix
self._tls_v3 = None
self._is_nginx_http3 = None
# 与letsencrypt对接
@property
def local_tool(self):
if self._local_ssl_info_tool is None:
self._local_ssl_info_tool = _LocalSSLInfoTool()
return self._local_ssl_info_tool
return self._local_ssl_info_tool
# 用于部署
@classmethod
def get_cert_for_deploy(cls, ssl_data: dict) -> Union[Dict, str]:
data = {
'privkey': public.readFile(ssl_data["path"] + '/privkey.pem'),
'fullchain': public.readFile(ssl_data["path"] + '/fullchain.pem')
}
if not isinstance(data["privkey"], str) or not isinstance(data["fullchain"], str):
return '证书读取错误!'
return data
# 是否刷新
@classmethod
def need_refresh(cls):
now = int(time.time())
if not os.path.isfile(cls._REFRESH_TIP):
public.writeFile(cls._REFRESH_TIP, str(now))
return True
last_time = int(public.readFile(cls._REFRESH_TIP))
if last_time + 60 * 60 * 4 < now:
public.writeFile(cls._REFRESH_TIP, str(now))
return True
return False
# 获取hash指纹
@staticmethod
def ssl_hash(cert_filename: str = None, certificate: str = None, ignore_errors: bool = False) -> Optional[str]:
if cert_filename is not None and os.path.isfile(cert_filename):
certificate = public.readFile(cert_filename)
if not isinstance(certificate, str) or not certificate.startswith("-----BEGIN"):
if ignore_errors:
return None
raise ValueError("证书格式错误")
md5_obj = md5()
md5_obj.update(certificate.encode("utf-8"))
return md5_obj.hexdigest()
@staticmethod
def strf_date(sdate):
return time.strftime('%Y-%m-%d', time.strptime(sdate, '%Y%m%d%H%M%S'))
# 获取证书信息
@classmethod
def get_cert_info(cls, cert_filename: str = None, certificate: str = None):
if cert_filename is not None and os.path.isfile(cert_filename):
certificate = public.readFile(cert_filename)
if "/www/server/panel/class" not in sys.path:
sys.path.insert(0, "/www/server/panel/class")
import ssl_info
return ssl_info.ssl_info().load_ssl_info_by_data(certificate)
# try:
# result = {
# "issuer": '',
# "dns": [],
# }
# x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, certificate.encode("utf-8"))
# # 取产品名称
# issuer = x509.get_issuer()
# result['issuer'] = ''
# if hasattr(issuer, 'CN'):
# result['issuer'] = issuer.CN
# if not result['issuer']:
# is_key = [b'0', '0']
# issue_comp = issuer.get_components()
# if len(issue_comp) == 1:
# is_key = [b'CN', 'CN']
# for iss in issue_comp:
# if iss[0] in is_key:
# result['issuer'] = iss[1].decode()
# break
# if not result['issuer']:
# if hasattr(issuer, 'O'):
# result['issuer'] = issuer.O
# # 取到期时间
# result['notAfter'] = cls.strf_date(x509.get_notAfter().decode("utf-8")[:-1])
# # 取申请时间
# result['notBefore'] = cls.strf_date(x509.get_notBefore().decode("utf-8")[:-1])
# # 取可选名称
# for i in range(x509.get_extension_count()):
# s_name = x509.get_extension(i)
# if s_name.get_short_name() in [b'subjectAltName', 'subjectAltName']:
# s_dns = str(s_name).split(',')
# for d in s_dns:
# result['dns'].append(d.split(':')[1])
# subject = x509.get_subject().get_components()
# # 取主要认证名称
# if len(subject) == 1:
# result['subject'] = subject[0][1].decode()
# else:
# if not result['dns']:
# for sub in subject:
# if sub[0] == b'CN':
# result['subject'] = sub[1].decode()
# break
# if 'subject' in result:
# result['dns'].append(result['subject'])
# else:
# result['subject'] = result['dns'][0]
# return result
# except:
# return None
# 通过文件名称检查并保存
def save_by_file(self, cert_filename, private_key_filename, cloud_id=None, other_data: Optional[Dict] = None):
if not os.path.isfile(cert_filename) or not os.path.isfile(private_key_filename):
raise ValueError("不存在的证书")
certificate = public.readFile(cert_filename)
private_key = public.readFile(private_key_filename)
if not isinstance(certificate, str) or not isinstance(private_key, str):
raise ValueError("证书格式错误")
return self.save_by_data(certificate, private_key, cloud_id=cloud_id)
# 通过证书内容检查并保存
def save_by_data(self, certificate: str,
private_key: str,
cloud_id: Optional[int] = None,
other_data: Optional[Dict] = None) -> Dict:
if not certificate.startswith("-----BEGIN") or not private_key.startswith("-----BEGIN"):
raise ValueError("证书格式检查错误")
if cloud_id is None:
cloud_id = -1
hash_data = self.ssl_hash(certificate=certificate)
db_data = self.get_ssl_info_by_hash(hash_data)
if db_data is not None: # 已经保存过的
# 检查 cloud_id 与 保存的 cloud_id 不同时,更新cloud_id
if db_data['cloud_id'] != cloud_id and cloud_id != -1:
ssl_db.connection().where("id = ?", (db_data["id"],)).update({"cloud_id": cloud_id})
db_data['cloud_id'] = cloud_id
db_data["dns"] = json.loads(db_data["dns"])
db_data["info"] = json.loads(db_data["info"])
db_data["auth_info"] = json.loads(db_data["auth_info"])
db_data["use_for_site"] = json.loads(db_data["use_for_site"])
return db_data
info = self.get_cert_info(certificate=certificate)
if info is None:
raise ValueError("证书信息解析错误")
auth_info = self.local_tool.get_auth(info['dns'])
if auth_info is None:
auth_info = {}
pdata = {
"hash": hash_data,
"path": "{}/{}".format(SSL_SAVE_PATH, hash_data),
"dns": json.dumps(info['dns']),
"subject": info['subject'],
"info": json.dumps(info),
"cloud_id": cloud_id,
"not_after": info["notAfter"],
"auth_info": json.dumps(auth_info)
}
if other_data:
for key, other_data in other_data.items():
if key in self._OTHER_DATA_NAME:
pdata[key] = other_data
res_id = ssl_db.connection().insert(pdata)
if isinstance(res_id, str) and res_id.startswith("error"):
raise ValueError("数据库写入错误:" + res_id)
pdata["id"] = res_id
if not os.path.exists(pdata["path"]):
os.makedirs(pdata["path"], 0o600)
public.writeFile("{}/privkey.pem".format(pdata["path"]), private_key)
public.writeFile("{}/fullchain.pem".format(pdata["path"]), certificate)
public.writeFile("{}/info.json".format(pdata["path"]), pdata["info"])
pdata["info"] = info
return pdata
# 通过hash指纹获取ssl信息
@staticmethod
def get_ssl_info_by_hash(hash_data: str) -> Optional[dict]:
data = ssl_db.connection().where("hash = ?", (hash_data,)).find()
if isinstance(data, str):
raise ValueError("数据库查询错误:" + data)
if len(data) == 0:
return None
return data
@staticmethod
def _get_cbc_key_and_iv(with_uer_info=True):
uer_info_file = "{}/data/userInfo.json".format(public.get_panel_path())
try:
user_info = json.loads(public.readFile(uer_info_file))
uid = user_info["uid"]
except (json.JSONDecodeError, KeyError):
return None, None, None
md5_obj = md5()
md5_obj.update(str(uid).encode('utf8'))
bytes_data = md5_obj.hexdigest()
key = ''
iv = ''
for i in range(len(bytes_data)):
if i % 2 == 0:
iv += bytes_data[i]
else:
key += bytes_data[i]
if with_uer_info:
return key, iv, user_info
return key, iv, None
def get_cert_list(self,
param: Optional[Tuple[str, List]] = None,
force_refresh: bool = False,
local_refresh: bool = False) -> List:
if self.need_refresh() or force_refresh:
self._refresh_ssl_info_by_cloud()
self._get_ssl_by_local_data()
elif local_refresh:
self._get_ssl_by_local_data()
return self._get_cert_list(param)
# 获取证书列表
@classmethod
def _get_cert_list(cls, param: Optional[Tuple[str, List]]) -> List:
db_conn = ssl_db.connection()
if param is not None and len(param) == 2 and isinstance(param[0], str) and isinstance(param[1], (tuple, list)):
db_conn.where(param[0], param[1])
res = db_conn.select()
if isinstance(res, str):
raise ValueError("数据库查询错误:" + res)
for value in res:
value["dns"] = json.loads(value["dns"])
value["info"] = json.loads(value["info"])
value["auth_info"] = json.loads(value["auth_info"])
value["use_for_site"] = json.loads(value["use_for_site"])
value['endtime'] = int((datetime.strptime(value['not_after'], "%Y-%m-%d").timestamp()
- datetime.today().timestamp()) / (60 * 60 * 24))
res.sort(key=lambda x: x["not_after"], reverse=True)
return res
# 从云端收集证书
def _refresh_ssl_info_by_cloud(self):
key, iv, user_info = self._get_cbc_key_and_iv(with_uer_info=True)
if key is None or iv is None:
raise ValueError('面板未登录,无法链接云端!')
AES = AesCryptPy3(key, "CBC", iv, char_set="utf8")
# 对接云端
url = "https://www.bt.cn/api/Cert_cloud_deploy/get_cert_list"
try:
res_text = public.httpPost(url, {
"uid": user_info["uid"],
"access_key": user_info["access_key"],
"serverid": user_info["serverid"],
})
res_data = json.loads(res_text)
if res_data["status"] is False:
raise ValueError("获取云端数据失败")
res_list = res_data['data']
except:
raise ValueError("链接云端失败")
change_set = set()
for data in res_list:
try:
privateKey = AES.aes_decrypt(data["privateKey"])
certificate = AES.aes_decrypt(data["certificate"])
cloud_id = data["id"]
change_data = self.save_by_data(certificate, privateKey, cloud_id)
change_set.add(change_data.get("id"))
except:
pass
all_ids = ssl_db.connection().field("id").select()
for ssl_id in all_ids:
if ssl_id["id"] not in change_set:
ssl_db.connection().where("id = ?", (ssl_id["id"],)).update({"cloud_id": -1})
# 从本地收集证书
def _get_ssl_by_local_data(self): # 从本地获取可用证书
local_paths = ['/www/server/panel/vhost/cert', '/www/server/panel/vhost/ssl']
for path in local_paths:
if not os.path.exists(path):
continue
for p_name in os.listdir(path):
pem_file = "{}/{}/fullchain.pem".format(path, p_name)
key_file = "{}/{}/privkey.pem".format(path, p_name)
if os.path.isfile(pem_file) and os.path.isfile(key_file):
try:
self.save_by_file(pem_file, key_file)
except:
pass
panel_pem_file = "/www/server/panel/ssl/fullchain.pem"
panel_key_file = "/www/server/panel/ssl/privkey.pem"
if os.path.isfile(panel_pem_file) and os.path.isfile(panel_key_file):
try:
self.save_by_file(panel_pem_file, panel_key_file, other_data={"use_for_panel": 1})
except:
pass
# 从源储存位置删除
@classmethod
def _remove_ssl_from_local(cls, ssh_hash: str):
local_path = '/www/server/panel/vhost/ssl'
if not os.path.exists(local_path):
return
for p_name in os.listdir(local_path):
pem_file = "{}/{}/fullchain.pem".format(local_path, p_name)
if os.path.isfile(pem_file):
hash_data = cls.ssl_hash(cert_filename=pem_file)
if hash_data == ssh_hash:
shutil.rmtree("{}/{}".format(local_path, p_name))
# 查询证书
@staticmethod
def find_ssl_info(ssl_id=None, ssl_hash=None) -> Optional[dict]:
tmp_conn = ssl_db.connection()
if ssl_id is None and ssl_hash is None:
raise ValueError("没有参数信息")
if ssl_id is not None:
tmp_conn.where("id = ?", (ssl_id,))
else:
tmp_conn.where("hash = ?", (ssl_hash,))
target = tmp_conn.find()
if isinstance(target, str) and target.startswith("error"):
raise ValueError("数据库查询错误:" + target)
if not bool(target):
return None
target["auth_info"] = json.loads(target["auth_info"])
target["use_for_site"] = json.loads(target["use_for_site"])
target["dns"] = json.loads(target["dns"])
target["info"] = json.loads(target["info"])
target['endtime'] = int((datetime.strptime(target['not_after'], "%Y-%m-%d").timestamp()
- datetime.today().timestamp()) / (60 * 60 * 24))
return target
@classmethod
def add_use_for_site(cls, site_id, ssl_id=None, ssl_hash=None) -> bool:
return cls.change_use_for_site(site_id, ssl_id, ssl_hash, is_add=True)
@classmethod
def remove_use_for_site(cls, site_id, ssl_id=None, ssl_hash=None):
return cls.change_use_for_site(site_id, ssl_id, ssl_hash, is_add=False)
@classmethod
def change_use_for_site(cls, site_id, ssl_id=None, ssl_hash=None, is_add=True):
target = cls.find_ssl_info(ssl_id=ssl_id, ssl_hash=ssl_hash)
if not target:
return False
try:
site_ids = json.loads(target["use_for_site"])
except:
site_ids = []
if site_id in site_ids and is_add is False:
site_ids.remove(site_id)
up_res = ssl_db.connection().where("id = ?", (target["id"],)).update({"use_for_site": json.dumps(site_ids)})
if isinstance(up_res, str) and up_res.startswith("error"):
raise ValueError("数据库查询错误:" + up_res)
if site_id not in site_ids and is_add is True:
site_ids.append(site_id)
up_res = ssl_db.connection().where("id = ?", (target["id"],)).update({"use_for_site": json.dumps(site_ids)})
if isinstance(up_res, str) and up_res.startswith("error"):
raise ValueError("数据库查询错误:" + up_res)
return True
def get_all_site_ssl(self):
all_sites = public.M("sites").select()
self.clear_use_for_site()
if isinstance(all_sites, str) and all_sites.startswith("error"):
raise ValueError(all_sites)
for site in all_sites:
prefix = "" if site["project_type"] == "PHP" else site["project_type"].lower() + "_"
tmp = self._get_site_ssl_info(site["name"], prefix=prefix)
if tmp is None:
continue
hash_data = self.ssl_hash(cert_filename=tmp[0])
self.add_use_for_site(site["id"], ssl_hash=hash_data)
@staticmethod
def clear_use_for_site():
ssl_db.connection().update({"use_for_site": "[]"})
@staticmethod
def _get_site_ssl_info(site_name, prefix='') -> Optional[Tuple[str, str]]:
path = os.path.join('/www/server/panel/vhost/cert/', site_name)
pem_file = os.path.join(path, "fullchain.pem")
key_file = os.path.join(path, "privkey.pem")
if not os.path.isfile(pem_file) or not os.path.isfile(key_file):
path = os.path.join('/etc/letsencrypt/live/', site_name)
pem_file = os.path.join(path, "fullchain.pem")
key_file = os.path.join(path, "privkey.pem")
if not os.path.isfile(pem_file) or not os.path.isfile(key_file):
return None
webserver = public.get_webserver()
if webserver == "nginx":
conf_file = "{}/vhost/nginx/{}{}.conf".format(public.get_panel_path(), prefix, site_name)
elif webserver == "apache":
conf_file = "{}/vhost/apache/{}{}.conf".format(public.get_panel_path(), prefix, site_name)
else:
conf_file = "{}/vhost/openlitespeed/detail/{}.conf".format(public.get_panel_path(), site_name)
conf = public.readFile(conf_file)
if not conf:
return None
if public.get_webserver() == 'nginx':
keyText = 'ssl_certificate'
elif public.get_webserver() == 'apache':
keyText = 'SSLCertificateFile'
else:
keyText = 'openlitespeed/detail/ssl'
if conf.find(keyText) == -1:
return None
return pem_file, key_file
# 删除证书
def remove_cert(self, ssl_id=None, ssl_hash=None, local: bool = False) -> Dict:
_, _, user_info = self._get_cbc_key_and_iv(with_uer_info=True)
if user_info is None:
raise ValueError('面板未登录,无法上传云端!')
target = self.find_ssl_info(ssl_id=ssl_id, ssl_hash=ssl_hash)
if not target:
raise ValueError('没有指定的证书')
if local:
shutil.rmtree(target["path"])
self._remove_ssl_from_local(target["hash"]) # 把ssl下的也删除
ssl_db.connection().delete(id=target["id"])
if target["cloud_id"] != -1:
url = "https://www.bt.cn/api/Cert_cloud_deploy/del_cert"
try:
res_text = public.httpPost(url, {
"cert_id": target["cloud_id"],
"hashVal": target["hash"],
"uid": user_info["uid"],
"access_key": user_info["access_key"],
"serverid": user_info["serverid"],
})
res_data = json.loads(res_text)
if res_data["status"] is False:
return res_data
except:
if local:
raise ValueError("本地以删除成功, 链接云端失败, 无法删除云端数据")
raise ValueError("链接云端失败, 无法删除云端数据")
if not local:
ssl_db.connection().where("id = ?", (target["id"],)).update({"cloud_id": -1})
return public.returnMsg(True, "删除成功")
def mutil_remove_cert(self, ssl_id_list: List[int], local: bool = False):
result = []
for i in ssl_id_list:
try:
ssl_id = int(i)
except:
result.append({"status": False, "msg": "id信息解析错误"})
continue
res = self.remove_cert(ssl_id=ssl_id, local=local)
result.append(res)
return result
# 下载证书
def upload_cert(self, ssl_id=None, ssl_hash=None) -> Dict:
key, iv, user_info = self._get_cbc_key_and_iv()
if key is None or iv is None:
raise ValueError(False, '面板未登录,无法上传云端!')
target = self.find_ssl_info(ssl_id=ssl_id, ssl_hash=ssl_hash)
if not target:
raise ValueError("没有指定的证书信息")
data = {
'privateKey': public.readFile(target["path"] + '/privkey.pem'),
'certificate': public.readFile(target["path"] + '/fullchain.pem'),
"encryptWay": "AES-128-CBC",
"hashVal": target['hash'],
"uid": user_info["uid"],
"access_key": user_info["access_key"],
"serverid": user_info["serverid"],
}
if data["privateKey"] is False or data["certificate"] is False:
raise ValueError('证书文件读取错误')
AES = AesCryptPy3(key, "CBC", iv, char_set="utf8")
data["privateKey"] = AES.aes_encrypt(data["privateKey"])
data["certificate"] = AES.aes_encrypt(data["certificate"])
# 对接云端
url = "https://www.bt.cn/api/Cert_cloud_deploy/cloud_deploy"
try:
res_text = public.httpPost(url, data)
res_data = json.loads(res_text)
if res_data["status"] is True:
cloud_id = int(res_data["data"].get("id"))
ssl_db.connection().where("id = ?", (target["id"],)).update({"cloud_id": cloud_id})
return res_data
else:
return res_data
except:
raise ValueError('链接云端失败')
# ssl_hash 证书储存记录的唯一值
def set_site_ssl_conf(self, site_name: str, ssl_data: dict, mutil=False) -> Optional[str]:
privkey = ssl_data["privkey"]
fullchain = ssl_data["fullchain"]
path = '/www/server/panel/vhost/cert/' + site_name
if not os.path.exists(path):
os.makedirs(path)
csrpath = path + "/fullchain.pem"
keypath = path + "/privkey.pem"
# 清理旧的证书链
remove_list = [keypath, csrpath, path + "/certOrderId", path + "/README"]
for i in remove_list:
if os.path.exists(i):
os.remove(i)
public.ExecShell('rm -rf ' + path + '-00*')
public.ExecShell('rm -rf /etc/letsencrypt/archive/' + site_name)
public.ExecShell('rm -rf /etc/letsencrypt/archive/' + site_name + '-00*')
public.ExecShell('rm -f /etc/letsencrypt/renewal/' + site_name + '.conf')
public.ExecShell('rm -f /etc/letsencrypt/renewal/' + site_name + '-00*.conf')
public.writeFile(keypath, privkey)
public.writeFile(csrpath, fullchain)
error_msg = self._set_ssl_conf_to_nginx(site_name, mutil)
if error_msg is not None and webserver() == "nginx":
return error_msg
error_msg = self._set_ssl_conf_to_apache(site_name, mutil)
if error_msg is not None and webserver() == "apache":
return error_msg
# http3是否可用
def is_nginx_http3(self):
"""判断nginx是否可以使用http3"""
if getattr(self, "_is_nginx_http3", None) is None:
_is_nginx_http3 = public.is_nginx_http3()
setattr(self, "_is_nginx_http3", _is_nginx_http3)
return self._is_nginx_http3
# 在防火墙放行443
@staticmethod
def open_firewall_443() -> None:
import firewalls
get = GET_CLASS()
get.port = '443'
get.ps = 'HTTPS'
firewalls.firewalls().AddAcceptPort(get)
# 在nginx配置文件中设置ssl信息
def _set_ssl_conf_to_nginx(self, site_name, mutil=False) -> Optional[str]:
# Nginx配置
file = '{}/nginx/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
ng_conf = read_file(file)
if not ng_conf:
return "配置文件丢失,配置失败"
http3_header = ""
if self.is_nginx_http3():
http3_header = '''\n add_header Alt-Svc 'quic=":443"; h3=":443"; h3-29=":443"; h3-27=":443";h3-25=":443"; h3-T050=":443"; h3-Q050=":443";h3-Q049=":443";h3-Q048=":443"; h3-Q046=":443"; h3-Q043=":443"';'''
if ng_conf.find('ssl_certificate') == -1:
sslStr = """#error_page 404/404.html;
ssl_certificate /www/server/panel/vhost/cert/%s/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/%s/privkey.pem;
ssl_protocols %s;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";%s
error_page 497 https://$host$request_uri;""" % (
site_name, site_name, self._get_tls_protocol(is_apache=False), http3_header
)
new_ng_conf = ng_conf.replace('#error_page 404/404.html;', sslStr)
# 添加端口
from .domain_tool import NginxDomainTool
new_ng_conf = NginxDomainTool.nginx_add_port_by_config(new_ng_conf, "443", is_http3=self.is_nginx_http3())
write_file(file, new_ng_conf)
if webserver() == "nginx" and check_server_config() is not None:
return "配置失败"
if webserver() == "nginx" and not mutil:
service_reload()
self.open_firewall_443()
# 在apache配置文件中设置ssl信息
def _set_ssl_conf_to_apache(self, site_name, mutil=False) -> Optional[str]:
ap_file = '{}/apache/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
ap_conf = read_file(ap_file)
if not ap_conf:
return "配置文件丢失,配置失败"
tmp_template_res = re.search(r"<VirtualHost(.|\n)*?</VirtualHost>", ap_conf)
if not tmp_template_res:
return "配置文件丢失,配置失败"
else:
tmp_template = tmp_template_res.group()
rep_template_with_ports = re.compile(r"<VirtualHost +.*:(?P<port>\d+)+\s*>(.|\n)*?</VirtualHost>")
target_vhost = None
for tmp in rep_template_with_ports.finditer(ap_conf):
if tmp.group("port") == "443":
target_vhost = tmp.group()
if target_vhost and (target_vhost.find("SSLEngine On") or target_vhost.find("SSLCertificateFile")):
return
if not target_vhost:
rep_ports = re.compile(r"<VirtualHost +.*:(?P<port>\d+)+\s*>")
target_vhost = rep_ports.sub("<VirtualHost *:443>", tmp_template, 1)
# 添加SSL配置
ssl_conf = """
#SSL
SSLEngine On
SSLCertificateFile /www/server/panel/vhost/cert/%s/fullchain.pem
SSLCertificateKeyFile /www/server/panel/vhost/cert/%s/privkey.pem
SSLCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5:ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLProtocol All -SSLv2 -SSLv3 %s
SSLHonorCipherOrder On
""" % (site_name, site_name, self._get_tls_protocol(is_apache=True))
rep_list = [
(re.compile(r"#DENY FILES"), True),
(re.compile(r"CustomLog[^\n]*\n"), False),
]
# 使用正则匹配确定插入位置
def set_by_rep_idx(tmp_rep: re.Pattern, use_start: bool) -> Optional[str]:
tmp_res = tmp_rep.search(target_vhost)
if not tmp_res:
return None
if use_start:
new_conf = target_vhost[:tmp_res.start()] + ssl_conf + tmp_res.group() + target_vhost[tmp_res.end():]
else:
new_conf = target_vhost[:tmp_res.start()] + tmp_res.group() + ssl_conf + target_vhost[tmp_res.end():]
return new_conf
ssl_vhost = None
for r, s in rep_list:
ssl_vhost = set_by_rep_idx(r, s)
if ssl_vhost is not None:
break
if ssl_vhost is None:
return "无法定位SSL配置文件位置,配置失败"
write_file(ap_file, ap_conf + "\n" + ssl_vhost)
# 添加端口
from .domain_tool import ApacheDomainTool
ApacheDomainTool.apache_add_ports("443")
web_server = webserver()
if web_server == "apache" and check_server_config() is not None:
write_file(ap_file, ap_conf)
return "配置失败"
if web_server == "apache" and not mutil:
service_reload()
self.open_firewall_443()
def close_site_ssl_conf(self, site_name) -> Optional[str]:
error_msg = self._close_ssl_conf_to_nginx(site_name)
if error_msg is not None and webserver() == "nginx":
return error_msg
error_msg = self._close_ssl_conf_to_apache(site_name)
if error_msg is not None and webserver() == "apache":
return error_msg
service_reload()
return None
def _close_ssl_conf_to_nginx(self, site_name) -> Optional[str]:
file = '{}/nginx/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
ng_conf = read_file(file)
if not ng_conf:
return "配置文件丢失,配置失败"
rep_list = (
re.compile(r"\n\s*#HTTP_TO_HTTPS_START(.|\n){1,300}#HTTP_TO_HTTPS_END"), # 关闭 强制https
re.compile(r"\s*ssl_(certificate|certificate_key|protocols|"
r"ciphers|prefer_server_ciphers|session_cache|session_timeout)[^;]*;"), # 关闭 强制https
re.compile(r"\s*add_header\s+(Strict-Transport-Security|Alt-Svc)[^;]*;"), # 关闭 https 请求头配置
re.compile(r"\s*error_page\s+497\s+[^;]*;"),
re.compile(r"\s+listen\s+(\[::]:)?443.*;"), # 关闭端口监听
)
new_conf = ng_conf
for rep in rep_list:
new_conf = rep.sub("", new_conf)
write_file(file, new_conf)
def _close_ssl_conf_to_apache(self, site_name) -> Optional[str]:
file = '{}/apache/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
ap_conf = read_file(file)
if not ap_conf:
return "配置文件丢失,配置失败"
rep_list = (
re.compile(r"\n\s*#HTTP_TO_HTTPS_START(.|\n){1,300}#HTTP_TO_HTTPS_END"),
re.compile(r"\n<VirtualHost\s+\*:443\s*>(.|\n)*</VirtualHost>"),
)
new_conf = ap_conf
for rep in rep_list:
new_conf = rep.sub("", new_conf)
write_file(file, new_conf)
def _get_tls_protocol(self, is_apache=False):
"""获取使用的协议
@author baozi <202-04-18>
@param:
@return
"""
protocols = {
"TLSv1": False,
"TLSv1.1": True,
"TLSv1.2": True,
"TLSv1.3": False,
}
tls1_3 = self.get_tls13()
file_path = public.get_panel_path() + "/data/ssl_protocol.json"
if os.path.exists(file_path):
data = public.readFile(file_path)
if data is not False:
protocols = json.loads(data)
if protocols["TLSv1.3"] and tls1_3 == "":
protocols["TLSv1.3"] = False
if is_apache is False:
return " ".join([p for p, v in protocols.items() if v is True])
else:
return " ".join(["-" + p for p, v in protocols.items() if v is False])
else:
if tls1_3 != "":
protocols["TLSv1.3"] = True
if is_apache is False:
return " ".join([p for p, v in protocols.items() if v is True])
else:
return " ".join(["-" + p for p, v in protocols.items() if v is False])
# 获取TLS1.3标记
def get_tls13(self):
if self._tls_v3 is not None:
return self._tls_v3
nginx_bin = '/www/server/nginx/sbin/nginx'
nginx_v = public.ExecShell(nginx_bin + ' -V 2>&1')[0]
nginx_v_re = re.search(r"nginx/(?P<ng_ver>\d\.\d+).+OpenSSL\s+(?P<ssl_ver>\d\.\d+)", nginx_v)
if nginx_v_re:
ng_ver = nginx_v_re.group("ng_ver")
ssl_ver = nginx_v_re.group("ssl_ver")
if float(ng_ver) >= 1.15 and float(ssl_ver) >= 1.1:
self._tls_v3 = 'TLSv1.3'
else:
can_ng_ver = re.search(r'nginx/1\.(1[5-9]|2\d)', nginx_v)
openssl_v = public.ExecShell(nginx_bin + ' -V 2>&1|grep OpenSSL')[0].find('OpenSSL 1.1.') != -1
if can_ng_ver and openssl_v:
self._tls_v3 = 'TLSv1.3'
if self._tls_v3 is None:
self._tls_v3 = ''
return self._tls_v3
# HttpToHttps
def set_http_to_https(self, site_name: str):
# Nginx配置
file = '{}/nginx/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
conf = read_file(file)
if conf:
if conf.find('ssl_certificate') == -1:
return public.returnMsg(False, '当前未开启SSL')
to_str = """#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
"""
conf = conf.replace('#error_page 404/404.html;', to_str)
write_file(file, conf)
file = '{}/apache/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
conf = public.readFile(file)
if conf:
to_str = '''
#HTTP_TO_HTTPS_START
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}$1 [L,R=301]
</IfModule>
#HTTP_TO_HTTPS_END
SSLEngine On'''
conf = re.sub('SSLEngine On', to_str, conf, 1)
public.writeFile(file, conf)
service_reload()
# CloseToHttps
def close_to_https(self, site_name):
file = '{}/nginx/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
conf = public.readFile(file)
if conf:
rep_https = re.compile(r"(#HTTP_TO_HTTPS_START\s*)?if\s+\(\s*\$server_port\s+!~\s+443\s*\)"
r"[^{]*\{[^}]*}\s*(#HTTP_TO_HTTPS_END\s*)?")
new_conf = rep_https.sub('', conf)
write_file(file, new_conf)
file = '{}/apache/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
conf = public.readFile(file)
if conf:
rep_https = re.compile("\n\s*#HTTP_TO_HTTPS_START(.|\n){1,300}#HTTP_TO_HTTPS_END")
new_conf = rep_https.sub('', conf)
write_file(file, new_conf)
service_reload()
# 是否有跳转到https
def is_to_https(self, site_name) -> bool:
file = '{}/nginx/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
conf = public.readFile(file)
if conf:
if conf.find('HTTP_TO_HTTPS_START') != -1:
return True
if conf.find('$server_port !~ 443') != -1:
return True
return False
def get_site_ssl_info(self, site_name: str) -> Optional[dict]:
try:
w_s = webserver()
if w_s == 'nginx':
conf_file = '{}/nginx/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
elif w_s == "apach":
conf_file = '{}/apache/{}{}.conf'.format(self._vhost_path, self.conf_prefix, site_name)
else:
return None
if not os.path.exists(conf_file):
return None
s_conf = public.readFile(conf_file)
if not s_conf:
return None
if w_s == "apach":
s_tmp = re.findall(r"SSLCertificateFile\s+(.+\.pem)", s_conf)
if not s_tmp:
return None
ssl_file = s_tmp[0]
else:
s_tmp = re.findall(r"ssl_certificate\s+(.+\.pem);", s_conf)
if not s_tmp:
return None
ssl_file = s_tmp[0]
ssl_info = self.get_cert_info(cert_filename=ssl_file)
if not ssl_info:
return None
ssl_info['endtime'] = int(
int(time.mktime(time.strptime(ssl_info['notAfter'], "%Y-%m-%d")) - time.time()) / 86400)
return ssl_info
except:
return None
class SSLManager:
def __init__(self, conf_prefix: str = ""):
self.conf_prefix = conf_prefix
def set_site_ssl_conf(self, get):
ssl_id = None
ssl_hash = None
try:
if "ssl_id" in get:
ssl_id = int(get.ssl_id)
if "ssl_hash" in get:
ssl_hash = get.ssl_hash.strip()
site_name = get.site_name.strip()
except (ValueError, AttributeError, KeyError):
return public.ReturnMsg(False, "参数错误")
ssl_mgr = RealSSLManger(self.conf_prefix)
try:
info = ssl_mgr.find_ssl_info(ssl_id=ssl_id, ssl_hash=ssl_hash)
if not info:
return json_response(status=False, msg="未查询到证书信息")
ssl_data = ssl_mgr.get_cert_for_deploy(info)
if isinstance(ssl_data, str):
return json_response(status=False, msg=ssl_data)
except ValueError as e:
return json_response(status=False, msg=str(e))
err_msg = ssl_mgr.set_site_ssl_conf(site_name=site_name, ssl_data=ssl_data)
if err_msg:
return json_response(status=False, msg=err_msg)
return json_response(status=True, msg="部署成功")
def mutil_set_site_ssl_conf(self, get):
ssl_id = None
ssl_hash = None
try:
if "ssl_id" in get:
ssl_id = int(get.ssl_id)
if "ssl_hash" in get:
ssl_hash = get.ssl_hash.strip()
site_names = json.loads(get.site_names.strip())
except (ValueError, AttributeError, KeyError, json.JSONDecodeError):
return public.ReturnMsg(False, "参数错误")
ssl_mgr = RealSSLManger(self.conf_prefix)
try:
info = ssl_mgr.find_ssl_info(ssl_id=ssl_id, ssl_hash=ssl_hash)
if not info:
return json_response(status=False, msg="未查询到证书信息")
ssl_data = ssl_mgr.get_cert_for_deploy(info)
if isinstance(ssl_data, str):
return json_response(status=False, msg=ssl_data)
except ValueError as e:
return json_response(status=False, msg=str(e))
result = {
"total": len(site_names),
"success": 0,
"failed": 0,
"success_list": [],
"failed_list": [],
"failed_msg": []
}
for i in site_names:
err_msg = ssl_mgr.set_site_ssl_conf(site_name=i, ssl_data=ssl_data)
if err_msg:
result["failed"] += 1
result["failed_list"].append(i)
result["failed_msg"].append(err_msg)
else:
result["success"] += 1
result["success_list"].append(i)
return json_response(status=True, data=result)
def close_site_ssl_conf(self, get):
try:
site_name = get.site_name.strip()
except (ValueError, AttributeError, KeyError):
return public.ReturnMsg(False, "参数错误")
ssl_mgr = RealSSLManger(self.conf_prefix)
try:
err_msg = ssl_mgr.close_site_ssl_conf(site_name)
if err_msg:
return json_response(status=False, msg=err_msg)
return json_response(status=True, msg="关闭成功")
except Exception as e:
return json_response(status=False, msg=str(e))
def upload_cert_to_cloud(self, get):
ssl_id = None
ssl_hash = None
try:
if "ssl_id" in get:
ssl_id = int(get.ssl_id)
if "ssl_hash" in get:
ssl_hash = get.ssl_hash.strip()
except (ValueError, AttributeError, KeyError):
return public.ReturnMsg(False, "参数错误")
try:
data = RealSSLManger(self.conf_prefix).upload_cert(ssl_id, ssl_hash)
return json_response(status=True, data=data)
except ValueError as e:
return json_response(status=False, msg=str(e))
except Exception as e:
return json_response(status=False, msg="操作错误:" + str(e))
def remove_cloud_cert(self, get):
ssl_id = None
ssl_hash = None
local = False
try:
if "ssl_id" in get:
ssl_id = int(get.ssl_id)
if "ssl_hash" in get:
ssl_hash = get.ssl_hash.strip()
if "local" in get and get.local.strip() in ("1", 1, True, "true"):
local = True
except (ValueError, AttributeError, KeyError):
return public.ReturnMsg(False, "参数错误")
try:
data = RealSSLManger(self.conf_prefix).remove_cert(ssl_id, ssl_hash, local=local)
return json_response(status=data.get("status", True), msg=data.get("msg", ""), data=data)
except ValueError as e:
return json_response(status=False, msg=str(e))
except Exception as e:
return json_response(status=False, msg="操作错误:" + str(e))
def mutil_remove_cloud_cert(self, get):
local = False
try:
ssl_id_list = json.loads(get.ssl_id_list.strip())
if "local" in get and get.local.strip() in ("1", 1, True, "true"):
local = True
except (ValueError, AttributeError, KeyError):
return public.ReturnMsg(False, "参数错误")
try:
data = RealSSLManger(self.conf_prefix).mutil_remove_cert(ssl_id_list, local=local)
return json_response(status=True, data=data)
except ValueError as e:
return json_response(status=False, msg=str(e))
except Exception as e:
return json_response(status=False, msg="操作错误:" + str(e))
# 未使用
def refresh_cert_list(self, get=None):
try:
data = RealSSLManger(self.conf_prefix).get_cert_list(force_refresh=True)
return json_response(status=True, data=data)
except ValueError as e:
return json_response(status=False, msg=str(e))
except Exception as e:
return json_response(status=False, msg="操作错误:" + str(e))
def get_cert_info(self, get):
ssl_id = None
ssl_hash = None
try:
if "ssl_id" in get:
ssl_id = int(get.ssl_id)
if "ssl_hash" in get:
ssl_hash = get.ssl_hash.strip()
except (ValueError, AttributeError, KeyError):
return public.ReturnMsg(False, "参数错误")
try:
ssl_mager = RealSSLManger(self.conf_prefix)
target = ssl_mager.find_ssl_info(ssl_id, ssl_hash)
if target is None:
return json_response(status=False, msg="未获取到证书信息")
data = ssl_mager.get_cert_for_deploy(target)
if isinstance(data, dict):
target.update(data)
return json_response(status=True, data=target)
else:
return json_response(status=False, msg=data)
except ValueError as e:
return json_response(status=False, msg=str(e))
except Exception as e:
return json_response(status=False, msg="操作错误:" + str(e))
def get_site_ssl_info(self, get):
try:
site_name = get.site_name.strip()
except (ValueError, AttributeError, KeyError):
return json_response(False, "参数错误")
ssl_info = RealSSLManger(self.conf_prefix).get_site_ssl_info(site_name)
if ssl_info is None:
return json_response(status=False, msg="未获取到证书信息")
else:
return json_response(status=True, data=ssl_info)
def get_cert_list(self, get):
"""
search_limit 0 -> 所有证书
search_limit 1 -> 没有过期的证书
search_limit 2 -> 有效期小于等于15天的证书 但未过期
search_limit 3 -> 过期的证书
search_limit 4 -> 过期时间1年以上的证书
"""
search_name = None
search_limit = 0
force_refresh = False
try:
if "search_name" in get:
search_name = get.search_name.strip()
if "search_limit" in get:
search_limit = int(get.search_limit.strip())
if "force_refresh" in get and get.force_refresh.strip() in ("1", 1, "True", True):
force_refresh = True
except (ValueError, AttributeError, KeyError):
return json_response(status=False, msg="参数错误")
param = None
if search_name is not None:
param = ['subject LIKE ?', ["%{}%".format(search_name)]]
now = datetime.now()
filter_func: Callable[[dict, ], bool] = lambda x: True
if search_limit == 1:
date = now.strftime("%Y-%m-%d")
filter_func: Callable[[dict, ], bool] = lambda x: x["not_after"] >= date
elif search_limit == 2:
date1 = now.strftime("%Y-%m-%d")
date2 = (now + timedelta(days=15)).strftime("%Y-%m-%d")
filter_func: Callable[[dict, ], bool] = lambda x: date1 <= x["not_after"] <= date2
elif search_limit == 3:
date = now.strftime("%Y-%m-%d")
filter_func: Callable[[dict, ], bool] = lambda x: x["not_after"] < date
elif search_limit == 4:
date = (now + timedelta(days=366)).strftime("%Y-%m-%d")
filter_func: Callable[[dict, ], bool] = lambda x: x["not_after"] > date
try:
res_list = RealSSLManger(self.conf_prefix).get_cert_list(param=param, force_refresh=force_refresh)
res_list = list(filter(filter_func, res_list))
res_list.sort(key=lambda x: x["not_after"])
return json_response(status=True, data=res_list)
except ValueError as e:
return json_response(False, str(e))
except Exception as e:
return json_response(False, "操作错误:" + str(e))
@staticmethod
def set_ssl_protocol(get):
""" 设置全局TLS版本
@author baozi <202-04-18>
@param:
@return
"""
protocols = {
"TLSv1": False,
"TLSv1.1": False,
"TLSv1.2": False,
"TLSv1.3": False,
}
if "use_protocols" in get:
use_protocols = getattr(get, "use_protocols", [])
if isinstance(use_protocols, list):
for protocol in use_protocols:
if protocol in protocols:
protocols[protocol] = True
elif isinstance(use_protocols, str):
for protocol in use_protocols.split(","):
if protocol in protocols:
protocols[protocol] = True
else:
protocols["TLSv1.1"] = True
protocols["TLSv1.2"] = True
protocols["TLSv1.3"] = True
else:
protocols["TLSv1.1"] = True
protocols["TLSv1.2"] = True
protocols["TLSv1.3"] = True
public.print_log(protocols)
public.WriteFile(public.get_panel_path() + "/data/ssl_protocol.json", json.dumps(protocols))
return public.returnMsg(True, 'SET_SUCCESS')
@staticmethod
def get_ssl_protocol(get=None):
""" 获取全局TLS版本
@author baozi <202-04-18>
@param:
@return
"""
protocols = {
"TLSv1": False,
"TLSv1.1": True,
"TLSv1.2": True,
"TLSv1.3": False,
}
file_path = public.get_panel_path() + "/data/ssl_protocol.json"
if os.path.exists(file_path):
data = public.readFile(file_path)
if data is not False:
protocols = json.loads(data)
return protocols
return protocols
|