chore: parse user's hosts before remoteDial

This commit is contained in:
wwqgtxx 2022-10-29 09:02:38 +08:00
parent 6106adc6a5
commit a46436f61a
3 changed files with 14 additions and 2 deletions

View File

@ -16,6 +16,7 @@ const (
DNSNormal DNSMode = iota
DNSFakeIP
DNSMapping
DNSHosts
)
type DNSMode int
@ -64,6 +65,8 @@ func (e DNSMode) String() string {
return "fake-ip"
case DNSMapping:
return "redir-host"
case DNSHosts:
return "hosts"
default:
return "unknown"
}

View File

@ -145,7 +145,7 @@ func (m *Metadata) Resolved() bool {
// 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.IsValid() {
if (m.DNSMode == DNSMapping || m.DNSMode == DNSHosts) && m.DstIP.IsValid() {
copyM := *m
copyM.Host = ""
if copyM.DstIP.Is4() {

View File

@ -331,9 +331,18 @@ func handleTCPConn(connCtx C.ConnContext) {
return
}
dialMetadata := metadata
if len(metadata.Host) > 0 {
if node := resolver.DefaultHosts.Search(metadata.Host); node != nil {
dialMetadata.DstIP = node.Data
dialMetadata.DNSMode = C.DNSHosts
dialMetadata = dialMetadata.Pure()
}
}
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout)
defer cancel()
remoteConn, err := proxy.DialContext(ctx, metadata)
remoteConn, err := proxy.DialContext(ctx, dialMetadata)
if err != nil {
if rule == nil {
log.Warnln("[TCP] dial %s to %s error: %s", proxy.Name(), metadata.RemoteAddress(), err.Error())