Chore: make fake ip pool start with the third ip
This commit is contained in:
parent
9ef90782ce
commit
598ec35701
4 changed files with 32 additions and 28 deletions
|
@ -147,10 +147,10 @@ type Options struct {
|
||||||
|
|
||||||
// New return Pool instance
|
// New return Pool instance
|
||||||
func New(options Options) (*Pool, error) {
|
func New(options Options) (*Pool, error) {
|
||||||
min := ipToUint(options.IPNet.IP) + 2
|
min := ipToUint(options.IPNet.IP) + 3
|
||||||
|
|
||||||
ones, bits := options.IPNet.Mask.Size()
|
ones, bits := options.IPNet.Mask.Size()
|
||||||
total := 1<<uint(bits-ones) - 3
|
total := 1<<uint(bits-ones) - 4
|
||||||
|
|
||||||
if total <= 0 {
|
if total <= 0 {
|
||||||
return nil, errors.New("ipnet don't have valid ip")
|
return nil, errors.New("ipnet don't have valid ip")
|
||||||
|
@ -160,7 +160,7 @@ func New(options Options) (*Pool, error) {
|
||||||
pool := &Pool{
|
pool := &Pool{
|
||||||
min: min,
|
min: min,
|
||||||
max: max,
|
max: max,
|
||||||
gateway: min - 1,
|
gateway: min - 2,
|
||||||
broadcast: max + 1,
|
broadcast: max + 1,
|
||||||
host: options.Host,
|
host: options.Host,
|
||||||
ipnet: options.IPNet,
|
ipnet: options.IPNet,
|
||||||
|
|
|
@ -49,7 +49,7 @@ func createCachefileStore(options Options) (*Pool, string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPool_Basic(t *testing.T) {
|
func TestPool_Basic(t *testing.T) {
|
||||||
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
|
_, ipnet, _ := net.ParseCIDR("192.168.0.0/28")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
|
@ -62,21 +62,22 @@ func TestPool_Basic(t *testing.T) {
|
||||||
last := pool.Lookup("bar.com")
|
last := pool.Lookup("bar.com")
|
||||||
bar, exist := pool.LookBack(last)
|
bar, exist := pool.LookBack(last)
|
||||||
|
|
||||||
assert.True(t, first.Equal(net.IP{192, 168, 0, 2}))
|
assert.True(t, first.Equal(net.IP{192, 168, 0, 3}))
|
||||||
assert.Equal(t, pool.Lookup("foo.com"), net.IP{192, 168, 0, 2})
|
assert.Equal(t, pool.Lookup("foo.com"), net.IP{192, 168, 0, 3})
|
||||||
assert.True(t, last.Equal(net.IP{192, 168, 0, 3}))
|
assert.True(t, last.Equal(net.IP{192, 168, 0, 4}))
|
||||||
assert.True(t, exist)
|
assert.True(t, exist)
|
||||||
assert.Equal(t, bar, "bar.com")
|
assert.Equal(t, bar, "bar.com")
|
||||||
assert.Equal(t, pool.Gateway(), net.IP{192, 168, 0, 1})
|
assert.Equal(t, pool.Gateway(), net.IP{192, 168, 0, 1})
|
||||||
|
assert.Equal(t, pool.Broadcast(), net.IP{192, 168, 0, 15})
|
||||||
assert.Equal(t, pool.IPNet().String(), ipnet.String())
|
assert.Equal(t, pool.IPNet().String(), ipnet.String())
|
||||||
assert.True(t, pool.Exist(net.IP{192, 168, 0, 3}))
|
assert.True(t, pool.Exist(net.IP{192, 168, 0, 4}))
|
||||||
assert.False(t, pool.Exist(net.IP{192, 168, 0, 4}))
|
assert.False(t, pool.Exist(net.IP{192, 168, 0, 5}))
|
||||||
assert.False(t, pool.Exist(net.ParseIP("::1")))
|
assert.False(t, pool.Exist(net.ParseIP("::1")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPool_CycleUsed(t *testing.T) {
|
func TestPool_CycleUsed(t *testing.T) {
|
||||||
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
|
_, ipnet, _ := net.ParseCIDR("192.168.0.16/28")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
|
@ -87,7 +88,7 @@ func TestPool_CycleUsed(t *testing.T) {
|
||||||
for _, pool := range pools {
|
for _, pool := range pools {
|
||||||
foo := pool.Lookup("foo.com")
|
foo := pool.Lookup("foo.com")
|
||||||
bar := pool.Lookup("bar.com")
|
bar := pool.Lookup("bar.com")
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 9; i++ {
|
||||||
pool.Lookup(fmt.Sprintf("%d.com", i))
|
pool.Lookup(fmt.Sprintf("%d.com", i))
|
||||||
}
|
}
|
||||||
baz := pool.Lookup("baz.com")
|
baz := pool.Lookup("baz.com")
|
||||||
|
@ -98,7 +99,7 @@ func TestPool_CycleUsed(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPool_Skip(t *testing.T) {
|
func TestPool_Skip(t *testing.T) {
|
||||||
_, ipnet, _ := net.ParseCIDR("192.168.0.1/30")
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
|
||||||
tree := trie.New()
|
tree := trie.New()
|
||||||
tree.Insert("example.com", tree)
|
tree.Insert("example.com", tree)
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
|
@ -169,8 +170,8 @@ func TestPool_Clone(t *testing.T) {
|
||||||
|
|
||||||
first := pool.Lookup("foo.com")
|
first := pool.Lookup("foo.com")
|
||||||
last := pool.Lookup("bar.com")
|
last := pool.Lookup("bar.com")
|
||||||
assert.True(t, first.Equal(net.IP{192, 168, 0, 2}))
|
assert.True(t, first.Equal(net.IP{192, 168, 0, 3}))
|
||||||
assert.True(t, last.Equal(net.IP{192, 168, 0, 3}))
|
assert.True(t, last.Equal(net.IP{192, 168, 0, 4}))
|
||||||
|
|
||||||
newPool, _ := New(Options{
|
newPool, _ := New(Options{
|
||||||
IPNet: ipnet,
|
IPNet: ipnet,
|
||||||
|
|
|
@ -20,7 +20,7 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int,
|
||||||
var (
|
var (
|
||||||
interfaceName = dev.Name()
|
interfaceName = dev.Name()
|
||||||
ip = addr.Masked().Addr().Next()
|
ip = addr.Masked().Addr().Next()
|
||||||
gw = ip
|
gw = ip.Next()
|
||||||
netmask = IPv4MaskString(addr.Bits())
|
netmask = IPv4MaskString(addr.Bits())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,10 +31,10 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// _, err = cmd.ExecCmd(fmt.Sprintf("ipconfig set %s automatic-v6", interfaceName))
|
_, err = cmd.ExecCmd(fmt.Sprintf("ipconfig set %s automatic-v6", interfaceName))
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
|
|
||||||
if autoRoute {
|
if autoRoute {
|
||||||
err = configInterfaceRouting(interfaceName, addr)
|
err = configInterfaceRouting(interfaceName, addr)
|
||||||
|
@ -43,15 +43,18 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int,
|
||||||
}
|
}
|
||||||
|
|
||||||
func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
||||||
routes := append(Routes, addr.String())
|
var (
|
||||||
|
routes = append(Routes, addr.String())
|
||||||
|
gateway = addr.Masked().Addr().Next()
|
||||||
|
)
|
||||||
|
|
||||||
for _, route := range routes {
|
for _, destination := range routes {
|
||||||
if err := execRouterCmd("add", "-inet", route, interfaceName); err != nil {
|
if _, err := cmd.ExecCmd(fmt.Sprintf("route add -net %s %s", destination, gateway)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// return execRouterCmd("add", "-inet6", "2000::/3", interfaceName)
|
return execRouterCmd("add", "-inet6", "2000::/3", interfaceName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ import (
|
||||||
// New TunAdapter
|
// New TunAdapter
|
||||||
func New(tunConf *config.Tun, tunAddressPrefix string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) {
|
func New(tunConf *config.Tun, tunAddressPrefix string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) {
|
||||||
var (
|
var (
|
||||||
tunAddress = netip.MustParsePrefix(tunAddressPrefix)
|
tunAddress, _ = netip.ParsePrefix(tunAddressPrefix)
|
||||||
devName = tunConf.Device
|
devName = tunConf.Device
|
||||||
stackType = tunConf.Stack
|
stackType = tunConf.Stack
|
||||||
autoRoute = tunConf.AutoRoute
|
autoRoute = tunConf.AutoRoute
|
||||||
|
|
Loading…
Reference in a new issue