diff --git a/bypass/example/main.go b/bypass/example/grpc/main.go similarity index 100% rename from bypass/example/main.go rename to bypass/example/grpc/main.go diff --git a/bypass/example/http/main.go b/bypass/example/http/main.go new file mode 100644 index 0000000..a654b4a --- /dev/null +++ b/bypass/example/http/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net" + "net/http" +) + +var ( + port = flag.Int("port", 8000, "The server port") +) + +type bypassRequest struct { + Network string `json:"network"` + Addr string `json:"addr"` + Host string `json:"host"` + Client string `json:"client"` +} + +type bypassResponse struct { + OK bool `json:"ok"` +} + +func main() { + flag.Parse() + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + log.Printf("server listening at %v", lis.Addr()) + + http.HandleFunc("/bypass", func(w http.ResponseWriter, r *http.Request) { + rb := bypassRequest{} + if err := json.NewDecoder(r.Body).Decode(&rb); err != nil { + log.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + resp := bypassResponse{ + OK: true, + } + + log.Printf("bypass: client=%s network=%s, addr=%s, host=%s", rb.Client, rb.Network, rb.Addr, rb.Host) + + json.NewEncoder(w).Encode(resp) + }) + + if err := http.Serve(lis, nil); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/hop/example/http/main.go b/hop/example/http/main.go index 934fa93..fa4fbfa 100644 --- a/hop/example/http/main.go +++ b/hop/example/http/main.go @@ -77,7 +77,7 @@ func main() { w.WriteHeader(http.StatusBadRequest) return } - log.Printf("hop: %s %s %s %s", rb.Network, rb.Addr, rb.Host, rb.Client) + log.Printf("hop: network=%s addr=%s host=%s client=%s", rb.Network, rb.Addr, rb.Host, rb.Client) node := nodes[counter.Add(1)%uint64(len(nodes))] v, _ := json.Marshal(node)