修订代码

This commit is contained in:
hahahrfool
2022-04-10 08:52:40 +08:00
parent 651bd12e72
commit 50de20ce12
8 changed files with 59 additions and 52 deletions

2
cli.go
View File

@@ -663,10 +663,12 @@ func interactively_hotRemoveServerOrClient() {
will_delete_index = int(theInt) will_delete_index = int(theInt)
if will_delete_dial { if will_delete_dial {
allClients[will_delete_index].Stop()
allClients = utils.TrimSlice(allClients, will_delete_index) allClients = utils.TrimSlice(allClients, will_delete_index)
} }
if will_delete_listen { if will_delete_listen {
listenerArray[will_delete_index].Close() listenerArray[will_delete_index].Close()
allServers[will_delete_index].Stop()
allServers = utils.TrimSlice(allServers, will_delete_index) allServers = utils.TrimSlice(allServers, will_delete_index)
listenerArray = utils.TrimSlice(listenerArray, will_delete_index) listenerArray = utils.TrimSlice(listenerArray, will_delete_index)

View File

@@ -42,6 +42,12 @@ func loopAccept(listener net.Listener, acceptFunc func(net.Conn)) {
// //
// 非阻塞在自己的goroutine中监听. // 非阻塞在自己的goroutine中监听.
func ListenAndAccept(network, addr string, acceptFunc func(net.Conn)) (listener net.Listener, err error) { 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 { switch network {
case "udp", "udp4", "udp6": case "udp", "udp4", "udp6":
var ua *net.UDPAddr var ua *net.UDPAddr
@@ -67,7 +73,6 @@ func ListenAndAccept(network, addr string, acceptFunc func(net.Conn)) (listener
if utils.FileExist(addr) { if utils.FileExist(addr) {
if ce := utils.CanLogDebug("unix file exist"); ce != nil { if ce := utils.CanLogDebug("unix file exist"); ce != nil {
//log.Println("unix file exist, deleting", addr)
ce.Write(zap.String("deleting", addr)) ce.Write(zap.String("deleting", addr))
} }
err = os.Remove(addr) err = os.Remove(addr)
@@ -79,9 +84,7 @@ func ListenAndAccept(network, addr string, acceptFunc func(net.Conn)) (listener
} }
fallthrough fallthrough
default: default:
if network == "" {
network = "tcp"
}
listener, err = net.Listen(network, addr) listener, err = net.Listen(network, addr)
if err != nil { if err != nil {
return return

View File

@@ -1,7 +1,28 @@
package netLayer package netLayer
import "testing" import (
"net"
"testing"
"time"
"github.com/hahahrfool/v2ray_simple/utils"
)
func TestIpv6(t *testing.T) { func TestIpv6(t *testing.T) {
t.Log("HasIpv6Interface()", HasIpv6Interface()) 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")
}

View File

@@ -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")
}

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"strings"
"github.com/hahahrfool/v2ray_simple/grpc" "github.com/hahahrfool/v2ray_simple/grpc"
"github.com/hahahrfool/v2ray_simple/netLayer" "github.com/hahahrfool/v2ray_simple/netLayer"
@@ -58,7 +59,8 @@ type Server interface {
} }
// FullName 可以完整表示 一个 代理的 VSI 层级. // 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 // An Example of a full name: tcp+tls+ws+vless
func GetFullName(pc ProxyCommon) string { func GetFullName(pc ProxyCommon) string {
return pc.Network() + pc.MiddleName() + pc.Name() return pc.Network() + pc.MiddleName() + pc.Name()
@@ -135,14 +137,14 @@ type ProxyCommon interface {
initGRPC_server() error initGRPC_server() error
IsMux() bool //如果用了grpc则此方法返回true IsMux() bool //如果用了grpc或者quic, 则此方法返回true
GetQuic_Client() *quic.Client //for outClient GetQuic_Client() *quic.Client //for outClient
setQuic_Client(*quic.Client) setQuic_Client(*quic.Client)
setListenCommonConnFunc(func() (newConnChan chan net.Conn, baseConn any)) setListenCommonConnFunc(func() (newConnChan chan net.Conn, baseConn any))
/////////////////// 私有方法 /////////////////// /////////////////// 其它私有方法 ///////////////////
setCantRoute(bool) setCantRoute(bool)
setTag(string) setTag(string)
@@ -214,14 +216,18 @@ func (pcs *ProxyCommonStruct) setFallback(a netLayer.Addr) {
} }
func (pcs *ProxyCommonStruct) MiddleName() string { func (pcs *ProxyCommonStruct) MiddleName() string {
str := "" var sb strings.Builder
sb.WriteString("")
if pcs.TLS { if pcs.TLS {
str += "+tls" sb.WriteString("+tls")
} }
if pcs.AdvancedL != "" { if pcs.AdvancedL != "" {
str += "+" + pcs.AdvancedL sb.WriteString("+")
sb.WriteString(pcs.AdvancedL)
} }
return str + "+" sb.WriteString("+")
return sb.String()
} }
func (pcs *ProxyCommonStruct) CantRoute() bool { func (pcs *ProxyCommonStruct) CantRoute() bool {
@@ -235,12 +241,12 @@ func (pcs *ProxyCommonStruct) GetTag() string {
func (pcs *ProxyCommonStruct) setTag(tag string) { func (pcs *ProxyCommonStruct) setTag(tag string) {
pcs.Tag = tag pcs.Tag = tag
} }
func (pcs *ProxyCommonStruct) setNetwork(net string) { func (pcs *ProxyCommonStruct) setNetwork(network string) {
if net == "" { if network == "" {
pcs.network = "tcp" pcs.network = "tcp"
} else { } else {
pcs.network = net pcs.network = network
} }
} }

View File

@@ -45,15 +45,13 @@ func AllSubSets_improve1[T comparable](set []T) (subsets [][]T) {
return subsets return subsets
} }
func CopySlice[T any](a []T) (r []T) { func DuplicateSlice[T any](a []T) (r []T) {
r = make([]T, len(a)) r = make([]T, len(a))
for i, v := range a { copy(r, a)
r[i] = v
}
return return
} }
//会直接改动原slice数据 // TrimSlice 从一个slice中移除一个元素, 会直接改动原slice数据
func TrimSlice[T any](a []T, deleteIndex int) []T { func TrimSlice[T any](a []T, deleteIndex int) []T {
j := 0 j := 0
for idx, val := range a { for idx, val := range a {

View File

@@ -12,14 +12,14 @@ var x = []string{"AA", "BB", "CC", "DD"}
var y = []int{1, 2, 3, 4} var y = []int{1, 2, 3, 4}
func TestSplice(t *testing.T) { func TestSplice(t *testing.T) {
t.Log(utils.TrimSlice(utils.CopySlice(x), 0)) t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 0))
t.Log(utils.TrimSlice(utils.CopySlice(x), 1)) t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 1))
t.Log(utils.TrimSlice(utils.CopySlice(x), 2)) t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 2))
t.Log(utils.TrimSlice(utils.CopySlice(x), 3)) t.Log(utils.TrimSlice(utils.DuplicateSlice(x), 3))
t.Log(utils.TrimSlice(utils.CopySlice(y), 0)) t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 0))
t.Log(utils.TrimSlice(utils.CopySlice(y), 1)) t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 1))
t.Log(utils.TrimSlice(utils.CopySlice(y), 2)) t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 2))
t.Log(utils.TrimSlice(utils.CopySlice(y), 3)) t.Log(utils.TrimSlice(utils.DuplicateSlice(y), 3))
} }
/* /*

View File

@@ -17,6 +17,7 @@ func init() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
} }
// bufio.Reader 和 bytes.Buffer 都实现了 ByteReader
type ByteReader interface { type ByteReader interface {
ReadByte() (byte, error) ReadByte() (byte, error)
Read(p []byte) (n int, err error) Read(p []byte) (n int, err error)