| 1 | package virtualnetwork |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "strconv" |
| 9 | |
| 10 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 11 | "github.com/inetaf/tcpproxy" |
| 12 | log "github.com/sirupsen/logrus" |
| 13 | "gvisor.dev/gvisor/pkg/tcpip" |
| 14 | "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" |
| 15 | "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" |
| 16 | ) |
| 17 | |
| 18 | func (n *VirtualNetwork) ServicesMux() *http.ServeMux { |
| 19 | mux := http.NewServeMux() |
| 20 | mux.Handle("/services/", http.StripPrefix("/services", n.servicesMux)) |
| 21 | mux.HandleFunc("/stats", func(w http.ResponseWriter, _ *http.Request) { |
| 22 | _ = json.NewEncoder(w).Encode(statsAsJSON(n.networkSwitch.Sent, n.networkSwitch.Received, n.stack.Stats())) |
| 23 | }) |
| 24 | mux.HandleFunc("/cam", func(w http.ResponseWriter, _ *http.Request) { |
| 25 | _ = json.NewEncoder(w).Encode(n.networkSwitch.CAM()) |
| 26 | }) |
| 27 | mux.HandleFunc("/leases", func(w http.ResponseWriter, _ *http.Request) { |
| 28 | _ = json.NewEncoder(w).Encode(n.ipPool.Leases()) |
| 29 | }) |
| 30 | mux.HandleFunc("/tunnel", func(w http.ResponseWriter, r *http.Request) { |
| 31 | ip := r.URL.Query().Get("ip") |
| 32 | if ip == "" { |
| 33 | http.Error(w, "ip is mandatory", http.StatusInternalServerError) |
| 34 | return |
| 35 | } |
| 36 | port, err := strconv.ParseUint(r.URL.Query().Get("port"), 10, 16) |
| 37 | if err != nil { |
| 38 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 39 | return |
| 40 | } |
| 41 | port16 := uint16(port) |
| 42 | |
| 43 | hj, ok := w.(http.Hijacker) |
| 44 | if !ok { |
| 45 | http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | conn, bufrw, err := hj.Hijack() |
| 50 | if err != nil { |
| 51 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 52 | return |
| 53 | } |
| 54 | defer conn.Close() |
| 55 | |
| 56 | if err := bufrw.Flush(); err != nil { |
| 57 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | if _, err := conn.Write([]byte(`OK`)); err != nil { |
| 62 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | remote := tcpproxy.DialProxy{ |
| 67 | DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| 68 | return gonet.DialContextTCP(ctx, n.stack, tcpip.FullAddress{ |
| 69 | NIC: 1, |
| 70 | Addr: tcpip.AddrFrom4Slice(net.ParseIP(ip).To4()), |
| 71 | Port: port16, |
| 72 | }, ipv4.ProtocolNumber) |
| 73 | }, |
| 74 | OnDialError: func(_ net.Conn, dstDialErr error) { |
| 75 | log.Errorf("cannot dial: %v", dstDialErr) |
| 76 | }, |
| 77 | } |
| 78 | remote.HandleConn(conn) |
| 79 | }) |
| 80 | return mux |
| 81 | } |
| 82 | |
| 83 | func (n *VirtualNetwork) Mux() *http.ServeMux { |
| 84 | mux := n.ServicesMux() |
| 85 | mux.HandleFunc(types.ConnectPath, func(w http.ResponseWriter, _ *http.Request) { |
| 86 | hj, ok := w.(http.Hijacker) |
| 87 | if !ok { |
| 88 | http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) |
| 89 | return |
| 90 | } |
| 91 | conn, bufrw, err := hj.Hijack() |
| 92 | if err != nil { |
| 93 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 94 | return |
| 95 | } |
| 96 | defer conn.Close() |
| 97 | |
| 98 | if err := bufrw.Flush(); err != nil { |
| 99 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | _ = n.networkSwitch.Accept(context.Background(), conn, n.configuration.Protocol) |
| 104 | }) |
| 105 | return mux |
| 106 | } |