-
Notifications
You must be signed in to change notification settings - Fork 75
/
net.go
108 lines (95 loc) · 2.51 KB
/
net.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
// Copyright (c) 2017-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package util
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net"
"net/http"
"os"
"time"
pdv1 "github.com/decred/politeia/politeiad/api/v1"
"github.com/gorilla/schema"
)
// NormalizeAddress returns addr with the passed default port appended if
// there is not already a port specified.
func NormalizeAddress(addr, defaultPort string) string {
_, _, err := net.SplitHostPort(addr)
if err != nil {
return net.JoinHostPort(addr, defaultPort)
}
return addr
}
// NewHTTPClient returns a new http Client.
func NewHTTPClient(skipVerify bool, certPath string) (*http.Client, error) {
tlsConfig := &tls.Config{
InsecureSkipVerify: skipVerify,
}
if !skipVerify && certPath != "" {
cert, err := os.ReadFile(certPath)
if err != nil {
return nil, err
}
certPool, err := x509.SystemCertPool()
if err != nil {
fmt.Printf("WARN: unable to get system cert pool: %v\n", err)
certPool = x509.NewCertPool()
}
certPool.AppendCertsFromPEM(cert)
tlsConfig.RootCAs = certPool
}
return &http.Client{
Timeout: 2 * time.Minute,
Transport: &http.Transport{
IdleConnTimeout: 2 * time.Minute,
ResponseHeaderTimeout: 2 * time.Minute,
TLSClientConfig: tlsConfig,
}}, nil
}
// ConvertBodyToByteArray converts a response body into a byte array
// and optionally prints it to stdout.
func ConvertBodyToByteArray(r io.Reader, print bool) []byte {
var mw io.Writer
var body bytes.Buffer
if print {
mw = io.MultiWriter(&body, os.Stdout)
} else {
mw = io.MultiWriter(&body)
}
io.Copy(mw, r)
if print {
fmt.Printf("\n")
}
return body.Bytes()
}
// ParseGetParams parses the query params from the GET request into a struct.
// This method requires the struct type to be defined with `schema` tags.
func ParseGetParams(r *http.Request, dst interface{}) error {
err := r.ParseForm()
if err != nil {
return err
}
return schema.NewDecoder().Decode(dst, r.Form)
}
// RespBody returns the response body as a byte slice.
func RespBody(r *http.Response) []byte {
var mw io.Writer
var body bytes.Buffer
mw = io.MultiWriter(&body)
io.Copy(mw, r.Body)
return body.Bytes()
}
// RemoteAddr returns a string of the remote address, i.e. the address that
// sent the request.
func RemoteAddr(r *http.Request) string {
via := r.RemoteAddr
xff := r.Header.Get(pdv1.Forward)
if xff != "" {
return fmt.Sprintf("%v via %v", xff, r.RemoteAddr)
}
return via
}