Skip to content

Commit d302bc0

Browse files
authored
feat: Add support for Mux SSL mode and update related settings (#11509)
* feat: Add support for Mux SSL mode and update related settings - Introduced a new SSL mode "Mux" in the settings, allowing for HTTP to HTTPS redirection. - Updated the `SSL` field in the `SettingUpdate` struct to include "Mux" as a valid option. - Modified the server logic to handle Mux connections, including certificate management and HTTP redirection. - Updated frontend components to reflect the new SSL options and improved user guidance in multiple languages. * fix: Update HTTPS related messages for improved clarity and security guidance in multiple languages
1 parent c6df20e commit d302bc0

File tree

17 files changed

+92
-26
lines changed

17 files changed

+92
-26
lines changed

core/app/dto/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type SettingUpdate struct {
6464
type SSLUpdate struct {
6565
SSLType string `json:"sslType" validate:"required,oneof=self select import import-paste import-local"`
6666
Domain string `json:"domain"`
67-
SSL string `json:"ssl" validate:"required,oneof=Enable Disable"`
67+
SSL string `json:"ssl" validate:"required,oneof=Enable Disable Mux"`
6868
Cert string `json:"cert"`
6969
Key string `json:"key"`
7070
SSLID uint `json:"sslID"`

core/constant/status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121

2222
StatusEnable = "Enable"
2323
StatusDisable = "Disable"
24+
StatusMux = "Mux"
2425

2526
StatusInstalling = "Installing"
2627
StatusNormal = "Normal"

core/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ require (
8181
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
8282
github.com/sagikazarmark/locafero v0.4.0 // indirect
8383
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
84+
github.com/soheilhy/cmux v0.1.5 // indirect
8485
github.com/sourcegraph/conc v0.3.0 // indirect
8586
github.com/spf13/afero v1.11.0 // indirect
8687
github.com/spf13/cast v1.6.0 // indirect

core/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
250250
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
251251
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
252252
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
253+
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
254+
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
253255
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
254256
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
255257
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
@@ -342,6 +344,7 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
342344
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
343345
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
344346
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
347+
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
345348
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
346349
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
347350
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
@@ -363,6 +366,7 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
363366
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
364367
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
365368
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
369+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
366370
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
367371
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
368372
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

core/server/server.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import (
44
"crypto/tls"
55
"encoding/gob"
66
"fmt"
7+
"net"
8+
"net/http"
9+
"os"
10+
"path"
11+
712
"github.com/1Panel-dev/1Panel/core/init/auth"
813
"github.com/1Panel-dev/1Panel/core/init/db"
914
"github.com/1Panel-dev/1Panel/core/init/geo"
@@ -12,10 +17,7 @@ import (
1217
"github.com/1Panel-dev/1Panel/core/init/proxy"
1318
"github.com/1Panel-dev/1Panel/core/init/run"
1419
"github.com/gin-gonic/gin"
15-
"net"
16-
"net/http"
17-
"os"
18-
"path"
20+
"github.com/soheilhy/cmux"
1921

2022
"github.com/1Panel-dev/1Panel/core/constant"
2123
"github.com/1Panel-dev/1Panel/core/global"
@@ -99,10 +101,64 @@ func Start() {
99101
if err := server.ServeTLS(tcpKeepAliveListener{ln.(*net.TCPListener)}, "", ""); err != nil {
100102
panic(err)
101103
}
104+
return
105+
} else if global.CONF.Conn.SSL == constant.StatusMux {
106+
certPath := path.Join(global.CONF.Base.InstallDir, "1panel/secret/server.crt")
107+
keyPath := path.Join(global.CONF.Base.InstallDir, "1panel/secret/server.key")
108+
certificate, err := os.ReadFile(certPath)
109+
if err != nil {
110+
panic(err)
111+
}
112+
key, err := os.ReadFile(keyPath)
113+
if err != nil {
114+
panic(err)
115+
}
116+
cert, err := tls.X509KeyPair(certificate, key)
117+
if err != nil {
118+
panic(err)
119+
}
120+
constant.CertStore.Store(&cert)
121+
122+
server.TLSConfig = &tls.Config{
123+
GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
124+
return constant.CertStore.Load().(*tls.Certificate), nil
125+
},
126+
}
127+
128+
global.LOG.Infof("listen at mux (http/https)://%s:%s [%s]", global.CONF.Conn.BindAddress, global.CONF.Conn.Port, tcpItem)
129+
130+
m := cmux.New(ln)
131+
132+
httpsL := m.Match(cmux.TLS())
133+
httpL := m.Match(cmux.Any())
134+
135+
go func() {
136+
if err := server.Serve(tls.NewListener(httpsL, server.TLSConfig)); err != nil {
137+
global.LOG.Errorf("HTTPS Serve Error: %v", err)
138+
}
139+
}()
140+
141+
go func() {
142+
redirectServer := &http.Server{
143+
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
144+
target := "https://" + r.Host + r.RequestURI
145+
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
146+
}),
147+
}
148+
if err := redirectServer.Serve(httpL); err != nil {
149+
global.LOG.Errorf("HTTP Redirect Serve Error: %v", err)
150+
}
151+
}()
152+
153+
if err := m.Serve(); err != nil {
154+
panic(err)
155+
}
156+
return
102157
} else {
103158
global.LOG.Infof("listen at http://%s:%s [%s]", global.CONF.Conn.BindAddress, global.CONF.Conn.Port, tcpItem)
104159
if err := server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}); err != nil {
105160
panic(err)
106161
}
162+
return
107163
}
108164
}

