packages/hyprspace: remove manual routing support
This commit is contained in:
parent
80fadfae3c
commit
33782687e9
5 changed files with 34 additions and 119 deletions
|
@ -17,27 +17,30 @@ var Route = cmd.Sub{
|
||||||
|
|
||||||
type RouteArgs struct {
|
type RouteArgs struct {
|
||||||
Action string
|
Action string
|
||||||
InterfaceName string
|
|
||||||
Args []string `zero:"true"`
|
Args []string `zero:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func RouteRun(r *cmd.Root, c *cmd.Sub) {
|
func RouteRun(r *cmd.Root, c *cmd.Sub) {
|
||||||
// Parse Command Args
|
// Parse Command Args
|
||||||
args := c.Args.(*RouteArgs)
|
args := c.Args.(*RouteArgs)
|
||||||
|
ifName := r.Flags.(*GlobalFlags).InterfaceName
|
||||||
|
if ifName == "" {
|
||||||
|
ifName = "hyprspace"
|
||||||
|
}
|
||||||
|
|
||||||
action := rpc.RouteAction(args.Action)
|
action := rpc.RouteAction(args.Action)
|
||||||
rArgs := rpc.RouteArgs{
|
rArgs := rpc.RouteArgs{
|
||||||
Action: action,
|
Action: action,
|
||||||
Args: args.Args,
|
Args: args.Args,
|
||||||
}
|
}
|
||||||
reply := rpc.Route(args.InterfaceName, rArgs)
|
reply := rpc.Route(ifName, rArgs)
|
||||||
for _, r := range reply.Routes {
|
for _, r := range reply.Routes {
|
||||||
var target string
|
var target string
|
||||||
connectStatus := ""
|
connectStatus := ""
|
||||||
if r.IsRelay {
|
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 {
|
} else {
|
||||||
target = fmt.Sprintf("%s direct", r.TargetAddr)
|
target = fmt.Sprintf("/p2p/%s", r.TargetAddr)
|
||||||
}
|
}
|
||||||
if r.IsConnected {
|
if r.IsConnected {
|
||||||
connectStatus = " connected"
|
connectStatus = " connected"
|
||||||
|
|
|
@ -198,12 +198,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
|
||||||
// Check route table for destination address.
|
// Check route table for destination address.
|
||||||
for _, route := range cfg.Routes {
|
for _, route := range cfg.Routes {
|
||||||
if route.Network.Contains(dstIP) {
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -58,70 +58,41 @@ func (hsr *HyprspaceRPC) Route(args *RouteArgs, reply *RouteReply) error {
|
||||||
var routes []RouteInfo
|
var routes []RouteInfo
|
||||||
for _, r := range hsr.config.Routes {
|
for _, r := range hsr.config.Routes {
|
||||||
connected := hsr.host.Network().Connectedness(r.Target.ID) == network.Connected
|
connected := hsr.host.Network().Connectedness(r.Target.ID) == network.Connected
|
||||||
reroute, found := p2p.FindReroute(r.Network, false)
|
relay := false
|
||||||
relayAddr := r.Target.ID
|
relayAddr := r.Target.ID
|
||||||
if found {
|
if connected {
|
||||||
relayAddr = reroute.To
|
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{
|
routes = append(routes, RouteInfo{
|
||||||
Network: r.Network,
|
Network: r.Network,
|
||||||
TargetAddr: r.Target.ID,
|
TargetAddr: r.Target.ID,
|
||||||
RelayAddr: relayAddr,
|
RelayAddr: relayAddr,
|
||||||
IsRelay: found,
|
IsRelay: relay,
|
||||||
IsConnected: connected,
|
IsConnected: connected,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
*reply = RouteReply{
|
*reply = RouteReply{
|
||||||
Routes: routes,
|
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:
|
default:
|
||||||
return errors.New("no such action")
|
return errors.New("no such action")
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,6 @@ type RouteAction string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Show RouteAction = "show"
|
Show RouteAction = "show"
|
||||||
Relay = "relay"
|
|
||||||
Reset = "reset"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type RouteInfo struct {
|
type RouteInfo struct {
|
||||||
|
|
Loading…
Reference in a new issue