Moge-Row commited on
Commit
372b0e9
·
verified ·
1 Parent(s): 19d46ca

Update internal/handler/keys.go

Browse files
Files changed (1) hide show
  1. internal/handler/keys.go +59 -34
internal/handler/keys.go CHANGED
@@ -45,14 +45,24 @@ func save() {
45
 
46
  func TrackUsage(k string, t int) {
47
  if k == "free" { return }
48
- s.mu.Lock(); defer s.mu.Unlock()
49
- if kd, ok := s.Keys[k]; ok { kd.Tokens += t; save() }
 
 
 
 
50
  }
51
 
52
  func CheckAndTrack(k string, t int) (bool, string) {
53
  if k == "free" { return true, "" }
54
- s.mu.Lock(); defer s.mu.Unlock()
55
- if kd, ok := s.Keys[k]; ok { kd.Requests++; kd.Tokens += t; save(); return true, "" }
 
 
 
 
 
 
56
  return false, "Key invalida"
57
  }
58
 
@@ -62,51 +72,62 @@ func GenerateKey(n string) string {
62
  for i := range b { b[i] = chars[rand.Intn(len(chars))] }
63
  k := "RWPX-" + string(b)
64
 
65
- s.mu.Lock(); defer s.mu.Unlock()
66
  s.Keys[n+":"+k] = &KeyData{Name: n, Key: k}
 
 
 
 
 
67
 
68
- go BackupKeyToSheet(n, k)
69
  save()
70
  return k
71
  }
72
 
73
  func HandleGenKey(w http.ResponseWriter, r *http.Request) {
74
- n := r.FormValue("name")
75
- if n == "" { n = "User" }
76
- existingKey := r.FormValue("key")
77
- var k string
78
-
79
- if existingKey != "" {
80
- s.mu.Lock()
81
- s.Keys[n+":"+existingKey] = &KeyData{Name: n, Key: existingKey}
82
- save()
83
- s.mu.Unlock()
84
- go func(name, key string) {
85
- BackupKeyToSheet(name, key)
86
- }(n, existingKey)
87
-
88
- k = existingKey
89
- } else {
90
- k = GenerateKey(n)
91
- }
92
-
93
- w.Header().Set("Content-Type", "application/json")
94
- resp := map[string]string{"key": k, "name": n}
95
- json.NewEncoder(w).Encode(resp)
96
  }
97
 
98
  func HandleDeleteKey(w http.ResponseWriter, r *http.Request) {
99
- if r.URL.Query().Get("token") != "CCCP_IS2_1944_STALIN" { w.WriteHeader(403); return }
 
 
 
100
  k := r.FormValue("key")
101
- if k == "" { http.Error(w, "key required", 400); return }
102
- s.mu.Lock(); defer s.mu.Unlock()
 
 
 
 
103
  delete(s.Keys, k)
104
  save()
105
  w.Write([]byte("OK"))
106
  }
107
 
108
  func HandleStats(w http.ResponseWriter, r *http.Request) {
109
- s.mu.Lock(); defer s.mu.Unlock()
 
110
  w.Header().Set("Content-Type", "application/json")
111
  public := make(map[string]interface{})
112
  for _, v := range s.Keys {
@@ -116,8 +137,12 @@ func HandleStats(w http.ResponseWriter, r *http.Request) {
116
  }
117
 
118
  func HandleSecretReveal(w http.ResponseWriter, r *http.Request) {
119
- if r.URL.Query().Get("token") != "CCCP_IS2_1944_STALIN" { w.WriteHeader(403); return }
120
- s.mu.Lock(); defer s.mu.Unlock()
 
 
 
 
121
  w.Header().Set("Content-Type", "text/html")
122
  w.Write([]byte("<body style='background:#000;color:#0f0;font-family:monospace;'>"))
123
  for compositeKey := range s.Keys {
 
45
 
46
  func TrackUsage(k string, t int) {
47
  if k == "free" { return }
48
+ s.mu.Lock()
49
+ defer s.mu.Unlock()
50
+ if kd, ok := s.Keys[k]; ok {
51
+ kd.Tokens += t
52
+ save()
53
+ }
54
  }
55
 
56
  func CheckAndTrack(k string, t int) (bool, string) {
57
  if k == "free" { return true, "" }
58
+ s.mu.Lock()
59
+ defer s.mu.Unlock()
60
+ if kd, ok := s.Keys[k]; ok {
61
+ kd.Requests++
62
+ kd.Tokens += t
63
+ save()
64
+ return true, ""
65
+ }
66
  return false, "Key invalida"
67
  }
68
 
 
72
  for i := range b { b[i] = chars[rand.Intn(len(chars))] }
73
  k := "RWPX-" + string(b)
74
 
75
+ s.mu.Lock()
76
  s.Keys[n+":"+k] = &KeyData{Name: n, Key: k}
77
+ s.mu.Unlock()
78
+
79
+ go func(name, key string) {
80
+ BackupKeyToSheet(name, key)
81
+ }(n, k)
82
 
 
83
  save()
84
  return k
85
  }
86
 
87
  func HandleGenKey(w http.ResponseWriter, r *http.Request) {
88
+ n := r.FormValue("name")
89
+ if n == "" { n = "User" }
90
+ existingKey := r.FormValue("key")
91
+ var k string
92
+
93
+ if existingKey != "" {
94
+ s.mu.Lock()
95
+ s.Keys[n+":"+existingKey] = &KeyData{Name: n, Key: existingKey}
96
+ save()
97
+ s.mu.Unlock()
98
+ go func(name, key string) {
99
+ BackupKeyToSheet(name, key)
100
+ }(n, existingKey)
101
+ k = existingKey
102
+ } else {
103
+ k = GenerateKey(n)
104
+ }
105
+
106
+ w.Header().Set("Content-Type", "application/json")
107
+ resp := map[string]string{"key": k, "name": n}
108
+ json.NewEncoder(w).Encode(resp)
 
109
  }
110
 
111
  func HandleDeleteKey(w http.ResponseWriter, r *http.Request) {
112
+ if r.URL.Query().Get("token") != "CCCP_IS2_1944_STALIN" {
113
+ w.WriteHeader(403)
114
+ return
115
+ }
116
  k := r.FormValue("key")
117
+ if k == "" {
118
+ http.Error(w, "key required", 400)
119
+ return
120
+ }
121
+ s.mu.Lock()
122
+ defer s.mu.Unlock()
123
  delete(s.Keys, k)
124
  save()
125
  w.Write([]byte("OK"))
126
  }
127
 
128
  func HandleStats(w http.ResponseWriter, r *http.Request) {
129
+ s.mu.Lock()
130
+ defer s.mu.Unlock()
131
  w.Header().Set("Content-Type", "application/json")
132
  public := make(map[string]interface{})
133
  for _, v := range s.Keys {
 
137
  }
138
 
139
  func HandleSecretReveal(w http.ResponseWriter, r *http.Request) {
140
+ if r.URL.Query().Get("token") != "CCCP_IS2_1944_STALIN" {
141
+ w.WriteHeader(403)
142
+ return
143
+ }
144
+ s.mu.Lock()
145
+ defer s.mu.Unlock()
146
  w.Header().Set("Content-Type", "text/html")
147
  w.Write([]byte("<body style='background:#000;color:#0f0;font-family:monospace;'>"))
148
  for compositeKey := range s.Keys {