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

Add some more functions to JSON admin socket

This commit is contained in:
Neil Alexander 2018-05-20 19:23:43 +01:00
parent 9713e73969
commit c3ca5c64b1
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -103,21 +103,37 @@ func (a *admin) init(c *Core, listenaddr string) {
} }
return admin_info{"sessions": sessions}, nil return admin_info{"sessions": sessions}, nil
}) })
a.addHandler("addPeer", []string{"uri"}, func(in admin_info) (admin_info, error) {
if a.addPeer(in["uri"].(string)) == nil {
return admin_info{
"peers_added": []string{
in["uri"].(string),
},
}, nil
} else {
return admin_info{
"peers_not_added": []string{
in["uri"].(string),
},
}, errors.New("Failed to add peer")
}
})
a.addHandler("removePeer", []string{"port"}, func(in admin_info) (admin_info, error) {
if a.removePeer(fmt.Sprint(in["port"])) == nil {
return admin_info{
"peers_removed": []string{
fmt.Sprint(in["port"]),
},
}, nil
} else {
return admin_info{
"peers_not_removed": []string{
fmt.Sprint(in["port"]),
},
}, errors.New("Failed to remove peer")
}
})
/* /*
a.addHandler("addPeer", []string{"<proto://address:port>"}, func(out *[]byte, saddr ...string) {
if a.addPeer(saddr[0]) == nil {
*out = []byte("Adding peer: " + saddr[0] + "\n")
} else {
*out = []byte("Failed to add peer: " + saddr[0] + "\n")
}
})
a.addHandler("removePeer", []string{"<port>"}, func(out *[]byte, sport ...string) {
if a.removePeer(sport[0]) == nil {
*out = []byte("Removing peer: " + sport[0] + "\n")
} else {
*out = []byte("Failed to remove peer: " + sport[0] + "\n")
}
})
a.addHandler("getTunTap", nil, func(out *[]byte, _ ...string) { a.addHandler("getTunTap", nil, func(out *[]byte, _ ...string) {
var info admin_nodeInfo var info admin_nodeInfo
defer func() { defer func() {
@ -218,6 +234,9 @@ func (a *admin) handleRequest(conn net.Conn) {
return return
} }
send["request"] = recv
send["status"] = "error"
handlers: handlers:
for _, handler := range a.handlers { for _, handler := range a.handlers {
if recv["request"] == handler.name { if recv["request"] == handler.name {
@ -232,8 +251,8 @@ func (a *admin) handleRequest(conn net.Conn) {
if _, ok := recv[arg]; !ok { if _, ok := recv[arg]; !ok {
fmt.Println("Missing required argument", arg) fmt.Println("Missing required argument", arg)
send = admin_info{ send = admin_info{
"error": "Missing field '" + arg + "'", "error": "One or more expected fields missing",
"fields": handler.args, "expecting": handler.args,
} }
break handlers break handlers
} }
@ -243,14 +262,14 @@ func (a *admin) handleRequest(conn net.Conn) {
// the handler // the handler
response, err := handler.handler(recv) response, err := handler.handler(recv)
if err != nil { if err != nil {
send = admin_info{ send["error"] = err.Error()
"request": recv["request"], if response != nil {
"error": err.Error(), send["response"] = response
} }
} else { } else {
send = admin_info{ send["status"] = "success"
"request": recv["request"], if response != nil {
"response": response, send["response"] = response
} }
} }
break break