修订代码,示例;修复trojan的mux在传递udp时的bug; 添加内层mux的udp的测试

This commit is contained in:
hahahrfool
2022-04-15 11:35:15 +08:00
parent ab46349c8b
commit d1f2696c1d
5 changed files with 38 additions and 23 deletions

View File

@@ -11,5 +11,5 @@ host = "your-domain-name.com"
port = 4434
insecure = true
utls = true
use_mux = true
use_mux = true # 只需要客户端指明使用mux即可, 服务端自动适配.

27
main.go
View File

@@ -254,7 +254,7 @@ func main() {
}
}
//没配置可用的listen或者dail而且还无法动态更改配置
//没配置可用的listen或者dial而且还无法动态更改配置
if !configFileQualifiedToRun && !isFlexible() {
utils.Fatal("No valid proxy settings available, exit now.")
return
@@ -1031,8 +1031,12 @@ func dialClient(targetAddr netLayer.Addr,
isudp := targetAddr.IsUDP()
hasInnerMux := false
//先过滤掉 innermux 通道已经建立的情况, 此时我们不必再次外部拨号,而是直接进行内层拨号.
if muxInt, innerProxyName := client.HasInnerMux(); muxInt == 2 {
hasInnerMux = true
if client.InnerMuxEstablished() {
wrc1, realudp_wrc, result1 := dialInnerMux(client, nil, innerProxyName, targetAddr, isudp)
@@ -1098,7 +1102,7 @@ func dialClient(targetAddr netLayer.Addr,
var clientConn net.Conn
var grpcClientConn grpc.ClientConn
var dailedCommonConn any
var dialedCommonConn any
dialHere := !(client.Name() == "direct" && isudp)
@@ -1121,12 +1125,12 @@ func dialClient(targetAddr netLayer.Addr,
}
case "quic":
//quic这里并不是在tls层基础上进行dial而是直接从传输层dial 或者获取之前已经存在的session
dailedCommonConn = client.GetQuic_Client().DialCommonConn(false, nil)
if dailedCommonConn != nil {
dialedCommonConn = client.GetQuic_Client().DialCommonConn(false, nil)
if dialedCommonConn != nil {
//跳过tls阶段
goto advLayerStep
} else {
//dail失败, 直接return掉
//dial失败, 直接return掉
result = -1
return
@@ -1210,7 +1214,7 @@ advLayerStep:
switch adv {
case "quic":
qclient := client.GetQuic_Client()
clientConn, err = qclient.DialSubConn(dailedCommonConn)
clientConn, err = qclient.DialSubConn(dialedCommonConn)
if err != nil {
eStr := err.Error()
if strings.Contains(eStr, "too many") {
@@ -1222,14 +1226,14 @@ advLayerStep:
}
//第一条连接已满再开一条session
dailedCommonConn = qclient.DialCommonConn(true, dailedCommonConn)
if dailedCommonConn == nil {
dialedCommonConn = qclient.DialCommonConn(true, dialedCommonConn)
if dialedCommonConn == nil {
//再dial还是nil也许是暂时性的网络错误, 先退出
result = -1
return
}
clientConn, err = qclient.DialSubConn(dailedCommonConn)
clientConn, err = qclient.DialSubConn(dialedCommonConn)
if err != nil {
if ce := utils.CanLogErr("DialSubConn failed after redialed new session"); ce != nil {
ce.Write(
@@ -1343,7 +1347,9 @@ advLayerStep:
////////////////////////////// 代理层 握手阶段 /////////////////////////////////////
if !isudp {
if !isudp || hasInnerMux {
//如果是udp但是有innermux, 依然用handshake
wrc, err = client.Handshake(clientConn, targetAddr)
if err != nil {
if ce := utils.CanLogErr("Handshake client failed"); ce != nil {
@@ -1356,6 +1362,7 @@ advLayerStep:
}
} else {
udp_wrc, err = client.EstablishUDPChannel(clientConn, targetAddr)
if err != nil {
if ce := utils.CanLogErr("EstablishUDPChannel failed"); ce != nil {

View File

@@ -119,7 +119,7 @@ func (s *Server) Handshake(underlay net.Conn) (newconn io.ReadWriteCloser, _ net
if isCONNECT {
underlay.Write(connectReturnBytes) //这个也是https代理的特征所以不适合 公网使用
//正常来说我们的服务器要先dialdial成功之后再返回200但是因为我们目前的架构是在main函数里dail
//正常来说我们的服务器要先dialdial成功之后再返回200但是因为我们目前的架构是在main函数里dial
// 所以就直接写入了.
//另外nginx是没有实现 CONNECT的不过有插件

View File

@@ -138,7 +138,7 @@ protocol = "direct"
}
func tryGetHttp(client *http.Client, path string, t *testing.T) {
t.Log("start dail", path)
t.Log("start dial", path)
resp, err := client.Get(path)
if err != nil {
t.Log("get http failed", err)

View File

@@ -50,41 +50,45 @@ func TestDNSLookup_CN(t *testing.T) {
*/
func TestUDP_vless(t *testing.T) {
testUDP("vless", 0, "tcp", false, false, t)
testUDP("vless", 0, "tcp", false, false, false, t)
}
//v0 没有fullcone
func TestUDP_vless_v1(t *testing.T) {
testUDP("vless", 1, "tcp", false, false, t)
testUDP("vless", 1, "tcp", false, false, false, t)
}
func TestUDP_vless_v1_fullcone(t *testing.T) {
testUDP("vless", 1, "tcp", false, true, t)
testUDP("vless", 1, "tcp", false, true, false, t)
}
func TestUDP_vless_v1_udpMulti(t *testing.T) {
testUDP("vless", 1, "tcp", true, false, t)
testUDP("vless", 1, "tcp", true, false, false, t)
}
func TestUDP_vless_v1_udpMulti_fullcone(t *testing.T) {
testUDP("vless", 1, "tcp", true, true, t)
testUDP("vless", 1, "tcp", true, true, false, t)
}
func TestUDP_trojan(t *testing.T) {
testUDP("trojan", 0, "tcp", false, false, t)
testUDP("trojan", 0, "tcp", false, false, false, t)
}
func TestUDP_trojan_mux(t *testing.T) {
testUDP("trojan", 0, "tcp", false, false, true, t)
}
func TestUDP_trojan_fullcone(t *testing.T) {
testUDP("trojan", 0, "tcp", false, true, t)
testUDP("trojan", 0, "tcp", false, true, false, t)
}
func TestUDP_trojan_through_udp(t *testing.T) {
testUDP("trojan", 0, "udp", false, false, t)
testUDP("trojan", 0, "udp", false, false, false, t)
}
//udp测试我们直接使用dns请求来测试.
func testUDP(protocol string, version int, network string, multi bool, fullcone bool, t *testing.T) {
func testUDP(protocol string, version int, network string, multi bool, fullcone bool, mux bool, t *testing.T) {
utils.LogLevel = utils.Log_debug
utils.InitLog()
@@ -120,8 +124,12 @@ version = %d
insecure = true
network = "%s"
`
if mux {
testClientConfFormatStr += "\nuse_mux = true\n"
}
if multi {
testClientConfFormatStr += "\nextra = { vless1_udp_multi = true }"
testClientConfFormatStr += "\nextra = { vless1_udp_multi = true }\n"
}
clientListenPort := netLayer.RandPortStr()