mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-09 10:40:10 +08:00
add an option to set cache size
This commit is contained in:
@@ -50,6 +50,7 @@ type CmdArgs struct {
|
|||||||
FakeIPRange *string
|
FakeIPRange *string
|
||||||
FakeDnsAddr *string
|
FakeDnsAddr *string
|
||||||
FakeDnsHosts *string
|
FakeDnsHosts *string
|
||||||
|
DnsCacheSize *int
|
||||||
ExceptionApps *string
|
ExceptionApps *string
|
||||||
ExceptionSendThrough *string
|
ExceptionSendThrough *string
|
||||||
Stats *bool
|
Stats *bool
|
||||||
|
@@ -13,10 +13,11 @@ func init() {
|
|||||||
args.FakeDnsAddr = flag.String("fakeDnsAddr", ":53", "listen address of fake DNS")
|
args.FakeDnsAddr = flag.String("fakeDnsAddr", ":53", "listen address of fake DNS")
|
||||||
args.FakeIPRange = flag.String("fakeIPRange", "198.18.0.1/16", "fake IP CIDR range for DNS")
|
args.FakeIPRange = flag.String("fakeIPRange", "198.18.0.1/16", "fake IP CIDR range for DNS")
|
||||||
args.FakeDnsHosts = flag.String("fakeDnsHosts", "", "Hosts mapping, e.g. 'a.com=1.1.1.1,b.net=2.2.2.2'")
|
args.FakeDnsHosts = flag.String("fakeDnsHosts", "", "Hosts mapping, e.g. 'a.com=1.1.1.1,b.net=2.2.2.2'")
|
||||||
|
args.DnsCacheSize = flag.Int("dnsCacheSize", 100, "Size of LRU-Cache to store DNS msg")
|
||||||
|
|
||||||
addPostFlagsInitFn(func() {
|
addPostFlagsInitFn(func() {
|
||||||
if *args.EnableFakeDns {
|
if *args.EnableFakeDns {
|
||||||
fakeDnsServer, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDnsHosts)
|
fakeDnsServer, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDnsHosts, *args.DnsCacheSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("create fake dns server error")
|
panic("create fake dns server error")
|
||||||
}
|
}
|
||||||
|
@@ -18,8 +18,6 @@ const (
|
|||||||
|
|
||||||
// var cacheDuration = time.Duration(dnsDefaultTTL) * time.Second
|
// var cacheDuration = time.Duration(dnsDefaultTTL) * time.Second
|
||||||
|
|
||||||
var cacheSize = 100
|
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*D.Server
|
*D.Server
|
||||||
c *cache.Cache
|
c *cache.Cache
|
||||||
@@ -68,7 +66,7 @@ func (s *Server) IPToHost(ip net.IP) (string, bool) {
|
|||||||
return strings.TrimRight(fqdn, "."), true
|
return strings.TrimRight(fqdn, "."), true
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(fakeIPRange, hostsLine string) (*Server, error) {
|
func NewServer(fakeIPRange, hostsLine string, size int) (*Server, error) {
|
||||||
_, ipnet, err := net.ParseCIDR(fakeIPRange)
|
_, ipnet, err := net.ParseCIDR(fakeIPRange)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -79,7 +77,7 @@ func NewServer(fakeIPRange, hostsLine string) (*Server, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hosts := lineToHosts(hostsLine)
|
hosts := lineToHosts(hostsLine)
|
||||||
cacheItem := cache.New(cacheSize)
|
cacheItem := cache.New(size)
|
||||||
handler := newHandler(hosts, cacheItem, pool)
|
handler := newHandler(hosts, cacheItem, pool)
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
|
Reference in New Issue
Block a user