cdp.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Package cdp controls a dedicated Chromium process through chromedp.
  2. package cdp
  3. import (
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "runtime"
  11. "sort"
  12. "sync"
  13. "clusterflow.dev/cfbrowser/api"
  14. "clusterflow.dev/cfbrowser/internal/devtools"
  15. "github.com/chromedp/cdproto/browser"
  16. "github.com/chromedp/cdproto/cdp"
  17. "github.com/chromedp/cdproto/input"
  18. "github.com/chromedp/cdproto/page"
  19. cruntime "github.com/chromedp/cdproto/runtime"
  20. "github.com/chromedp/chromedp"
  21. "github.com/chromedp/chromedp/kb"
  22. )
  23. type Driver struct{}
  24. func (*Driver) Probe(ctx context.Context) (api.Capabilities, error) {
  25. if err := ctx.Err(); err != nil {
  26. return api.Capabilities{}, err
  27. }
  28. features := map[string]api.Capability{}
  29. for _, n := range []string{"displayless", "evalResult", "isolatedWorld", "trustedInput", "domSnapshot", "screenshotViewport", "screenshotFullPage", "openShadowDOM", "downloads"} {
  30. features[n] = api.Capability{Status: "supported"}
  31. }
  32. for _, n := range []string{"axSnapshot", "crossOriginFrames", "fileUpload", "persistentProfile", "userHandoff", "proxy"} {
  33. features[n] = api.Capability{Status: "unsupported", Reason: "not implemented in prototype v0.1"}
  34. }
  35. features["networkPolicy"] = api.Capability{Status: "degraded", Reason: "only explicit top-level navigation is checked; use a trusted network or OS egress firewall"}
  36. return api.Capabilities{Engine: "cdp", Platform: runtime.GOOS + "/" + runtime.GOARCH, Features: features}, nil
  37. }
  38. // FindExecutable searches explicit and standard browser locations without installing software.
  39. func FindExecutable(explicit string) (string, error) {
  40. if explicit != "" {
  41. p, err := exec.LookPath(explicit)
  42. if err != nil {
  43. return "", api.Err("BROWSER_ENGINE_NOT_FOUND", "configured executable was not found")
  44. }
  45. return p, nil
  46. }
  47. 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"}
  48. for _, env := range []string{"PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA"} {
  49. if base := os.Getenv(env); base != "" {
  50. paths = append(paths, filepath.Join(base, "Google", "Chrome", "Application", "chrome.exe"), filepath.Join(base, "Microsoft", "Edge", "Application", "msedge.exe"))
  51. }
  52. }
  53. for _, p := range paths {
  54. if path, e := exec.LookPath(p); e == nil {
  55. return path, nil
  56. }
  57. }
  58. return "", api.Err("BROWSER_ENGINE_NOT_FOUND", "configure a Chrome, Chromium or Edge executable")
  59. }
  60. type session struct {
  61. ctx context.Context
  62. cancel, allocCancel context.CancelFunc
  63. mu sync.Mutex
  64. downloads map[string]api.Download
  65. dir string
  66. limit int64
  67. once sync.Once
  68. }
  69. type enginePage struct {
  70. ctx context.Context
  71. cancel context.CancelFunc
  72. once sync.Once
  73. }
  74. func (*Driver) Open(ctx context.Context, o api.EngineOptions) (api.EngineSession, error) {
  75. if err := ctx.Err(); err != nil {
  76. return nil, err
  77. }
  78. executable, err := FindExecutable(o.Executable)
  79. if err != nil {
  80. return nil, err
  81. }
  82. opts := append([]chromedp.ExecAllocatorOption{}, chromedp.DefaultExecAllocatorOptions[:]...)
  83. 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))
  84. if o.NoSandbox {
  85. opts = append(opts, chromedp.NoSandbox)
  86. }
  87. allocator, allocCancel := chromedp.NewExecAllocator(context.Background(), opts...)
  88. base, cancel := chromedp.NewContext(allocator, chromedp.WithLogf(func(string, ...any) {}), chromedp.WithErrorf(func(string, ...any) {}))
  89. s := &session{ctx: base, cancel: cancel, allocCancel: allocCancel, downloads: map[string]api.Download{}, dir: o.DownloadDir, limit: o.MaxDownloadBytes}
  90. stop := context.AfterFunc(ctx, cancel)
  91. defer stop()
  92. chromedp.ListenBrowser(base, func(ev any) { s.downloadEvent(ev) })
  93. err = chromedp.Run(base, chromedp.ActionFunc(func(c context.Context) error {
  94. return browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).WithDownloadPath(o.DownloadDir).WithEventsEnabled(true).Do(cdp.WithExecutor(c, chromedp.FromContext(c).Browser))
  95. }))
  96. if err != nil {
  97. _ = s.Close()
  98. return nil, err
  99. }
  100. return s, nil
  101. }
  102. func (s *session) downloadEvent(ev any) {
  103. s.mu.Lock()
  104. defer s.mu.Unlock()
  105. switch e := ev.(type) {
  106. case *browser.EventDownloadWillBegin:
  107. if len(s.downloads) >= 64 {
  108. go s.cancelDownload(e.GUID)
  109. return
  110. }
  111. s.downloads[e.GUID] = api.Download{ID: e.GUID, Name: e.SuggestedFilename, State: "running", Path: filepath.Join(s.dir, e.GUID)}
  112. case *browser.EventDownloadProgress:
  113. d, ok := s.downloads[e.GUID]
  114. if !ok {
  115. return
  116. }
  117. d.Bytes = int64(e.ReceivedBytes)
  118. if d.State == "canceled" {
  119. return
  120. }
  121. if d.Bytes > s.limit || int64(e.TotalBytes) > s.limit {
  122. d.State = "canceled"
  123. s.downloads[e.GUID] = d
  124. go s.cancelDownload(e.GUID)
  125. return
  126. }
  127. d.State = string(e.State)
  128. s.downloads[e.GUID] = d
  129. }
  130. }
  131. func (s *session) cancelDownload(id string) {
  132. _ = chromedp.Run(s.ctx, chromedp.ActionFunc(func(c context.Context) error {
  133. return browser.CancelDownload(id).Do(cdp.WithExecutor(c, chromedp.FromContext(c).Browser))
  134. }))
  135. _ = os.Remove(filepath.Join(s.dir, id))
  136. }
  137. func (s *session) Downloads() []api.Download {
  138. s.mu.Lock()
  139. defer s.mu.Unlock()
  140. out := make([]api.Download, 0, len(s.downloads))
  141. for _, d := range s.downloads {
  142. out = append(out, d)
  143. }
  144. sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
  145. return out
  146. }
  147. func (s *session) Close() error { s.once.Do(func() { s.cancel(); s.allocCancel() }); return nil }
  148. func (s *session) NewPage(ctx context.Context) (api.EnginePage, error) {
  149. p, cancel := chromedp.NewContext(s.ctx)
  150. stop := context.AfterFunc(ctx, cancel)
  151. defer stop()
  152. if err := chromedp.Run(p); err != nil {
  153. cancel()
  154. return nil, err
  155. }
  156. return &enginePage{ctx: p, cancel: cancel}, nil
  157. }
  158. func (p *enginePage) run(ctx context.Context, actions ...chromedp.Action) error {
  159. if err := ctx.Err(); err != nil {
  160. return err
  161. }
  162. call, cancel := context.WithCancel(p.ctx)
  163. stop := context.AfterFunc(ctx, cancel)
  164. defer stop()
  165. defer cancel()
  166. return chromedp.Run(call, actions...)
  167. }
  168. func (p *enginePage) Navigate(ctx context.Context, url, op string) (string, error) {
  169. var action chromedp.Action
  170. switch op {
  171. case "goto":
  172. action = chromedp.Navigate(url)
  173. case "back":
  174. action = chromedp.NavigateBack()
  175. case "forward":
  176. action = chromedp.NavigateForward()
  177. case "reload":
  178. action = chromedp.Reload()
  179. default:
  180. return "", api.Err("BROWSER_INVALID_ARGUMENT", "invalid navigation")
  181. }
  182. var location string
  183. err := p.run(ctx, action, chromedp.Location(&location))
  184. return location, err
  185. }
  186. func (p *enginePage) Evaluate(ctx context.Context, script string) (json.RawMessage, error) {
  187. var value json.RawMessage
  188. err := p.run(ctx, chromedp.ActionFunc(func(c context.Context) error {
  189. tree, err := page.GetFrameTree().Do(c)
  190. if err != nil {
  191. return err
  192. }
  193. world, err := page.CreateIsolatedWorld(tree.Frame.ID).WithWorldName("cfbrowser-prototype").Do(c)
  194. if err != nil {
  195. return err
  196. }
  197. result, exception, err := cruntime.Evaluate(script).WithContextID(world).WithReturnByValue(true).Do(c)
  198. if err != nil {
  199. return err
  200. }
  201. if exception != nil {
  202. return errors.New("page evaluation failed")
  203. }
  204. if result == nil {
  205. return errors.New("empty evaluation result")
  206. }
  207. value = append(json.RawMessage{}, result.Value...)
  208. return nil
  209. }))
  210. return value, err
  211. }
  212. func (p *enginePage) Input(ctx context.Context, action, value string, x, y float64) error {
  213. switch action {
  214. case "click":
  215. return p.run(ctx, chromedp.MouseClickXY(x, y))
  216. case "hover":
  217. return p.run(ctx, chromedp.MouseEvent(input.MouseMoved, x, y))
  218. case "fill":
  219. modifier := input.ModifierCtrl
  220. if runtime.GOOS == "darwin" {
  221. modifier = input.ModifierMeta
  222. }
  223. 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) }))
  224. case "type":
  225. return p.run(ctx, chromedp.ActionFunc(func(c context.Context) error { return input.InsertText(value).Do(c) }))
  226. case "press":
  227. keys := map[string]string{"Enter": kb.Enter, "Tab": kb.Tab, "Escape": kb.Escape, "ArrowDown": kb.ArrowDown, "ArrowUp": kb.ArrowUp, "Backspace": kb.Backspace}
  228. key, ok := keys[value]
  229. if !ok {
  230. return api.Err("BROWSER_INVALID_ARGUMENT", "unsupported key")
  231. }
  232. return p.run(ctx, chromedp.KeyEvent(key))
  233. default:
  234. return api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "input action is unavailable")
  235. }
  236. }
  237. func (p *enginePage) Capture(ctx context.Context) ([]byte, error) { return p.capture(ctx, false) }
  238. func (p *enginePage) CaptureFullPage(ctx context.Context) ([]byte, error) {
  239. return p.capture(ctx, true)
  240. }
  241. func (p *enginePage) capture(ctx context.Context, full bool) ([]byte, error) {
  242. var data []byte
  243. err := p.run(ctx, chromedp.ActionFunc(func(c context.Context) error {
  244. var resultErr error
  245. devtools.Capture(func(method string, params json.RawMessage, done func(json.RawMessage, error)) {
  246. var raw json.RawMessage
  247. e := cdp.Execute(c, method, params, &raw)
  248. done(raw, e)
  249. }, full, func(b []byte, e error) { data = b; resultErr = e })
  250. return resultErr
  251. }))
  252. return data, err
  253. }
  254. func (p *enginePage) Close() error { p.once.Do(p.cancel); return nil }