From 33782687e950c1f0a042453c2969493834748911 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 22 Oct 2023 23:39:39 +0200 Subject: [PATCH] packages/hyprspace: remove manual routing support --- packages/networking/hyprspace/cli/route.go | 15 ++-- packages/networking/hyprspace/cli/up.go | 7 +- .../networking/hyprspace/p2p/rerouting.go | 52 ------------- packages/networking/hyprspace/rpc/server.go | 75 ++++++------------- packages/networking/hyprspace/rpc/types.go | 4 +- 5 files changed, 34 insertions(+), 119 deletions(-) delete mode 100644 packages/networking/hyprspace/p2p/rerouting.go diff --git a/packages/networking/hyprspace/cli/route.go b/packages/networking/hyprspace/cli/route.go index 45b47b1..4826a34 100644 --- a/packages/networking/hyprspace/cli/route.go +++ b/packages/networking/hyprspace/cli/route.go @@ -16,28 +16,31 @@ var Route = cmd.Sub{ } type RouteArgs struct { - Action string - InterfaceName string - Args []string `zero:"true"` + Action string + Args []string `zero:"true"` } func RouteRun(r *cmd.Root, c *cmd.Sub) { // Parse Command Args args := c.Args.(*RouteArgs) + ifName := r.Flags.(*GlobalFlags).InterfaceName + if ifName == "" { + ifName = "hyprspace" + } action := rpc.RouteAction(args.Action) rArgs := rpc.RouteArgs{ Action: action, Args: args.Args, } - reply := rpc.Route(args.InterfaceName, rArgs) + reply := rpc.Route(ifName, rArgs) for _, r := range reply.Routes { var target string connectStatus := "" if r.IsRelay { - target = fmt.Sprintf("%s relay target %s", r.RelayAddr, r.TargetAddr) + target = fmt.Sprintf("/p2p/%s/p2p-circuit/p2p/%s", r.RelayAddr, r.TargetAddr) } else { - target = fmt.Sprintf("%s direct", r.TargetAddr) + target = fmt.Sprintf("/p2p/%s", r.TargetAddr) } if r.IsConnected { connectStatus = " connected" diff --git a/packages/networking/hyprspace/cli/up.go b/packages/networking/hyprspace/cli/up.go index 6089017..4461871 100644 --- a/packages/networking/hyprspace/cli/up.go +++ b/packages/networking/hyprspace/cli/up.go @@ -198,12 +198,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) { // Check route table for destination address. for _, route := range cfg.Routes { if route.Network.Contains(dstIP) { - reroute, found := p2p.FindReroute(route.Network, false) - if found { - dst = &reroute.To - } else { - dst = &route.Target.ID - } + dst = &route.Target.ID break } } diff --git a/packages/networking/hyprspace/p2p/rerouting.go b/packages/networking/hyprspace/p2p/rerouting.go deleted file mode 100644 index bbd75ec..0000000 --- a/packages/networking/hyprspace/p2p/rerouting.go +++ /dev/null @@ -1,52 +0,0 @@ -package p2p - -import ( - "net" - "sync" - - "github.com/libp2p/go-libp2p/core/peer" -) - -type Reroute struct { - Network net.IPNet - To peer.ID -} - -var ( - reroutes []Reroute - mut sync.Mutex -) - -func findReroute(network net.IPNet, doDelete bool) (int, *Reroute, bool) { - for i, r := range reroutes { - bits1, _ := r.Network.Mask.Size() - bits2, _ := network.Mask.Size() - if r.Network.IP.Equal(network.IP) && bits1 == bits2 { - if doDelete { - reroutes = append(reroutes[:i], reroutes[i+1:]...) - } - return i, &r, true - } - } - return 0, nil, false -} - -func FindReroute(network net.IPNet, doDelete bool) (*Reroute, bool) { - mut.Lock() - defer mut.Unlock() - _, i, r := findReroute(network, doDelete) - return i, r -} - -func AddReroute(network net.IPNet, peerID peer.ID) { - mut.Lock() - defer mut.Unlock() - if i, _, found := findReroute(network, false); found { - reroutes[i].To = peerID - } else { - reroutes = append(reroutes, Reroute{ - Network: network, - To: peerID, - }) - } -} diff --git a/packages/networking/hyprspace/rpc/server.go b/packages/networking/hyprspace/rpc/server.go index 714b377..670f8d6 100644 --- a/packages/networking/hyprspace/rpc/server.go +++ b/packages/networking/hyprspace/rpc/server.go @@ -58,70 +58,41 @@ func (hsr *HyprspaceRPC) Route(args *RouteArgs, reply *RouteReply) error { var routes []RouteInfo for _, r := range hsr.config.Routes { connected := hsr.host.Network().Connectedness(r.Target.ID) == network.Connected - reroute, found := p2p.FindReroute(r.Network, false) + relay := false relayAddr := r.Target.ID - if found { - relayAddr = reroute.To + if connected { + ConnLoop: + for _, c := range hsr.host.Network().ConnsToPeer(r.Target.ID) { + for _, s := range c.GetStreams() { + if s.Protocol() == p2p.Protocol { + if _, err := c.RemoteMultiaddr().ValueForProtocol(multiaddr.P_CIRCUIT); err == nil { + relay = true + if ra, err := c.RemoteMultiaddr().ValueForProtocol(multiaddr.P_P2P); err == nil { + relayAddr, err = peer.Decode(ra) + if err != nil { + relayAddr = r.Target.ID + } + } + } else { + relay = false + relayAddr = r.Target.ID + break ConnLoop + } + } + } + } } routes = append(routes, RouteInfo{ Network: r.Network, TargetAddr: r.Target.ID, RelayAddr: relayAddr, - IsRelay: found, + IsRelay: relay, IsConnected: connected, }) } *reply = RouteReply{ Routes: routes, } - case Relay: - if len(args.Args) != 2 { - return errors.New("expected exactly 2 arguments") - } - var networks []net.IPNet - if args.Args[0] == "all" { - for _, r := range hsr.config.Routes { - networks = append(networks, r.Network) - } - } else { - _, network, err := net.ParseCIDR(args.Args[0]) - if err != nil { - return err - } else if _, found := config.FindRoute(hsr.config.Routes, *network); !found { - return errors.New("no such network") - } - networks = []net.IPNet{*network} - } - p, err := peer.Decode(args.Args[1]) - if err != nil { - return err - } else if _, found := config.FindPeer(hsr.config.Peers, p); !found { - return errors.New("no such peer") - } - for _, n := range networks { - p2p.AddReroute(n, p) - } - case Reset: - if len(args.Args) != 1 { - return errors.New("expected exactly 1 argument") - } - var networks []net.IPNet - if args.Args[0] == "all" { - for _, r := range hsr.config.Routes { - networks = append(networks, r.Network) - } - } else { - _, network, err := net.ParseCIDR(args.Args[0]) - if err != nil { - return err - } else if _, found := config.FindRoute(hsr.config.Routes, *network); !found { - return errors.New("no such network") - } - networks = []net.IPNet{*network} - } - for _, n := range networks { - p2p.FindReroute(n, true) - } default: return errors.New("no such action") } diff --git a/packages/networking/hyprspace/rpc/types.go b/packages/networking/hyprspace/rpc/types.go index d21b3e8..d663821 100644 --- a/packages/networking/hyprspace/rpc/types.go +++ b/packages/networking/hyprspace/rpc/types.go @@ -25,9 +25,7 @@ type PeersReply struct { type RouteAction string const ( - Show RouteAction = "show" - Relay = "relay" - Reset = "reset" + Show RouteAction = "show" ) type RouteInfo struct {