| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- // Package cdp controls a dedicated Chromium process through chromedp.
- package cdp
- import (
- "context"
- "encoding/json"
- "errors"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "sort"
- "sync"
- "clusterflow.dev/cfbrowser/api"
- "clusterflow.dev/cfbrowser/internal/devtools"
- "github.com/chromedp/cdproto/browser"
- "github.com/chromedp/cdproto/cdp"
- "github.com/chromedp/cdproto/input"
- "github.com/chromedp/cdproto/page"
- cruntime "github.com/chromedp/cdproto/runtime"
- "github.com/chromedp/chromedp"
- "github.com/chromedp/chromedp/kb"
- )
- type Driver struct{}
- func (*Driver) Probe(ctx context.Context) (api.Capabilities, error) {
- if err := ctx.Err(); err != nil {
- return api.Capabilities{}, err
- }
- features := map[string]api.Capability{}
- for _, n := range []string{"displayless", "evalResult", "isolatedWorld", "trustedInput", "domSnapshot", "screenshotViewport", "screenshotFullPage", "openShadowDOM", "downloads"} {
- features[n] = api.Capability{Status: "supported"}
- }
- for _, n := range []string{"axSnapshot", "crossOriginFrames", "fileUpload", "persistentProfile", "userHandoff", "proxy"} {
- features[n] = api.Capability{Status: "unsupported", Reason: "not implemented in prototype v0.1"}
- }
- features["networkPolicy"] = api.Capability{Status: "degraded", Reason: "only explicit top-level navigation is checked; use a trusted network or OS egress firewall"}
- return api.Capabilities{Engine: "cdp", Platform: runtime.GOOS + "/" + runtime.GOARCH, Features: features}, nil
- }
- // FindExecutable searches explicit and standard browser locations without installing software.
- func FindExecutable(explicit string) (string, error) {
- if explicit != "" {
- p, err := exec.LookPath(explicit)
- if err != nil {
- return "", api.Err("BROWSER_ENGINE_NOT_FOUND", "configured executable was not found")
- }
- return p, nil
- }
- paths := []string{"google-chrome", "chromium", "chromium-browser", "msedge", "chrome", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"}
- for _, env := range []string{"PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA"} {
- if base := os.Getenv(env); base != "" {
- paths = append(paths, filepath.Join(base, "Google", "Chrome", "Application", "chrome.exe"), filepath.Join(base, "Microsoft", "Edge", "Application", "msedge.exe"))
- }
- }
- for _, p := range paths {
- if path, e := exec.LookPath(p); e == nil {
- return path, nil
- }
- }
- return "", api.Err("BROWSER_ENGINE_NOT_FOUND", "configure a Chrome, Chromium or Edge executable")
- }
- type session struct {
- ctx context.Context
- cancel, allocCancel context.CancelFunc
- mu sync.Mutex
- downloads map[string]api.Download
- dir string
- limit int64
- once sync.Once
- }
- type enginePage struct {
- ctx context.Context
- cancel context.CancelFunc
- once sync.Once
- }
- func (*Driver) Open(ctx context.Context, o api.EngineOptions) (api.EngineSession, error) {
- if err := ctx.Err(); err != nil {
- return nil, err
- }
- executable, err := FindExecutable(o.Executable)
- if err != nil {
- return nil, err
- }
- opts := append([]chromedp.ExecAllocatorOption{}, chromedp.DefaultExecAllocatorOptions[:]...)
- opts = append(opts, chromedp.ExecPath(executable), chromedp.Flag("headless", o.Headless), chromedp.WindowSize(o.ViewportWidth, o.ViewportHeight), chromedp.Flag("disable-quic", true), chromedp.Flag("disable-background-networking", true))
- if o.NoSandbox {
- opts = append(opts, chromedp.NoSandbox)
- }
- allocator, allocCancel := chromedp.NewExecAllocator(context.Background(), opts...)
- base, cancel := chromedp.NewContext(allocator, chromedp.WithLogf(func(string, ...any) {}), chromedp.WithErrorf(func(string, ...any) {}))
- s := &session{ctx: base, cancel: cancel, allocCancel: allocCancel, downloads: map[string]api.Download{}, dir: o.DownloadDir, limit: o.MaxDownloadBytes}
- stop := context.AfterFunc(ctx, cancel)
- defer stop()
- chromedp.ListenBrowser(base, func(ev any) { s.downloadEvent(ev) })
- err = chromedp.Run(base, chromedp.ActionFunc(func(c context.Context) error {
- return browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).WithDownloadPath(o.DownloadDir).WithEventsEnabled(true).Do(cdp.WithExecutor(c, chromedp.FromContext(c).Browser))
- }))
- if err != nil {
- _ = s.Close()
- return nil, err
- }
- return s, nil
- }
- func (s *session) downloadEvent(ev any) {
- s.mu.Lock()
- defer s.mu.Unlock()
- switch e := ev.(type) {
- case *browser.EventDownloadWillBegin:
- if len(s.downloads) >= 64 {
- go s.cancelDownload(e.GUID)
- return
- }
- s.downloads[e.GUID] = api.Download{ID: e.GUID, Name: e.SuggestedFilename, State: "running", Path: filepath.Join(s.dir, e.GUID)}
- case *browser.EventDownloadProgress:
- d, ok := s.downloads[e.GUID]
- if !ok {
- return
- }
- d.Bytes = int64(e.ReceivedBytes)
- if d.State == "canceled" {
- return
- }
- if d.Bytes > s.limit || int64(e.TotalBytes) > s.limit {
- d.State = "canceled"
- s.downloads[e.GUID] = d
- go s.cancelDownload(e.GUID)
- return
- }
- d.State = string(e.State)
- s.downloads[e.GUID] = d
- }
- }
- func (s *session) cancelDownload(id string) {
- _ = chromedp.Run(s.ctx, chromedp.ActionFunc(func(c context.Context) error {
- return browser.CancelDownload(id).Do(cdp.WithExecutor(c, chromedp.FromContext(c).Browser))
- }))
- _ = os.Remove(filepath.Join(s.dir, id))
- }
- func (s *session) Downloads() []api.Download {
- s.mu.Lock()
- defer s.mu.Unlock()
- out := make([]api.Download, 0, len(s.downloads))
- for _, d := range s.downloads {
- out = append(out, d)
- }
- sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
- return out
- }
- func (s *session) Close() error { s.once.Do(func() { s.cancel(); s.allocCancel() }); return nil }
- func (s *session) NewPage(ctx context.Context) (api.EnginePage, error) {
- p, cancel := chromedp.NewContext(s.ctx)
- stop := context.AfterFunc(ctx, cancel)
- defer stop()
- if err := chromedp.Run(p); err != nil {
- cancel()
- return nil, err
- }
- return &enginePage{ctx: p, cancel: cancel}, nil
- }
- func (p *enginePage) run(ctx context.Context, actions ...chromedp.Action) error {
- if err := ctx.Err(); err != nil {
- return err
- }
- call, cancel := context.WithCancel(p.ctx)
- stop := context.AfterFunc(ctx, cancel)
- defer stop()
- defer cancel()
- return chromedp.Run(call, actions...)
- }
- func (p *enginePage) Navigate(ctx context.Context, url, op string) (string, error) {
- var action chromedp.Action
- switch op {
- case "goto":
- action = chromedp.Navigate(url)
- case "back":
- action = chromedp.NavigateBack()
- case "forward":
- action = chromedp.NavigateForward()
- case "reload":
- action = chromedp.Reload()
- default:
- return "", api.Err("BROWSER_INVALID_ARGUMENT", "invalid navigation")
- }
- var location string
- err := p.run(ctx, action, chromedp.Location(&location))
- return location, err
- }
- func (p *enginePage) Evaluate(ctx context.Context, script string) (json.RawMessage, error) {
- var value json.RawMessage
- err := p.run(ctx, chromedp.ActionFunc(func(c context.Context) error {
- tree, err := page.GetFrameTree().Do(c)
- if err != nil {
- return err
- }
- world, err := page.CreateIsolatedWorld(tree.Frame.ID).WithWorldName("cfbrowser-prototype").Do(c)
- if err != nil {
- return err
- }
- result, exception, err := cruntime.Evaluate(script).WithContextID(world).WithReturnByValue(true).Do(c)
- if err != nil {
- return err
- }
- if exception != nil {
- return errors.New("page evaluation failed")
- }
- if result == nil {
- return errors.New("empty evaluation result")
- }
- value = append(json.RawMessage{}, result.Value...)
- return nil
- }))
- return value, err
- }
- func (p *enginePage) Input(ctx context.Context, action, value string, x, y float64) error {
- switch action {
- case "click":
- return p.run(ctx, chromedp.MouseClickXY(x, y))
- case "hover":
- return p.run(ctx, chromedp.MouseEvent(input.MouseMoved, x, y))
- case "fill":
- modifier := input.ModifierCtrl
- if runtime.GOOS == "darwin" {
- modifier = input.ModifierMeta
- }
- return p.run(ctx, chromedp.KeyEvent("a", chromedp.KeyModifiers(modifier)), chromedp.KeyEvent(kb.Backspace), chromedp.ActionFunc(func(c context.Context) error { return input.InsertText(value).Do(c) }))
- case "type":
- return p.run(ctx, chromedp.ActionFunc(func(c context.Context) error { return input.InsertText(value).Do(c) }))
- case "press":
- keys := map[string]string{"Enter": kb.Enter, "Tab": kb.Tab, "Escape": kb.Escape, "ArrowDown": kb.ArrowDown, "ArrowUp": kb.ArrowUp, "Backspace": kb.Backspace}
- key, ok := keys[value]
- if !ok {
- return api.Err("BROWSER_INVALID_ARGUMENT", "unsupported key")
- }
- return p.run(ctx, chromedp.KeyEvent(key))
- default:
- return api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "input action is unavailable")
- }
- }
- func (p *enginePage) Capture(ctx context.Context) ([]byte, error) { return p.capture(ctx, false) }
- func (p *enginePage) CaptureFullPage(ctx context.Context) ([]byte, error) {
- return p.capture(ctx, true)
- }
- func (p *enginePage) capture(ctx context.Context, full bool) ([]byte, error) {
- var data []byte
- err := p.run(ctx, chromedp.ActionFunc(func(c context.Context) error {
- var resultErr error
- devtools.Capture(func(method string, params json.RawMessage, done func(json.RawMessage, error)) {
- var raw json.RawMessage
- e := cdp.Execute(c, method, params, &raw)
- done(raw, e)
- }, full, func(b []byte, e error) { data = b; resultErr = e })
- return resultErr
- }))
- return data, err
- }
- func (p *enginePage) Close() error { p.once.Do(p.cancel); return nil }
|