mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-03-05 04:10:16 +01:00
🎨 Fix network serving TLS on mobile devices (#17119)
* Until now, the TLS would only work via the fixed port proxy, which isn't used on mobile devices. * Move the logic for the multiplexer out of the fixed port logic * Use the newly moved multiplexer logic for the regular server as well, whenever the fixed port and the server port match.
This commit is contained in:
parent
7912100136
commit
0cc061dec8
3 changed files with 71 additions and 32 deletions
52
kernel/util/cmux.go
Normal file
52
kernel/util/cmux.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/soheilhy/cmux"
|
||||
)
|
||||
|
||||
func ServeMultiplexed(ln net.Listener, handler http.Handler, certPath, keyPath string, httpServer *http.Server) error {
|
||||
m := cmux.New(ln)
|
||||
|
||||
tlsL := m.Match(cmux.TLS())
|
||||
httpL := m.Match(cmux.Any())
|
||||
|
||||
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
|
||||
if err != nil {
|
||||
logging.LogErrorf("failed to load TLS cert for multiplexing: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
tlsConfig := &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
}
|
||||
|
||||
tlsListener := tls.NewListener(tlsL, tlsConfig)
|
||||
|
||||
if httpServer == nil {
|
||||
httpServer = &http.Server{Handler: handler}
|
||||
} else {
|
||||
httpServer.Handler = handler
|
||||
}
|
||||
|
||||
httpsServer := &http.Server{Handler: handler}
|
||||
|
||||
go func() {
|
||||
if serveErr := httpServer.Serve(httpL); serveErr != nil && serveErr != cmux.ErrListenerClosed && !errors.Is(serveErr, http.ErrServerClosed) {
|
||||
logging.LogErrorf("multiplexed HTTP server error: %s", serveErr)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if serveErr := httpsServer.Serve(tlsListener); serveErr != nil && serveErr != cmux.ErrListenerClosed && !errors.Is(serveErr, http.ErrServerClosed) {
|
||||
logging.LogErrorf("multiplexed HTTPS server error: %s", serveErr)
|
||||
}
|
||||
}()
|
||||
|
||||
return m.Serve()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue