-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (180 loc) · 4.24 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
198
199
package main
/**
* Module dependencies
*/
import (
"context"
"fmt"
"net/http"
"log"
"encoding/json"
"time"
"os/exec"
"io/ioutil"
"bytes"
"github.com/gobs/args"
"github.com/raff/godet"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/cdp"
httpDriver "github.com/MontFerret/ferret/pkg/drivers/http"
)
var remote *godet.RemoteDebugger
type text struct {
Query string `json:"text"`
}
type errResult struct {
Code int `json:"code"`
Type string `json:"type"`
Message string `json:"message"`
Description string `json:"description"`
Ip string `json:"ip"`
}
type successResult struct {
Type string `json:"type"`
Message string `json:"message"`
Data string `json:"data"`
Ip string `json:"ip"`
}
/**
* Main
*/
func main() {
log.Println(chrome("/usr/bin/google-chrome"))
//log.Println(chrome("/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome"))
http.HandleFunc("/", reqHandler)
log.Println("Go!")
http.ListenAndServe(":8080", nil)
}
/**
* @desc request handler
*/
func reqHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
// 1. Decode json body
d := json.NewDecoder(r.Body)
p := &text{}
err := d.Decode(p)
// 2. Get external ip for log
ip, err := getIp()
// 3. Magic Ferret stuff
data, err := ferret(p.Query)
// 4. Answer
if err != nil {
var r *errResult = &errResult{
Code: 422,
Type: "error",
Message: "Unprocessable Entity",
Description: string(err.Error()),
Ip: ip,
}
j, _ := json.Marshal(r)
w.WriteHeader(422)
w.Write(j)
} else {
var r *successResult = &successResult{
Type: "success",
Message: "ferret executed without errors ",
Ip: ip,
Data: string(data),
}
j, _ := json.Marshal(r)
w.Write(j)
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "I can't do that, please send me a Post request { text : 'your ferret things' } ")
}
}
/**
* @desc launch chrome and wait for it's availabality
* @param {String} chrome path
* @return {String} chrome status
* some chrome options :
* path += " --single-process"
* path += " --disable-dev-shm-usage"
* path += " --ignore-certificate-errors"
* path += " --enable-logging"
* path += " --log-level=0"
* path += " --disable-application-cache"
* path += " --hide-scrollbars"
*/
func chrome(path string) string {
path += " --no-sandbox"
path += " --headless"
path += " --disable-gpu"
path += " --remote-debugging-port=9222"
path += " --disable-dev-shm-usage"
if errRun := command(path, "start"); errRun != nil {
// log.Println("cannot start browser", fmt.Sprint(errRun))
return "cannot start browser" + fmt.Sprint(errRun)
}
var err error
for i := 0; i < 20; i++ {
if i > 0 {
time.Sleep(500 * time.Millisecond)
}
remote, err = godet.Connect("localhost:9222", false)
if err == nil {
break
}
// log.Println("Error", err)
}
if err != nil {
return "cannot connect to browser"
}
return "google ok"
}
/**
* @desc ewecute command and arguments
* @input {String} command
* @input {kind} kind of execution, 'run' - wait result, 'start' background
* @return {error} error
*/
func command(input string, kind string) error {
parts := args.GetArgs(input)
cmd := exec.Command(parts[0], parts[1:]...)
switch kind {
case "run":
return cmd.Run()
default:
return cmd.Start()
}
}
/**
* @desc launch ferret command and return return
* @param {String} term command for launch fql
* @return {String} result
*/
func ferret(text string) ([]byte, error) {
comp := compiler.New()
program, err := comp.Compile(text)
if err != nil {
return nil, err
}
ctx := context.Background()
ctx = drivers.WithContext(ctx, cdp.NewDriver())
ctx = drivers.WithContext(ctx, httpDriver.NewDriver(), drivers.AsDefault())
out, err := program.Run(ctx)
if err != nil {
return nil, err
}
return out, nil
}
/**
* @desc get external ip adress
* @return {String, error}
*/
func getIp() (string, error) {
rsp, err := http.Get("http://checkip.amazonaws.com")
if err != nil {
return "", err
}
defer rsp.Body.Close()
buf, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return "", err
}
return string(bytes.TrimSpace(buf)), nil
}