─── Inbound handlers ────────────────────────────────────────────────────────
(w http.ResponseWriter, r *http.Request)
| 40 | // ─── Inbound handlers ──────────────────────────────────────────────────────── |
| 41 | |
| 42 | func (a *inboundAPI) handleInbounds(w http.ResponseWriter, r *http.Request) { |
| 43 | switch r.Method { |
| 44 | case http.MethodGet: |
| 45 | var items []inbounds.Inbound |
| 46 | var err error |
| 47 | if nodeID := r.URL.Query().Get("node_id"); nodeID != "" { |
| 48 | items, err = a.store.ListInboundsByNode(nodeID) |
| 49 | } else { |
| 50 | items, err = a.store.ListInbounds() |
| 51 | } |
| 52 | if err != nil { |
| 53 | internalError(w, r, err) |
| 54 | return |
| 55 | } |
| 56 | userCounts, err := a.userStore.CountUsersByInbound() |
| 57 | if err != nil { |
| 58 | internalError(w, r, err) |
| 59 | return |
| 60 | } |
| 61 | writeJSON(w, http.StatusOK, map[string]any{"inbounds": items, "user_counts": userCounts}) |
| 62 | case http.MethodPost: |
| 63 | var req inbounds.Inbound |
| 64 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 65 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "invalid json body"}) |
| 66 | return |
| 67 | } |
| 68 | if req.NodeID == "" || req.Protocol == "" || req.Port == 0 { |
| 69 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "node_id, protocol and port are required"}) |
| 70 | return |
| 71 | } |
| 72 | if !supportedProtocol(req.Protocol) { |
| 73 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "unsupported protocol"}) |
| 74 | return |
| 75 | } |
| 76 | if req.ID == "" { |
| 77 | req.ID = idgen.NextString() |
| 78 | } |
| 79 | if req.Tag == "" { |
| 80 | req.Tag = fmt.Sprintf("%s-%d", req.Protocol, req.Port) |
| 81 | } |
| 82 | trimInbound(&req) |
| 83 | item, err := a.store.UpsertInbound(req) |
| 84 | if err != nil { |
| 85 | internalError(w, r, err) |
| 86 | return |
| 87 | } |
| 88 | a.applyInboundNode(item.NodeID) |
| 89 | writeJSON(w, http.StatusOK, item) |
| 90 | default: |
| 91 | writeMethodNotAllowed(w, http.MethodGet+", "+http.MethodPost) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (a *inboundAPI) handleInboundRoutes(w http.ResponseWriter, r *http.Request) { |
| 96 | path := strings.TrimPrefix(r.URL.Path, "/v1/inbounds/") |
nothing calls this directly
no test coverage detected