forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdp.go
245 lines (223 loc) · 5.42 KB
/
cdp.go
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
package runn
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/chromedp/chromedp"
)
const cdpNewKey = "new"
const (
cdpTimeoutByStep = 60 * time.Second
cdpWindowWidth = 1920
cdpWindowHeight = 1080
)
type cdpRunner struct {
name string
ctx context.Context
cancel context.CancelFunc
store map[string]any
operator *operator
opts []chromedp.ExecAllocatorOption
timeoutByStep time.Duration
}
type CDPActions []CDPAction
type CDPAction struct {
Fn string
Args map[string]any
}
func newCDPRunner(name, remote string) (*cdpRunner, error) {
if remote != cdpNewKey {
return nil, errors.New("remote connect mode is planned, but not yet implemented")
}
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.WindowSize(cdpWindowWidth, cdpWindowHeight),
)
if os.Getenv("RUNN_DISABLE_HEADLESS") != "" {
opts = append(opts,
chromedp.Flag("headless", false),
chromedp.Flag("hide-scrollbars", false),
chromedp.Flag("mute-audio", false),
)
}
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, _ := chromedp.NewContext(allocCtx)
return &cdpRunner{
name: name,
ctx: ctx,
cancel: cancel,
store: map[string]any{},
opts: opts,
timeoutByStep: cdpTimeoutByStep,
}, nil
}
func (rnr *cdpRunner) Close() error {
if rnr.cancel == nil {
return nil
}
rnr.cancel()
rnr.cancel = nil
return nil
}
func (rnr *cdpRunner) Renew() error {
if err := rnr.Close(); err != nil {
return err
}
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), rnr.opts...)
ctx, _ := chromedp.NewContext(allocCtx)
rnr.ctx = ctx
rnr.cancel = cancel
rnr.store = map[string]any{}
return nil
}
func (rnr *cdpRunner) Run(_ context.Context, cas CDPActions) error {
rnr.operator.capturers.captureCDPStart(rnr.name)
defer rnr.operator.capturers.captureCDPEnd(rnr.name)
// Set a timeout (cdpTimeoutByStep) for each step because Chrome operations may get stuck depending on the actions: specified.
called := false
defer func() {
called = true
}()
timer := time.NewTimer(rnr.timeoutByStep)
go func() {
<-timer.C
if !called {
rnr.Close()
}
}()
before := []chromedp.Action{
chromedp.EmulateViewport(cdpWindowWidth, cdpWindowHeight),
}
if err := chromedp.Run(rnr.ctx, before...); err != nil {
return err
}
for i, ca := range cas {
rnr.operator.capturers.captureCDPAction(ca)
k, fn, err := findCDPFn(ca.Fn)
if err != nil {
return fmt.Errorf("actions[%d] error: %w", i, err)
}
if k == "latestTab" {
infos, err := chromedp.Targets(rnr.ctx)
if err != nil {
return err
}
latestCtx, _ := chromedp.NewContext(rnr.ctx, chromedp.WithTargetID(infos[0].TargetID))
rnr.ctx = latestCtx
continue
}
as, err := rnr.evalAction(ca)
if err != nil {
return fmt.Errorf("actions[%d] error: %w", i, err)
}
if err := chromedp.Run(rnr.ctx, as...); err != nil {
return fmt.Errorf("actions[%d] error: %w", i, err)
}
ras := fn.Args.ResArgs()
if len(ras) > 0 {
// capture
res := map[string]any{}
for _, arg := range ras {
v := rnr.store[arg.Key]
switch vv := v.(type) {
case *string:
res[arg.Key] = *vv
case *map[string]string:
res[arg.Key] = *vv
case *[]byte:
res[arg.Key] = *vv
default:
res[arg.Key] = vv
}
}
rnr.operator.capturers.captureCDPResponse(ca, res)
}
}
// record
r := map[string]any{}
for k, v := range rnr.store {
switch vv := v.(type) {
case *string:
r[k] = *vv
case *map[string]string:
r[k] = *vv
case *[]byte:
r[k] = *vv
default:
r[k] = vv
}
}
rnr.operator.record(r)
rnr.store = map[string]any{} // clear
return nil
}
func (rnr *cdpRunner) evalAction(ca CDPAction) ([]chromedp.Action, error) {
_, fn, err := findCDPFn(ca.Fn)
if err != nil {
return nil, err
}
// path resolution for setUploadFile.path
if ca.Fn == "setUploadFile" {
p, ok := ca.Args["path"]
if !ok {
return nil, fmt.Errorf("invalid action: %v: arg '%s' not found", ca, "path")
}
pp, ok := p.(string)
if !ok {
return nil, fmt.Errorf("invalid action: %v", ca)
}
if !strings.HasPrefix(pp, "/") {
ca.Args["path"] = filepath.Join(rnr.operator.root, pp)
}
}
fv := reflect.ValueOf(fn.Fn)
vs := []reflect.Value{}
for i, a := range fn.Args {
switch a.Typ {
case CDPArgTypeArg:
v, ok := ca.Args[a.Key]
if !ok {
return nil, fmt.Errorf("invalid action: %v: arg '%s' not found", ca, a.Key)
}
if v == nil {
return nil, fmt.Errorf("invalid action arg: %s.%s = %v", ca.Fn, a.Key, v)
}
vs = append(vs, reflect.ValueOf(v))
case CDPArgTypeRes:
k := a.Key
switch reflect.TypeOf(fn.Fn).In(i).Elem().Kind() {
case reflect.String:
var v string
rnr.store[k] = &v
vs = append(vs, reflect.ValueOf(&v))
case reflect.Map:
// ex. attributes
v := map[string]string{}
rnr.store[k] = &v
vs = append(vs, reflect.ValueOf(&v))
case reflect.Slice:
var v []byte
rnr.store[k] = &v
vs = append(vs, reflect.ValueOf(&v))
default:
return nil, fmt.Errorf("invalid action: %v", ca)
}
default:
return nil, fmt.Errorf("invalid action: %v", ca)
}
}
res := fv.Call(vs)
a, ok := res[0].Interface().(chromedp.Action)
if ok {
return []chromedp.Action{a}, nil
}
as, ok := res[0].Interface().([]chromedp.Action)
if ok {
return as, nil
}
return nil, fmt.Errorf("invalid action: %v", ca)
}