From 07da0a2b7815c125134398b486f5b00b7ba8e7ca Mon Sep 17 00:00:00 2001 From: Kostas Drakontidis Date: Fri, 25 Jul 2025 16:20:54 +0300 Subject: [PATCH] Fixed location header bug & added graceful quit --- client/backend/peer.go | 74 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/client/backend/peer.go b/client/backend/peer.go index 46f0cfd..f7c22c3 100644 --- a/client/backend/peer.go +++ b/client/backend/peer.go @@ -6,14 +6,18 @@ package main import ( + "bufio" "bytes" "encoding/gob" "encoding/json" "fmt" "io" "net/http" + "net/url" "os" + "os/signal" "sync" + "syscall" "github.com/go-yaml/yaml" "github.com/google/uuid" @@ -24,6 +28,41 @@ import ( var config_t Config var http_port string +/* + * This function gets: + * id: uuid + * p2p: pointer to the peer connection + * conn: pointer to the websocket connection + * Gracefully quits the program by closing connections. + */ +func quit(id uuid.UUID, p2p *webrtc.PeerConnection, conn *websocket.Conn) { + if p2p != nil { + p2p_close_error := p2p.Close() + if p2p_close_error != nil { + fmt.Fprintf(os.Stderr, config_t.Messages.WEBRTC.CLOSE.ERROR, p2p_close_error) + } else { + fmt.Print(config_t.Messages.WEBRTC.CLOSE.SUCCESS) + } + } + + ws_close_msg := websocket.FormatCloseMessage(1000, id.String()) + ws_close_msg_error := conn.WriteMessage(websocket.CloseMessage, ws_close_msg) + if ws_close_msg_error != nil { + fmt.Fprintf(os.Stderr, config_t.Messages.WS.CLOSE_MESSAGE.ERROR, ws_close_msg_error) + } else { + fmt.Print(config_t.Messages.WS.CLOSE_MESSAGE.SUCCESS) + } + + ws_close_error := conn.Close() + if ws_close_error != nil { + fmt.Fprintf(os.Stderr, config_t.Messages.WS.CLOSE.ERROR, ws_close_error) + } else { + fmt.Print(config_t.Messages.WS.CLOSE.SUCCESS) + } + + os.Exit(0) +} + /* * This function gets: * pending_mutex: pointer to mutex @@ -90,6 +129,18 @@ func httpHandler(pending_mutex *sync.Mutex, pending map[string]chan Response, // Setting HTTP headers and writing response for key, vals := range response.Headers { for _, val := range vals { + // Updating Location header - needed for HTTP redirects + if key == "Location" { + redirect_url, url_parse_error := url.Parse(val) + if url_parse_error != nil { + fmt.Fprintf(os.Stderr, config_t.Messages.HTTP.URL_PARSE.ERROR, url_parse_error) + os.Exit(-1) + } else { + fmt.Print(config_t.Messages.HTTP.URL_PARSE.SUCCESS) + redirect_url.Host = redirect_url.Hostname() + ":" + http_port + val = redirect_url.String() + } + } w.Header().Add(key, val) } } @@ -393,11 +444,24 @@ func main() { // Attempt to close WebSocket connection on quit defer func() { - ws_close_error := conn.Close() - if ws_close_error != nil { - fmt.Fprintf(os.Stderr, config_t.Messages.WS.CLOSE.ERROR, ws_close_error) - } else { - fmt.Print(config_t.Messages.WS.CLOSE.SUCCESS) + quit(id, p2p, conn) + }() + + // Handling SIGINT and SIGTERM signals + signal_channel := make(chan os.Signal, 1) + signal.Notify(signal_channel, syscall.SIGINT, syscall.SIGTERM) + go func() { + <-signal_channel + quit(id, p2p, conn) + }() + + // Handling exit command + go func() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + if scanner.Text() == "exit" { + quit(id, p2p, conn) + } } }()