diff --git a/cmd/telebit/telebit.go b/cmd/telebit/telebit.go --- a/cmd/telebit/telebit.go +++ b/cmd/telebit/telebit.go @@ -854,7 +854,6 @@ func muxAll( httpsrv.Serve(apiListener) }() fmt.Printf("Will respond to Websocket and API requests to %q\n", config.apiHostname) - mux.HandleTLS(config.apiHostname, acme, mux, "[Terminate TLS & Recurse] for "+config.apiHostname) mux.HandleTCP(config.apiHostname, telebit.HandlerFunc(func(client net.Conn) error { if dbg.Debug { fmt.Printf("[debug] Accepting API or WebSocket client %q\n", config.apiHostname) diff --git a/internal/telebit/connwrap.go b/internal/telebit/connwrap.go --- a/internal/telebit/connwrap.go +++ b/internal/telebit/connwrap.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/hex" "net" + "strings" "time" "git.rootprojects.org/root/telebit/internal/dbg" @@ -126,7 +127,44 @@ func (c *ConnWrap) Servername() string { // this will get the servername _ = c.isEncrypted() + if "" == c.servername { + c.servername = c.httpHost() + } return c.servername } +func (c *ConnWrap) httpHost() string { + c.SetDeadline(time.Now().Add(200 * time.Millisecond)) + b, _ := c.Peek(defaultPeekerSize) + defer c.SetDeadline(time.Time{}) + + if 0 == len(b) { + return "" + } + + lines := strings.Split(string(b), "\r\n") + if len(lines) < 2 { + return "" + } + + for _, line := range lines[1:] { + if "" == line { + break + } + if !strings.HasPrefix(strings.ToLower(line), "host:") { + continue + } + + host := strings.TrimSpace(line[len("host:"):]) + if "" == host { + return "" + } + if h, _, err := net.SplitHostPort(host); nil == err { + host = h + } + host = strings.Trim(host, "[]") + return strings.ToLower(host) + } + + return "" +} + // isEncrypted returns true if peeking at net.Conn reveals that it is TLS-encrypted func (c *ConnWrap) isEncrypted() bool { if nil != c.encrypted { diff --git a/internal/telebit/admin.go b/internal/telebit/admin.go --- a/internal/telebit/admin.go +++ b/internal/telebit/admin.go @@ -223,7 +223,7 @@ func upgradeWebsocket(w http.ResponseWriter, r *http.Request) { log.Println("websocket opening ", r.RemoteAddr, " ", r.Host) w.Header().Set("Content-Type", "application/json") - if "Upgrade" != r.Header.Get("Connection") && "WebSocket" != r.Header.Get("Upgrade") { + if "upgrade" != strings.ToLower(r.Header.Get("Connection")) && "websocket" != strings.ToLower(r.Header.Get("Upgrade")) { w.Write(apiNotFoundContent) return } diff --git a/internal/telebit/routemux.go b/internal/telebit/routemux.go --- a/internal/telebit/routemux.go +++ b/internal/telebit/routemux.go @@ -65,6 +65,9 @@ func (m *RouteMux) Serve(client net.Conn) error { parts := strings.Split(addr, ":") port = ":" + parts[len(parts)-1] servername = strings.Join(parts[:len(parts)-1], ":") + if detected := wconn.Servername(); "" != detected { + servername = detected + } } fmt.Println("\nAddr:", fam, servername, port)