修订代码

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)
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)

View File

@@ -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

View File

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

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

View File

@@ -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 {

View File

@@ -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))
}
/*

View File

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