Fix: fakeip pool cycle used
This commit is contained in:
parent
21446ba5d4
commit
4be17653e0
1 changed files with 19 additions and 18 deletions
|
@ -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,6 +98,7 @@ func (p *Pool) CloneFrom(o *Pool) {
|
|||
}
|
||||
|
||||
func (p *Pool) get(host string) netip.Addr {
|
||||
for {
|
||||
p.offset = p.offset.Next()
|
||||
|
||||
if !p.offset.Less(p.last) {
|
||||
|
@ -113,6 +108,12 @@ func (p *Pool) get(host string) netip.Addr {
|
|||
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue