Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Local HTTPS + HTTP/2 support https://github.com/siyuan-note/siyuan/issues/17822

Daniel413a0a4adf8a
diff --git a/app/electron/main.js b/app/electron/main.js
index 306b6948c6..e42ded1314 100644
--- a/app/electron/main.js
+++ b/app/electron/main.js
@@ -20,6 +20,7 @@ const {
BrowserWindow,
Notification,
shell,
+ session,
Menu,
MenuItem,
screen,
@@ -285,7 +286,7 @@ const exitApp = (port, errorWindowId) => {
}
};
-const localServer = "http://127.0.0.1";
+const localServer = "https://127.0.0.1";
const getServer = (port = kernelPort) => {
return localServer + ":" + port;
@@ -734,6 +735,15 @@ const initKernel = (workspace, port, lang) => {
};
app.whenReady().then(() => {
+ // Trust self-signed TLS certificates for local HTTPS server
+ session.defaultSession.setCertificateVerifyProc((request, callback) => {
+ if (request.hostname === "127.0.0.1" || request.hostname === "localhost") {
+ callback(0); // VERIFY_OK
+ } else {
+ callback(-3); // default Chromium handling
+ }
+ });
+
const resetTrayMenu = (tray, lang, mainWindow) => {
if (!mainWindow || mainWindow.isDestroyed()) {
return;
diff --git a/kernel/server/proxy/fixedport.go b/kernel/server/proxy/fixedport.go
index ca36df053b..1929a359d4 100644
--- a/kernel/server/proxy/fixedport.go
+++ b/kernel/server/proxy/fixedport.go
@@ -28,7 +28,7 @@ import (
"github.com/soheilhy/cmux"
)
-func InitFixedPortService(host string, useTLS bool, certPath, keyPath string) {
+func InitFixedPortService(host string, certPath, keyPath string) {
if util.FixedPort != util.ServerPort {
if util.IsPortOpen(util.FixedPort) {
return
@@ -38,17 +38,16 @@ func InitFixedPortService(host string, useTLS bool, certPath, keyPath string) {
// 启动一个固定 6806 端口的反向代理服务器,这样浏览器扩展才能直接使用 127.0.0.1:6806,不用配置端口
proxy := httputil.NewSingleHostReverseProxy(util.ServerURL)
+ proxy.Transport = &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ }
- if useTLS {
- proxy.Transport = &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- }
-
+ if "" != certPath {
logging.LogInfof("fixed port service [%s] is running (HTTP/HTTPS dual mode)", addr)
- ln, err := net.Listen("tcp", addr)
- if err != nil {
- logging.LogWarnf("boot fixed port service [%s] failed: %s", addr, err)
+ ln, listenErr := net.Listen("tcp", addr)
+ if listenErr != nil {
+ logging.LogWarnf("boot fixed port service [%s] failed: %s", addr, listenErr)
return
}
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index 34c0d8109c..ce4a451869 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -135,7 +135,6 @@ var (
func Serve(fastMode bool, cookieKey string) {
gin.SetMode(gin.ReleaseMode)
ginServer := gin.New()
- ginServer.UseH2C = true
ginServer.MaxMultipartMemory = 1024 * 1024 * 32 // 插入较大的资源文件时内存占用较大 https://github.com/siyuan-note/siyuan/issues/5023
ginServer.Use(
model.ControlConcurrency, // 请求串行化 Concurrency control when requesting the kernel API https://github.com/siyuan-note/siyuan/issues/9939
@@ -205,7 +204,18 @@ func Serve(fastMode bool, cookieKey string) {
model.Conf.ServerAddrs = util.GetServerAddrs()
model.Conf.Save()
- util.ServerURL, err = url.Parse("http://127.0.0.1:" + port)
+ // Generate TLS certificates for local HTTPS + HTTP/2 support
+ certPath, keyPath, certErr := util.GetOrCreateTLSCert()
+ if certErr != nil {
+ logging.LogWarnf("failed to get TLS certificates, local HTTPS/HTTP2 unavailable: %s", certErr)
+ certPath = ""
+ }
+
+ if "" != certPath {
+ util.ServerURL, err = url.Parse("https://127.0.0.1:" + port)
+ } else {
+ util.ServerURL, err = url.Parse("http://127.0.0.1:" + port)
+ }
if err != nil {
logging.LogErrorf("parse server url failed: %s", err)
}
@@ -215,21 +225,11 @@ func Serve(fastMode bool, cookieKey string) {
rewritePortJSON(pid, 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 if "" != certPath {
+ logging.LogInfof("kernel [pid=%s] http server [%s] is booting (local HTTPS + HTTP/2 enabled)", pid, host+":"+port)
} else {
logging.LogInfof("kernel [pid=%s] http server [%s] is booting", pid, host+":"+port)
}
@@ -239,21 +239,17 @@ func Serve(fastMode bool, cookieKey string) {
go func() {
time.Sleep(1 * time.Second)
- go proxy.InitFixedPortService(host, useTLS, certPath, keyPath)
+ go proxy.InitFixedPortService(host, certPath, keyPath)
go proxy.InitPublishService()
// 反代服务器启动失败不影响核心服务器启动
}()
httpHandler := ginServer.Handler()
- p := &http.Protocols{}
- p.SetHTTP1(true)
- p.SetUnencryptedHTTP2(true)
util.HttpServer = &http.Server{
- Handler: httpHandler,
- Protocols: p,
+ Handler: httpHandler,
}
- if useTLS && (util.FixedPort == util.ServerPort || util.IsPortOpen(util.FixedPort)) {
+ if "" != certPath {
if err = util.ServeMultiplexed(ln, httpHandler, certPath, keyPath, util.HttpServer); err != nil {
if errors.Is(err, http.ErrServerClosed) || errors.Is(err, cmux.ErrListenerClosed) {
return