🎨 Implement HTTPS network serving (#16912)

* Add use TLS for network serving configuration option

* kernel: Implement TLS certificate generation

* kernel: server: Use https for fixed port proxy when needed

* Allow exporting the CA Certificate file

* Implement import and export of CA Certs
This commit is contained in:
Davide Garberi 2026-01-27 05:59:11 +01:00 committed by GitHub
parent e7621b7a5f
commit 43ea6757d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 759 additions and 10 deletions

View file

@ -17,6 +17,7 @@
package proxy
import (
"crypto/tls"
"net/http"
"net/http/httputil"
@ -24,7 +25,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func InitFixedPortService(host string) {
func InitFixedPortService(host string, useTLS bool, certPath, keyPath string) {
if util.FixedPort != util.ServerPort {
if util.IsPortOpen(util.FixedPort) {
return
@ -32,9 +33,23 @@ func InitFixedPortService(host string) {
// 启动一个固定 6806 端口的反向代理服务器,这样浏览器扩展才能直接使用 127.0.0.1:6806不用配置端口
proxy := httputil.NewSingleHostReverseProxy(util.ServerURL)
logging.LogInfof("fixed port service [%s:%s] is running", host, util.FixedPort)
if proxyErr := http.ListenAndServe(host+":"+util.FixedPort, proxy); nil != proxyErr {
logging.LogWarnf("boot fixed port service [%s] failed: %s", util.ServerURL, proxyErr)
if useTLS {
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
if useTLS {
logging.LogInfof("fixed port service [%s:%s] is running with TLS", host, util.FixedPort)
if proxyErr := http.ListenAndServeTLS(host+":"+util.FixedPort, certPath, keyPath, proxy); nil != proxyErr {
logging.LogWarnf("boot fixed port service [%s] failed: %s", util.ServerURL, proxyErr)
}
} else {
logging.LogInfof("fixed port service [%s:%s] is running", host, util.FixedPort)
if proxyErr := http.ListenAndServe(host+":"+util.FixedPort, proxy); nil != proxyErr {
logging.LogWarnf("boot fixed port service [%s] failed: %s", util.ServerURL, proxyErr)
}
}
logging.LogInfof("fixed port service [%s:%s] is stopped", host, util.FixedPort)
}

View file

@ -210,14 +210,32 @@ func Serve(fastMode bool, cookieKey string) {
if !fastMode {
rewritePortJSON(pid, port)
}
logging.LogInfof("kernel [pid=%s] http server [%s] is booting", pid, host+":"+port)
// Prepare TLS if enabled
var certPath, keyPath string
useTLS := model.Conf.System.NetworkServeTLS && model.Conf.System.NetworkServe
if useTLS {
// Ensure TLS certificates exist (proxy will use them directly)
var tlsErr error
certPath, keyPath, tlsErr = util.GetOrCreateTLSCert()
if tlsErr != nil {
logging.LogErrorf("failed to get TLS certificates: %s", tlsErr)
if !fastMode {
os.Exit(logging.ExitCodeUnavailablePort)
}
return
}
logging.LogInfof("kernel [pid=%s] http server [%s] is booting (TLS will be enabled on fixed port proxy)", pid, host+":"+port)
} else {
logging.LogInfof("kernel [pid=%s] http server [%s] is booting", pid, host+":"+port)
}
util.HttpServing = true
go util.HookUILoaded()
go func() {
time.Sleep(1 * time.Second)
go proxy.InitFixedPortService(host)
go proxy.InitFixedPortService(host, useTLS, certPath, keyPath)
go proxy.InitPublishService()
// 反代服务器启动失败不影响核心服务器启动
}()