Fix: unexpected proxy dial behavior on mapping mode
This commit is contained in:
70
constant/dns.go
Normal file
70
constant/dns.go
Normal file
@ -0,0 +1,70 @@
|
||||
package constant
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// DNSModeMapping is a mapping for EnhancedMode enum
|
||||
var DNSModeMapping = map[string]DNSMode{
|
||||
DNSNormal.String(): DNSNormal,
|
||||
DNSFakeIP.String(): DNSFakeIP,
|
||||
DNSMapping.String(): DNSMapping,
|
||||
}
|
||||
|
||||
const (
|
||||
DNSNormal DNSMode = iota
|
||||
DNSFakeIP
|
||||
DNSMapping
|
||||
)
|
||||
|
||||
type DNSMode int
|
||||
|
||||
// UnmarshalYAML unserialize EnhancedMode with yaml
|
||||
func (e *DNSMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var tp string
|
||||
if err := unmarshal(&tp); err != nil {
|
||||
return err
|
||||
}
|
||||
mode, exist := DNSModeMapping[tp]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*e = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML serialize EnhancedMode with yaml
|
||||
func (e DNSMode) MarshalYAML() (interface{}, error) {
|
||||
return e.String(), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON unserialize EnhancedMode with json
|
||||
func (e *DNSMode) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
mode, exist := DNSModeMapping[tp]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*e = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize EnhancedMode with json
|
||||
func (e DNSMode) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(e.String())
|
||||
}
|
||||
|
||||
func (e DNSMode) String() string {
|
||||
switch e {
|
||||
case DNSNormal:
|
||||
return "normal"
|
||||
case DNSFakeIP:
|
||||
return "fake-ip"
|
||||
case DNSMapping:
|
||||
return "redir-host"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
@ -71,6 +71,7 @@ type Metadata struct {
|
||||
DstPort string `json:"destinationPort"`
|
||||
AddrType int `json:"-"`
|
||||
Host string `json:"host"`
|
||||
DNSMode DNSMode `json:"dnsMode"`
|
||||
}
|
||||
|
||||
func (m *Metadata) RemoteAddress() string {
|
||||
@ -85,6 +86,23 @@ func (m *Metadata) Resolved() bool {
|
||||
return m.DstIP != nil
|
||||
}
|
||||
|
||||
// Pure is used to solve unexpected behavior
|
||||
// when dialing proxy connection in DNSMapping mode.
|
||||
func (m *Metadata) Pure() *Metadata {
|
||||
if m.DNSMode == DNSMapping && m.DstIP != nil {
|
||||
copy := *m
|
||||
copy.Host = ""
|
||||
if copy.DstIP.To4() != nil {
|
||||
copy.AddrType = AtypIPv4
|
||||
} else {
|
||||
copy.AddrType = AtypIPv6
|
||||
}
|
||||
return ©
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Metadata) UDPAddr() *net.UDPAddr {
|
||||
if m.NetWork != UDP || m.DstIP == nil {
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user