-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
101 lines (95 loc) · 2.36 KB
/
conn.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
package doh
import (
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
type Conn struct {
io.Writer
response func() (io.Reader, error)
Reset func()
HttpGet bool
}
var _ net.Conn = (*Conn)(nil)
func NewConn(client *http.Client, ctx context.Context, server string) (conn *Conn) {
if client == nil {
client = http.DefaultClient
}
body := new(bytes.Buffer)
conn = &Conn{Writer: body}
request := func() (reader io.Reader, err error) {
link := fmt.Sprintf("https://%s/dns-query", server)
if strings.HasPrefix(server, "https://") || strings.HasPrefix(server, "http://") {
link = server
}
body.Next(2) // skip uint16 length [2]byte
var req *http.Request
if conn.HttpGet {
var u *url.URL
u, err = url.Parse(link)
if err != nil {
return
}
b64 := base64.RawURLEncoding.EncodeToString(body.Bytes())
q := u.Query()
q.Set("dns", b64)
u.RawQuery = q.Encode()
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
} else {
req, err = http.NewRequest(http.MethodPost, link, body)
}
if err != nil {
return
}
if ctx != nil {
req = req.WithContext(ctx)
}
req.Header.Set("Content-Type", "application/dns-message")
req.Header.Add("Accept", "application/dns-message")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("expect code 200, but got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
length := new(bytes.Buffer)
binary.Write(length, binary.BigEndian, uint16(len(body)))
reader = io.MultiReader(length, bytes.NewBuffer(body))
return reader, nil
}
conn.Reset = func() {
body.Reset()
conn.response = sync.OnceValues(request)
}
conn.Reset()
return conn
}
func (conn *Conn) Read(b []byte) (n int, err error) {
reader, err := conn.response()
if err != nil {
return
}
n, err = reader.Read(b)
return
}
func (conn *Conn) Close() error { return nil }
func (*Conn) LocalAddr() net.Addr { return nil }
func (*Conn) RemoteAddr() net.Addr { return nil }
func (*Conn) SetDeadline(t time.Time) error { return nil }
func (*Conn) SetReadDeadline(t time.Time) error { return nil }
func (*Conn) SetWriteDeadline(t time.Time) error { return nil }