MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / handleInboundRoutes

Method handleInboundRoutes

internal/serverapi/inbounds.go:95–179  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

93}
94
95func (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

Callers

nothing calls this directly

Calls 12

handleInboundUsersMethod · 0.95
applyInboundNodeMethod · 0.95
writeInboundErrorFunction · 0.85
supportedProtocolFunction · 0.85
trimInboundFunction · 0.85
internalErrorFunction · 0.85
writeMethodNotAllowedFunction · 0.85
writeJSONFunction · 0.70
GetInboundMethod · 0.65
UpsertInboundMethod · 0.65
DeleteInboundMethod · 0.65

Tested by

no test coverage detected