5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 00:11:36 +00:00

switch from []byte to boxPubKey in ckr code, and start adding admin functions for existing code (yggdrasilctl.go still needs pretty printing support for the responses to the new get functions)

This commit is contained in:
Arceliar 2018-11-21 00:10:20 -06:00
parent 5fa23b1e38
commit 5953027411
3 changed files with 56 additions and 10 deletions

View File

@ -232,6 +232,54 @@ func (a *admin) init(c *Core, listenaddr string) {
}, errors.New("Failed to remove allowed key") }, errors.New("Failed to remove allowed key")
} }
}) })
a.addHandler("addSourceSubnet", []string{"subnet"}, func(in admin_info) (admin_info, error) {
var err error
a.core.router.doAdmin(func() {
err = a.core.router.cryptokey.addSourceSubnet(in["subnet"].(string))
})
if err == nil {
return admin_info{"added": []string{in["subnet"].(string)}}, nil
} else {
return admin_info{"not_added": []string{in["subnet"].(string)}}, errors.New("Failed to add source subnet")
}
})
a.addHandler("addRoute", []string{"subnet", "destPubKey"}, func(in admin_info) (admin_info, error) {
var err error
a.core.router.doAdmin(func() {
err = a.core.router.cryptokey.addRoute(in["subnet"].(string), in["destPubKey"].(string))
})
if err == nil {
return admin_info{"added": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["destPubKey"].(string))}}, nil
} else {
return admin_info{"not_added": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["destPubKey"].(string))}}, errors.New("Failed to add route")
}
})
a.addHandler("getSourceSubnets", []string{}, func(in admin_info) (admin_info, error) {
var subnets []string
a.core.router.doAdmin(func() {
getSourceSubnets := func(snets []net.IPNet) {
for _, subnet := range snets {
subnets = append(subnets, subnet.String())
}
}
getSourceSubnets(a.core.router.cryptokey.ipv4sources)
getSourceSubnets(a.core.router.cryptokey.ipv6sources)
})
return admin_info{"source_subnets": subnets}, nil
})
a.addHandler("getRoutes", []string{}, func(in admin_info) (admin_info, error) {
var routes []string
a.core.router.doAdmin(func() {
getRoutes := func(ckrs []cryptokey_route) {
for _, ckr := range ckrs {
routes = append(routes, fmt.Sprintf("%s via %s", ckr.subnet.String(), hex.EncodeToString(ckr.destination[:])))
}
}
getRoutes(a.core.router.cryptokey.ipv4routes)
getRoutes(a.core.router.cryptokey.ipv6routes)
})
return admin_info{"routes": routes}, nil
})
} }
// start runs the admin API socket to listen for / respond to admin API calls. // start runs the admin API socket to listen for / respond to admin API calls.

View File

@ -25,7 +25,7 @@ type cryptokey struct {
type cryptokey_route struct { type cryptokey_route struct {
subnet net.IPNet subnet net.IPNet
destination []byte destination boxPubKey
} }
// Initialise crypto-key routing. This must be done before any other CKR calls. // Initialise crypto-key routing. This must be done before any other CKR calls.
@ -171,13 +171,15 @@ func (c *cryptokey) addRoute(cidr string, dest string) error {
} }
} }
// Decode the public key // Decode the public key
if boxPubKey, err := hex.DecodeString(dest); err != nil { if bpk, err := hex.DecodeString(dest); err != nil && len(bpk) == boxPubKeyLen {
return err return err
} else { } else {
// Add the new crypto-key route // Add the new crypto-key route
var key boxPubKey
copy(key[:], bpk)
*routingtable = append(*routingtable, cryptokey_route{ *routingtable = append(*routingtable, cryptokey_route{
subnet: *ipnet, subnet: *ipnet,
destination: boxPubKey, destination: key,
}) })
// Sort so most specific routes are first // Sort so most specific routes are first
@ -227,9 +229,7 @@ func (c *cryptokey) getPublicKeyForAddress(addr address, addrlen int) (boxPubKey
// Check if there's a cache entry for this addr // Check if there's a cache entry for this addr
if route, ok := (*routingcache)[addr]; ok { if route, ok := (*routingcache)[addr]; ok {
var box boxPubKey return route.destination, nil
copy(box[:boxPubKeyLen], route.destination)
return box, nil
} }
// No cache was found - start by converting the address into a net.IP // No cache was found - start by converting the address into a net.IP
@ -245,9 +245,7 @@ func (c *cryptokey) getPublicKeyForAddress(addr address, addrlen int) (boxPubKey
(*routingcache)[addr] = route (*routingcache)[addr] = route
// Return the boxPubKey // Return the boxPubKey
var box boxPubKey return route.destination, nil
copy(box[:boxPubKeyLen], route.destination)
return box, nil
} }
} }

View File

@ -231,7 +231,7 @@ func main() {
uint(k), uint(v), uint(queuesizepercent), uint(portqueuepackets[k])) uint(k), uint(v), uint(queuesizepercent), uint(portqueuepackets[k]))
} }
} }
case "addpeer", "removepeer", "addallowedencryptionpublickey", "removeallowedencryptionpublickey": case "addpeer", "removepeer", "addallowedencryptionpublickey", "removeallowedencryptionpublickey", "addsourcesubnet", "addroute":
if _, ok := res["added"]; ok { if _, ok := res["added"]; ok {
for _, v := range res["added"].([]interface{}) { for _, v := range res["added"].([]interface{}) {
fmt.Println("Added:", fmt.Sprint(v)) fmt.Println("Added:", fmt.Sprint(v))