-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
267 lines (230 loc) · 7.7 KB
/
Copy pathmain.go
File metadata and controls
267 lines (230 loc) · 7.7 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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
)
// version is set at build time via -ldflags "-X main.version=<tag>"
var version = "dev"
func main() {
var debug bool
var configFile string
var templateMode bool
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
flag.StringVar(&configFile, "config", "", "Path to config file (JSON)")
flag.BoolVar(&templateMode, "template", false, "Generate a template config file and exit")
var tuiMode bool
flag.BoolVar(&tuiMode, "tui", false, "Enable terminal UI (disables plain log output)")
var webuiAddr, webuiUser, webuiPass string
var webuiReset bool
flag.StringVar(&webuiAddr, "webui", "", "Bind address for web UI (e.g. :8080)")
flag.StringVar(&webuiUser, "webui-user", "", "HTTP Basic Auth username (required with -webui when config has no webui)")
flag.StringVar(&webuiPass, "webui-pass", "", "HTTP Basic Auth password (required with -webui when config has no webui)")
flag.BoolVar(&webuiReset, "webui-reset", false, "Force -webui/-webui-user/-webui-pass to overwrite config file webui settings")
flag.Parse()
args := flag.Args()
if templateMode {
if err := SaveConfigTemplate("hdhomerun_proxy.json"); err != nil {
fmt.Fprintf(os.Stderr, "Error saving template: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
// Load configuration
cfg, err := LoadConfig(configFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
os.Exit(1)
}
// Override debug flag if set in config or command line
if debug {
cfg.Debug = true
}
// Initialize structured logging
level := slog.LevelInfo
if cfg.Debug {
level = slog.LevelDebug
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
}))
slog.SetDefault(logger)
// Resolve webui settings: config takes priority unless -webui-reset or config has no webui.
// When CLI values are used, they are merged into cfg and persisted to the config file.
var mergedWebUIFromCLI bool
if cfg.WebUI.Addr != "" && !webuiReset {
if webuiAddr != "" {
slog.Warn("-webui flags ignored; webui settings loaded from config (use -webui-reset to override)")
}
} else if webuiAddr != "" {
if webuiUser == "" || webuiPass == "" {
fmt.Fprintf(os.Stderr, "Error: -webui-user and -webui-pass are required when -webui is set\n")
os.Exit(1)
}
cfg.WebUI.Addr = webuiAddr
cfg.WebUI.User = webuiUser
cfg.WebUI.Pass = webuiPass
mergedWebUIFromCLI = true
}
store := newConfigStore(cfg, configFile)
// If webui is active without TUI, install tuiHandler{nil} so log entries
// reach the ring buffer (served at /api/logs) and still appear on stderr.
if cfg.WebUI.Addr != "" && !tuiMode {
slog.SetDefault(slog.New(newTuiHandler(nil, level)))
}
// Persist webui CLI values to the config file so they survive restarts.
if mergedWebUIFromCLI && configFile != "" {
if err := store.Set(cfg); err != nil {
slog.Warn("Could not persist webui settings to config", "err", err)
}
}
if len(args) < 1 {
printUsage()
os.Exit(1)
}
mode := args[0]
switch mode {
case "app":
runAppProxy(args[1:], store, tuiMode)
case "tuner":
runTunerProxy(args[1:], store, tuiMode)
default:
fmt.Fprintf(os.Stderr, "Unknown mode: %s\n", mode)
printUsage()
os.Exit(1)
}
}
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " %s app [bind_address] [hdhomerun_ip]\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s tuner <app_proxy_host_or_hdhomerun_ip> [-direct]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nFlags:\n")
fmt.Fprintf(os.Stderr, " -config string\n\tPath to JSON config file\n")
fmt.Fprintf(os.Stderr, " -debug\n\tEnable debug logging\n")
fmt.Fprintf(os.Stderr, " -template\n\tGenerate a template config file and exit\n")
fmt.Fprintf(os.Stderr, " -tui\n\tEnable terminal UI dashboard\n")
fmt.Fprintf(os.Stderr, " -webui string\n\tBind address for web UI (e.g. :8080)\n")
fmt.Fprintf(os.Stderr, " -webui-user string\n\tHTTP Basic Auth username (required with -webui when config has no webui)\n")
fmt.Fprintf(os.Stderr, " -webui-pass string\n\tHTTP Basic Auth password (required with -webui when config has no webui)\n")
fmt.Fprintf(os.Stderr, " -webui-reset\n\tForce CLI -webui flags to override config file webui settings\n")
fmt.Fprintf(os.Stderr, "\nNote: Webui credentials are stored in the config file after first use.\n")
fmt.Fprintf(os.Stderr, " If the config has webui settings, -webui flags are ignored unless -webui-reset is passed.\n")
fmt.Fprintf(os.Stderr, "Generate template with: %s -template\n", os.Args[0])
}
func runAppProxy(args []string, store *configStore, tuiMode bool) {
cfg := store.Get()
var bindAddr, directIP string
if len(args) > 0 {
bindAddr = args[0]
}
if len(args) > 1 {
directIP = args[1]
}
if bindAddr == "" && cfg.App.BindAddress != "" {
bindAddr = cfg.App.BindAddress
}
if directIP == "" && cfg.App.DirectHDHRIP != "" {
directIP = cfg.App.DirectHDHRIP
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
slog.Info("Shutdown signal received")
cancel()
}()
proxy := NewAppProxy(store)
if store.Get().WebUI.Addr != "" {
ws := newWebServer(store, proxy)
go func() {
if err := ws.start(ctx); err != nil {
slog.Error("Web UI error", "err", err)
}
}()
}
if tuiMode {
runWithTUI(ctx, cancel, proxy, func() error {
return proxy.Run(ctx, bindAddr, directIP, store)
})
return
}
if err := proxy.Run(ctx, bindAddr, directIP, store); err != nil {
slog.Error("App proxy error", "err", err)
os.Exit(1)
}
}
func runTunerProxy(args []string, store *configStore, tuiMode bool) {
cfg := store.Get()
if len(args) > 2 {
fmt.Fprintf(os.Stderr, "Error: too many arguments for tuner mode\n")
fmt.Fprintf(os.Stderr, "Usage: %s [-flags] tuner [<host>] [-direct]\n", os.Args[0])
os.Exit(1)
}
// Detect flags placed after the mode (e.g. "tuner -config file.json") — flag
// parsing stops at the first non-flag word, so they land in args instead.
if len(args) >= 1 && len(args[0]) > 0 && args[0][0] == '-' {
fmt.Fprintf(os.Stderr, "Error: flags must appear before the mode, e.g.:\n")
fmt.Fprintf(os.Stderr, " %s -config file.json tuner <host>\n", os.Args[0])
os.Exit(1)
}
var hostOrIP string
isDirectMode := false
if len(args) >= 1 {
hostOrIP = args[0]
}
if len(args) == 2 {
isDirectMode = args[1] == "-direct"
}
if hostOrIP == "" {
if isDirectMode && cfg.Tuner.DirectHDHRIP != "" {
hostOrIP = cfg.Tuner.DirectHDHRIP
} else if !isDirectMode && cfg.Tuner.ProxyHost != "" {
hostOrIP = cfg.Tuner.ProxyHost
}
}
if !isDirectMode && cfg.Tuner.DirectMode {
isDirectMode = true
if hostOrIP == "" && cfg.Tuner.DirectHDHRIP != "" {
hostOrIP = cfg.Tuner.DirectHDHRIP
}
}
if hostOrIP == "" {
fmt.Fprintf(os.Stderr, "Error: no host specified and none found in config\n")
fmt.Fprintf(os.Stderr, "Usage: %s [-config file.json] tuner <host> [-direct]\n", os.Args[0])
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
slog.Info("Shutdown signal received")
cancel()
}()
proxy := NewTunerProxy(store)
if store.Get().WebUI.Addr != "" {
ws := newWebServer(store, proxy)
go func() {
if err := ws.start(ctx); err != nil {
slog.Error("Web UI error", "err", err)
}
}()
}
if tuiMode {
runWithTUI(ctx, cancel, proxy, func() error {
return proxy.Run(ctx, hostOrIP, isDirectMode, store)
})
return
}
if err := proxy.Run(ctx, hostOrIP, isDirectMode, store); err != nil {
slog.Error("Tuner proxy error", "err", err)
os.Exit(1)
}
}