Skip to content

Commit 90e0e57

Browse files
committed
Refactor wire conn
1 parent 933e96d commit 90e0e57

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

daze.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,31 @@ func ResolverDot(addr string) *net.Resolver {
121121
}
122122
}
123123

124-
// Cdoh structure can be used for DoH protocol processing.
125-
type Cdoh struct {
126-
Server string
127-
Buffer *bytes.Buffer
128-
}
129-
130-
func (c Cdoh) Read(b []byte) (n int, err error) { return c.Buffer.Read(b) }
131-
func (c Cdoh) Close() error { return nil }
132-
func (c Cdoh) LocalAddr() net.Addr { return nil }
133-
func (c Cdoh) RemoteAddr() net.Addr { return nil }
134-
func (c Cdoh) SetDeadline(t time.Time) error { return nil }
135-
func (c Cdoh) SetReadDeadline(t time.Time) error { return nil }
136-
func (c Cdoh) SetWriteDeadline(t time.Time) error { return nil }
137-
func (c Cdoh) Write(b []byte) (n int, err error) {
124+
// WireConn structure can be used for DoH protocol processing.
125+
type WireConn struct {
126+
Call func(b []byte) ([]byte, error)
127+
Data *bytes.Buffer
128+
}
129+
130+
func (c *WireConn) Read(b []byte) (n int, err error) { return c.Data.Read(b) }
131+
func (c *WireConn) Close() error { return nil }
132+
func (c *WireConn) LocalAddr() net.Addr { return nil }
133+
func (c *WireConn) RemoteAddr() net.Addr { return nil }
134+
func (c *WireConn) SetDeadline(t time.Time) error { return nil }
135+
func (c *WireConn) SetReadDeadline(t time.Time) error { return nil }
136+
func (c *WireConn) SetWriteDeadline(t time.Time) error { return nil }
137+
func (c *WireConn) Write(b []byte) (n int, err error) {
138138
size := int(binary.BigEndian.Uint16(b[:2]))
139139
doa.Doa(size == len(b)-2)
140-
resp, err := http.Post(c.Server, "application/dns-message", bytes.NewReader(b[2:]))
140+
r, err := c.Call(b[2:])
141141
if err != nil {
142-
log.Println("cdoh:", err)
142+
log.Println("daze:", err)
143143
return len(b), nil
144144
}
145-
body, err := io.ReadAll(resp.Body)
146-
if err != nil {
147-
log.Println("cdoh:", err)
148-
return len(b), nil
149-
}
150-
data := make([]byte, 2+len(body))
151-
binary.BigEndian.PutUint16(data[:2], uint16(len(body)))
152-
copy(data[2:], body)
153-
c.Buffer.Write(data)
145+
data := make([]byte, 2+len(r))
146+
binary.BigEndian.PutUint16(data[:2], uint16(len(r)))
147+
copy(data[2:], r)
148+
c.Data.Write(data)
154149
return len(b), nil
155150
}
156151

@@ -166,9 +161,23 @@ func ResolverDoh(addr string) *net.Resolver {
166161
return &net.Resolver{
167162
PreferGo: true,
168163
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
169-
conn := &Cdoh{
170-
Server: urls.String(),
171-
Buffer: bytes.NewBuffer([]byte{}),
164+
conn := &WireConn{
165+
Call: func(b []byte) ([]byte, error) {
166+
resp, err := http.Post(urls.String(), "application/dns-message", bytes.NewReader(b))
167+
if err != nil {
168+
return nil, err
169+
}
170+
defer resp.Body.Close()
171+
body, err := io.ReadAll(resp.Body)
172+
if err != nil {
173+
return nil, err
174+
}
175+
if resp.StatusCode != http.StatusOK {
176+
return nil, errors.New(string(body))
177+
}
178+
return body, nil
179+
},
180+
Data: bytes.NewBuffer([]byte{}),
172181
}
173182
return conn, nil
174183
},

0 commit comments

Comments
 (0)