handleUserInbounds 处理用户的节点访问凭据列表(GET / POST)。
(w http.ResponseWriter, r *http.Request, userID string)
| 351 | |
| 352 | // handleUserInbounds 处理用户的节点访问凭据列表(GET / POST)。 |
| 353 | func (a *userAPI) handleUserInbounds(w http.ResponseWriter, r *http.Request, userID string) { |
| 354 | switch r.Method { |
| 355 | case http.MethodGet: |
| 356 | accesses, err := a.users.ListUserInboundsByUser(userID) |
| 357 | if err != nil { |
| 358 | internalError(w, r, err) |
| 359 | return |
| 360 | } |
| 361 | writeJSON(w, http.StatusOK, map[string]any{"inbounds": accesses, "total": len(accesses)}) |
| 362 | case http.MethodPost: |
| 363 | if _, err := a.users.GetUser(userID); err != nil { |
| 364 | writeUserError(w, err) |
| 365 | return |
| 366 | } |
| 367 | var req createAccessRequest |
| 368 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 369 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "invalid json body"}) |
| 370 | return |
| 371 | } |
| 372 | if req.InboundID == "" { |
| 373 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "inbound_id is required"}) |
| 374 | return |
| 375 | } |
| 376 | ib, err := a.inboundStore.GetInbound(req.InboundID) |
| 377 | if err != nil { |
| 378 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "inbound not found"}) |
| 379 | return |
| 380 | } |
| 381 | if strings.TrimSpace(req.ID) == "" { |
| 382 | req.ID = idgen.NextString() |
| 383 | } |
| 384 | if req.UUID == "" { |
| 385 | req.UUID = randomUUID() |
| 386 | } |
| 387 | if req.Secret == "" { |
| 388 | req.Secret = randomToken(12) |
| 389 | } |
| 390 | acc, err := a.users.UpsertUserInbound(users.UserInbound{ |
| 391 | ID: req.ID, |
| 392 | UserID: userID, |
| 393 | InboundID: req.InboundID, |
| 394 | NodeID: ib.NodeID, |
| 395 | UUID: req.UUID, |
| 396 | Secret: req.Secret, |
| 397 | }) |
| 398 | if err != nil { |
| 399 | internalError(w, r, err) |
| 400 | return |
| 401 | } |
| 402 | a.applyNodes([]string{ib.NodeID}) |
| 403 | writeJSON(w, http.StatusOK, acc) |
| 404 | default: |
| 405 | writeMethodNotAllowed(w, http.MethodGet+", "+http.MethodPost) |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | func (a *userAPI) handleUserInbound(w http.ResponseWriter, r *http.Request, userID, ibID string) { |
| 410 | switch r.Method { |
no test coverage detected