-
Notifications
You must be signed in to change notification settings - Fork 66
/
https.go
89 lines (74 loc) · 1.79 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
82
83
84
85
86
87
88
89
/**
* dingo: a DNS caching proxy written in Go
* This file implements common code for HTTPS+JSON requests
*
* Copyright (C) 2016-2017 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"
import "github.com/lucas-clemente/quic-go/h2quic"
type Https struct {
client http.Client
}
func NewHttps(sni string, forceh1 bool) *Https {
H := Https{}
/* TLS setup */
tlscfg := new(tls.Config)
tlscfg.ServerName = sni
/* HTTP transport */
var tr http.RoundTripper
switch {
case forceh1 || *opt_h1:
h1 := new(http.Transport)
h1.TLSClientConfig = tlscfg
tr = h1
case *opt_quic:
quic := new(h2quic.RoundTripper)
// quic.TLSClientConfig = tlscfg // FIXME
tr = quic
default:
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 // FIXME: doesn't have an effect for QUIC
/* 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
}