2019-05-02 16:05:14 +00:00
|
|
|
package fakeip
|
|
|
|
|
|
|
|
import (
|
2022-04-12 13:54:54 +00:00
|
|
|
"encoding/binary"
|
2019-05-02 16:05:14 +00:00
|
|
|
"errors"
|
2022-04-11 16:31:04 +00:00
|
|
|
"math/bits"
|
|
|
|
"net/netip"
|
2019-05-23 15:27:29 +00:00
|
|
|
"sync"
|
2019-07-26 11:09:13 +00:00
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
"github.com/Dreamacro/clash/component/profile/cachefile"
|
2020-05-28 04:13:05 +00:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2019-05-02 16:05:14 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 21:55:08 +00:00
|
|
|
const (
|
|
|
|
offsetKey = "key-offset-fake-ip"
|
|
|
|
cycleKey = "key-cycle-fake-ip"
|
|
|
|
)
|
|
|
|
|
2022-04-11 16:31:04 +00:00
|
|
|
type uint128 struct {
|
|
|
|
hi uint64
|
|
|
|
lo uint64
|
|
|
|
}
|
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
type store interface {
|
2022-04-11 16:31:04 +00:00
|
|
|
GetByHost(host string) (netip.Addr, bool)
|
|
|
|
PutByHost(host string, ip netip.Addr)
|
|
|
|
GetByIP(ip netip.Addr) (string, bool)
|
|
|
|
PutByIP(ip netip.Addr, host string)
|
|
|
|
DelByIP(ip netip.Addr)
|
|
|
|
Exist(ip netip.Addr) bool
|
2021-10-11 12:48:58 +00:00
|
|
|
CloneTo(store)
|
2022-03-22 17:05:43 +00:00
|
|
|
FlushFakeIP() error
|
2021-10-11 12:48:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 16:05:14 +00:00
|
|
|
// Pool is a implementation about fake ip generator without storage
|
|
|
|
type Pool struct {
|
2022-04-11 16:31:04 +00:00
|
|
|
gateway netip.Addr
|
|
|
|
first netip.Addr
|
|
|
|
last netip.Addr
|
|
|
|
offset netip.Addr
|
|
|
|
cycle bool
|
|
|
|
mux sync.Mutex
|
|
|
|
host *trie.DomainTrie[bool]
|
|
|
|
ipnet *netip.Prefix
|
|
|
|
store store
|
2019-07-26 11:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup return a fake ip with host
|
2022-04-11 16:31:04 +00:00
|
|
|
func (p *Pool) Lookup(host string) netip.Addr {
|
2019-07-26 11:09:13 +00:00
|
|
|
p.mux.Lock()
|
|
|
|
defer p.mux.Unlock()
|
2021-10-11 12:48:58 +00:00
|
|
|
if ip, exist := p.store.GetByHost(host); exist {
|
2019-10-11 06:01:16 +00:00
|
|
|
return ip
|
2019-07-26 11:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ip := p.get(host)
|
2021-10-11 12:48:58 +00:00
|
|
|
p.store.PutByHost(host, ip)
|
2019-07-26 11:09:13 +00:00
|
|
|
return ip
|
2019-05-02 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:09:13 +00:00
|
|
|
// LookBack return host with the fake ip
|
2022-04-11 16:31:04 +00:00
|
|
|
func (p *Pool) LookBack(ip netip.Addr) (string, bool) {
|
2019-05-23 15:27:29 +00:00
|
|
|
p.mux.Lock()
|
|
|
|
defer p.mux.Unlock()
|
2019-07-26 11:09:13 +00:00
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
return p.store.GetByIP(ip)
|
2019-07-26 11:09:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
// ShouldSkipped return if domain should be skipped
|
|
|
|
func (p *Pool) ShouldSkipped(domain string) bool {
|
2019-12-27 16:10:06 +00:00
|
|
|
if p.host == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-28 10:44:01 +00:00
|
|
|
return p.host.Search(domain) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exist returns if given ip exists in fake-ip pool
|
2022-04-11 16:31:04 +00:00
|
|
|
func (p *Pool) Exist(ip netip.Addr) bool {
|
2019-12-28 10:44:01 +00:00
|
|
|
p.mux.Lock()
|
|
|
|
defer p.mux.Unlock()
|
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
return p.store.Exist(ip)
|
2019-12-27 16:10:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:09:13 +00:00
|
|
|
// Gateway return gateway ip
|
2022-04-11 16:31:04 +00:00
|
|
|
func (p *Pool) Gateway() netip.Addr {
|
|
|
|
return p.gateway
|
2019-07-26 11:09:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:31:04 +00:00
|
|
|
// Broadcast return the last ip
|
|
|
|
func (p *Pool) Broadcast() netip.Addr {
|
|
|
|
return p.last
|
2022-03-14 18:43:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 04:52:43 +00:00
|
|
|
// IPNet return raw ipnet
|
2022-04-11 16:31:04 +00:00
|
|
|
func (p *Pool) IPNet() *netip.Prefix {
|
2020-10-17 04:52:43 +00:00
|
|
|
return p.ipnet
|
|
|
|
}
|
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
// CloneFrom clone cache from old pool
|
|
|
|
func (p *Pool) CloneFrom(o *Pool) {
|
|
|
|
o.store.CloneTo(p.store)
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:31:04 +00:00
|
|
|
func (p *Pool) get(host string) netip.Addr {
|
2022-04-12 21:55:08 +00:00
|
|
|
p.offset = p.offset.Next()
|
2019-05-02 16:05:14 +00:00
|
|
|
|
2022-04-12 21:55:08 +00:00
|
|
|
if !p.offset.Less(p.last) {
|
|
|
|
p.cycle = true
|
|
|
|
p.offset = p.first
|
|
|
|
}
|
2022-04-12 13:54:54 +00:00
|
|
|
|
2022-04-12 21:55:08 +00:00
|
|
|
if p.cycle || p.store.Exist(p.offset) {
|
|
|
|
p.store.DelByIP(p.offset)
|
2022-04-11 16:31:04 +00:00
|
|
|
}
|
2022-03-22 17:05:43 +00:00
|
|
|
|
2022-04-11 16:31:04 +00:00
|
|
|
p.store.PutByIP(p.offset, host)
|
|
|
|
return p.offset
|
2019-05-02 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:31:04 +00:00
|
|
|
func (p *Pool) FlushFakeIP() error {
|
2022-04-12 21:55:08 +00:00
|
|
|
err := p.store.FlushFakeIP()
|
|
|
|
if err == nil {
|
|
|
|
p.cycle = false
|
|
|
|
p.offset = p.first
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pool) StoreState() {
|
|
|
|
if s, ok := p.store.(*cachefileStore); ok {
|
|
|
|
s.PutByHost(offsetKey, p.offset)
|
|
|
|
if p.cycle {
|
|
|
|
s.PutByHost(cycleKey, p.offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pool) restoreState() {
|
|
|
|
if s, ok := p.store.(*cachefileStore); ok {
|
|
|
|
if _, exist := s.GetByHost(cycleKey); exist {
|
|
|
|
p.cycle = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset, exist := s.GetByHost(offsetKey); exist {
|
|
|
|
if p.ipnet.Contains(offset) {
|
|
|
|
p.offset = offset
|
|
|
|
} else {
|
|
|
|
_ = p.FlushFakeIP()
|
|
|
|
}
|
|
|
|
} else if s.Exist(p.first) {
|
|
|
|
_ = p.FlushFakeIP()
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
type Options struct {
|
2022-04-11 16:31:04 +00:00
|
|
|
IPNet *netip.Prefix
|
2022-04-05 20:25:53 +00:00
|
|
|
Host *trie.DomainTrie[bool]
|
2021-10-11 12:48:58 +00:00
|
|
|
|
|
|
|
// 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-02 16:05:14 +00:00
|
|
|
// New return Pool instance
|
2021-10-11 12:48:58 +00:00
|
|
|
func New(options Options) (*Pool, error) {
|
2022-04-11 16:31:04 +00:00
|
|
|
var (
|
|
|
|
hostAddr = options.IPNet.Masked().Addr()
|
|
|
|
gateway = hostAddr.Next()
|
|
|
|
first = gateway.Next().Next()
|
|
|
|
last = add(hostAddr, 1<<uint64(hostAddr.BitLen()-options.IPNet.Bits())-1)
|
|
|
|
)
|
|
|
|
|
|
|
|
if !options.IPNet.IsValid() || !first.Less(last) || !options.IPNet.Contains(last) {
|
2019-05-02 16:05:14 +00:00
|
|
|
return nil, errors.New("ipnet don't have valid ip")
|
|
|
|
}
|
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
pool := &Pool{
|
2022-04-11 16:31:04 +00:00
|
|
|
gateway: gateway,
|
|
|
|
first: first,
|
|
|
|
last: last,
|
|
|
|
offset: first.Prev(),
|
|
|
|
cycle: false,
|
|
|
|
host: options.Host,
|
|
|
|
ipnet: options.IPNet,
|
2021-10-11 12:48:58 +00:00
|
|
|
}
|
|
|
|
if options.Persistence {
|
|
|
|
pool.store = &cachefileStore{
|
|
|
|
cache: cachefile.Cache(),
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-05 12:23:16 +00:00
|
|
|
pool.store = newMemoryStore(options.Size)
|
2021-10-11 12:48:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 21:55:08 +00:00
|
|
|
pool.restoreState()
|
|
|
|
|
2021-10-11 12:48:58 +00:00
|
|
|
return pool, nil
|
2019-05-02 16:05:14 +00:00
|
|
|
}
|
2022-04-11 16:31:04 +00:00
|
|
|
|
|
|
|
// add returns addr + n.
|
|
|
|
func add(addr netip.Addr, n uint64) netip.Addr {
|
|
|
|
buf := addr.As16()
|
|
|
|
|
|
|
|
u := uint128{
|
2022-04-12 13:54:54 +00:00
|
|
|
binary.BigEndian.Uint64(buf[:8]),
|
|
|
|
binary.BigEndian.Uint64(buf[8:]),
|
2022-04-11 16:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lo, carry := bits.Add64(u.lo, n, 0)
|
|
|
|
|
|
|
|
u.hi = u.hi + carry
|
|
|
|
u.lo = lo
|
|
|
|
|
2022-04-12 13:54:54 +00:00
|
|
|
binary.BigEndian.PutUint64(buf[:8], u.hi)
|
|
|
|
binary.BigEndian.PutUint64(buf[8:], u.lo)
|
2022-04-11 16:31:04 +00:00
|
|
|
|
|
|
|
a := netip.AddrFrom16(buf)
|
|
|
|
|
|
|
|
if addr.Is4() {
|
|
|
|
return a.Unmap()
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|