Spaces:
Paused
Paused
File size: 3,482 Bytes
2c7e082 068ab77 51e6443 068ab77 b5962b7 bf9eaec b5962b7 068ab77 8cfdb90 bf9eaec 068ab77 4a63a7a bf9eaec 372b0e9 bf9eaec 372b0e9 4a63a7a 068ab77 6c5c62f 372b0e9 bf9eaec 372b0e9 068ab77 bf9eaec 068ab77 372b0e9 0b8b186 372b0e9 068ab77 00db127 068ab77 372b0e9 bf9eaec 372b0e9 068ab77 a6bdc93 372b0e9 a6bdc93 372b0e9 bf9eaec a6bdc93 068ab77 372b0e9 1a1ca42 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | package handler
import (
"encoding/json"
"math/rand"
"net/http"
"os"
"strings"
"sync"
)
type KeyData struct {
Name string `json:"name"`
Key string `json:"key"`
Requests int `json:"requests"`
Tokens int `json:"tokens"`
}
type Store struct {
mu sync.Mutex
Keys map[string]*KeyData `json:"keys"`
}
var s = &Store{Keys: make(map[string]*KeyData)}
const p = "/data/keys.json"
func init() {
os.MkdirAll("/data", 0755)
f, _ := os.ReadFile(p)
if len(f) > 0 {
json.Unmarshal(f, &s)
} else if backup := os.Getenv("BACKUP_KEYS"); backup != "" {
var keys map[string]*KeyData
if json.Unmarshal([]byte(backup), &keys) == nil {
s.Keys = keys
save()
}
}
if s.Keys == nil {
s.Keys = make(map[string]*KeyData)
}
}
func save() {
b, _ := json.MarshalIndent(s, "", " ")
os.WriteFile(p, b, 0644)
}
func TrackUsage(k string, t int) {
if k == "free" {
return
}
s.mu.Lock()
defer s.mu.Unlock()
for _, kd := range s.Keys {
if kd.Key == k {
kd.Tokens += t
save()
return
}
}
}
func CheckAndTrack(k string, t int) (bool, string) {
if strings.HasPrefix(k, "eyJ") {
return true, ""
}
s.mu.Lock()
defer s.mu.Unlock()
for _, kd := range s.Keys {
if kd.Key == k {
kd.Requests++
kd.Tokens += t
save()
return true, ""
}
}
return false, "Key invalida"
}
func GenerateKey(n string) string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, 8)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
}
k := "RWPX-" + string(b)
s.mu.Lock()
s.Keys[n+":"+k] = &KeyData{Name: n, Key: k}
s.mu.Unlock()
go func(name, key string) {
BackupKeyToSheet(name, key)
}(n, k)
save()
return k
}
func HandleGenKey(w http.ResponseWriter, r *http.Request) {
n := r.FormValue("name")
if n == "" {
n = "User"
}
existingKey := r.FormValue("key")
var k string
if existingKey != "" {
s.mu.Lock()
s.Keys[n+":"+existingKey] = &KeyData{Name: n, Key: existingKey}
save()
s.mu.Unlock()
go func(name, key string) {
BackupKeyToSheet(name, key)
}(n, existingKey)
k = existingKey
} else {
k = GenerateKey(n)
}
w.Header().Set("Content-Type", "application/json")
resp := map[string]string{"key": k, "name": n}
json.NewEncoder(w).Encode(resp)
}
func HandleDeleteKey(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("token") != "CCCP_IS2_1944_STALIN" {
w.WriteHeader(403)
return
}
k := r.FormValue("key")
if k == "" {
http.Error(w, "key required", 400)
return
}
s.mu.Lock()
defer s.mu.Unlock()
for compositeKey, kd := range s.Keys {
if kd.Key == k {
delete(s.Keys, compositeKey)
save()
break
}
}
w.Write([]byte("OK"))
}
func HandleStats(w http.ResponseWriter, r *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
public := make(map[string]interface{})
for _, v := range s.Keys {
public[v.Name] = map[string]interface{}{"name": v.Name, "requests": v.Requests}
}
json.NewEncoder(w).Encode(map[string]interface{}{"keys": public})
}
func HandleSecretReveal(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("token") != "CCCP_IS2_1944_STALIN" {
w.WriteHeader(403)
return
}
s.mu.Lock()
defer s.mu.Unlock()
w.Header().Set("Content-Type", "text/html")
w.Write([]byte("<body style='background:#000;color:#0f0;font-family:monospace;'>"))
for compositeKey := range s.Keys {
w.Write([]byte("<p>" + compositeKey + "</p>"))
}
w.Write([]byte("</body>"))
} |