frontend/src/lang/modules/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ const message = {
19951995
error444: 'Connection Closed',
19961996
error500: 'Internal Server Error',
19971997

1998-
https: 'Setting up HTTPS protocol access for the panel can enhance the security of panel access.',
1998+
https: 'Setting up HTTPS for the panel improves access security.\nStrict mode blocks non-HTTPS traffic from reaching the panel.\nMux mode redirects HTTP to HTTPS, but may reduce security.',
19991999
certType: 'Certificate type',
20002000
selfSigned: 'Self signed',
20012001
selfSignedHelper: `Browsers may not trust self-signed certificates and may display security warnings.`,

frontend/src/lang/modules/es-es.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,7 @@ const message = {
20092009
error416: 'Rango no satisfactorio',
20102010
error444: 'Conexión cerrada',
20112011
error500: 'Error interno del servidor',
2012-
https: 'Configurar acceso al panel mediante protocolo HTTPS puede mejorar la seguridad del acceso.',
2012+
https: 'Configurar HTTPS en el panel mejora la seguridad de acceso.\nEn modo Strict, el tráfico sin HTTPS no puede conectarse al panel.\nEl modo Mux redirige HTTP a HTTPS, pero puede reducir la seguridad.',
20132013
certType: 'Tipo de certificado',
20142014
selfSigned: 'Autofirmado',
20152015
selfSignedHelper:

frontend/src/lang/modules/ja.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,7 @@ const message = {
19211921
error444: '接続が閉じた',
19221922
error500: 'サーバーエラー',
19231923

1924-
https: 'パネル用のHTTPSプロトコルアクセスをセットアップすると、パネルアクセスのセキュリティが強化されます。',
1924+
https: 'パネルに HTTPS を設定するとアクセスの安全性が向上します。\nStrict モードでは HTTPS 以外の通信はパネルに接続できません。\nMux モードは HTTP を HTTPS にリダイレクトしますが、安全性が低下する可能性があります。',
19251925
certType: '証明書の種類',
19261926
selfSigned: '自己署名',
19271927
selfSignedHelper: `ブラウザは、自己署名の証明書を信頼していない場合があり、セキュリティ警告を表示する場合があります。`,

frontend/src/lang/modules/ko.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ const message = {
18901890
error444: '연결 닫힘',
18911891
error500: '서버 오류',
18921892

1893-
https: '패널의 HTTPS 프로토콜 접근 설정은 패널 접근 보안을 강화할 수 있습니다.',
1893+
https: '패널에 HTTPS를 설정하면 접근 보안이 향상됩니다.\nStrict 모드에서는 HTTPS가 아닌 트래픽이 패널에 연결할 수 없습니다.\nMux 모드는 HTTP를 HTTPS로 리다이렉트하지만 보안이 다소 낮아질 수 있습니다.',
18941894
certType: '인증서 유형',
18951895
selfSigned: '자가 서명',
18961896
selfSignedHelper: '자가 서명 인증서는 브라우저에서 신뢰하지 않을 수 있으며 보안 경고가 표시될 수 있습니다.',

frontend/src/lang/modules/ms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ const message = {
19771977
error444: 'Sambungan ditutup',
19781978
error500: 'Ralat Pelayan',
19791979

1980-
https: 'Menetapkan protokol akses HTTPS untuk panel boleh meningkatkan keselamatan akses panel.',
1980+
https: 'Menetapkan HTTPS untuk panel meningkatkan keselamatan akses.\nDalam mod Strict, trafik bukan HTTPS tidak boleh menyambung ke panel.\nMod Mux mengalih hala HTTP ke HTTPS, tetapi mungkin mengurangkan keselamatan.',
19811981
certType: 'Jenis sijil',
19821982
selfSigned: 'Diterbitkan sendiri',
19831983
selfSignedHelper:

0 commit comments

Comments
 (0)