From 59cf86d7867ad5d8a5457e7b0336e9458d3b5396 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Wed, 24 Jan 2024 10:45:49 +0100 Subject: [PATCH] Add "welcome" endpoint to proxy. --- proxy/proxy_server.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/proxy/proxy_server.go b/proxy/proxy_server.go index 4cd1dc9..d2ef29a 100644 --- a/proxy/proxy_server.go +++ b/proxy/proxy_server.go @@ -26,6 +26,7 @@ import ( "crypto/rand" "encoding/json" "fmt" + "io" "log" "net" "net/http" @@ -82,8 +83,9 @@ var ( ) type ProxyServer struct { - version string - country string + version string + country string + welcomeMessage string url string mcu signaling.Mcu @@ -161,9 +163,20 @@ func NewProxyServer(r *mux.Router, version string, config *goconf.ConfigFile) (* log.Printf("Not sending country information") } + welcome := map[string]string{ + "nextcloud-spreed-signaling-proxy": "Welcome", + "version": version, + } + welcomeMessage, err := json.Marshal(welcome) + if err != nil { + // Should never happen. + return nil, err + } + result := &ProxyServer{ - version: version, - country: country, + version: version, + country: country, + welcomeMessage: string(welcomeMessage) + "\n", shutdownChannel: make(chan struct{}), @@ -197,6 +210,7 @@ func NewProxyServer(r *mux.Router, version string, config *goconf.ConfigFile) (* } } + r.HandleFunc("/welcome", result.setCommonHeaders(result.welcomeHandler)).Methods("GET") r.HandleFunc("/proxy", result.setCommonHeaders(result.proxyHandler)).Methods("GET") r.HandleFunc("/stats", result.setCommonHeaders(result.validateStatsRequest(result.statsHandler))).Methods("GET") r.HandleFunc("/metrics", result.setCommonHeaders(result.validateStatsRequest(result.metricsHandler))).Methods("GET") @@ -410,6 +424,12 @@ func getRealUserIP(r *http.Request) string { return r.RemoteAddr } +func (s *ProxyServer) welcomeHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(http.StatusOK) + io.WriteString(w, s.welcomeMessage) // nolint +} + func (s *ProxyServer) proxyHandler(w http.ResponseWriter, r *http.Request) { addr := getRealUserIP(r) conn, err := s.upgrader.Upgrade(w, r, nil)