{"id":1146,"date":"2024-02-17T21:37:44","date_gmt":"2024-02-17T13:37:44","guid":{"rendered":"https:\/\/imuslab.com\/wordpress\/?p=1146"},"modified":"2024-02-17T21:47:30","modified_gmt":"2024-02-17T13:47:30","slug":"zoraxy-tls-sni","status":"publish","type":"post","link":"https:\/\/imuslab.com\/wordpress\/2024\/02\/17\/zoraxy-tls-sni\/","title":{"rendered":"\u5728 Zoraxy \u52a0\u5165 TLS SNI \u529f\u80fd"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"523\" src=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/02\/\u5716\u7247-8-1024x523.png\" alt=\"\" class=\"wp-image-1148\" srcset=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/02\/\u5716\u7247-8-1024x523.png 1024w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/02\/\u5716\u7247-8-300x153.png 300w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/02\/\u5716\u7247-8-768x392.png 768w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/02\/\u5716\u7247-8-1200x613.png 1200w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/02\/\u5716\u7247-8.png 1371w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u6700\u8fd1\u6211\u5728\u958b\u767c\u7684 Zoraxy v3<\/figcaption><\/figure>\n\n\n\n<p>\u5728\u6211\u958b\u767c Zoraxy \u4e4b\u524d\u6211\u4e5f\u4e0d\u77e5\u9053 https \u8b49\u66f8\u9019\u6771\u897f\u5c0d\u65bc reverse proxy \u4f3a\u670d\u5668\u4f86\u8aaa\u5230\u5e95\u6709\u591a\u8907\u96dc\u3002\u4e00\u958b\u59cb\u7684\u6642\u5019\u56e0\u70ba\u6211\u90fd\u662f\u7528\u4e00\u500b\u57df\u540d\uff08domain name\uff09\uff0c\u6240\u4ee5 reverse proxy \u88e1\u9762\u4e5f\u662f\u53ea\u9700\u8981\u8655\u7406\u4e00\u500b\u4e3b\u6a5f\u540d\u7a31\uff08host name\uff09\u3002\u800c\u521d\u4ee3\u7684 Zoraxy \u5247\u662f\u76f4\u63a5\u7528 TLS hello info \u88e1\u9762\u7684 server name \u4f5c\u70ba certificate \u7684 key \u4f86\u627e\u5230\u5408\u9069\u7684 certificate \u4e26\u56de\u50b3\u7d66\u5ba2\u6236\u7aef\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\uff08time complexity\uff09\u800c\u8a00\u662f O(1) \u7684\u901f\u5ea6\u3002\u63db\u6210\u7a0b\u5f0f\u78bc\u5927\u6982\u9577\u9019\u6a23\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http.ListenAndServeTLS(\":443\", \"server.crt\", \"server.key\", nil)<\/code><\/pre>\n\n\n\n<p>\u5230\u5f8c\u4f86\u767c\u73fe virtual directory \u5e36\u4f86\u7684\u9ebb\u7169\u5f8c\uff0cZoraxy v2 \u52a0\u5165\u4e86 sub-domain \u7684\u652f\u63f4\u3002\u70ba\u4e86\u89e3\u6c7a\u627e certificate \u7684\u554f\u984c\uff0c v2 \u7684\u505a\u6cd5\u662f\u8b93\u4f7f\u7528\u8005\u628a\u6bcf\u500b sub-domain \u90fd map \u5230\u4e00\u5f35 certificate \u4e0a\u9762\uff0c\u6240\u4ee5\u7d50\u679c\u5c31\u662f\u540c\u4e00\u500b\u57df\u540d\u4e0b\u6703\u6709\u5f88\u591a\u5f35\u8b49\u66f8\uff08\u4f8b\u5982\u8aaa a.example.com \u6703\u6709 a.example.com \u7684\u8b49\u66f8\u3001b.example.com \u6703\u6709 b.example.com \u7684\u8b49\u66f8\uff09\u3002\u9019\u6a23\u8a2d\u8a08\u7684\u597d\u8655\u662f\u4e00\u4f86\u627e certificate \u7684\u6f14\u7b97\u6cd5\u6bd4\u8f03\u7c21\u55ae\uff0c\u4e8c\u4f86\u6211\u5011\u53ef\u4ee5\u6839\u64da hello info \u7684 server name \u9032\u884c\u4e5f\u662f O(1) \u6642\u9593\u8907\u96dc\u5ea6\u7684 certificate lookup\uff0c\u4e0d\u7528\u4efb\u4f55\u5b58\u53d6 file system \u7684 loop \u4fbf\u53ef\u4ee5\u76f4\u63a5\u627e\u5230\u8b49\u66f8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if fileExists(helloInfo.ServerName+\".pem\") &amp;&amp; fileExists( helloInfo.ServerName+\".key\") {\n  \/\/Direct hit\n  pubKey = helloInfo.ServerName+\".pem\"\n  priKey = helloInfo.ServerName+\".key\"\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u5728 Zoraxy v2 \u7528\u4e86\u9019\u4e00\u5e74\u591a\u4e4b\u5f8c\u5c31\u53c8\u51fa\u73fe\u554f\u984c\u4e86\u3002coauthor \u7684\u5176\u4e2d\u4e00\u5f35\u8b49\u66f8\u88e1\u9762\u5305\u542b\u4e86\u5e7e\u500b\u4e0d\u540c\u7684 domain \u8ddf subdomain\uff0c\u4e5f\u6709\u4f7f\u7528\u8005\u7684\u8b49\u66f8\u662f\u542b wildcard \u7684\uff0c\u4e5f\u6709\u662f\u820a\u7248\u7528 Common Name \u4f86\u5b9a\u7fa9\uff08\u800c\u4e0d\u662f DNS entry\uff09\u4f86\u6a19\u8a18 host name \u7684\uff0c\u7d50\u679c\u5c31\u662f\u4e00\u5927\u5806 issue \u5c31\u51fa\u73fe\u5728 repo \u4e0a\u9762\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SNI \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<p>SNI \u7684\u539f\u7406\u5927\u6982\u5c31\u662f\u7531\u4f3a\u670d\u5668\u7aef\u8ddf\u64da TLS Hello Info \u7684 Server Name \u4f86\u81ea\u52d5\u56de\u50b3\u5408\u9069\u7684 certificate \u7d66 Client \uff08\u9019\u88e1\u56e0\u70ba Zoraxy \u662f\u4e00\u500b http proxy\uff0c\u90a3\u6211\u5c31\u4ee5\u700f\u89bd\u5668\u70ba client \u7684\u4f8b\u5b50\uff09\u3002\u5c0d\u65bc\u5f88\u76f4\u63a5\u7684\u57df\u540d\u4f8b\u5982\u8aaa v1 \u8ddf v2 \u88e1\u9762\u51fa\u73fe\u7684\uff0c\u57fa\u672c\u4e0a\u5c31\u662f\u53ea\u8981\u628a certificate map \u5230\u4e00\u500b string \u8f49 certificate \u7684\u8cc7\u6599\u7d50\u69cb\u88e1\u9762\u5c31\u53ef\u4ee5\u4e86\u3002\u4f46\u662f\u5c0d\u65bc v3 \u4e4b\u5f8c\u7684\u8907\u96dc\u6848\u4f8b\uff0c\u5247\u662f\u9700\u8981\u4e00\u4e9b\u66f4\u8907\u96dc\u7684\u908f\u8f2f\u4f86\u8655\u7406\u3002<\/p>\n\n\n\n<p>\u76f8\u4fe1\u4f86\u5f97\u6211\u90e8\u843d\u683c\u7684\u4eba\u4e0d\u662f\u5de5\u7a0b\u5e2b\u61c9\u8a72\u90fd\u662f\u6280\u8853\u5927\u4f6c\uff0c\u6240\u4ee5\u6211\u5c31\u76f4\u63a5\u628a code \u62ff\u51fa\u4f86\u8b1b\u597d\u4e86\u3002\u9019\u88e1\u662f Zoraxy v3 TLS \u89e3\u91cb\u5668\u88e1\u9762\u6700\u91cd\u8981\u7684 function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func(m * Manager) CertMatchExists(serverName string) bool {\n  for _, certCacheEntry: = range m.LoadedCerts {\n    if certCacheEntry.Cert.VerifyHostname(serverName) == nil || certCacheEntry.Cert.Issuer.CommonName == serverName {\n      return true\n    }\n  }\n  return false\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>func(m * Manager) GetCertByX509CNHostname(serverName string)(string, string) {\n  for _, certCacheEntry: = range m.LoadedCerts {\n    if certCacheEntry.Cert.VerifyHostname(serverName) == nil || certCacheEntry.Cert.Issuer.CommonName == serverName {\n      return certCacheEntry.PubKey, certCacheEntry.PriKey\n    }\n  }\n\n  return \"\", \"\"\n}<\/code><\/pre>\n\n\n\n<p>\u9019\u88e1\u6709\u5169\u500b\u5f88\u91cd\u8981\u7684\u9ede\uff0c\u5c31\u662f<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u96d6\u7136\u8aaa\u73fe\u4ee3\u7684 certificate \u4e00\u822c\u90fd\u662f\u7528 DNS \u4f5c\u70ba\u5132\u5b58 host name \u7684\u6b04\u4f4d\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u8f03\u820a\u7684 certificate \/ certificate generator \u9084\u662f\u4f7f\u7528 Common Name \uff08\u901a\u7a31 CN\uff09\u4f86\u653e host name<\/li><li>\u7531\u65bc\u5f9e x509 Certificate \u6293\u51fa\u4f86\u7684 host name \u6709\u6a5f\u6703\u662f wildcard\u3001\u751a\u81f3\u5728\u4e0a\u9762\u9084\u6703\u6709 exclusion rules\uff0c\u6240\u4ee5\u662f\u6c92\u8fa6\u6cd5\u628a\u6240\u6709\u53ef\u80fd\u6027\u90fd\u6293\u51fa\u4f86\u5efa map \u9032\u884c O(1) time complexity \u7684\u641c\u5c0b<\/li><\/ul>\n\n\n\n<p>\u6240\u4ee5\u552f\u4e00\u80fd\u505a\u4f86\u52a0\u901f O(n) \u904b\u7b97\u7684\u5c31\u53ea\u6709 in-memory \u7684\u6838\u5c0d\u8ddf caching \u4e86\uff08\u54ed\u5594<\/p>\n\n\n\n<p><s>\uff08\u5662\uff0c\u4f60\u8aaa\u9019\u5169\u500b function \u53ef\u4ee5\u6574\u5408\u505a\u4e00\u500b\uff1f O(n) + O(n) \u7684 time complexity \u90fd\u662f O(n) \u6c92\u5dee\u5566\uff0c\u800c\u4e14\u90a3\u6a23\u53c8\u4e0d\u7b26\u5408 clean code \u8a2d\u8a08\u539f\u5247\u4e86\uff09<\/s><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Zoraxy v3 \u7684\u505a\u6cd5\u7e3d\u7d50<\/h3>\n\n\n\n<p>\u6240\u4ee5 Zoraxy v3 \u4fdd\u7559\u4e86\u539f\u5148 v1 \u8ddf v2 \u7684\u505a\u6cd5\uff0c\u4e26\u5728 v1 \u8ddf v2 \u90fd\u6c92\u6709 hit \u7684\u6642\u5019\u518d\u8dd1\u4e0a\u9762\u90a3\u5169\u6bb5 function \u4f86\u6aa2\u67e5\u6709\u54ea\u5f35\u8b49\u66f8\u662f\u80fd\u7528\u7684\uff0c\u5982\u679c\u90fd\u6c92\u6709\u5c31\u6703\u6389\u5230\u53bb\u6700\u4e0b\u9762\u4f7f\u7528 fall-back certificate \u3002\u96d6\u7136\u8aaa\u9019\u6a23\u5b50 worst case \u7684 TLS certificate resolve time complexity \u6389\u5230\u4e86 O(n)\uff0c\u4f46\u662f\u4e00\u822c\u800c\u8a00\u5c0d\u65bc\u50cf\u6211\u7684\u7db2\u7ad9\u9019\u7a2e\u7c21\u55ae user case \u4e00\u822c\u90fd\u6703\u843d\u5728 O(1) \u7684\u901f\u5ea6\uff0c\u518d\u52a0\u4e0a\u4f7f\u7528\u4e86 in-memory \u7684 certificate validation \uff08\u5c31\u662f\u9810\u5148\u628a certificate \u90fd\u8f09\u5165\u5230 RAM \u88e1\u9762\uff09\uff0c\u6240\u4ee5\u5c31\u7b97\u662f O(n) \u901f\u5ea6\u9084\u7b97\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u6c34\u5e73\u3002\u5982\u679c\u4e4b\u5f8c\u9084\u662f\u89ba\u5f97\u6162\u7684\u8a71\uff0c\u9084\u53ef\u4ee5\u52a0\u5165\u57fa\u65bc sync.Map \u7684 cache \u4f86\u628a\u6700\u8fd1 resolve \u904e\u7684 hostname \u548c\u5c0d\u61c9\u7684 certificate \u585e\u9032\u53bb\uff0c\u4f46\u662f\u73fe\u5728\u4f86\u8aaa\u9019\u901f\u5ea6\u5df2\u7d93\u5920\u7528\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6211\u958b\u767c Zoraxy \u4e4b\u524d\u6211\u4e5f\u4e0d\u77e5\u9053 https \u8b49\u66f8\u9019\u6771\u897f\u5c0d\u65bc reverse proxy \u4f3a\u670d\u5668\u4f86\u8aaa\u5230\u5e95\u6709\u591a\u8907\u96dc\u3002\u4e00\u958b\u59cb\u7684\u6642\u5019\u56e0\u70ba\u6211\u90fd\u662f\u7528\u4e00\u500b\u57df\u540d\uff08domain name\uff09\uff0c\u6240\u4ee5 reverse proxy \u88e1\u9762\u4e5f\u662f\u53ea\u9700\u8981\u8655\u7406\u4e00\u500b\u4e3b\u6a5f\u540d\u7a31\uff08host name\uff09\u3002\u800c\u521d\u4ee3\u7684 Zoraxy \u5247\u662f\u76f4\u63a5\u7528 TLS hello info \u88e1\u9762\u7684 server name \u4f5c\u70ba certificate \u7684 key \u4f86\u627e\u5230\u5408\u9069\u7684 certificate \u4e26\u56de\u50b3\u7d66\u5ba2\u6236\u7aef\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\uff08time complexity\uff09\u800c\u8a00\u662f O(1) \u7684\u901f\u5ea6\u3002\u63db\u6210\u7a0b\u5f0f\u78bc\u5927\u6982\u9577\u9019\u6a23\uff1a \u5230\u5f8c\u4f86\u767c\u73fe virtual directory \u5e36\u4f86\u7684\u9ebb\u7169\u5f8c\uff0cZoraxy v2 \u52a0\u5165\u4e86 sub-domain \u7684\u652f\u63f4\u3002\u70ba\u4e86\u89e3\u6c7a\u627e certificate \u7684\u554f\u984c\uff0c v2 \u7684\u505a\u6cd5\u662f\u8b93\u4f7f\u7528\u8005\u628a\u6bcf\u500b sub-domain \u90fd map \u5230\u4e00\u5f35 certificate \u4e0a\u9762\uff0c\u6240\u4ee5\u7d50\u679c\u5c31\u662f\u540c\u4e00\u500b\u57df\u540d\u4e0b\u6703\u6709\u5f88\u591a\u5f35\u8b49\u66f8\uff08\u4f8b\u5982\u8aaa a.example.com \u6703\u6709 a.example.com \u7684\u8b49\u66f8\u3001b.example.com \u6703\u6709 b.example.com [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,1,106],"tags":[],"class_list":["post-1146","post","type-post","status-publish","format-standard","hentry","category-development-logs","category-uncategorized","category-zoraxy"],"_links":{"self":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1146"}],"version-history":[{"count":5,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1146\/revisions"}],"predecessor-version":[{"id":1152,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1146\/revisions\/1152"}],"wp:attachment":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}