Skip to content

Commit

Permalink
fix: when host's ip in fakeip's range, don't send to remote server
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Nov 10, 2022
1 parent 7c8d8f5 commit 64552fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
15 changes: 7 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
}
config.DNS = dnsCfg

err = parseTun(rawCfg.Tun, config.General, dnsCfg)
err = parseTun(rawCfg.Tun, config.General)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1053,8 +1053,9 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[netip.Addr], rules []C.R
}
}

fakeIPRange, err := netip.ParsePrefix(cfg.FakeIPRange)
T.SetFakeIPRange(fakeIPRange)
if cfg.EnhancedMode == C.DNSFakeIP {
ipnet, err := netip.ParsePrefix(cfg.FakeIPRange)
if err != nil {
return nil, err
}
Expand All @@ -1081,7 +1082,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[netip.Addr], rules []C.R
}

pool, err := fakeip.New(fakeip.Options{
IPNet: &ipnet,
IPNet: &fakeIPRange,
Size: 1000,
Host: host,
Persistence: rawCfg.Profile.StoreFakeIP,
Expand Down Expand Up @@ -1124,7 +1125,7 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser {
return users
}

func parseTun(rawTun RawTun, general *General, dnsCfg *DNS) error {
func parseTun(rawTun RawTun, general *General) error {
var dnsHijack []netip.AddrPort

for _, d := range rawTun.DNSHijack {
Expand All @@ -1140,10 +1141,8 @@ func parseTun(rawTun RawTun, general *General, dnsCfg *DNS) error {
dnsHijack = append(dnsHijack, addrPort)
}

var tunAddressPrefix netip.Prefix
if dnsCfg.FakeIPRange != nil {
tunAddressPrefix = *dnsCfg.FakeIPRange.IPNet()
} else {
tunAddressPrefix := T.FakeIPRange()
if !tunAddressPrefix.IsValid() {
tunAddressPrefix = netip.MustParsePrefix("198.18.0.1/16")
}
tunAddressPrefix = netip.PrefixFrom(tunAddressPrefix.Addr(), 30)
Expand Down
18 changes: 15 additions & 3 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ var (
udpTimeout = 60 * time.Second

alwaysFindProcess = false

fakeIPRange netip.Prefix
)

func SetFakeIPRange(p netip.Prefix) {
fakeIPRange = p
}

func FakeIPRange() netip.Prefix {
return fakeIPRange
}

func SetSniffing(b bool) {
if sniffer.Dispatcher.Enable() {
configMux.Lock()
Expand Down Expand Up @@ -334,9 +344,11 @@ func handleTCPConn(connCtx C.ConnContext) {
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()
if dstIp := node.Data(); !FakeIPRange().Contains(dstIp) {
dialMetadata.DstIP = dstIp
dialMetadata.DNSMode = C.DNSHosts
dialMetadata = dialMetadata.Pure()
}
}
}

Expand Down

0 comments on commit 64552fb

Please sign in to comment.