5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 14:10:28 +00:00

Convert rest of functions, fix setTunTap

This commit is contained in:
Neil Alexander 2018-05-20 21:54:15 +01:00
parent c75566d5ac
commit c765e0566f
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -103,13 +103,13 @@ func (a *admin) init(c *Core, listenaddr string) {
a.addHandler("addPeer", []string{"uri"}, func(in admin_info) (admin_info, error) { a.addHandler("addPeer", []string{"uri"}, func(in admin_info) (admin_info, error) {
if a.addPeer(in["uri"].(string)) == nil { if a.addPeer(in["uri"].(string)) == nil {
return admin_info{ return admin_info{
"peers_added": []string{ "added": []string{
in["uri"].(string), in["uri"].(string),
}, },
}, nil }, nil
} else { } else {
return admin_info{ return admin_info{
"peers_not_added": []string{ "not_added": []string{
in["uri"].(string), in["uri"].(string),
}, },
}, errors.New("Failed to add peer") }, errors.New("Failed to add peer")
@ -118,13 +118,13 @@ func (a *admin) init(c *Core, listenaddr string) {
a.addHandler("removePeer", []string{"port"}, func(in admin_info) (admin_info, error) { a.addHandler("removePeer", []string{"port"}, func(in admin_info) (admin_info, error) {
if a.removePeer(fmt.Sprint(in["port"])) == nil { if a.removePeer(fmt.Sprint(in["port"])) == nil {
return admin_info{ return admin_info{
"peers_removed": []string{ "removed": []string{
fmt.Sprint(in["port"]), fmt.Sprint(in["port"]),
}, },
}, nil }, nil
} else { } else {
return admin_info{ return admin_info{
"peers_not_removed": []string{ "not_removed": []string{
fmt.Sprint(in["port"]), fmt.Sprint(in["port"]),
}, },
}, errors.New("Failed to remove peer") }, errors.New("Failed to remove peer")
@ -145,7 +145,7 @@ func (a *admin) init(c *Core, listenaddr string) {
}) })
a.addHandler("setTunTap", []string{"name", "<tap_mode>", "<mtu>"}, func(in admin_info) (admin_info, error) { a.addHandler("setTunTap", []string{"name", "<tap_mode>", "<mtu>"}, func(in admin_info) (admin_info, error) {
// Set sane defaults // Set sane defaults
iftapmode := false iftapmode := getDefaults().defaultIfTAPMode
ifmtu := getDefaults().defaultIfMTU ifmtu := getDefaults().defaultIfMTU
// Has TAP mode been specified? // Has TAP mode been specified?
if tap, ok := in["tap_mode"]; ok { if tap, ok := in["tap_mode"]; ok {
@ -153,8 +153,8 @@ func (a *admin) init(c *Core, listenaddr string) {
} }
// Check we have enough params for MTU // Check we have enough params for MTU
if mtu, ok := in["mtu"]; ok { if mtu, ok := in["mtu"]; ok {
if mtu.(int) >= 1280 && ifmtu <= getDefaults().maximumIfMTU { if mtu.(float64) >= 1280 && ifmtu <= getDefaults().maximumIfMTU {
ifmtu = in["mtu"].(int) ifmtu = int(in["mtu"].(float64))
} }
} }
// Start the TUN adapter // Start the TUN adapter
@ -163,7 +163,7 @@ func (a *admin) init(c *Core, listenaddr string) {
} else { } else {
return admin_info{ return admin_info{
"name": a.core.tun.iface.Name(), "name": a.core.tun.iface.Name(),
"tap_mode": iftapmode, "tap_mode": a.core.tun.iface.IsTAP(),
"mtu": ifmtu, "mtu": ifmtu,
}, nil }, nil
} }
@ -171,21 +171,36 @@ func (a *admin) init(c *Core, listenaddr string) {
a.addHandler("getAllowedBoxPubs", nil, func(in admin_info) (admin_info, error) { a.addHandler("getAllowedBoxPubs", nil, func(in admin_info) (admin_info, error) {
return admin_info{"allowed_box_pubs": a.getAllowedBoxPubs()}, nil return admin_info{"allowed_box_pubs": a.getAllowedBoxPubs()}, nil
}) })
/* a.addHandler("addAllowedBoxPub", []string{"box_pub_key"}, func(in admin_info) (admin_info, error) {
a.addHandler("addAllowedBoxPub", []string{"<boxPubKey>"}, func(out *[]byte, saddr ...string) { if a.addAllowedBoxPub(in["box_pub_key"].(string)) == nil {
if a.addAllowedBoxPub(saddr[0]) == nil { return admin_info{
*out = []byte("Adding key: " + saddr[0] + "\n") "added": []string{
} else { in["box_pub_key"].(string),
*out = []byte("Failed to add key: " + saddr[0] + "\n") },
} }, nil
}) } else {
a.addHandler("removeAllowedBoxPub", []string{"<boxPubKey>"}, func(out *[]byte, sport ...string) { return admin_info{
if a.removeAllowedBoxPub(sport[0]) == nil { "not_added": []string{
*out = []byte("Removing key: " + sport[0] + "\n") in["box_pub_key"].(string),
} else { },
*out = []byte("Failed to remove key: " + sport[0] + "\n") }, errors.New("Failed to add allowed box pub key")
} }
})*/ })
a.addHandler("removeAllowedBoxPub", []string{"box_pub_key"}, func(in admin_info) (admin_info, error) {
if a.removeAllowedBoxPub(in["box_pub_key"].(string)) == nil {
return admin_info{
"removed": []string{
in["box_pub_key"].(string),
},
}, nil
} else {
return admin_info{
"not_removed": []string{
in["box_pub_key"].(string),
},
}, errors.New("Failed to remove allowed box pub key")
}
})
go a.listen() go a.listen()
} }
@ -219,6 +234,7 @@ func (a *admin) handleRequest(conn net.Conn) {
"status": "error", "status": "error",
"error": "Unrecoverable error, possibly as a result of invalid input types or malformed syntax", "error": "Unrecoverable error, possibly as a result of invalid input types or malformed syntax",
} }
fmt.Println("Admin socket error:", r)
if err := encoder.Encode(&send); err != nil { if err := encoder.Encode(&send); err != nil {
fmt.Println("Admin socket JSON encode error:", err) fmt.Println("Admin socket JSON encode error:", err)
} }