// Copyright (c) 2022, Přemysl Eric Janouch
// SPDX-License-Identifier: 0BSD package main import ( "context" "encoding/binary" "encoding/json" "fmt" "html/template" "io" "log" "net" "net/http" "os" "time" "nhooyr.io/websocket" ) var ( addressBind string addressConnect string addressWS string ) func clientToRelay( ctx context.Context, ws *websocket.Conn, conn net.Conn) bool { t, b, err := ws.Read(ctx) if err != nil { log.Println("Command receive failed: " + err.Error()) return false } if t != websocket.MessageText { log.Println("Command receive failed: " + "binary messages are not supported") return false } log.Printf("?> %s\n", b) var m RelayCommandMessage if err := json.Unmarshal(b, &m); err != nil { log.Println("Command unmarshalling failed: " + err.Error()) return false } b, ok := m.AppendTo(make([]byte, 4)) if !ok { log.Println("Command serialization failed") return false } binary.BigEndian.PutUint32(b[:4], uint32(len(b)-4)) if _, err := conn.Write(b); err != nil { log.Println("Command send failed: " + err.Error()) return false } log.Printf("-> %v\n", b) return true } func relayToClient( ctx context.Context, ws *websocket.Conn, conn net.Conn) bool { var length uint32 if err := binary.Read(conn, binary.BigEndian, &length); err != nil { log.Println("Event receive failed: " + err.Error()) return false } b := make([]byte, length) if _, err := io.ReadFull(conn, b); err != nil { log.Println("Event receive failed: " + err.Error()) return false } log.Printf(" %v\n", b) var m RelayEventMessage if after, ok := m.ConsumeFrom(b); !ok { log.Println("Event deserialization failed") return false } else if len(after) != 0 { log.Println("Event deserialization failed: trailing data") return false } j, err := json.Marshal(&m) if err != nil { log.Println("Event marshalling failed: " + err.Error()) return false } if err := ws.Write(ctx, websocket.MessageText, j); err != nil { log.Println("Event send failed: " + err.Error()) return false } log.Printf("<- %s\n", j) return true } func errorToClient(ctx context.Context, ws *websocket.Conn, err error) bool { j, err := json.Marshal(&RelayEventMessage{ EventSeq: 0, Data: RelayEventData{ Interface: RelayEventDataError{ Event: RelayEventError, CommandSeq: 0, Error: err.Error(), }, }, }) if err != nil { log.Println("Event marshalling failed: " + err.Error()) return false } if err := ws.Write(ctx, websocket.MessageText, j); err != nil { log.Println("Event send failed: " + err.Error()) return false } return true } func handleWS(w http.ResponseWriter, r *http.Request) { ws, err := websocket.Accept(w, r, &websocket.AcceptOptions{ InsecureSkipVerify: true, CompressionMode: websocket.CompressionContextTakeover, // This is for the payload, and happens to trigger on all messages. CompressionThreshold: 16, }) if err != nil { log.Println("Client rejected: " + err.Error()) return } defer ws.Close(websocket.StatusGoingAway, "Goodbye") ctx, cancel := context.WithCancel(r.Context()) defer cancel() conn, err := net.Dial("tcp", addressConnect) if err != nil { errorToClient(ctx, ws, err) return } // We don't need to intervene, so it's just two separate pipes so far. go func() { for clientToRelay(ctx, ws, conn) { } cancel() }() go func() { for relayToClient(ctx, ws, conn) { } cancel() }() <-ctx.Done() } var staticHandler = http.FileServer(http.Dir(".")) var page = template.Must(template.New("/").Parse(`