Compare commits
No commits in common. '9d3d523309eae7cbb501b8d17abcb9d5bf49baff' and '2d4867ee39c94032824ae7c18e0314e42020dc2f' have entirely different histories.
9d3d523309
...
2d4867ee39
@ -1,59 +1,59 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpclient struct {
|
type httpclient struct {
|
||||||
Net string
|
Net string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
HTTPClient *http.Client
|
HTTPClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpclient) Exchange(msg *dns.Msg, upstream string) (*dns.Msg, int, error) {
|
func (c *httpclient) Exchange(msg *dns.Msg, upstream string) (*dns.Msg, int, error) {
|
||||||
data, err := msg.Pack()
|
data, err := msg.Pack()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", upstream, bytes.NewReader(data))
|
req, err := http.NewRequest("POST", upstream, bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", dnsMsgType)
|
req.Header.Set("Content-Type", dnsMsgType)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
resp, err := c.HTTPClient.Do(req)
|
resp, err := c.HTTPClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, 0, fmt.Errorf("http error %d", resp.StatusCode)
|
return nil, 0, fmt.Errorf("http error %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err = io.ReadAll(resp.Body)
|
data, err = ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
m := new(dns.Msg)
|
m := new(dns.Msg)
|
||||||
if err = m.Unpack(data); err != nil {
|
if err = m.Unpack(data); err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, 0, nil
|
return m, 0, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue