udptun: fixed a bug in nat mapping. #91

This commit is contained in:
nadoo committed 2019-03-12 23:32:23 +08:00
1 parent 4ece9ece25
commit 47406ce4ce
9 files changed
+84 -65

No files matched your search

+11 -11
View File
@@ -13,10 +13,10 @@ import (
"github.com/nadoo/glider/proxy"
)
// HandleFunc function handles the dns TypeA or TypeAAAA answer
// HandleFunc function handles the dns TypeA or TypeAAAA answer.
type HandleFunc func(Domain, ip string) error
// Config for dns
// Config for dns.
type Config struct {
Servers []string
Timeout int
@@ -26,7 +26,7 @@ type Config struct {
AlwaysTCP bool
}
// Client is a dns client struct
// Client is a dns client struct.
type Client struct {
dialer proxy.Dialer
cache *Cache
@@ -36,7 +36,7 @@ type Client struct {
handlers []HandleFunc
}
// NewClient returns a new dns client
// NewClient returns a new dns client.
func NewClient(dialer proxy.Dialer, config *Config) (*Client, error) {
c := &Client{
dialer: dialer,
@@ -54,7 +54,7 @@ func NewClient(dialer proxy.Dialer, config *Config) (*Client, error) {
return c, nil
}
// Exchange handles request msg and returns response msg
// Exchange handles request message and returns response message.
// reqBytes = reqLen + reqMsg
func (c *Client) Exchange(reqBytes []byte, clientAddr string, preferTCP bool) ([]byte, error) {
req, err := UnmarshalMessage(reqBytes[2:])
@@ -122,7 +122,7 @@ func (c *Client) Exchange(reqBytes []byte, clientAddr string, preferTCP bool) ([
return respBytes, nil
}
// exchange choose a upstream dns server based on qname, communicate with it on the network
// exchange choose a upstream dns server based on qname, communicate with it on the network.
func (c *Client) exchange(qname string, reqBytes []byte, preferTCP bool) (server, network string, respBytes []byte, err error) {
// use tcp to connect upstream server default
network = "tcp"
@@ -170,7 +170,7 @@ func (c *Client) exchange(qname string, reqBytes []byte, preferTCP bool) (server
return server, network, respBytes, err
}
// exchangeTCP exchange with server over tcp
// exchangeTCP exchange with server over tcp.
func (c *Client) exchangeTCP(rc net.Conn, reqBytes []byte) ([]byte, error) {
if _, err := rc.Write(reqBytes); err != nil {
log.F("[dns] failed to write req message: %v", err)
@@ -195,7 +195,7 @@ func (c *Client) exchangeTCP(rc net.Conn, reqBytes []byte) ([]byte, error) {
return respBytes, nil
}
// exchangeUDP exchange with server over udp
// exchangeUDP exchange with server over udp.
func (c *Client) exchangeUDP(rc net.Conn, reqBytes []byte) ([]byte, error) {
if _, err := rc.Write(reqBytes[2:]); err != nil {
log.F("[dns] failed to write req message: %v", err)
@@ -212,7 +212,7 @@ func (c *Client) exchangeUDP(rc net.Conn, reqBytes []byte) ([]byte, error) {
return reqBytes[:2+n], nil
}
// SetServers sets upstream dns servers for the given domain
// SetServers sets upstream dns servers for the given domain.
func (c *Client) SetServers(domain string, servers ...string) {
c.upServerMap[domain] = append(c.upServerMap[domain], servers...)
}
@@ -232,7 +232,7 @@ func (c *Client) GetServers(domain string) []string {
return c.upServers
}
// AddHandler adds a custom handler to handle the resolved result (A and AAAA)
// AddHandler adds a custom handler to handle the resolved result (A and AAAA).
func (c *Client) AddHandler(h HandleFunc) {
c.handlers = append(c.handlers, h)
}
@@ -258,7 +258,7 @@ func (c *Client) AddRecord(record string) error {
return nil
}
// GenResponse generates a dns response message for the given domani an ip address
// GenResponse generates a dns response message for the given domain and ip address.
func (c *Client) GenResponse(domain string, ip string) (*Message, error) {
ipb := net.ParseIP(ip)
if ipb == nil {