feat: add dedicated WebSocket service for subdomain ping
All checks were successful
Docker Build and Push / build-and-push (push) Successful in 6m33s
All checks were successful
Docker Build and Push / build-and-push (push) Successful in 6m33s
This commit is contained in:
@ -6,9 +6,12 @@ import (
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"tunnel_pls/session"
|
||||
"tunnel_pls/utils"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func NewHTTPSServer() error {
|
||||
@ -62,6 +65,40 @@ func HandlerTLS(conn net.Conn) {
|
||||
}
|
||||
slug := host[0]
|
||||
|
||||
if slug == "ping" {
|
||||
req, err := http.ReadRequest(reader)
|
||||
if err != nil {
|
||||
log.Println("failed to parse HTTP request:", err)
|
||||
return
|
||||
}
|
||||
rw := &connResponseWriter{conn: conn}
|
||||
|
||||
wsConn, err := upgrader.Upgrade(rw, req, nil)
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "the client is not using the websocket protocol") {
|
||||
log.Println("Upgrade failed:", err)
|
||||
}
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
log.Println("failed to close connection:", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = wsConn.WriteMessage(websocket.TextMessage, []byte("pong"))
|
||||
if err != nil {
|
||||
log.Println("failed to write pong:", err)
|
||||
return
|
||||
}
|
||||
err = wsConn.Close()
|
||||
if err != nil {
|
||||
log.Println("websocket close failed :", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
sshSession, ok := session.Clients[slug]
|
||||
if !ok {
|
||||
conn.Write([]byte("HTTP/1.1 400 Bad Request\r\n\r\n"))
|
||||
|
||||
Reference in New Issue
Block a user