269 lines
7.3 KiB
Go
269 lines
7.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
"git.asio.asia/TapRemly/server/internal/auth"
|
|
"git.asio.asia/TapRemly/server/internal/store"
|
|
)
|
|
|
|
type deviceView struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
EndpointID string `json:"endpoint_id"`
|
|
GroupID *int64 `json:"group_id"`
|
|
Presence string `json:"presence"`
|
|
Password string `json:"login_password,omitempty"`
|
|
}
|
|
|
|
func viewDevice(d store.Device, presence string) deviceView {
|
|
if presence == "" {
|
|
presence = "unknown"
|
|
}
|
|
return deviceView{
|
|
ID: d.ID,
|
|
Name: d.Name,
|
|
EndpointID: d.EndpointID,
|
|
GroupID: d.GroupID,
|
|
Presence: presence,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) listDevices(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := h.requireUser(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
devices, err := h.store.ListDevices(r.Context(), user.ID)
|
|
if err != nil {
|
|
h.log.Error("list devices", "err", err)
|
|
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
|
return
|
|
}
|
|
groups, err := h.store.ListGroups(r.Context(), user.ID)
|
|
if err != nil {
|
|
h.log.Error("list groups", "err", err)
|
|
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
|
return
|
|
}
|
|
views := make([]deviceView, len(devices))
|
|
var wg sync.WaitGroup
|
|
sem := make(chan struct{}, 8)
|
|
for i := range devices {
|
|
wg.Add(1)
|
|
go func(i int) {
|
|
defer wg.Done()
|
|
sem <- struct{}{}
|
|
defer func() { <-sem }()
|
|
views[i] = viewDevice(devices[i], h.presence(r, devices[i].EndpointID))
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
groupViews := make([]map[string]any, 0, len(groups))
|
|
for _, g := range groups {
|
|
groupViews = append(groupViews, map[string]any{"id": g.ID, "name": g.Name})
|
|
}
|
|
writeOK(w, map[string]any{"devices": views, "groups": groupViews})
|
|
}
|
|
|
|
func (h *Handler) presence(r *http.Request, endpointID string) string {
|
|
if h.nix == nil {
|
|
return "unknown"
|
|
}
|
|
found, online, err := h.nix.Lookup(r.Context(), endpointID)
|
|
if err != nil || !found {
|
|
return "unknown"
|
|
}
|
|
if online {
|
|
return "online"
|
|
}
|
|
return "offline"
|
|
}
|
|
|
|
func (h *Handler) createDevice(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := h.requireUser(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
EndpointID string `json:"endpoint_id"`
|
|
GroupID *int64 `json:"group_id"`
|
|
}
|
|
if !decodeJSON(w, r, maxJSONBody, &req) {
|
|
return
|
|
}
|
|
name := strings.TrimSpace(req.Name)
|
|
if !validDisplayName(name, 64) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "请填写设备名称,最多 64 个字")
|
|
return
|
|
}
|
|
endpointID := strings.TrimSpace(req.EndpointID)
|
|
var password string
|
|
provisioned := false
|
|
if endpointID == "" {
|
|
if h.nix == nil {
|
|
writeError(w, http.StatusServiceUnavailable, "unavailable", "暂时无法新建设备")
|
|
return
|
|
}
|
|
id, err := h.store.AllocateEndpointID(r.Context())
|
|
if errors.Is(err, store.ErrEndpointExhausted) {
|
|
writeError(w, http.StatusConflict, "conflict", "不能再新增设备")
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.log.Error("allocate endpoint id", "err", err)
|
|
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
|
return
|
|
}
|
|
pw, err := h.nix.CreateEndpoint(r.Context(), id, name)
|
|
if err != nil {
|
|
h.log.Error("provision endpoint failed")
|
|
writeError(w, http.StatusServiceUnavailable, "unavailable", "暂时无法新建设备")
|
|
return
|
|
}
|
|
endpointID = id
|
|
password = pw
|
|
provisioned = true
|
|
} else if !validEndpointID(endpointID) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "端编号不正确")
|
|
return
|
|
} else {
|
|
found, _, err := h.nix.Lookup(r.Context(), endpointID)
|
|
if err != nil {
|
|
h.log.Error("lookup endpoint")
|
|
writeError(w, http.StatusServiceUnavailable, "unavailable", "暂时无法确认这台设备")
|
|
return
|
|
}
|
|
if !found {
|
|
writeError(w, http.StatusNotFound, "not_found", "找不到这台设备")
|
|
return
|
|
}
|
|
}
|
|
|
|
var hash string
|
|
if provisioned {
|
|
var err error
|
|
hash, err = auth.HashPassword(password)
|
|
if err != nil {
|
|
h.rollbackEndpoint(r, endpointID)
|
|
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
|
return
|
|
}
|
|
}
|
|
device, err := h.store.AddDevice(r.Context(), user.ID, name, endpointID, hash, req.GroupID)
|
|
if err != nil {
|
|
if provisioned {
|
|
h.rollbackEndpoint(r, endpointID)
|
|
}
|
|
switch {
|
|
case errors.Is(err, store.ErrConflict):
|
|
writeError(w, http.StatusConflict, "conflict", "这台设备已经在列表里")
|
|
case errors.Is(err, store.ErrNotFound):
|
|
writeError(w, http.StatusNotFound, "not_found", "找不到这个分组")
|
|
default:
|
|
h.log.Error("add device", "err", err)
|
|
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
|
}
|
|
return
|
|
}
|
|
view := viewDevice(device, h.presence(r, device.EndpointID))
|
|
if provisioned {
|
|
view.Password = password
|
|
}
|
|
writeOK(w, view)
|
|
}
|
|
|
|
func (h *Handler) rollbackEndpoint(r *http.Request, endpointID string) {
|
|
if h.nix == nil || endpointID == "" {
|
|
return
|
|
}
|
|
if err := h.nix.DeleteEndpoint(r.Context(), endpointID); err != nil {
|
|
h.log.Error("rollback endpoint", "endpoint", endpointID)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) updateDevice(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := h.requireUser(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := parseID(r.PathValue("id"))
|
|
if !ok {
|
|
writeError(w, http.StatusNotFound, "not_found", "找不到这台设备")
|
|
return
|
|
}
|
|
var req struct {
|
|
Name *string `json:"name"`
|
|
GroupID json.RawMessage `json:"group_id"`
|
|
}
|
|
if !decodeJSON(w, r, maxJSONBody, &req) {
|
|
return
|
|
}
|
|
var name *string
|
|
if req.Name != nil {
|
|
trimmed := strings.TrimSpace(*req.Name)
|
|
if !validDisplayName(trimmed, 64) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "请填写设备名称,最多 64 个字")
|
|
return
|
|
}
|
|
name = &trimmed
|
|
}
|
|
var group store.GroupChange
|
|
if req.GroupID != nil {
|
|
group.Set = true
|
|
if string(req.GroupID) == "null" {
|
|
group.ID = nil
|
|
} else {
|
|
var n int64
|
|
if err := json.Unmarshal(req.GroupID, &n); err != nil || n <= 0 {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "找不到这个分组")
|
|
return
|
|
}
|
|
group.ID = &n
|
|
}
|
|
}
|
|
if name == nil && !group.Set {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "没有要修改的内容")
|
|
return
|
|
}
|
|
device, err := h.store.UpdateDevice(r.Context(), user.ID, id, name, group)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
writeError(w, http.StatusNotFound, "not_found", "找不到这台设备或分组")
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.log.Error("update device", "err", err)
|
|
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
|
return
|
|
}
|
|
writeOK(w, viewDevice(device, h.presence(r, device.EndpointID)))
|
|
}
|
|
|
|
func (h *Handler) deleteDevice(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := h.requireUser(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := parseID(r.PathValue("id"))
|
|
if !ok {
|
|
writeError(w, http.StatusNotFound, "not_found", "找不到这台设备")
|
|
return
|
|
}
|
|
err := h.store.DeleteDevice(r.Context(), user.ID, id)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
writeError(w, http.StatusNotFound, "not_found", "找不到这台设备")
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.log.Error("delete device", "err", err)
|
|
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
|
return
|
|
}
|
|
writeOK(w, map[string]any{})
|
|
}
|