52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func (s *Server) connectionRoutes(w http.ResponseWriter, r *http.Request, path string) {
|
|
if path == "connections" && r.Method == "GET" {
|
|
scopes, ok := s.scopeIndex(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
all := s.manager.Connections()
|
|
result := all[:0]
|
|
q := r.URL.Query()
|
|
for _, c := range all {
|
|
if scopes != nil && !scopes.Matches("gateways", c.GatewayID, requestScope(r)) {
|
|
continue
|
|
}
|
|
if id := q.Get("gatewayId"); id != "" && c.GatewayID != id {
|
|
continue
|
|
}
|
|
if username := q.Get("username"); username != "" && c.Username != username {
|
|
continue
|
|
}
|
|
if source := q.Get("source"); source != "" && c.Source != source {
|
|
continue
|
|
}
|
|
result = append(result, c)
|
|
}
|
|
respond(w, 200, result)
|
|
return
|
|
}
|
|
parts := strings.Split(path, "/")
|
|
if len(parts) != 3 || parts[2] != "disconnect" {
|
|
fail(w, 404, "connection action not found")
|
|
return
|
|
}
|
|
if r.Method != "POST" {
|
|
fail(w, 405, "method not allowed")
|
|
return
|
|
}
|
|
connection, ok := s.manager.Disconnect(parts[1])
|
|
if !ok {
|
|
fail(w, 404, "connection no longer active")
|
|
return
|
|
}
|
|
s.store.Event(connection.GatewayID, source(r), "connection_disconnected_by_operator", connection.ID)
|
|
respond(w, 200, map[string]bool{"ok": true})
|
|
}
|