handleResetTraffic 重置用户流量。
(w http.ResponseWriter, r *http.Request, userID string)
| 852 | |
| 853 | // handleResetTraffic 重置用户流量。 |
| 854 | func (a *userAPI) handleResetTraffic(w http.ResponseWriter, r *http.Request, userID string) { |
| 855 | if r.Method != http.MethodPost { |
| 856 | writeJSON(w, http.StatusMethodNotAllowed, map[string]any{"error": "method not allowed"}) |
| 857 | return |
| 858 | } |
| 859 | user, err := a.users.GetUser(userID) |
| 860 | if err != nil { |
| 861 | writeJSON(w, http.StatusNotFound, map[string]any{"error": "user not found"}) |
| 862 | return |
| 863 | } |
| 864 | now := time.Now().UTC() |
| 865 | user.UploadBytes = 0 |
| 866 | user.DownloadBytes = 0 |
| 867 | user.UsedBytes = 0 |
| 868 | user.RawUploadBytes = 0 |
| 869 | user.RawDownloadBytes = 0 |
| 870 | user.LastTrafficResetAt = &now |
| 871 | if _, err := a.users.UpsertUser(user); err != nil { |
| 872 | internalError(w, r, fmt.Errorf("failed to reset traffic: %w", err)) |
| 873 | return |
| 874 | } |
| 875 | if err := a.users.ClearUserNodeDailyUsage(userID); err != nil { |
| 876 | internalError(w, r, fmt.Errorf("failed to clear node daily usage: %w", err)) |
| 877 | return |
| 878 | } |
| 879 | accesses, err := a.users.ListUserInboundsByUser(userID) |
| 880 | if err != nil { |
| 881 | log.Printf("handleResetTraffic: list user inbounds %s: %v", userID, err) |
| 882 | } |
| 883 | affectedNodeIDs := make(map[string]struct{}) |
| 884 | for _, acc := range accesses { |
| 885 | affectedNodeIDs[acc.NodeID] = struct{}{} |
| 886 | } |
| 887 | nodeIDs := make([]string, 0, len(affectedNodeIDs)) |
| 888 | for id := range affectedNodeIDs { |
| 889 | nodeIDs = append(nodeIDs, id) |
| 890 | } |
| 891 | a.applyNodes(nodeIDs) |
| 892 | writeJSON(w, http.StatusOK, map[string]any{"ok": true}) |
| 893 | } |
| 894 | |
| 895 | // handleSubLogs 返回用户的订阅访问日志。 |
| 896 | func (a *userAPI) handleSubLogs(w http.ResponseWriter, r *http.Request, userID string) { |
no test coverage detected