update example
This commit is contained in:
parent
8bc0679b5c
commit
bc86458bf2
55
bypass/example/http/main.go
Normal file
55
bypass/example/http/main.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -77,7 +77,7 @@ func main() {
|
|||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
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))]
|
node := nodes[counter.Add(1)%uint64(len(nodes))]
|
||||||
v, _ := json.Marshal(node)
|
v, _ := json.Marshal(node)
|
||||||
|
Loading…
Reference in New Issue
Block a user