Fix: fakeip pool cycle used

This commit is contained in:
yaling888 2022-04-12 21:54:54 +08:00 committed by Meta Gowork
parent 21446ba5d4
commit 4be17653e0

View file

@ -1,22 +1,16 @@
package fakeip
import (
"encoding/binary"
"errors"
"math/bits"
"net/netip"
"sync"
_ "unsafe"
"github.com/Dreamacro/clash/component/profile/cachefile"
"github.com/Dreamacro/clash/component/trie"
)
//go:linkname beUint64 net/netip.beUint64
func beUint64(b []byte) uint64
//go:linkname bePutUint64 net/netip.bePutUint64
func bePutUint64(b []byte, v uint64)
type uint128 struct {
hi uint64
lo uint64
@ -104,15 +98,22 @@ func (p *Pool) CloneFrom(o *Pool) {
}
func (p *Pool) get(host string) netip.Addr {
p.offset = p.offset.Next()
for {
p.offset = p.offset.Next()
if !p.offset.Less(p.last) {
p.cycle = true
p.offset = p.first
}
if !p.offset.Less(p.last) {
p.cycle = true
p.offset = p.first
}
if p.cycle {
p.store.DelByIP(p.offset)
if p.cycle {
p.store.DelByIP(p.offset)
break
}
if !p.store.Exist(p.offset) {
break
}
}
p.store.PutByIP(p.offset, host)
@ -174,8 +175,8 @@ func add(addr netip.Addr, n uint64) netip.Addr {
buf := addr.As16()
u := uint128{
beUint64(buf[:8]),
beUint64(buf[8:]),
binary.BigEndian.Uint64(buf[:8]),
binary.BigEndian.Uint64(buf[8:]),
}
lo, carry := bits.Add64(u.lo, n, 0)
@ -183,8 +184,8 @@ func add(addr netip.Addr, n uint64) netip.Addr {
u.hi = u.hi + carry
u.lo = lo
bePutUint64(buf[:8], u.hi)
bePutUint64(buf[8:], u.lo)
binary.BigEndian.PutUint64(buf[:8], u.hi)
binary.BigEndian.PutUint64(buf[8:], u.lo)
a := netip.AddrFrom16(buf)