Skip to content
This repository has been archived by the owner on Apr 3, 2021. It is now read-only.

Commit

Permalink
get fake ip range from arguments
Browse files Browse the repository at this point in the history
The network 241.0.0.0 does not seem to work on Windows, by
default use the 172.x.x.x subnet and allow configuring the
range in arguments
  • Loading branch information
eycorsican committed May 22, 2019
1 parent 7391db8 commit 8b3d04b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/tun2socks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type CmdArgs struct {
DnsFallback *bool
LogLevel *string
EnableFakeDns *bool
FakeDnsMinIP *string
FakeDnsMaxIP *string
}

type cmdFlag uint
Expand Down
4 changes: 3 additions & 1 deletion cmd/tun2socks/main_fakedns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

func init() {
args.EnableFakeDns = flag.Bool("fakeDns", false, "Enable fake DNS (SOCKS and Shadowsocks handler)")
args.FakeDnsMinIP = flag.String("fakeDnsMinIP", "172.255.0.0", "Minimum fake IP used by fake DNS")
args.FakeDnsMaxIP = flag.String("fakeDnsMaxIP", "172.255.255.255", "Maximum fake IP used by fake DNS")

addPostFlagsInitFn(func() {
if *args.EnableFakeDns {
fakeDns = fakedns.NewSimpleFakeDns()
fakeDns = fakedns.NewSimpleFakeDns(*args.FakeDnsMinIP, *args.FakeDnsMaxIP)
} else {
fakeDns = nil
}
Expand Down
22 changes: 14 additions & 8 deletions common/dns/fakedns/fakedns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
)

const (
// If fake dns response ttl is set to 1, 256 fake ips should be suffice.
MinFakeIPCursor uint32 = 0xf1000000 // 241.0.0.0
MaxFakeIPCursor uint32 = 0xf10000ff // 241.0.0.255
FakeResponseTtl uint32 = 1 // in sec
)

Expand Down Expand Up @@ -73,10 +70,19 @@ func ip2uint32(ip net.IP) uint32 {
return binary.BigEndian.Uint32([]byte(ip)[net.IPv6len-net.IPv4len:])
}

func NewSimpleFakeDns() cdns.FakeDns {
func NewSimpleFakeDns(minIP, maxIP string) cdns.FakeDns {
parsedMinIP := net.ParseIP(minIP)
parsedMaxIP := net.ParseIP(maxIP)
if parsedMinIP == nil || parsedMaxIP == nil {
return nil
}
minFakeIPCursor := ip2uint32(parsedMinIP)
maxFakeIPCursor := ip2uint32(parsedMaxIP)
return &simpleFakeDns{
ip2domain: make(map[uint32]string, 64),
cursor: MinFakeIPCursor,
cursor: minFakeIPCursor,
minCursor: minFakeIPCursor,
maxCursor: maxFakeIPCursor,
}
}

Expand All @@ -86,8 +92,8 @@ func (f *simpleFakeDns) allocateIP(domain string) net.IP {
f.ip2domain[f.cursor] = domain
ip := uint322ip(f.cursor)
f.cursor += 1
if f.cursor > MaxFakeIPCursor {
f.cursor = MinFakeIPCursor
if f.cursor > f.maxCursor {
f.cursor = f.minCursor
}
return ip
}
Expand Down Expand Up @@ -151,7 +157,7 @@ func (f *simpleFakeDns) GenerateFakeResponse(request []byte) ([]byte, error) {

func (f *simpleFakeDns) IsFakeIP(ip net.IP) bool {
c := ip2uint32(ip)
if c >= MinFakeIPCursor && c <= MaxFakeIPCursor {
if c >= f.minCursor && c <= f.maxCursor {
return true
}
return false
Expand Down

0 comments on commit 8b3d04b

Please sign in to comment.