forked from pforemski/dingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
https.go
81 lines (68 loc) · 1.55 KB
/
https.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
/**
* dingo: a DNS caching proxy written in Go
* This file implements common code for HTTPS+JSON requests
*
* Copyright (C) 2016 Pawel Foremski <[email protected]>
* Licensed under GNU GPL v3
*/
package main
import "time"
import "net/http"
import "io/ioutil"
import "crypto/tls"
import "errors"
import "golang.org/x/net/http2"
type Https struct {
client http.Client
}
func NewHttps(sni string) *Https {
H := Https{}
/* TLS setup */
tlscfg := new(tls.Config)
tlscfg.ServerName = sni
/* HTTP transport */
var tr http.RoundTripper
if (*opt_h1) {
h1 := new(http.Transport)
h1.TLSClientConfig = tlscfg
tr = h1
} else {
h2 := new(http2.Transport)
h2.TLSClientConfig = tlscfg
tr = h2
}
/* HTTP client */
H.client.Timeout = time.Second * 10
H.client.Transport = tr
return &H
}
func (R *Https) Get(ip string, host string, uri string) ([]byte, error) {
url := "https://" + ip + uri
hreq, err := http.NewRequest("GET", url, nil)
if err != nil {
dbg(1, "http.NewRequest(): %s", err)
return nil, err
}
hreq.Host = host
/* send the query */
resp, err := R.client.Do(hreq)
if err != nil {
dbg(1, "http.Do(): %s", err)
return nil, err
}
dbg(3, "http.Do(%s): %s %s", url, resp.Status, resp.Proto)
/* read */
buf, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
dbg(1, "ioutil.ReadAll(%s): %s", url, err)
return nil, err
}
dbg(7, " reply: %s", buf)
/* HTTP 200 OK? */
if resp.StatusCode != 200 {
dbg(1, "resp.StatusCode != 200: %s", url)
return nil, errors.New("response code != 200")
}
return buf, nil
}