5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-30 02:01:35 +00:00

Merge pull request #395 from neilalexander/fix-385

Handle admin socket error cases better
This commit is contained in:
Neil Alexander 2019-07-07 19:44:17 +01:00 committed by GitHub
commit f831f9d2cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -381,11 +381,11 @@ func (a *AdminSocket) handleRequest(conn net.Conn) {
if r != nil { if r != nil {
send = Info{ send = Info{
"status": "error", "status": "error",
"error": "Unrecoverable error, possibly as a result of invalid input types or malformed syntax", "error": "Check your syntax and input types",
} }
a.log.Errorln("Admin socket error:", r) a.log.Debugln("Admin socket error:", r)
if err := encoder.Encode(&send); err != nil { if err := encoder.Encode(&send); err != nil {
a.log.Errorln("Admin socket JSON encode error:", err) a.log.Debugln("Admin socket JSON encode error:", err)
} }
conn.Close() conn.Close()
} }
@ -407,13 +407,14 @@ func (a *AdminSocket) handleRequest(conn net.Conn) {
send["request"] = recv send["request"] = recv
send["status"] = "error" send["status"] = "error"
n := strings.ToLower(recv["request"].(string))
if _, ok := recv["request"]; !ok { if _, ok := recv["request"]; !ok {
send["error"] = "No request sent" send["error"] = "No request sent"
break goto respond
} }
n := strings.ToLower(recv["request"].(string)) if h, ok := a.handlers[n]; ok {
if h, ok := a.handlers[strings.ToLower(n)]; ok {
// Check that we have all the required arguments // Check that we have all the required arguments
for _, arg := range h.args { for _, arg := range h.args {
// An argument in [square brackets] is optional and not required, // An argument in [square brackets] is optional and not required,
@ -428,7 +429,7 @@ func (a *AdminSocket) handleRequest(conn net.Conn) {
"error": "Expected field missing: " + arg, "error": "Expected field missing: " + arg,
"expecting": arg, "expecting": arg,
} }
break goto respond
} }
} }
@ -439,16 +440,28 @@ func (a *AdminSocket) handleRequest(conn net.Conn) {
send["error"] = err.Error() send["error"] = err.Error()
if response != nil { if response != nil {
send["response"] = response send["response"] = response
goto respond
} }
} else { } else {
send["status"] = "success" send["status"] = "success"
if response != nil { if response != nil {
send["response"] = response send["response"] = response
goto respond
} }
} }
} else {
// Start with a clean response on each request, which defaults to an error
// state. If a handler is found below then this will be overwritten
send = Info{
"request": recv,
"status": "error",
"error": fmt.Sprintf("Unknown action '%s', try 'list' for help", recv["request"].(string)),
}
goto respond
} }
// Send the response back // Send the response back
respond:
if err := encoder.Encode(&send); err != nil { if err := encoder.Encode(&send); err != nil {
return return
} }