(w http.ResponseWriter, r *http.Request)
| 93 | } |
| 94 | |
| 95 | func (a *inboundAPI) handleInboundRoutes(w http.ResponseWriter, r *http.Request) { |
| 96 | path := strings.TrimPrefix(r.URL.Path, "/v1/inbounds/") |
| 97 | parts := strings.SplitN(path, "/", 2) |
| 98 | id := parts[0] |
| 99 | if id == "" { |
| 100 | writeJSON(w, http.StatusNotFound, map[string]any{"error": "inbound id is required"}) |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | // Sub-routes: /v1/inbounds/{id}/users |
| 105 | if len(parts) == 2 && parts[1] == "users" { |
| 106 | a.handleInboundUsers(w, r, id) |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | switch r.Method { |
| 111 | case http.MethodGet: |
| 112 | item, err := a.store.GetInbound(id) |
| 113 | if err != nil { |
| 114 | writeInboundError(w, err) |
| 115 | return |
| 116 | } |
| 117 | writeJSON(w, http.StatusOK, item) |
| 118 | case http.MethodPut: |
| 119 | var req inbounds.Inbound |
| 120 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 121 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "invalid json body"}) |
| 122 | return |
| 123 | } |
| 124 | req.ID = id |
| 125 | if req.Protocol != "" && !supportedProtocol(req.Protocol) { |
| 126 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "unsupported protocol"}) |
| 127 | return |
| 128 | } |
| 129 | // 合并现有字段 |
| 130 | existing, err := a.store.GetInbound(id) |
| 131 | if err != nil { |
| 132 | writeInboundError(w, err) |
| 133 | return |
| 134 | } |
| 135 | if req.NodeID == "" { |
| 136 | req.NodeID = existing.NodeID |
| 137 | } |
| 138 | if req.Protocol == "" { |
| 139 | req.Protocol = existing.Protocol |
| 140 | } |
| 141 | if req.Tag == "" { |
| 142 | req.Tag = existing.Tag |
| 143 | } |
| 144 | if req.Port == 0 { |
| 145 | req.Port = existing.Port |
| 146 | } |
| 147 | trimInbound(&req) |
| 148 | nodeChanged := req.NodeID != existing.NodeID |
| 149 | item, err := a.store.UpsertInbound(req) |
| 150 | if err != nil { |
| 151 | internalError(w, r, err) |
| 152 | return |
nothing calls this directly
no test coverage detected