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 package fakeip
import ( import (
"encoding/binary"
"errors" "errors"
"math/bits" "math/bits"
"net/netip" "net/netip"
"sync" "sync"
_ "unsafe"
"github.com/Dreamacro/clash/component/profile/cachefile" "github.com/Dreamacro/clash/component/profile/cachefile"
"github.com/Dreamacro/clash/component/trie" "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 { type uint128 struct {
hi uint64 hi uint64
lo uint64 lo uint64
@ -104,6 +98,7 @@ func (p *Pool) CloneFrom(o *Pool) {
} }
func (p *Pool) get(host string) netip.Addr { func (p *Pool) get(host string) netip.Addr {
for {
p.offset = p.offset.Next() p.offset = p.offset.Next()
if !p.offset.Less(p.last) { if !p.offset.Less(p.last) {
@ -113,6 +108,12 @@ func (p *Pool) get(host string) netip.Addr {
if p.cycle { if p.cycle {
p.store.DelByIP(p.offset) p.store.DelByIP(p.offset)
break
}
if !p.store.Exist(p.offset) {
break
}
} }
p.store.PutByIP(p.offset, host) p.store.PutByIP(p.offset, host)
@ -174,8 +175,8 @@ func add(addr netip.Addr, n uint64) netip.Addr {
buf := addr.As16() buf := addr.As16()
u := uint128{ u := uint128{
beUint64(buf[:8]), binary.BigEndian.Uint64(buf[:8]),
beUint64(buf[8:]), binary.BigEndian.Uint64(buf[8:]),
} }
lo, carry := bits.Add64(u.lo, n, 0) 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.hi = u.hi + carry
u.lo = lo u.lo = lo
bePutUint64(buf[:8], u.hi) binary.BigEndian.PutUint64(buf[:8], u.hi)
bePutUint64(buf[8:], u.lo) binary.BigEndian.PutUint64(buf[8:], u.lo)
a := netip.AddrFrom16(buf) a := netip.AddrFrom16(buf)