High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
refactor: code quality pass — cache registry, context deadlines, consistent errors, stealth logging
Boscha03e2e1298c0
diff --git a/bridge.go b/bridge.goindex 39c849c5..ca57030c 100644--- a/bridge.go+++ b/bridge.go@@ -34,7 +34,8 @@ type Bridge struct {browserCtx context.Contexttabs map[string]*TabEntrysnapshots map[string]*refCache- stealthScript string // injected on every new tab+ stealthScript string // injected on every new tab+ actions map[string]ActionFunc // cached action registry (built once)locks *lockManagermu sync.RWMutex}@@ -45,12 +46,14 @@ func (b *Bridge) injectStealth(ctx context.Context) {if b.stealthScript == "" {return}- _ = chromedp.Run(ctx,+ if err := chromedp.Run(ctx,chromedp.ActionFunc(func(ctx context.Context) error {_, err := page.AddScriptToEvaluateOnNewDocument(b.stealthScript).Do(ctx)return err}),- )+ ); err != nil {+ slog.Warn("stealth injection failed", "err", err)+ }}// TabContext returns the chromedp context for a tab and the resolved tabID.diff --git a/cdp.go b/cdp.goindex 9dd44fa8..5dfcf6c9 100644--- a/cdp.go+++ b/cdp.go@@ -29,9 +29,13 @@ func navigatePage(ctx context.Context, url string) error {}return nil}),- // Poll document.readyState until interactive/complete or timeout+ // Poll document.readyState until interactive/complete or context deadline.chromedp.ActionFunc(func(ctx context.Context) error {- deadline := time.Now().Add(10 * time.Second)+ // Use context deadline if set, otherwise fall back to 10s.+ deadline, ok := ctx.Deadline()+ if !ok {+ deadline = time.Now().Add(10 * time.Second)+ }for time.Now().Before(deadline) {var readyState stringevalP := map[string]any{diff --git a/handler_actions.go b/handler_actions.goindex 3be655fa..3479826a 100644--- a/handler_actions.go+++ b/handler_actions.go@@ -34,8 +34,10 @@ type actionRequest struct {// ActionFunc handles a single action kind.type ActionFunc func(ctx context.Context, req actionRequest) (map[string]any, error)-func (b *Bridge) actionRegistry() map[string]ActionFunc {- return map[string]ActionFunc{+// initActionRegistry builds the action registry once. Call from main() after+// Bridge is initialised; the result is stored in b.actions.+func (b *Bridge) initActionRegistry() {+ b.actions = map[string]ActionFunc{actionClick: func(ctx context.Context, req actionRequest) (map[string]any, error) {var err errorif req.Selector != "" {@@ -223,7 +225,7 @@ func (b *Bridge) handleAction(w http.ResponseWriter, r *http.Request) {}}- registry := b.actionRegistry()+ registry := b.actionsif req.Kind == "" {kinds := make([]string, 0, len(registry))for k := range registry {@@ -278,7 +280,7 @@ func (b *Bridge) handleActions(w http.ResponseWriter, r *http.Request) {}if len(req.Actions) == 0 {- jsonResp(w, 400, map[string]string{"error": "actions array is empty"})+ jsonErr(w, 400, fmt.Errorf("actions array is empty"))return}@@ -289,7 +291,7 @@ func (b *Bridge) handleActions(w http.ResponseWriter, r *http.Request) {}results := make([]actionResult, 0, len(req.Actions))- registry := b.actionRegistry()+ registry := b.actionsfor i, action := range req.Actions {if action.TabID == "" {diff --git a/handler_actions_test.go b/handler_actions_test.goindex 62edb4cb..ed13d6b2 100644--- a/handler_actions_test.go+++ b/handler_actions_test.go@@ -157,7 +157,8 @@ func TestHandleFingerprintRotate_NoTab(t *testing.T) {func TestActionRegistry_HumanActions(t *testing.T) {b := &Bridge{}- registry := b.actionRegistry()+ b.initActionRegistry()+ registry := b.actions// Check new human actions existif _, ok := registry[actionHumanClick]; !ok {diff --git a/handler_cookies.go b/handler_cookies.goindex 1d62da1a..1322bb38 100644--- a/handler_cookies.go+++ b/handler_cookies.go@@ -107,12 +107,12 @@ func (b *Bridge) handleSetCookies(w http.ResponseWriter, r *http.Request) {}if req.URL == "" {- jsonResp(w, 400, map[string]string{"error": "url is required"})+ jsonErr(w, 400, fmt.Errorf("url is required"))return}if len(req.Cookies) == 0 {- jsonResp(w, 400, map[string]string{"error": "cookies array is empty"})+ jsonErr(w, 400, fmt.Errorf("cookies array is empty"))return}diff --git a/handlers.go b/handlers.goindex 7fb6f562..c145c4a1 100644--- a/handlers.go+++ b/handlers.go@@ -199,7 +199,7 @@ func (b *Bridge) handleNavigate(w http.ResponseWriter, r *http.Request) {return}if req.URL == "" {- jsonResp(w, 400, map[string]string{"error": "url required"})+ jsonErr(w, 400, fmt.Errorf("url required"))return}@@ -302,7 +302,7 @@ func (b *Bridge) handleEvaluate(w http.ResponseWriter, r *http.Request) {return}if req.Expression == "" {- jsonResp(w, 400, map[string]string{"error": "expression required"})+ jsonErr(w, 400, fmt.Errorf("expression required"))return}@@ -352,7 +352,7 @@ func (b *Bridge) handleTab(w http.ResponseWriter, r *http.Request) {case tabActionClose:if req.TabID == "" {- jsonResp(w, 400, map[string]string{"error": "tabId required"})+ jsonErr(w, 400, fmt.Errorf("tabId required"))return}@@ -363,7 +363,7 @@ func (b *Bridge) handleTab(w http.ResponseWriter, r *http.Request) {jsonResp(w, 200, map[string]any{"closed": true})default:- jsonResp(w, 400, map[string]string{"error": "action must be 'new' or 'close'"})+ jsonErr(w, 400, fmt.Errorf("action must be 'new' or 'close'"))}}@@ -380,7 +380,7 @@ func (b *Bridge) handleTabLock(w http.ResponseWriter, r *http.Request) {return}if req.TabID == "" || req.Owner == "" {- jsonResp(w, 400, map[string]string{"error": "tabId and owner required"})+ jsonErr(w, 400, fmt.Errorf("tabId and owner required"))return}@@ -390,7 +390,7 @@ func (b *Bridge) handleTabLock(w http.ResponseWriter, r *http.Request) {}if err := b.locks.Lock(req.TabID, req.Owner, timeout); err != nil {- jsonResp(w, 409, map[string]string{"error": err.Error()})+ jsonErr(w, 409, err)return}@@ -414,12 +414,12 @@ func (b *Bridge) handleTabUnlock(w http.ResponseWriter, r *http.Request) {return}if req.TabID == "" || req.Owner == "" {- jsonResp(w, 400, map[string]string{"error": "tabId and owner required"})+ jsonErr(w, 400, fmt.Errorf("tabId and owner required"))return}if err := b.locks.Unlock(req.TabID, req.Owner); err != nil {- jsonResp(w, 409, map[string]string{"error": err.Error()})+ jsonErr(w, 409, err)return}diff --git a/main.go b/main.goindex f1f7fb7d..42f334f1 100644--- a/main.go+++ b/main.go@@ -222,6 +222,7 @@ func main() {bridge.browserCtx = browserCtxbridge.tabs = make(map[string]*TabEntry)bridge.snapshots = make(map[string]*refCache)+ bridge.initActionRegistry()bridge.locks = newLockManager()// Register the initial tabdiff --git a/middleware.go b/middleware.goindex 07852cf1..ee5c4949 100644--- a/middleware.go+++ b/middleware.go@@ -3,6 +3,7 @@ package mainimport ("context""encoding/json"+ "fmt""log/slog""net/http""time"@@ -38,7 +39,7 @@ func authMiddleware(next http.Handler) http.Handler {if token != "" {auth := r.Header.Get("Authorization")if auth != "Bearer "+token {- jsonResp(w, 401, map[string]string{"error": "unauthorized"})+ jsonErr(w, 401, fmt.Errorf("unauthorized"))return}}