(w http.ResponseWriter, r *http.Request)
| 650 | |
| 651 | |
| 652 | func (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 | } |
nothing calls this directly
no test coverage detected