view host/https.luan @ 1636:8efb10bf569a

remove duplicate files
author fffilimonov
date Wed, 15 Dec 2021 08:26:29 +0000
parents 0344a535b1db
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local load_file = Luan.load_file or error()
local ipairs = Luan.ipairs or error()
local Boot = require "luan:Boot.luan"
local Io = require "luan:Io.luan"
local ip = Io.ip or error()
local uri = Io.uri or error()
local output_of = Io.output_of or error()
local String = require "luan:String.luan"
local regex_quote = String.regex_quote or error()
local matches = String.matches or error()
local Http = require "luan:http/Http.luan"
local Hosted = require "luan:host/Hosted.luan"
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "https"


local local_cer = [[
-----BEGIN CERTIFICATE-----
MIIGGDCCBQCgAwIBAgISA2I3DK1t/znI/s4eJX23gWOPMA0GCSqGSIb3DQEBCwUA
MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD
ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMzAeFw0xODA2MTEwODQ1MDJaFw0x
ODA5MDkwODQ1MDJaMCIxIDAeBgNVBAMTF2h0dHBzLnRlc3QubHVhbmhvc3QuY29t
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvlvCVOGcE/79DJFCh45W
pmqfwljQMYWDNbyNb6zND4QzG80RPHgWNpoDyVUXG8eLn/rDazi0ldwkFH5usrO+
m3IC5lT/Y/wIr+wT9dQv0l0IJKdkA+cnpOB0z8BU0nBnw6TKhhEcZrPsMRzjbFTJ
BLeUEf855Y+t/8TeSykBbV9ufiisLnR2/5gvOBW19LHkcxQYzKbmHPVh3sD2wyK8
gg8B3jgqHJh+m/amOfaxZnOEtEd/TGd6QxCbVvh1jewneBoM9niVOScGUk9JuVol
tEgToV7vKYpwNRObbN7pitdwudbB3gieK1/BoxHNudjArur4Vapkx/+GSX1aHIsO
wQIDAQABo4IDHjCCAxowDgYDVR0PAQH/BAQDAgWg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NpQg690PJ+38003NKh9rHNPbbNMIgeN7SNkr0jhuWX2RkxIXYm3
TdgpRoaZYJaGCahN90SkG+0CgYBWM1J9Pv+/V68mKYaJh2im9IwEzZs8ybC8o63H
LW/rJTTnKg+k9HyydVQR/2r2Ra8DCGmrxeRH+7NgSZWkyafYolO48LEVtvbUHZ3h
/YEGkha4jUHS1J6faZBFMKS0pVkxyKfqkpYsGVzDbqN+hJqU4ksUOXZk9z3/i5zA
vx/7iwKBgQCELMiakqU/tw3U+VwTRKoWYgCxg9SyG3UWpyuZSguK5k+4C+BvCaXN
PT7RjA5Gb8oQKUd9kcn1x/ljyTw3mqn5AT7TqfJkG/sMh+Fkl+JMpWFVfmexWihG
eW2FbldbFg8IUzTPHAOFmO7+9h5oN0pBWwcml2D1YUqfGOyezisuAA==
-----END RSA PRIVATE KEY-----
]]


function Hosted.set_https(is_https)
	local domain = Http.domain
	local dir = uri("site:").parent()
	local nginx_file = dir.child("nginx.ssl.conf")
	local key_file = dir.child(domain..".key")
	local local_cer_file = dir.child("fullchain.cer")
	local local_ca_file = dir.child("ca.cer")
	local top_dir = uri("file:.").canonical().to_string()
	local changed = false
	if is_https then  -- https
		if not key_file.exists() then
			local is_local = ip(domain) == "127.0.0.1"
			logger.info("is_local "..is_local)
			if is_local then
				key_file.write(local_key)
				local_cer_file.write(local_cer)
			else
				local cmd = [[
./acme.sh --debug --issue -d "]]..domain..[[" --stateless --cert-home "]]..top_dir..[[/sites" --config-home "]]..top_dir..[[/local/letsencrypt/config";
]]
				local s = uri("bash:"..cmd).read_text()
				logger.info("issue certificate "..s)
			end
			if key_file.exists() and local_cer_file.exists() then
				changed = true
				local conf = load_file "file:startup/nginx/nginx.ssl.conf.luan"
				local nginx = output_of( function() conf(top_dir,domain) end )
				nginx_file.write(nginx)
			end
		end
	else  -- http
		if key_file.exists() or nginx_file.exists() then
			changed = true
			nginx_file.delete()
			local_cer_file.delete()
			local_ca_file.delete()
			local ptn = [[^]]..regex_quote(domain)..[[\.]]
			for _, file in ipairs(dir.children()) do
				if matches(file.name(),ptn) then
					file.delete()
				end
			end
		end
	end
	if changed then
		local cmd = [[
sudo $(which nginx) -t -c "]]..top_dir..[[/local/nginx.conf" && sudo $(which nginx) -s reload;
]]
		local s = uri("bash:"..cmd).read_text()
		logger.info("reload_nginx "..s)
	end
	--logger.info "done"
end
Hosted.set_https = Boot.no_security(Hosted.set_https)