/
client.go
64 lines (56 loc) · 1.64 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
package luminati
import (
"encoding/base64"
"math/rand"
"net/http"
"net/url"
"strconv"
)
// Client represents a luminati.io client that setups internally the proxy
type Client struct {
username string
password string
host string
port int
SessionID string
}
// NewClient Creates a Client with the luminati.io credentials
func NewClient(username string, password string, host string, port int) Client {
c := Client{
username: username,
password: password,
host: host,
port: port,
}
c.NewSession()
return c
}
// NewSession generates a new SessionId to be used with luminati network
func (c *Client) NewSession() {
s := randomString(10)
c.SessionID = s
}
// Do executes a reqeuquest using a proxy from luminaati.io network
func (c *Client) Do(request *http.Request) (resp *http.Response, err error) {
client := &http.Client{}
c.setProxy(client, request)
response, err := client.Do(request)
return response, err
}
func (c *Client) setProxy(client *http.Client, request *http.Request) {
proxyURL, _ := url.Parse("http://" + c.host + ":" + strconv.Itoa(c.port) + " " + request.URL.String())
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
login := c.username + "-session-" + c.SessionID
auth := login + ":" + c.password
basic := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
request.Header.Add("Proxy-Authorization", basic)
}
func randomString(n int) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}