Add configurable shortcuts and log them on startup

This commit is contained in:
Tobias Gesellchen
2026-02-15 00:27:25 +01:00
parent aaf067088a
commit 02026a9f3a
4 changed files with 67 additions and 18 deletions
+18 -10
View File
@@ -165,16 +165,19 @@ func main() {
if !settingsExist {
log.Printf("Creating default settings.json in %s", config.dataDir)
_ = ds.SaveSettings(datastore.Settings{
ServerURL: config.serverURL,
ProxyURL: config.targetURL,
HTTPServerURL: config.httpsServerURL,
RedactLogs: config.redact,
LogBodies: config.logBody,
RecordInteractions: config.record,
DiscoveryInterval: config.discoveryInterval.String(),
DiscoveryDisabled: false,
})
persisted.ServerURL = config.serverURL
persisted.ProxyURL = config.targetURL
persisted.HTTPServerURL = config.httpsServerURL
persisted.RedactLogs = config.redact
persisted.LogBodies = config.logBody
persisted.RecordInteractions = config.record
persisted.DiscoveryInterval = config.discoveryInterval.String()
persisted.DiscoveryDisabled = false
persisted.Shortcuts = map[string]int{
"/.well-known/appspecific/com.chrome.devtools.json": http.StatusNotFound,
"/sw.js": http.StatusNotFound,
}
_ = ds.SaveSettings(persisted)
}
// Recalculate domains if settings changed
@@ -191,6 +194,10 @@ func main() {
server.SetHTTPServerURL(config.httpsServerURL)
server.SetVersionInfo(version, commit, date)
server.SetDiscoverySettings(config.discoveryInterval, persisted.DiscoveryDisabled)
server.SetShortcuts(persisted.Shortcuts)
for path, status := range persisted.Shortcuts {
log.Printf("Warning: configured shortcut: %s -> %d", path, status)
}
recorder := proxy.NewRecorder(config.dataDir)
recorder.Redact = config.redact
@@ -440,6 +447,7 @@ func setupRouter(server *handlers.Server, pyProxy *httputil.ReverseProxy) *chi.M
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(server.ShortcutMiddleware)
r.Use(server.RecordMiddleware)
r.Get("/", server.HandleRoot)
+9 -8
View File
@@ -709,14 +709,15 @@ func (ds *DataStore) GetETagForAccount(account, device string) int64 {
// Settings represents the global service settings.
type Settings struct {
ServerURL string `json:"server_url"`
ProxyURL string `json:"proxy_url"`
HTTPServerURL string `json:"https_server_url,omitempty"`
RedactLogs bool `json:"redact_logs"`
LogBodies bool `json:"log_bodies"`
RecordInteractions bool `json:"record_interactions"`
DiscoveryInterval string `json:"discovery_interval,omitempty"`
DiscoveryDisabled bool `json:"discovery_disabled"`
ServerURL string `json:"server_url"`
ProxyURL string `json:"proxy_url"`
HTTPServerURL string `json:"https_server_url,omitempty"`
RedactLogs bool `json:"redact_logs"`
LogBodies bool `json:"log_bodies"`
RecordInteractions bool `json:"record_interactions"`
DiscoveryInterval string `json:"discovery_interval,omitempty"`
DiscoveryDisabled bool `json:"discovery_disabled"`
Shortcuts map[string]int `json:"shortcuts,omitempty"`
}
// GetSettings retrieves the global service settings.
+17
View File
@@ -27,6 +27,7 @@ type Server struct {
recordEnabled bool
discoveryInterval time.Duration
discoveryDisabled bool
shortcuts map[string]int
recorder *proxy.Recorder
Version string
Commit string
@@ -66,6 +67,22 @@ func (s *Server) SetDiscoverySettings(interval time.Duration, disabled bool) {
s.discoveryDisabled = disabled
}
// SetShortcuts sets the request shortcuts for the server.
func (s *Server) SetShortcuts(shortcuts map[string]int) {
s.mu.Lock()
defer s.mu.Unlock()
s.shortcuts = shortcuts
}
// GetShortcuts returns the current request shortcuts.
func (s *Server) GetShortcuts() map[string]int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.shortcuts
}
// GetDiscoverySettings returns the current discovery settings.
func (s *Server) GetDiscoverySettings() (time.Duration, bool) {
s.mu.RLock()
@@ -0,0 +1,23 @@
package handlers
import (
"net/http"
)
// ShortcutMiddleware returns a middleware that shortcuts requests to specific paths.
func (s *Server) ShortcutMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
shortcuts := s.shortcuts
s.mu.RUnlock()
if shortcuts != nil {
if status, ok := shortcuts[r.URL.Path]; ok {
w.WriteHeader(status)
return
}
}
next.ServeHTTP(w, r)
})
}