-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
386 lines (355 loc) · 11.4 KB
/
Copy pathconfig.go
File metadata and controls
386 lines (355 loc) · 11.4 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
)
// WhitelabelConfig stores branding info for the client.
type WhitelabelConfig struct {
ID int `json:"id"`
Name string `json:"name"`
LogoURL string `json:"logo_url,omitempty"`
PrimaryColor string `json:"primary_color,omitempty"`
AccentColor string `json:"accent_color,omitempty"`
}
// PrinterDPIEntry stores the DPI for a printer along with how it was determined.
type PrinterDPIEntry struct {
DPI int `json:"dpi"`
Source string `json:"source"` // "driver", "tspl_probe", "manual"
}
// AppConfig is the persisted configuration for tsc-bridge.
type AppConfig struct {
Port int `json:"port"`
DefaultPrinter string `json:"default_printer"`
DefaultPreset string `json:"default_preset"`
CustomPresets []LabelPreset `json:"custom_presets"`
AutoStart bool `json:"auto_start"`
NetworkScanEnabled bool `json:"network_scan_enabled"`
NetworkScanInterval int `json:"network_scan_interval"`
ManualPrinters []string `json:"manual_printers"`
ShareEnabled bool `json:"share_enabled"`
SharePort int `json:"share_port"`
SharePrinter string `json:"share_printer"`
ApiURL string `json:"api_url"`
ApiToken string `json:"api_token"`
ApiKey string `json:"api_key"`
ApiSecret string `json:"api_secret"`
ApiWhiteLabel int `json:"api_wl"`
Whitelabel WhitelabelConfig `json:"whitelabel"`
PrinterDPI map[string]PrinterDPIEntry `json:"printer_dpi"`
}
var (
appConfig AppConfig
configMu sync.RWMutex
configPath string
)
func defaultConfig() AppConfig {
return AppConfig{
Port: 9638,
DefaultPrinter: "",
DefaultPreset: "matrix-3x1-30x22",
CustomPresets: []LabelPreset{},
AutoStart: true,
NetworkScanEnabled: true,
NetworkScanInterval: 30,
ManualPrinters: []string{},
ShareEnabled: false,
SharePort: 9100,
SharePrinter: "",
PrinterDPI: map[string]PrinterDPIEntry{},
}
}
// configDir returns the platform-specific config directory.
func configDir() string {
if runtime.GOOS == "windows" {
appdata := os.Getenv("APPDATA")
if appdata == "" {
appdata = os.Getenv("LOCALAPPDATA")
}
return filepath.Join(appdata, "tsc-bridge")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".tsc-bridge")
}
// initConfig loads or creates the config file.
func initConfig() {
configPath = filepath.Join(configDir(), "config.json")
appConfig = defaultConfig()
data, err := os.ReadFile(configPath)
if err != nil {
log.Printf("[config] No config file found, using defaults (%s)", configPath)
return
}
if err := json.Unmarshal(data, &appConfig); err != nil {
log.Printf("[config] Error parsing config: %v, using defaults", err)
appConfig = defaultConfig()
return
}
if appConfig.CustomPresets == nil {
appConfig.CustomPresets = []LabelPreset{}
}
if appConfig.ManualPrinters == nil {
appConfig.ManualPrinters = []string{}
}
if appConfig.PrinterDPI == nil {
appConfig.PrinterDPI = map[string]PrinterDPIEntry{}
}
// Decrypt secrets (supports plaintext migration — unencrypted values pass through)
if appConfig.ApiKey != "" {
if dec, err := decryptString(appConfig.ApiKey); err == nil {
appConfig.ApiKey = dec
} else {
log.Printf("[config] Warning: could not decrypt api_key: %v", err)
}
}
if appConfig.ApiSecret != "" {
if dec, err := decryptString(appConfig.ApiSecret); err == nil {
appConfig.ApiSecret = dec
} else {
log.Printf("[config] Warning: could not decrypt api_secret: %v", err)
}
}
if appConfig.ApiToken != "" {
if dec, err := decryptString(appConfig.ApiToken); err == nil {
appConfig.ApiToken = dec
} else {
log.Printf("[config] Warning: could not decrypt api_token: %v", err)
}
}
// Re-encrypt if loaded as plaintext (auto-migration)
needsSave := false
if appConfig.ApiKey != "" && !isEncrypted(appConfig.ApiKey) {
needsSave = true
}
if appConfig.ApiSecret != "" && !isEncrypted(appConfig.ApiSecret) {
needsSave = true
}
log.Printf("[config] Loaded from %s (printer=%s, preset=%s, api=%v)", configPath, appConfig.DefaultPrinter, appConfig.DefaultPreset, appConfig.ApiURL != "")
if needsSave {
log.Printf("[config] Migrating plaintext secrets to encrypted — re-saving config")
saveConfig()
}
}
// saveConfig persists the current config to disk with secrets encrypted.
func saveConfig() error {
configMu.RLock()
// Copy config and encrypt sensitive fields before serialization
cfgCopy := appConfig
configMu.RUnlock()
if cfgCopy.ApiKey != "" {
if enc, err := encryptString(cfgCopy.ApiKey); err == nil {
cfgCopy.ApiKey = enc
}
}
if cfgCopy.ApiSecret != "" {
if enc, err := encryptString(cfgCopy.ApiSecret); err == nil {
cfgCopy.ApiSecret = enc
}
}
if cfgCopy.ApiToken != "" {
if enc, err := encryptString(cfgCopy.ApiToken); err == nil {
cfgCopy.ApiToken = enc
}
}
data, err := json.MarshalIndent(cfgCopy, "", " ")
if err != nil {
return err
}
dir := filepath.Dir(configPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
return os.WriteFile(configPath, data, 0644)
}
// getConfig returns a copy of the current config.
func getConfig() AppConfig {
configMu.RLock()
defer configMu.RUnlock()
c := appConfig
c.CustomPresets = make([]LabelPreset, len(appConfig.CustomPresets))
copy(c.CustomPresets, appConfig.CustomPresets)
return c
}
// safeConfigForClient returns config without sensitive API fields.
func safeConfigForClient() map[string]any {
cfg := getConfig()
return map[string]any{
"port": cfg.Port,
"default_printer": cfg.DefaultPrinter,
"default_preset": cfg.DefaultPreset,
"custom_presets": cfg.CustomPresets,
"auto_start": cfg.AutoStart,
"network_scan_enabled": cfg.NetworkScanEnabled,
"network_scan_interval": cfg.NetworkScanInterval,
"manual_printers": cfg.ManualPrinters,
"share_enabled": cfg.ShareEnabled,
"share_port": cfg.SharePort,
"share_printer": cfg.SharePrinter,
"api_configured": cfg.ApiURL != "" && (cfg.ApiKey != "" || cfg.ApiToken != ""),
"api_url": cfg.ApiURL,
"whitelabel": cfg.Whitelabel,
"printer_dpi": func() map[string]int {
flat := make(map[string]int, len(cfg.PrinterDPI))
for name, entry := range cfg.PrinterDPI {
flat[name] = entry.DPI
}
return flat
}(),
}
}
// --- HTTP handlers ---
func handleConfig(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
jsonResponse(w, http.StatusOK, safeConfigForClient())
case http.MethodPut:
var incoming AppConfig
if err := json.NewDecoder(r.Body).Decode(&incoming); err != nil {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()})
return
}
configMu.Lock()
if incoming.Port > 0 {
appConfig.Port = incoming.Port
}
appConfig.DefaultPrinter = incoming.DefaultPrinter
if incoming.DefaultPreset != "" {
appConfig.DefaultPreset = incoming.DefaultPreset
}
appConfig.AutoStart = incoming.AutoStart
appConfig.NetworkScanEnabled = incoming.NetworkScanEnabled
if incoming.NetworkScanInterval > 0 {
appConfig.NetworkScanInterval = incoming.NetworkScanInterval
}
if incoming.ManualPrinters != nil {
appConfig.ManualPrinters = incoming.ManualPrinters
}
appConfig.ShareEnabled = incoming.ShareEnabled
if incoming.SharePort > 0 {
appConfig.SharePort = incoming.SharePort
}
appConfig.SharePrinter = incoming.SharePrinter
if incoming.ApiURL != "" {
appConfig.ApiURL = incoming.ApiURL
}
if incoming.ApiKey != "" {
appConfig.ApiKey = incoming.ApiKey
}
if incoming.ApiSecret != "" {
appConfig.ApiSecret = incoming.ApiSecret
}
if incoming.ApiToken != "" {
appConfig.ApiToken = incoming.ApiToken
}
if incoming.ApiWhiteLabel > 0 {
appConfig.ApiWhiteLabel = incoming.ApiWhiteLabel
}
if incoming.Whitelabel.ID > 0 {
appConfig.Whitelabel = incoming.Whitelabel
}
configMu.Unlock()
if err := saveConfig(); err != nil {
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "save failed: " + err.Error()})
return
}
jsonResponse(w, http.StatusOK, map[string]string{"status": "saved"})
default:
jsonResponse(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
}
}
func handleWhitelabel(w http.ResponseWriter, r *http.Request) {
cfg := getConfig()
wl := cfg.Whitelabel
if wl.Name == "" {
wl.Name = "TSC Bridge"
}
jsonResponse(w, http.StatusOK, wl)
}
func handlePresets(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
cfg := getConfig()
jsonResponse(w, http.StatusOK, map[string]any{
"presets": getAllPresets(cfg.CustomPresets),
})
case http.MethodPost:
var preset LabelPreset
if err := json.NewDecoder(r.Body).Decode(&preset); err != nil {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()})
return
}
if preset.ID == "" || preset.Name == "" {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "id and name are required"})
return
}
// Prevent overwriting built-in presets
for _, bp := range builtinPresets {
if bp.ID == preset.ID {
jsonResponse(w, http.StatusConflict, map[string]string{"error": "cannot overwrite built-in preset"})
return
}
}
preset.Builtin = false
configMu.Lock()
// Replace if exists, otherwise append
found := false
for i, cp := range appConfig.CustomPresets {
if cp.ID == preset.ID {
appConfig.CustomPresets[i] = preset
found = true
break
}
}
if !found {
appConfig.CustomPresets = append(appConfig.CustomPresets, preset)
}
configMu.Unlock()
if err := saveConfig(); err != nil {
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "save failed: " + err.Error()})
return
}
jsonResponse(w, http.StatusOK, map[string]string{"status": "saved", "id": preset.ID})
case http.MethodDelete:
// Extract preset ID from path: /presets/my-preset-id
path := strings.TrimPrefix(r.URL.Path, "/presets/")
if path == "" || path == r.URL.Path {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "preset id required in path"})
return
}
// Prevent deleting built-in presets
for _, bp := range builtinPresets {
if bp.ID == path {
jsonResponse(w, http.StatusForbidden, map[string]string{"error": "cannot delete built-in preset"})
return
}
}
configMu.Lock()
filtered := appConfig.CustomPresets[:0]
deleted := false
for _, cp := range appConfig.CustomPresets {
if cp.ID == path {
deleted = true
continue
}
filtered = append(filtered, cp)
}
appConfig.CustomPresets = filtered
configMu.Unlock()
if !deleted {
jsonResponse(w, http.StatusNotFound, map[string]string{"error": "preset not found"})
return
}
if err := saveConfig(); err != nil {
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "save failed: " + err.Error()})
return
}
jsonResponse(w, http.StatusOK, map[string]string{"status": "deleted"})
default:
jsonResponse(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
}
}