-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlscan.go
66 lines (57 loc) · 1.51 KB
/
urlscan.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
package urlscango
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func SubmitUrl(URL, public, customAgent, referer, APIKey string) *ScanResponse {
r := new(ScanRequest)
r.APIKey = APIKey
r.URL = URL
r.Public = public
r.CustomAgent = customAgent
r.Referer = referer
r.Validate()
response, _ := AuthenticatedRequest(SCAN_URL, "POST", r, r.APIKey)
resp := new(ScanResponse)
json.Unmarshal(response, resp)
return resp
}
func FetchResults(response *ScanResponse, APIKey string) *JobResult {
jobUUID := response.UUID
body, code := AuthenticatedRequest(fmt.Sprintf(RESULT_URL, jobUUID), "GET", nil, APIKey)
if code == http.StatusNotFound {
tickerStatus := make(chan *JobResult)
ticker := time.NewTicker(2 * time.Second)
go func() {
for range ticker.C {
body, code := AuthenticatedRequest(fmt.Sprintf(RESULT_URL, jobUUID), "GET", nil, APIKey)
if code == http.StatusOK {
ticker.Stop()
jobResult := new(JobResult)
json.Unmarshal(body, jobResult)
tickerStatus <- jobResult
}
}
}()
jobResult := <-tickerStatus
return jobResult
} else {
jobResult := new(JobResult)
json.Unmarshal(body, jobResult)
return jobResult
}
}
func GetScreenshot(jobUUID string, fileName string, APIKey string) {
png, code := AuthenticatedRequest(fmt.Sprintf(SCREENSHOT_URL, jobUUID), "GET", nil, APIKey)
if code != http.StatusOK {
panic("Something went wrong: " + string(code))
} else {
err := ioutil.WriteFile(fileName, png, 0644)
if err != nil {
panic(err)
}
}
}