forked from MontFerret/ferret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (152 loc) · 3.11 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/MontFerret/ferret/cli"
"github.com/MontFerret/ferret/cli/browser"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type Params []string
func (p *Params) String() string {
return "[" + strings.Join(*p, ",") + "]"
}
func (p *Params) Set(value string) error {
*p = append(*p, value)
return nil
}
func (p *Params) ToMap() (map[string]interface{}, error) {
res := make(map[string]interface{})
for _, entry := range *p {
pair := strings.SplitN(entry, ":", 2)
if len(pair) < 2 {
return nil, core.Error(core.ErrInvalidArgument, entry)
}
var value interface{}
key := pair[0]
err := json.Unmarshal([]byte(pair[1]), &value)
if err != nil {
fmt.Println(pair[1])
return nil, err
}
res[key] = value
}
return res, nil
}
var (
version string
conn = flag.String(
"cdp",
"",
"set CDP address",
)
launchBrowser = flag.Bool(
"cdp-launch",
false,
"launch Chrome",
)
cdpKeepCookies = flag.Bool(
"cdp-keep-cookies",
false,
"keep cookies between queries (i.e. do not open tabs in incognito mode)",
)
proxyAddress = flag.String(
"proxy",
"",
"address of proxy server to use (only applicable to static pages, proxy for dynamic pages controlled by Chrome)",
)
userAgent = flag.String(
"user-agent",
"",
"set custom user agent. '*' triggers UA generation",
)
showTime = flag.Bool(
"time",
false,
"show how much time was taken to execute a query",
)
showVersion = flag.Bool(
"version",
false,
"show REPL version",
)
help = flag.Bool(
"help",
false,
"show this list",
)
)
func main() {
var params Params
flag.Var(
¶ms,
"param",
`query parameter (--param=foo:\"bar\", --param=id:1)`,
)
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(0)
return
}
if *showVersion {
fmt.Println(version)
os.Exit(0)
return
}
cdpConn := *conn
if cdpConn == "" && *launchBrowser {
opts := make([]browser.Option, 0, 2)
//if *noUserData {
// opts = append(opts, browser.WithoutUserDataDir())
//}
// TODO: Make it optional.
opts = append(opts, browser.WithoutUserDataDir())
// we need to launch Chrome instance
b, err := browser.Launch(opts...)
if err != nil {
fmt.Println(fmt.Sprintf("Failed to launch browser:"))
fmt.Println(err)
os.Exit(1)
}
cdpConn = b.DebuggingAddress()
defer b.Close()
}
p, err := params.ToMap()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
opts := cli.Options{
Cdp: cdpConn,
Params: p,
Proxy: *proxyAddress,
UserAgent: *userAgent,
ShowTime: *showTime,
KeepCookies: *cdpKeepCookies,
}
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// check whether the app is getting a query via standard input
std := bufio.NewReader(os.Stdin)
b, err := ioutil.ReadAll(std)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
cli.Exec(string(b), opts)
return
}
// filename was passed
if flag.NArg() > 0 {
cli.ExecFile(flag.Arg(0), opts)
return
}
// nothing was passed, run REPL
cli.Repl(version, opts)
}