2019-05-03 00:05:14 +08:00
|
|
|
package fakeip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2022-08-12 13:47:51 +08:00
|
|
|
"strings"
|
2019-05-23 23:27:29 +08:00
|
|
|
"sync"
|
2019-07-26 19:09:13 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
2021-10-11 20:48:58 +08:00
|
|
|
"github.com/Dreamacro/clash/component/profile/cachefile"
|
2020-05-28 12:13:05 +08:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2019-05-03 00:05:14 +08:00
|
|
|
)
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
type store interface {
|
|
|
|
GetByHost(host string) (net.IP, bool)
|
|
|
|
PutByHost(host string, ip net.IP)
|
|
|
|
GetByIP(ip net.IP) (string, bool)
|
|
|
|
PutByIP(ip net.IP, host string)
|
2021-11-23 22:01:49 +08:00
|
|
|
DelByIP(ip net.IP)
|
2021-10-11 20:48:58 +08:00
|
|
|
Exist(ip net.IP) bool
|
|
|
|
CloneTo(store)
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:05:14 +08:00
|
|
|
// Pool is a implementation about fake ip generator without storage
|
|
|
|
type Pool struct {
|
2019-07-26 19:09:13 +08:00
|
|
|
max uint32
|
|
|
|
min uint32
|
|
|
|
gateway uint32
|
|
|
|
offset uint32
|
2019-10-27 21:44:07 +08:00
|
|
|
mux sync.Mutex
|
2020-05-28 12:13:05 +08:00
|
|
|
host *trie.DomainTrie
|
2020-10-17 12:52:43 +08:00
|
|
|
ipnet *net.IPNet
|
2021-10-11 20:48:58 +08:00
|
|
|
store store
|
2019-07-26 19:09:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup return a fake ip with host
|
|
|
|
func (p *Pool) Lookup(host string) net.IP {
|
|
|
|
p.mux.Lock()
|
|
|
|
defer p.mux.Unlock()
|
2022-08-12 13:47:51 +08:00
|
|
|
|
|
|
|
// RFC4343: DNS Case Insensitive, we SHOULD return result with all cases.
|
|
|
|
host = strings.ToLower(host)
|
2021-10-11 20:48:58 +08:00
|
|
|
if ip, exist := p.store.GetByHost(host); exist {
|
2019-10-11 14:01:16 +08:00
|
|
|
return ip
|
2019-07-26 19:09:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ip := p.get(host)
|
2021-10-11 20:48:58 +08:00
|
|
|
p.store.PutByHost(host, ip)
|
2019-07-26 19:09:13 +08:00
|
|
|
return ip
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
// LookBack return host with the fake ip
|
|
|
|
func (p *Pool) LookBack(ip net.IP) (string, bool) {
|
2019-05-23 23:27:29 +08:00
|
|
|
p.mux.Lock()
|
|
|
|
defer p.mux.Unlock()
|
2019-07-26 19:09:13 +08:00
|
|
|
|
|
|
|
if ip = ip.To4(); ip == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
return p.store.GetByIP(ip)
|
2019-07-26 19:09:13 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
// ShouldSkipped return if domain should be skipped
|
|
|
|
func (p *Pool) ShouldSkipped(domain string) bool {
|
2019-12-28 00:10:06 +08:00
|
|
|
if p.host == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-28 18:44:01 +08:00
|
|
|
return p.host.Search(domain) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exist returns if given ip exists in fake-ip pool
|
|
|
|
func (p *Pool) Exist(ip net.IP) bool {
|
|
|
|
p.mux.Lock()
|
|
|
|
defer p.mux.Unlock()
|
|
|
|
|
|
|
|
if ip = ip.To4(); ip == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
return p.store.Exist(ip)
|
2019-12-28 00:10:06 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
// Gateway return gateway ip
|
|
|
|
func (p *Pool) Gateway() net.IP {
|
|
|
|
return uintToIP(p.gateway)
|
|
|
|
}
|
|
|
|
|
2020-10-17 12:52:43 +08:00
|
|
|
// IPNet return raw ipnet
|
|
|
|
func (p *Pool) IPNet() *net.IPNet {
|
|
|
|
return p.ipnet
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
// CloneFrom clone cache from old pool
|
|
|
|
func (p *Pool) CloneFrom(o *Pool) {
|
|
|
|
o.store.CloneTo(p.store)
|
2020-09-17 10:48:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
func (p *Pool) get(host string) net.IP {
|
|
|
|
current := p.offset
|
|
|
|
for {
|
|
|
|
p.offset = (p.offset + 1) % (p.max - p.min)
|
|
|
|
// Avoid infinite loops
|
|
|
|
if p.offset == current {
|
2021-11-23 22:01:49 +08:00
|
|
|
p.offset = (p.offset + 1) % (p.max - p.min)
|
|
|
|
ip := uintToIP(p.min + p.offset - 1)
|
|
|
|
p.store.DelByIP(ip)
|
2019-07-26 19:09:13 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
ip := uintToIP(p.min + p.offset - 1)
|
|
|
|
if !p.store.Exist(ip) {
|
2019-07-26 19:09:13 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ip := uintToIP(p.min + p.offset - 1)
|
2021-10-11 20:48:58 +08:00
|
|
|
p.store.PutByIP(ip, host)
|
2019-05-03 00:05:14 +08:00
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipToUint(ip net.IP) uint32 {
|
|
|
|
v := uint32(ip[0]) << 24
|
|
|
|
v += uint32(ip[1]) << 16
|
|
|
|
v += uint32(ip[2]) << 8
|
|
|
|
v += uint32(ip[3])
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func uintToIP(v uint32) net.IP {
|
2020-09-21 22:22:07 +08:00
|
|
|
return net.IP{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
type Options struct {
|
|
|
|
IPNet *net.IPNet
|
|
|
|
Host *trie.DomainTrie
|
|
|
|
|
|
|
|
// Size sets the maximum number of entries in memory
|
|
|
|
// and does not work if Persistence is true
|
|
|
|
Size int
|
|
|
|
|
|
|
|
// Persistence will save the data to disk.
|
|
|
|
// Size will not work and record will be fully stored.
|
|
|
|
Persistence bool
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:05:14 +08:00
|
|
|
// New return Pool instance
|
2021-10-11 20:48:58 +08:00
|
|
|
func New(options Options) (*Pool, error) {
|
|
|
|
min := ipToUint(options.IPNet.IP) + 2
|
2019-05-03 00:05:14 +08:00
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
ones, bits := options.IPNet.Mask.Size()
|
2019-05-03 00:05:14 +08:00
|
|
|
total := 1<<uint(bits-ones) - 2
|
|
|
|
|
|
|
|
if total <= 0 {
|
|
|
|
return nil, errors.New("ipnet don't have valid ip")
|
|
|
|
}
|
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
max := min + uint32(total) - 1
|
2021-10-11 20:48:58 +08:00
|
|
|
pool := &Pool{
|
2019-07-26 19:09:13 +08:00
|
|
|
min: min,
|
|
|
|
max: max,
|
|
|
|
gateway: min - 1,
|
2021-10-11 20:48:58 +08:00
|
|
|
host: options.Host,
|
|
|
|
ipnet: options.IPNet,
|
|
|
|
}
|
|
|
|
if options.Persistence {
|
|
|
|
pool.store = &cachefileStore{
|
|
|
|
cache: cachefile.Cache(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pool.store = &memoryStore{
|
|
|
|
cache: cache.NewLRUCache(cache.WithSize(options.Size * 2)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool, nil
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|