mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-10 19:20:15 +08:00
add new modules & remove old
This commit is contained in:
4
common/dns/fakedns/cache.go → common/cache/cache.go
vendored
Normal file → Executable file
4
common/dns/fakedns/cache.go → common/cache/cache.go
vendored
Normal file → Executable file
@@ -1,4 +1,4 @@
|
|||||||
package fakedns
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -93,7 +93,7 @@ func stopJanitor(c *Cache) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New return *Cache
|
// New return *Cache
|
||||||
func NewCache(interval time.Duration) *Cache {
|
func New(interval time.Duration) *Cache {
|
||||||
j := &janitor{
|
j := &janitor{
|
||||||
interval: interval,
|
interval: interval,
|
||||||
stop: make(chan struct{}),
|
stop: make(chan struct{}),
|
70
common/cache/cache_test.go
vendored
Executable file
70
common/cache/cache_test.go
vendored
Executable file
@@ -0,0 +1,70 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCache_Basic(t *testing.T) {
|
||||||
|
interval := 200 * time.Millisecond
|
||||||
|
ttl := 20 * time.Millisecond
|
||||||
|
c := New(interval)
|
||||||
|
c.Put("int", 1, ttl)
|
||||||
|
c.Put("string", "a", ttl)
|
||||||
|
|
||||||
|
i := c.Get("int")
|
||||||
|
if i.(int) != 1 {
|
||||||
|
t.Error("should recv 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
s := c.Get("string")
|
||||||
|
if s.(string) != "a" {
|
||||||
|
t.Error("should recv 'a'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_TTL(t *testing.T) {
|
||||||
|
interval := 200 * time.Millisecond
|
||||||
|
ttl := 20 * time.Millisecond
|
||||||
|
c := New(interval)
|
||||||
|
c.Put("int", 1, ttl)
|
||||||
|
|
||||||
|
i := c.Get("int")
|
||||||
|
if i.(int) != 1 {
|
||||||
|
t.Error("should recv 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(ttl * 2)
|
||||||
|
i = c.Get("int")
|
||||||
|
if i != nil {
|
||||||
|
t.Error("should recv nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_AutoCleanup(t *testing.T) {
|
||||||
|
interval := 10 * time.Millisecond
|
||||||
|
ttl := 15 * time.Millisecond
|
||||||
|
c := New(interval)
|
||||||
|
c.Put("int", 1, ttl)
|
||||||
|
|
||||||
|
time.Sleep(ttl * 2)
|
||||||
|
i := c.Get("int")
|
||||||
|
if i != nil {
|
||||||
|
t.Error("should recv nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache_AutoGC(t *testing.T) {
|
||||||
|
sign := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
interval := 10 * time.Millisecond
|
||||||
|
ttl := 15 * time.Millisecond
|
||||||
|
c := New(interval)
|
||||||
|
c.Put("int", 1, ttl)
|
||||||
|
sign <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
<-sign
|
||||||
|
runtime.GC()
|
||||||
|
}
|
83
common/domain-trie/trie_test.go
Executable file
83
common/domain-trie/trie_test.go
Executable file
@@ -0,0 +1,83 @@
|
|||||||
|
package trie
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var localIP = net.IP{127, 0, 0, 1}
|
||||||
|
|
||||||
|
func TestTrie_Basic(t *testing.T) {
|
||||||
|
tree := New()
|
||||||
|
domains := []string{
|
||||||
|
"example.com",
|
||||||
|
"google.com",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, domain := range domains {
|
||||||
|
tree.Insert(domain, localIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
node := tree.Search("example.com")
|
||||||
|
if node == nil {
|
||||||
|
t.Error("should not recv nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !node.Data.(net.IP).Equal(localIP) {
|
||||||
|
t.Error("should equal 127.0.0.1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrie_Wildcard(t *testing.T) {
|
||||||
|
tree := New()
|
||||||
|
domains := []string{
|
||||||
|
"*.example.com",
|
||||||
|
"sub.*.example.com",
|
||||||
|
"*.dev",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, domain := range domains {
|
||||||
|
tree.Insert(domain, localIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Search("sub.example.com") == nil {
|
||||||
|
t.Error("should not recv nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Search("sub.foo.example.com") == nil {
|
||||||
|
t.Error("should not recv nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Search("foo.sub.example.com") != nil {
|
||||||
|
t.Error("should recv nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Search("foo.example.dev") != nil {
|
||||||
|
t.Error("should recv nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Search("example.com") != nil {
|
||||||
|
t.Error("should recv nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrie_Boundary(t *testing.T) {
|
||||||
|
tree := New()
|
||||||
|
tree.Insert("*.dev", localIP)
|
||||||
|
|
||||||
|
if err := tree.Insert(".", localIP); err == nil {
|
||||||
|
t.Error("should recv err")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tree.Insert(".com", localIP); err == nil {
|
||||||
|
t.Error("should recv err")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Search("dev") != nil {
|
||||||
|
t.Error("should recv nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Search(".dev") != nil {
|
||||||
|
t.Error("should recv nil")
|
||||||
|
}
|
||||||
|
}
|
4
common/dns/fakedns/pool.go → common/fakeip/pool.go
Normal file → Executable file
4
common/dns/fakedns/pool.go → common/fakeip/pool.go
Normal file → Executable file
@@ -1,4 +1,4 @@
|
|||||||
package fakedns
|
package fakeip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -36,7 +36,7 @@ func uintToIP(v uint32) net.IP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New return Pool instance
|
// New return Pool instance
|
||||||
func NewPool(ipnet *net.IPNet) (*Pool, error) {
|
func New(ipnet *net.IPNet) (*Pool, error) {
|
||||||
min := ipToUint(ipnet.IP) + 1
|
min := ipToUint(ipnet.IP) + 1
|
||||||
|
|
||||||
ones, bits := ipnet.Mask.Size()
|
ones, bits := ipnet.Mask.Size()
|
44
common/fakeip/pool_test.go
Executable file
44
common/fakeip/pool_test.go
Executable file
@@ -0,0 +1,44 @@
|
|||||||
|
package fakeip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPool_Basic(t *testing.T) {
|
||||||
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/30")
|
||||||
|
pool, _ := New(ipnet)
|
||||||
|
|
||||||
|
first := pool.Get()
|
||||||
|
last := pool.Get()
|
||||||
|
|
||||||
|
if !first.Equal(net.IP{192, 168, 0, 1}) {
|
||||||
|
t.Error("should get right first ip, instead of", first.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !last.Equal(net.IP{192, 168, 0, 2}) {
|
||||||
|
t.Error("should get right last ip, instead of", first.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPool_Cycle(t *testing.T) {
|
||||||
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/30")
|
||||||
|
pool, _ := New(ipnet)
|
||||||
|
|
||||||
|
first := pool.Get()
|
||||||
|
pool.Get()
|
||||||
|
same := pool.Get()
|
||||||
|
|
||||||
|
if !first.Equal(same) {
|
||||||
|
t.Error("should return same ip", first.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPool_Error(t *testing.T) {
|
||||||
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/31")
|
||||||
|
_, err := New(ipnet)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Error("should return err")
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user