From ebacc76433794d9bbb94d3259ab0ca19c9e35e10 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Tue, 12 Apr 2022 21:54:54 +0800 Subject: [PATCH] Fix: fakeip pool cycle used --- component/fakeip/pool.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/component/fakeip/pool.go b/component/fakeip/pool.go index 2b887fc3..e1a8150a 100644 --- a/component/fakeip/pool.go +++ b/component/fakeip/pool.go @@ -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)