-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Client.go
130 lines (121 loc) · 3.24 KB
/
Client.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
package LeakIXClient
import (
"encoding/json"
"errors"
"fmt"
"github.com/LeakIX/l9format"
"github.com/gorilla/websocket"
"io/ioutil"
"log"
"net/http"
url2 "net/url"
"strings"
"time"
)
var LeakIXProxy = &l9format.ServicePluginBase{}
var LeakIXHttpTranport = &http.Transport{
DialContext: LeakIXProxy.DialContext,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 5 * time.Second,
}
var HttpClient = &http.Client{
Transport: LeakIXHttpTranport,
Timeout: 5 * time.Second,
}
type SearchResultsClient struct {
Scope string
Query string
SearchResults []l9format.L9Event
Position int
Page int
ApiKey string
Endpoint string
LastError error
}
const defaultEndpoint = "https://leakix.net"
func (sc *SearchResultsClient) GetEndpoint() string {
if len(sc.Endpoint) > 8 {
return sc.Endpoint
}
return defaultEndpoint
}
func (sc *SearchResultsClient) Next() bool {
var results []l9format.L9Event
if len(sc.SearchResults) > sc.Position {
sc.Position++
return true
}
// Try to load next page
results, sc.LastError = sc.GetSearchResults(sc.Scope, sc.Query, sc.Page)
for _, result := range results {
sc.SearchResults = append(sc.SearchResults, result)
}
sc.Page++
if len(sc.SearchResults) > sc.Position {
sc.Position++
return true
}
return false
}
func (sc *SearchResultsClient) SearchResult() l9format.L9Event {
return sc.SearchResults[sc.Position-1]
}
func (sc *SearchResultsClient) GetSearchResults(scope string, query string, page int) ([]l9format.L9Event, error) {
url := fmt.Sprintf(
"%s/search?scope=%s&q=%s&page=%d", sc.GetEndpoint(), url2.QueryEscape(scope), url2.QueryEscape(query), page)
var searchResults []l9format.L9Event
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/json")
req.Header.Set("api-key", sc.ApiKey)
resp, err := HttpClient.Do(req)
if err != nil {
return searchResults, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return searchResults, err
}
if resp.StatusCode == 429 {
// parse wait header and try again
duration, err := time.ParseDuration(resp.Header.Get("x-limited-for"))
if err != nil {
panic(err)
}
time.Sleep(duration)
return sc.GetSearchResults(scope, query, page)
}
err = json.Unmarshal(body, &searchResults)
if err != nil {
return searchResults, err
}
return searchResults, nil
}
func (sc *SearchResultsClient) GetChannel(scope string) (chan l9format.L9Event, error) {
channel := make(chan l9format.L9Event)
endpointUrl, err := url2.Parse(sc.GetEndpoint())
if err != nil {
return nil, errors.New("invalid endpoint")
}
endpointUrl.Scheme = strings.Replace(endpointUrl.Scheme, "http", "ws", -1)
log.Println(endpointUrl.String())
wsConnection, _, err := websocket.DefaultDialer.Dial(endpointUrl.String()+"/ws/"+scope, map[string][]string{
"Origin": {endpointUrl.Host + ":" + endpointUrl.Port()},
"api-key": {sc.ApiKey},
})
if err != nil {
return nil, err
}
go func() {
searchResult := l9format.L9Event{}
for {
err := wsConnection.ReadJSON(&searchResult)
if err != nil {
log.Println("Error parsing websocket results. Is your scope correct?")
log.Fatal(err)
}
channel <- searchResult
}
}()
return channel, nil
}