comparison host/https.luan @ 1630:b735ed134662

add nginx and ssl for host
author fffilimonov
date Fri, 10 Dec 2021 17:08:17 +0000
parents
children 0344a535b1db
comparison
equal deleted inserted replaced
1629:124887e02dab 1630:b735ed134662
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local load_file = Luan.load_file or error()
4 local ipairs = Luan.ipairs or error()
5 local Boot = require "luan:Boot.luan"
6 local Io = require "luan:Io.luan"
7 local ip = Io.ip or error()
8 local uri = Io.uri or error()
9 local output_of = Io.output_of or error()
10 local String = require "luan:String.luan"
11 local regex_quote = String.regex_quote or error()
12 local matches = String.matches or error()
13 local Http = require "luan:http/Http.luan"
14 local Hosted = require "luan:host/Hosted.luan"
15 local Logging = require "luan:logging/Logging.luan"
16 local logger = Logging.logger "https"
17
18
19 local local_cer = [[
20 -----BEGIN CERTIFICATE-----
21 MIIGGDCCBQCgAwIBAgISA2I3DK1t/znI/s4eJX23gWOPMA0GCSqGSIb3DQEBCwUA
22 MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD
23 ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMzAeFw0xODA2MTEwODQ1MDJaFw0x
24 ODA5MDkwODQ1MDJaMCIxIDAeBgNVBAMTF2h0dHBzLnRlc3QubHVhbmhvc3QuY29t
25 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvlvCVOGcE/79DJFCh45W
26 pmqfwljQMYWDNbyNb6zND4QzG80RPHgWNpoDyVUXG8eLn/rDazi0ldwkFH5usrO+
27 m3IC5lT/Y/wIr+wT9dQv0l0IJKdkA+cnpOB0z8BU0nBnw6TKhhEcZrPsMRzjbFTJ
28 BLeUEf855Y+t/8TeSykBbV9ufiisLnR2/5gvOBW19LHkcxQYzKbmHPVh3sD2wyK8
29 gg8B3jgqHJh+m/amOfaxZnOEtEd/TGd6QxCbVvh1jewneBoM9niVOScGUk9JuVol
30 tEgToV7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EkROb3N6
80 KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
81 -----END CERTIFICATE-----
82 ]]
83
84 local local_key = [[
85 -----BEGIN RSA PRIVATE KEY-----
86 MIIEpAIBAAKCAQEAvlvCVOGcE/79DJFCh45WpmqfwljQMYWDNbyNb6zND4QzG80R
87 PHgWNpoDyVUXG8eLn/rDazi0ldwkFH5usrO+m3IC5lT/Y/wIr+wT9dQv0l0IJKdk
88 A+cnpOB0z8BU0nBnw6TKhhEcZrPsMRzjbFTJBLeUEf855Y+t/8TeSykBbV9ufiis
89 LnR2/5gvOBW19LHkcxQYzKbmHPVh3sD2wyK8gg8B3jgqHJh+m/amOfaxZnOEtEd/
90 TGd6QxCbVvh1jewneBoM9niVOScGUk9JuVoltEgToV7vKYpwNRObbN7pitdwudbB
91 3gieK1/BoxHNudjArur4Vapkx/+GSX1aHIsOwQIDAQABAoIBAFRgJcYr7luqpY2B
92 gYTGYcAH+OJTLY0Vye/ysJp0dLxQ87p57g7oh6gSImWGUuTcURZLN316OAlI97Eh
93 zX1mULFkGsbvm/g8ibk4oixPrhbHZ8mfbKSSPC9H0l8XuEFC06uTirVcNkOLssCe
94 NmwZNLTjjj1f7NDFG/IH2CRme5x3cRdfVDQ1tYiK8lHxoHRj2uN3P1gsJIlDYR1a
95 A3TqbLDFYGsvoMPtgkcK/wRpBQCX0FL2IDrigGF33Eb5BiqFHx7KXO9Qjsyb6Y6z
96 B/OusjQ6LA//Oq2aizdoDD/op5GRAG7R5wB/8SrjlkME4q8psLRYalcgPwBStQjL
97 9tWVHGUCgYEA7xK3xB29TwYoTRY9RwtTILnYXdKM5tY0npC9jDeL96KZeibd0l/S
98 2jDqn32O8WUGYHticWYFuUS8evcGtsaBmZPUFT5ndp4DmfahttyS+CoF/NDPlZGq
99 cpl92ZDDCQrY68zSY9UhHY/JaslOVv963UfrL30Wok1Bg0A0sqDVgY8CgYEAy9YS
100 KjnspIWgC8Fq2kkMAIdBXQVnaRP5l7gyRmVuVwDXS+iG/0DGZfmv1g9S17lUz3R0
101 rpKp4iorboJ2A4Wiv0dgar7CmzLMcMSUREX4L7DoRsEXUuxsCOf0F2Vt3baVdo3N
102 PtfV8QzpcRAlN4Fap7s0ErNP21OE/ZXocwQ1oq8CgYEA5+1GrVEUiAc3LaF3jOZl
103 nIfuaQaLfp0rqvBLki2I8ZGiRqhR5XbkfJub4WHTPEd/ajYIiG4q+1K9bqUatFHb
104 BHwu8PT7Nk2QvNpQg690PJ+38003NKh9rHNPbbNMIgeN7SNkr0jhuWX2RkxIXYm3
105 TdgpRoaZYJaGCahN90SkG+0CgYBWM1J9Pv+/V68mKYaJh2im9IwEzZs8ybC8o63H
106 LW/rJTTnKg+k9HyydVQR/2r2Ra8DCGmrxeRH+7NgSZWkyafYolO48LEVtvbUHZ3h
107 /YEGkha4jUHS1J6faZBFMKS0pVkxyKfqkpYsGVzDbqN+hJqU4ksUOXZk9z3/i5zA
108 vx/7iwKBgQCELMiakqU/tw3U+VwTRKoWYgCxg9SyG3UWpyuZSguK5k+4C+BvCaXN
109 PT7RjA5Gb8oQKUd9kcn1x/ljyTw3mqn5AT7TqfJkG/sMh+Fkl+JMpWFVfmexWihG
110 eW2FbldbFg8IUzTPHAOFmO7+9h5oN0pBWwcml2D1YUqfGOyezisuAA==
111 -----END RSA PRIVATE KEY-----
112 ]]
113
114
115 function Hosted.set_https(is_https)
116 local domain = Http.domain
117 local dir = uri("site:").parent()
118 local nginx_file = dir.child("nginx.ssl.conf")
119 local key_file = dir.child(domain..".key")
120 local local_cer_file = dir.child("fullchain.cer")
121 local local_ca_file = dir.child("ca.cer")
122 local top_dir = uri("file:.").canonical().to_string()
123 local changed = false
124 if is_https then -- https
125 if not key_file.exists() then
126 local is_local = ip(domain) == "127.0.0.1"
127 logger.info("is_local "..is_local)
128 if is_local then
129 key_file.write(local_key)
130 local_cer_file.write(local_cer)
131 else
132 local cmd = [[
133 ./acme.sh --debug --issue -d "]]..domain..[[" --stateless --cert-home "]]..top_dir..[[/sites" --config-home "]]..top_dir..[[/local/letsencrypt/config";
134 ]]
135 local s = uri("bash:"..cmd).read_text()
136 logger.info("issue certificate "..s)
137 end
138 if key_file.exists() and local_cer_file.exists() then
139 changed = true
140 local conf = load_file "file:startup/nginx.ssl.conf.luan"
141 local nginx = output_of( function() conf(top_dir,domain) end )
142 nginx_file.write(nginx)
143 end
144 end
145 else -- http
146 if key_file.exists() or nginx_file.exists() then
147 changed = true
148 nginx_file.delete()
149 local_cer_file.delete()
150 local_ca_file.delete()
151 local ptn = [[^]]..regex_quote(domain)..[[\.]]
152 for _, file in ipairs(dir.children()) do
153 if matches(file.name(),ptn) then
154 file.delete()
155 end
156 end
157 end
158 end
159 if changed then
160 local cmd = [[
161 sudo $(which nginx) -t -c "]]..top_dir..[[/local/nginx.conf" && sudo $(which nginx) -s reload;
162 ]]
163 local s = uri("bash:"..cmd).read_text()
164 logger.info("reload_nginx "..s)
165 end
166 --logger.info "done"
167 end
168 Hosted.set_https = Boot.no_security(Hosted.set_https)