Compare commits

..

2 Commits

@ -1,59 +1,59 @@
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/miekg/dns"
)
type httpclient struct {
Net string
Timeout time.Duration
HTTPClient *http.Client
}
func (c *httpclient) Exchange(msg *dns.Msg, upstream string) (*dns.Msg, int, error) {
data, err := msg.Pack()
if err != nil {
return nil, 0, err
}
req, err := http.NewRequest("POST", upstream, bytes.NewReader(data))
if err != nil {
return nil, 0, err
}
req.Header.Set("Content-Type", dnsMsgType)
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, 0, fmt.Errorf("http error %d", resp.StatusCode)
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}
m := new(dns.Msg)
if err = m.Unpack(data); err != nil {
return nil, 0, err
}
return m, 0, nil
}
package main
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/miekg/dns"
)
type httpclient struct {
Net string
Timeout time.Duration
HTTPClient *http.Client
}
func (c *httpclient) Exchange(msg *dns.Msg, upstream string) (*dns.Msg, int, error) {
data, err := msg.Pack()
if err != nil {
return nil, 0, err
}
req, err := http.NewRequest("POST", upstream, bytes.NewReader(data))
if err != nil {
return nil, 0, err
}
req.Header.Set("Content-Type", dnsMsgType)
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, 0, fmt.Errorf("http error %d", resp.StatusCode)
}
data, err = io.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}
m := new(dns.Msg)
if err = m.Unpack(data); err != nil {
return nil, 0, err
}
return m, 0, nil
}

@ -1,7 +1,7 @@
package main
import (
"io/ioutil"
"os"
"github.com/go-yaml/yaml"
)
@ -21,7 +21,7 @@ type listen struct {
}
func loadConfig(f string) (*conf, error) {
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
return nil, err
}

@ -61,9 +61,7 @@ func main() {
makeServers(cfg)
ch := make(chan os.Signal, 2)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
select {
case s := <-ch:
log.Errorf("received signal %s, exit...", s)
}
s := <-ch
log.Errorf("received signal %s, exit...", s)
log.Println("exit.")
}

@ -1,7 +1,7 @@
package main
import (
"io/ioutil"
"io"
"net/http"
log "github.com/fangdingjun/go-log/v5"
@ -26,7 +26,7 @@ func (srv *server) handleHTTPReq(w http.ResponseWriter, r *http.Request) {
return
}
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
log.Errorln("read request body", err)
http.Error(w, "read request failed", http.StatusBadRequest)

Loading…
Cancel
Save