packages/hyprspace: remove manual routing support

This commit is contained in:
Max Headroom 2023-10-22 23:39:39 +02:00
parent 80fadfae3c
commit 33782687e9
5 changed files with 34 additions and 119 deletions

View file

@ -17,27 +17,30 @@ var Route = cmd.Sub{
type RouteArgs struct {
Action string
InterfaceName 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"

View file

@ -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
}
break
}
}

View file

@ -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,
})
}
}

View file

@ -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")
}

View file

@ -26,8 +26,6 @@ type RouteAction string
const (
Show RouteAction = "show"
Relay = "relay"
Reset = "reset"
)
type RouteInfo struct {