Chore: IpToAddr
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"math/bits"
|
||||
"net/netip"
|
||||
"sync"
|
||||
|
||||
"github.com/Dreamacro/clash/common/nnip"
|
||||
"github.com/Dreamacro/clash/component/profile/cachefile"
|
||||
"github.com/Dreamacro/clash/component/trie"
|
||||
)
|
||||
@ -16,11 +15,6 @@ const (
|
||||
cycleKey = "key-cycle-fake-ip"
|
||||
)
|
||||
|
||||
type uint128 struct {
|
||||
hi uint64
|
||||
lo uint64
|
||||
}
|
||||
|
||||
type store interface {
|
||||
GetByHost(host string) (netip.Addr, bool)
|
||||
PutByHost(host string, ip netip.Addr)
|
||||
@ -122,7 +116,7 @@ func (p *Pool) FlushFakeIP() error {
|
||||
err := p.store.FlushFakeIP()
|
||||
if err == nil {
|
||||
p.cycle = false
|
||||
p.offset = p.first
|
||||
p.offset = p.first.Prev()
|
||||
}
|
||||
return err
|
||||
}
|
||||
@ -173,10 +167,10 @@ func New(options Options) (*Pool, error) {
|
||||
hostAddr = options.IPNet.Masked().Addr()
|
||||
gateway = hostAddr.Next()
|
||||
first = gateway.Next().Next()
|
||||
last = add(hostAddr, 1<<uint64(hostAddr.BitLen()-options.IPNet.Bits())-1)
|
||||
last = nnip.UnMasked(*options.IPNet)
|
||||
)
|
||||
|
||||
if !options.IPNet.IsValid() || !first.Less(last) || !options.IPNet.Contains(last) {
|
||||
if !options.IPNet.IsValid() || !first.IsValid() || !first.Less(last) {
|
||||
return nil, errors.New("ipnet don't have valid ip")
|
||||
}
|
||||
|
||||
@ -201,29 +195,3 @@ func New(options Options) (*Pool, error) {
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// add returns addr + n.
|
||||
func add(addr netip.Addr, n uint64) netip.Addr {
|
||||
buf := addr.As16()
|
||||
|
||||
u := uint128{
|
||||
binary.BigEndian.Uint64(buf[:8]),
|
||||
binary.BigEndian.Uint64(buf[8:]),
|
||||
}
|
||||
|
||||
lo, carry := bits.Add64(u.lo, n, 0)
|
||||
|
||||
u.hi = u.hi + carry
|
||||
u.lo = lo
|
||||
|
||||
binary.BigEndian.PutUint64(buf[:8], u.hi)
|
||||
binary.BigEndian.PutUint64(buf[8:], u.lo)
|
||||
|
||||
a := netip.AddrFrom16(buf)
|
||||
|
||||
if addr.Is4() {
|
||||
return a.Unmap()
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
@ -240,13 +240,15 @@ func TestPool_FlushFileCache(t *testing.T) {
|
||||
err = pool.FlushFakeIP()
|
||||
assert.Nil(t, err)
|
||||
|
||||
baz := pool.Lookup("foo.com")
|
||||
next := pool.Lookup("baz.com")
|
||||
baz := pool.Lookup("foo.com")
|
||||
nero := pool.Lookup("foo.com")
|
||||
|
||||
assert.True(t, foo == fox)
|
||||
assert.True(t, foo == next)
|
||||
assert.False(t, foo == baz)
|
||||
assert.True(t, bar == bax)
|
||||
assert.True(t, bar == baz)
|
||||
assert.False(t, bar == next)
|
||||
assert.True(t, baz == nero)
|
||||
}
|
||||
@ -267,13 +269,15 @@ func TestPool_FlushMemoryCache(t *testing.T) {
|
||||
err := pool.FlushFakeIP()
|
||||
assert.Nil(t, err)
|
||||
|
||||
baz := pool.Lookup("foo.com")
|
||||
next := pool.Lookup("baz.com")
|
||||
baz := pool.Lookup("foo.com")
|
||||
nero := pool.Lookup("foo.com")
|
||||
|
||||
assert.True(t, foo == fox)
|
||||
assert.True(t, foo == next)
|
||||
assert.False(t, foo == baz)
|
||||
assert.True(t, bar == bax)
|
||||
assert.True(t, bar == baz)
|
||||
assert.False(t, bar == next)
|
||||
assert.True(t, baz == nero)
|
||||
}
|
||||
|
Reference in New Issue
Block a user