diff --git a/cli.go b/cli.go index aa98534..b706380 100644 --- a/cli.go +++ b/cli.go @@ -663,10 +663,12 @@ func interactively_hotRemoveServerOrClient() { will_delete_index = int(theInt) if will_delete_dial { + allClients[will_delete_index].Stop() allClients = utils.TrimSlice(allClients, will_delete_index) } if will_delete_listen { listenerArray[will_delete_index].Close() + allServers[will_delete_index].Stop() allServers = utils.TrimSlice(allServers, will_delete_index) listenerArray = utils.TrimSlice(listenerArray, will_delete_index) diff --git a/netLayer/listen.go b/netLayer/listen.go index 154b083..4c56f9a 100644 --- a/netLayer/listen.go +++ b/netLayer/listen.go @@ -42,6 +42,12 @@ func loopAccept(listener net.Listener, acceptFunc func(net.Conn)) { // // 非阻塞,在自己的goroutine中监听. func ListenAndAccept(network, addr string, acceptFunc func(net.Conn)) (listener net.Listener, err error) { + if addr == "" || acceptFunc == nil { + return nil, utils.ErrNilParameter + } + if network == "" { + network = "tcp" + } switch network { case "udp", "udp4", "udp6": var ua *net.UDPAddr @@ -67,7 +73,6 @@ func ListenAndAccept(network, addr string, acceptFunc func(net.Conn)) (listener if utils.FileExist(addr) { if ce := utils.CanLogDebug("unix file exist"); ce != nil { - //log.Println("unix file exist, deleting", addr) ce.Write(zap.String("deleting", addr)) } err = os.Remove(addr) @@ -79,9 +84,7 @@ func ListenAndAccept(network, addr string, acceptFunc func(net.Conn)) (listener } fallthrough default: - if network == "" { - network = "tcp" - } + listener, err = net.Listen(network, addr) if err != nil { return diff --git a/netLayer/netLayer_test.go b/netLayer/netLayer_test.go index f33ee50..2d360a4 100644 --- a/netLayer/netLayer_test.go +++ b/netLayer/netLayer_test.go @@ -1,7 +1,28 @@ package netLayer -import "testing" +import ( + "net" + "testing" + "time" + + "github.com/hahahrfool/v2ray_simple/utils" +) func TestIpv6(t *testing.T) { t.Log("HasIpv6Interface()", HasIpv6Interface()) } + +func TestUDP(t *testing.T) { + //测试setdeadline的情况. 实测证明 SetReadDeadline 在Read过程中也可以使用, 这样就可以防止阻塞 + + laddr, _ := net.ResolveUDPAddr("udp", ":"+RandPortStr()) + + udpConn, _ := net.ListenUDP("udp", laddr) + + go func() { + time.Sleep(time.Second) + udpConn.SetReadDeadline(time.Now()) + }() + udpConn.ReadFrom(utils.GetPacket()) + t.Log("ok") +} diff --git a/netLayer/udp_test.go b/netLayer/udp_test.go deleted file mode 100644 index 31f9ef1..0000000 --- a/netLayer/udp_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package netLayer - -import ( - "net" - "testing" - "time" - - "github.com/hahahrfool/v2ray_simple/utils" -) - -func TestUDP(t *testing.T) { - //测试setdeadline的情况. 实测证明 SetReadDeadline 在Read过程中也可以使用, 这样就可以防止阻塞 - - laddr, _ := net.ResolveUDPAddr("udp", ":"+RandPortStr()) - - udpConn, _ := net.ListenUDP("udp", laddr) - - go func() { - time.Sleep(time.Second) - udpConn.SetReadDeadline(time.Now()) - }() - udpConn.ReadFrom(utils.GetPacket()) - t.Log("ok") -} diff --git a/proxy/proxy.go b/proxy/proxy.go index e93c695..8dcfc19 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net" + "strings" "github.com/hahahrfool/v2ray_simple/grpc" "github.com/hahahrfool/v2ray_simple/netLayer" @@ -58,7 +59,8 @@ type Server interface { } // FullName 可以完整表示 一个 代理的 VSI 层级. -// 这里认为, tcp/udp/kcp/raw_socket 是FirstName,具体的协议名称是 LastName, 中间层是 MiddleName +// 这里认为, tcp/udp/kcp/raw_socket 是FirstName,具体的协议名称是 LastName, 中间层是 MiddleName。 +// // An Example of a full name: tcp+tls+ws+vless func GetFullName(pc ProxyCommon) string { return pc.Network() + pc.MiddleName() + pc.Name() @@ -135,14 +137,14 @@ type ProxyCommon interface { initGRPC_server() error - IsMux() bool //如果用了grpc则此方法返回true + IsMux() bool //如果用了grpc或者quic, 则此方法返回true GetQuic_Client() *quic.Client //for outClient setQuic_Client(*quic.Client) setListenCommonConnFunc(func() (newConnChan chan net.Conn, baseConn any)) - /////////////////// 私有方法 /////////////////// + /////////////////// 其它私有方法 /////////////////// setCantRoute(bool) setTag(string) @@ -214,14 +216,18 @@ func (pcs *ProxyCommonStruct) setFallback(a netLayer.Addr) { } func (pcs *ProxyCommonStruct) MiddleName() string { - str := "" + var sb strings.Builder + sb.WriteString("") + if pcs.TLS { - str += "+tls" + sb.WriteString("+tls") } if pcs.AdvancedL != "" { - str += "+" + pcs.AdvancedL + sb.WriteString("+") + sb.WriteString(pcs.AdvancedL) } - return str + "+" + sb.WriteString("+") + return sb.String() } func (pcs *ProxyCommonStruct) CantRoute() bool { @@ -235,12 +241,12 @@ func (pcs *ProxyCommonStruct) GetTag() string { func (pcs *ProxyCommonStruct) setTag(tag string) { pcs.Tag = tag } -func (pcs *ProxyCommonStruct) setNetwork(net string) { - if net == "" { +func (pcs *ProxyCommonStruct) setNetwork(network string) { + if network == "" { pcs.network = "tcp" } else { - pcs.network = net + pcs.network = network } } diff --git a/utils/algo.go b/utils/algo.go index 152144d..150f047 100644 --- a/utils/algo.go +++ b/utils/algo.go @@ -45,15 +45,13 @@ func AllSubSets_improve1[T comparable](set []T) (subsets [][]T) { return subsets } -func CopySlice[T any](a []T) (r []T) { +func DuplicateSlice[T any](a []T) (r []T) { r = make([]T, len(a)) - for i, v := range a { - r[i] = v - } + copy(r, a) return } -//会直接改动原slice数据 +// TrimSlice 从一个slice中移除一个元素, 会直接改动原slice数据 func TrimSlice[T any](a []T, deleteIndex int) []T { j := 0 for idx, val := range a { diff --git a/utils/algo_test.go b/utils/algo_test.go index 0770ad3..4aebb12 100644 --- a/utils/algo_test.go +++ b/utils/algo_test.go @@ -12,14 +12,14 @@ var x = []string{"AA", "BB", "CC", "DD"} var y = []int{1, 2, 3, 4} func TestSplice(t *testing.T) { - t.Log(utils.TrimSlice(utils.CopySlice(x), 0)) - t.Log(utils.TrimSlice(utils.CopySlice(x), 1)) - t.Log(utils.TrimSlice(utils.CopySlice(x), 2)) - t.Log(utils.TrimSlice(utils.CopySlice(x), 3)) - t.Log(utils.TrimSlice(utils.CopySlice(y), 0)) - t.Log(utils.TrimSlice(utils.CopySlice(y), 1)) - t.Log(utils.TrimSlice(utils.CopySlice(y), 2)) - t.Log(utils.TrimSlice(utils.CopySlice(y), 3)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 0)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 1)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 2)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 3)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 0)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 1)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 2)) + t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 3)) } /* diff --git a/utils/utils.go b/utils/utils.go index 567d2d8..a83a5db 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -17,6 +17,7 @@ func init() { rand.Seed(time.Now().Unix()) } +// bufio.Reader 和 bytes.Buffer 都实现了 ByteReader type ByteReader interface { ReadByte() (byte, error) Read(p []byte) (n int, err error)