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

Various fixes and error catching, add setTunTap

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

View File

@ -143,44 +143,35 @@ func (a *admin) init(c *Core, listenaddr string) {
"mtu": a.core.tun.mtu, "mtu": a.core.tun.mtu,
}, nil }, nil
}) })
/* a.addHandler("setTunTap", []string{"name", "<tap_mode>", "<mtu>"}, func(in admin_info) (admin_info, error) {
a.addHandler("setTunTap", []string{"<ifname|auto|none>", "[<tun|tap>]", "[<mtu>]"}, func(out *[]byte, ifparams ...string) {
// Set sane defaults // Set sane defaults
iftapmode := false iftapmode := false
ifmtu := 1280 ifmtu := getDefaults().defaultIfMTU
var err error // Has TAP mode been specified?
// Check we have enough params for TAP mode if tap, ok := in["tap_mode"]; ok {
if len(ifparams) > 1 { iftapmode = tap.(bool)
// Is it a TAP adapter?
if ifparams[1] == "tap" {
iftapmode = true
}
} }
// Check we have enough params for MTU // Check we have enough params for MTU
if len(ifparams) > 2 { if mtu, ok := in["mtu"]; ok {
// Make sure the MTU is sane if mtu.(int) >= 1280 && ifmtu <= getDefaults().maximumIfMTU {
ifmtu, err = strconv.Atoi(ifparams[2]) ifmtu = in["mtu"].(int)
if err != nil || ifmtu < 1280 || ifmtu > 65535 {
ifmtu = 1280
} }
} }
// Start the TUN adapter // Start the TUN adapter
if err := a.startTunWithMTU(ifparams[0], iftapmode, ifmtu); err != nil { if err := a.startTunWithMTU(in["name"].(string), iftapmode, ifmtu); err != nil {
*out = []byte(fmt.Sprintf("Failed to set TUN: %v\n", err)) return admin_info{}, errors.New("Failed to configure adapter")
} else { } else {
info := admin_nodeInfo{ return admin_info{
{"Interface name", ifparams[0]}, "name": a.core.tun.iface.Name(),
{"TAP mode", strconv.FormatBool(iftapmode)}, "tap_mode": iftapmode,
{"mtu", strconv.Itoa(ifmtu)}, "mtu": ifmtu,
} }, nil
*out = []byte(a.printInfos([]admin_nodeInfo{info}))
} }
}) })
*/ a.addHandler("getAllowedBoxPubs", nil, func(in admin_info) (admin_info, error) {
return admin_info{"allowed_box_pubs": a.getAllowedBoxPubs()}, nil
})
/* /*
a.addHandler("getAllowedBoxPubs", nil, func(out *[]byte, _ ...string) {
*out = []byte(a.getAllowedBoxPubs())
})
a.addHandler("addAllowedBoxPub", []string{"<boxPubKey>"}, func(out *[]byte, saddr ...string) { a.addHandler("addAllowedBoxPub", []string{"<boxPubKey>"}, func(out *[]byte, saddr ...string) {
if a.addAllowedBoxPub(saddr[0]) == nil { if a.addAllowedBoxPub(saddr[0]) == nil {
*out = []byte("Adding key: " + saddr[0] + "\n") *out = []byte("Adding key: " + saddr[0] + "\n")
@ -215,24 +206,45 @@ func (a *admin) listen() {
} }
func (a *admin) handleRequest(conn net.Conn) { func (a *admin) handleRequest(conn net.Conn) {
defer conn.Close()
decoder := json.NewDecoder(conn) decoder := json.NewDecoder(conn)
encoder := json.NewEncoder(conn) encoder := json.NewEncoder(conn)
encoder.SetIndent("", " ") encoder.SetIndent("", " ")
recv := make(admin_info) recv := make(admin_info)
send := make(admin_info) send := make(admin_info)
defer func() {
r := recover()
if r != nil {
send = admin_info{
"status": "error",
"error": "Unrecoverable error, possibly as a result of invalid input types or malformed syntax",
}
if err := encoder.Encode(&send); err != nil {
fmt.Println("Admin socket JSON encode error:", err)
}
conn.Close()
}
}()
for { for {
// Start with a clean slate on each request
recv = admin_info{}
send = admin_info{}
// Decode the input
if err := decoder.Decode(&recv); err != nil { if err := decoder.Decode(&recv); err != nil {
fmt.Println("Admin socket JSON decode error:", err) fmt.Println("Admin socket JSON decode error:", err)
return return
} }
// Send the request back with the response, and default to "error"
// unless the status is changed below by one of the handlers
send["request"] = recv send["request"] = recv
send["status"] = "error" send["status"] = "error"
handlers: handlers:
for _, handler := range a.handlers { for _, handler := range a.handlers {
// We've found the handler that matches the request
if recv["request"] == handler.name { if recv["request"] == handler.name {
// Check that we have all the required arguments // Check that we have all the required arguments
for _, arg := range handler.args { for _, arg := range handler.args {
@ -243,10 +255,9 @@ func (a *admin) handleRequest(conn net.Conn) {
} }
// Check if the field is missing // Check if the field is missing
if _, ok := recv[arg]; !ok { if _, ok := recv[arg]; !ok {
fmt.Println("Missing required argument", arg)
send = admin_info{ send = admin_info{
"error": "One or more expected fields missing", "error": "Expected field missing",
"expecting": handler.args, "expecting": arg,
} }
break handlers break handlers
} }
@ -266,16 +277,21 @@ func (a *admin) handleRequest(conn net.Conn) {
send["response"] = response send["response"] = response
} }
} }
break break
} }
} }
// Send the response back
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)
return return
} }
return // If "keepalive" isn't true then close the connection
if keepalive, ok := recv["keepalive"]; !ok || !keepalive.(bool) {
conn.Close()
}
} }
} }
@ -476,14 +492,13 @@ func (a *admin) getData_getSessions() []admin_nodeInfo {
return infos return infos
} }
func (a *admin) getAllowedBoxPubs() string { func (a *admin) getAllowedBoxPubs() []string {
pubs := a.core.peers.getAllowedBoxPubs() pubs := a.core.peers.getAllowedBoxPubs()
var out []string var out []string
for _, pub := range pubs { for _, pub := range pubs {
out = append(out, hex.EncodeToString(pub[:])) out = append(out, hex.EncodeToString(pub[:]))
} }
out = append(out, "") return out
return strings.Join(out, "\n")
} }
func (a *admin) addAllowedBoxPub(bstr string) (err error) { func (a *admin) addAllowedBoxPub(bstr string) (err error) {