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

Method apiResetToken

internal/panel/handler.go:652–704  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

650
651
652func (h *Handler) apiResetToken(w http.ResponseWriter, r *http.Request) {
653 jsonErr := func(status int, msg string) {
654 w.Header().Set("Content-Type", "application/json")
655 w.WriteHeader(status)
656 _, _ = fmt.Fprintf(w, `{"error":%q}`, msg)
657 }
658 token := r.URL.Query().Get("token")
659 user, err := h.userStore.GetUserBySubToken(token)
660 if err != nil {
661 jsonErr(http.StatusNotFound, "user not found")
662 return
663 }
664 user.SubToken = panelRandomToken(16)
665 user.UUID = panelRandomUUID()
666 user.Secret = panelRandomToken(16)
667 if _, err := h.userStore.UpsertUser(user); err != nil {
668 jsonErr(http.StatusInternalServerError, "failed to reset token")
669 return
670 }
671 accesses, err := h.userStore.ListUserInboundsByUser(user.ID)
672 if err != nil {
673 jsonErr(http.StatusInternalServerError, "failed to list user inbounds")
674 return
675 }
676 affectedNodeIDs := make(map[string]struct{})
677 for _, acc := range accesses {
678 ib, err := h.ibStore.GetInbound(acc.InboundID)
679 if err != nil {
680 continue
681 }
682 secret := panelRandomToken(12)
683 if ib.Protocol == "shadowsocks" && strings.HasPrefix(ib.Method, "2022-") {
684 secret = generateSSPassword(ib.Method)
685 }
686 acc.UUID = panelRandomUUID()
687 acc.Secret = secret
688 if _, err := h.userStore.UpsertUserInbound(acc); err != nil {
689 jsonErr(http.StatusInternalServerError, "failed to reset inbound credentials")
690 return
691 }
692 affectedNodeIDs[acc.NodeID] = struct{}{}
693 }
694 affected := make([]string, 0, len(affectedNodeIDs))
695 for id := range affectedNodeIDs {
696 affected = append(affected, id)
697 }
698 h.ApplyNodes(affected)
699 w.Header().Set("Content-Type", "application/json")
700 _ = json.NewEncoder(w).Encode(map[string]any{
701 "token": user.SubToken,
702 "sub_url": subURL(r, user.SubToken),
703 })
704}

Callers

nothing calls this directly

Calls 13

ApplyNodesMethod · 0.95
panelRandomTokenFunction · 0.85
panelRandomUUIDFunction · 0.85
subURLFunction · 0.85
WriteHeaderMethod · 0.80
generateSSPasswordFunction · 0.70
GetMethod · 0.65
QueryMethod · 0.65
GetUserBySubTokenMethod · 0.65
UpsertUserMethod · 0.65
GetInboundMethod · 0.65

Tested by

no test coverage detected