Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
203 lines (178 loc) · 5.2 KB
/
Copy pathconfig.go
File metadata and controls
203 lines (178 loc) · 5.2 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
type Config struct {
ListenAddr string
UpstreamBaseURL string
AuthTokens []string
RotationInterval time.Duration
RequestTimeout time.Duration
UserAgent string
APIKeys []string
HTTPProxy string
}
type rawConfig struct {
ListenAddr string `json:"LISTEN_ADDR"`
UpstreamBaseURL string `json:"UPSTREAM_BASE_URL"`
AuthTokens []string `json:"AUTH_TOKENS"`
RotationInterval string `json:"ROTATION_INTERVAL"`
RequestTimeout string `json:"REQUEST_TIMEOUT"`
APIKeys []string `json:"API_KEYS"`
HTTPProxy string `json:"HTTP_PROXY"`
}
func loadConfig(configPath string) (Config, error) {
cfg, err := loadRawConfig(configPath)
if err != nil {
return Config{}, err
}
overrideString(&cfg.ListenAddr, "LISTEN_ADDR")
overrideString(&cfg.UpstreamBaseURL, "UPSTREAM_BASE_URL")
overrideString(&cfg.RotationInterval, "ROTATION_INTERVAL")
overrideString(&cfg.RequestTimeout, "REQUEST_TIMEOUT")
overrideCSV(&cfg.AuthTokens, "AUTH_TOKENS")
overrideCSV(&cfg.APIKeys, "API_KEYS")
overrideString(&cfg.HTTPProxy, "HTTP_PROXY")
rotationInterval, err := time.ParseDuration(strings.TrimSpace(cfg.RotationInterval))
if err != nil {
return Config{}, fmt.Errorf("parse rotation interval: %w", err)
}
requestTimeout, err := time.ParseDuration(strings.TrimSpace(cfg.RequestTimeout))
if err != nil {
return Config{}, fmt.Errorf("parse request timeout: %w", err)
}
finalCfg := Config{
ListenAddr: strings.TrimSpace(cfg.ListenAddr),
UpstreamBaseURL: normalizeUpstreamBaseURL(cfg.UpstreamBaseURL),
AuthTokens: dedupeStrings(cfg.AuthTokens),
RotationInterval: rotationInterval,
RequestTimeout: requestTimeout,
UserAgent: generateUserAgent(),
APIKeys: dedupeStrings(cfg.APIKeys),
HTTPProxy: strings.TrimSpace(cfg.HTTPProxy),
}
switch {
case finalCfg.ListenAddr == "":
return Config{}, errors.New("LISTEN_ADDR cannot be empty")
case finalCfg.UpstreamBaseURL == "":
return Config{}, errors.New("UPSTREAM_BASE_URL cannot be empty")
case len(finalCfg.AuthTokens) == 0:
return Config{}, errors.New("at least one AUTH_TOKENS is required")
case finalCfg.RotationInterval <= 0:
return Config{}, errors.New("ROTATION_INTERVAL must be greater than zero")
case finalCfg.RequestTimeout <= 0:
return Config{}, errors.New("REQUEST_TIMEOUT must be greater than zero")
}
return finalCfg, nil
}
func normalizeUpstreamBaseURL(raw string) string {
raw = strings.TrimRight(strings.TrimSpace(raw), "/")
if raw == "" {
return ""
}
parsed, err := url.Parse(raw)
if err != nil {
return raw
}
if strings.EqualFold(parsed.Host, "codebuff.com") {
parsed.Host = "www.codebuff.com"
}
return strings.TrimRight(parsed.String(), "/")
}
func loadRawConfig(configPath string) (rawConfig, error) {
cfg := rawConfig{
ListenAddr: ":8080",
UpstreamBaseURL: "https://www.codebuff.com",
RotationInterval: "6h",
RequestTimeout: "15m",
}
if configPath != "" {
path, err := filepath.Abs(configPath)
if err != nil {
return rawConfig{}, fmt.Errorf("resolve config path: %w", err)
}
data, err := os.ReadFile(path)
if err != nil {
return rawConfig{}, fmt.Errorf("read config file: %w", err)
}
if err := json.Unmarshal(data, &cfg); err != nil {
return rawConfig{}, fmt.Errorf("parse config file: %w", err)
}
}
return cfg, nil
}
func overrideString(target *string, envName string) {
if value := strings.TrimSpace(os.Getenv(envName)); value != "" {
*target = value
}
}
func overrideCSV(target *[]string, envName string) {
value := strings.TrimSpace(os.Getenv(envName))
if value == "" {
return
}
*target = splitList(value)
}
func splitList(value string) []string {
fields := strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == '\n' || r == '\r'
})
return compactStrings(fields)
}
func compactStrings(values []string) []string {
out := make([]string, 0, len(values))
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" {
continue
}
out = append(out, value)
}
return out
}
func dedupeStrings(values []string) []string {
seen := make(map[string]struct{}, len(values))
out := make([]string, 0, len(values))
for _, value := range compactStrings(values) {
if _, exists := seen[value]; exists {
continue
}
seen[value] = struct{}{}
out = append(out, value)
}
return out
}
func containsString(values []string, needle string) bool {
for _, value := range values {
if value == needle {
return true
}
}
return false
}
func generateUserAgent() string {
return "ai-sdk/openai-compatible/1.0.25/codebuff"
}
// generateClientSessionId generates a per-request session ID matching the
// official SDK: Math.random().toString(36).substring(2, 15) — a ~13-char
// base-36 alphanumeric string.
func generateClientSessionId() string {
buf := make([]byte, 10)
if _, err := rand.Read(buf); err != nil {
buf = []byte(fmt.Sprintf("%d", time.Now().UnixNano()))
}
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
out := make([]byte, 13)
for i := range out {
out[i] = alphabet[buf[i%len(buf)]%36]
}
return string(out)
}