-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
46 lines (39 loc) · 1.09 KB
/
http.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
package victor
import (
"encoding/base64"
"fmt"
"net/http"
"go.uber.org/zap"
)
// Client is the Victor's wrapper around a *http.Client that handles
// authentication and request management.
type Client struct {
version string
verbose bool
username, password *string
sub *http.Client
}
// NewClient is a *Client factory.
func NewClient(version string, verbose bool, username, password *string) *Client {
return &Client{
version: version,
verbose: verbose,
username: username,
password: password,
sub: &http.Client{},
}
}
func (client *Client) Do(req *http.Request) (*http.Response, error) {
if client.verbose {
Log().Info("getting distant content",
zap.String("location", req.URL.String()),
)
}
req.Header.Set("User-Agent", fmt.Sprintf("CTFer.io Victor (%s)", client.version))
if client.username != nil && client.password != nil {
bauth := fmt.Sprintf("%s:%s", *client.username, *client.password)
bauth = base64.StdEncoding.EncodeToString([]byte(bauth))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", bauth))
}
return client.sub.Do(req)
}