handleAccessSubscription 返回该节点访问凭据对应的所有订阅链接。
(w http.ResponseWriter, r *http.Request, userID, ibID string)
| 566 | |
| 567 | // handleAccessSubscription 返回该节点访问凭据对应的所有订阅链接。 |
| 568 | func (a *userAPI) handleAccessSubscription(w http.ResponseWriter, r *http.Request, userID, ibID string) { |
| 569 | if r.Method != http.MethodGet { |
| 570 | writeMethodNotAllowed(w, http.MethodGet) |
| 571 | return |
| 572 | } |
| 573 | acc, err := a.users.GetUserInbound(ibID) |
| 574 | if err != nil { |
| 575 | writeUserInboundError(w, err) |
| 576 | return |
| 577 | } |
| 578 | if acc.UserID != userID { |
| 579 | writeJSON(w, http.StatusNotFound, map[string]any{"error": "user inbound not found"}) |
| 580 | return |
| 581 | } |
| 582 | user, err := a.users.GetUser(userID) |
| 583 | if err != nil { |
| 584 | writeUserError(w, err) |
| 585 | return |
| 586 | } |
| 587 | |
| 588 | // 按 InboundID 获取对应的 inbound 列表;旧版记录(InboundID 为空)回退到节点级别 |
| 589 | var ibList []inbounds.Inbound |
| 590 | if acc.InboundID != "" { |
| 591 | ib, err := a.inboundStore.GetInbound(acc.InboundID) |
| 592 | if err != nil { |
| 593 | internalError(w, r, err) |
| 594 | return |
| 595 | } |
| 596 | ibList = []inbounds.Inbound{ib} |
| 597 | } else { |
| 598 | ibList, err = a.inboundStore.ListInboundsByNode(acc.NodeID) |
| 599 | if err != nil { |
| 600 | internalError(w, r, err) |
| 601 | return |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | type linkItem struct { |
| 606 | Protocol string `json:"protocol"` |
| 607 | Remark string `json:"remark"` |
| 608 | Link string `json:"link"` |
| 609 | } |
| 610 | |
| 611 | links := make([]linkItem, 0) |
| 612 | for _, ib := range ibList { |
| 613 | hosts, err := a.inboundStore.ListHostsByInbound(ib.ID) |
| 614 | if err != nil { |
| 615 | continue |
| 616 | } |
| 617 | for _, h := range hosts { |
| 618 | link := subscription.Link(ib, h, acc, user) |
| 619 | remark := h.Remark |
| 620 | if remark == "" { |
| 621 | remark = h.Address |
| 622 | } |
| 623 | links = append(links, linkItem{ |
| 624 | Protocol: ib.Protocol, |
| 625 | Remark: remark, |
no test coverage detected