Skip to content

Commit 345d3d7

Browse files
committed
chore: add inbound test for anytls
1 parent 3d806b5 commit 345d3d7

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

listener/anytls/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func New(config LC.AnyTLSServer, tunnel C.Tunnel, additions ...inbound.Addition)
8686
if err != nil {
8787
return nil, err
8888
}
89+
if len(tlsConfig.Certificates) > 0 {
90+
l = tls.NewListener(l, tlsConfig)
91+
} else {
92+
return nil, errors.New("disallow using AnyTLS without certificates config")
93+
}
8994
sl.listeners = append(sl.listeners, l)
9095

9196
go func() {
@@ -130,8 +135,6 @@ func (l *Listener) AddrList() (addrList []net.Addr) {
130135

131136
func (l *Listener) HandleConn(conn net.Conn, h *sing.ListenerHandler) {
132137
ctx := context.TODO()
133-
134-
conn = tls.Server(conn, l.tlsConfig)
135138
defer conn.Close()
136139

137140
b := buf.NewPacket()

listener/inbound/anytls_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package inbound_test
2+
3+
import (
4+
"net/netip"
5+
"testing"
6+
7+
"github.com/metacubex/mihomo/adapter/outbound"
8+
"github.com/metacubex/mihomo/listener/inbound"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func testInboundAnyTLS(t *testing.T, inboundOptions inbound.AnyTLSOption, outboundOptions outbound.AnyTLSOption) {
14+
inboundOptions.BaseOption = inbound.BaseOption{
15+
NameStr: "anytls_inbound",
16+
Listen: "127.0.0.1",
17+
Port: "0",
18+
}
19+
inboundOptions.Users = map[string]string{"test": userUUID}
20+
in, err := inbound.NewAnyTLS(&inboundOptions)
21+
assert.NoError(t, err)
22+
23+
tunnel := NewHttpTestTunnel()
24+
defer tunnel.Close()
25+
26+
err = in.Listen(tunnel)
27+
assert.NoError(t, err)
28+
defer in.Close()
29+
30+
addrPort, err := netip.ParseAddrPort(in.Address())
31+
assert.NoError(t, err)
32+
33+
outboundOptions.Name = "anytls_outbound"
34+
outboundOptions.Server = addrPort.Addr().String()
35+
outboundOptions.Port = int(addrPort.Port())
36+
outboundOptions.Password = userUUID
37+
38+
out, err := outbound.NewAnyTLS(outboundOptions)
39+
assert.NoError(t, err)
40+
defer out.Close()
41+
42+
tunnel.DoTest(t, out)
43+
}
44+
45+
func TestInboundAnyTLS_TLS(t *testing.T) {
46+
inboundOptions := inbound.AnyTLSOption{
47+
Certificate: tlsCertificate,
48+
PrivateKey: tlsPrivateKey,
49+
}
50+
outboundOptions := outbound.AnyTLSOption{
51+
Fingerprint: tlsFingerprint,
52+
}
53+
testInboundAnyTLS(t, inboundOptions, outboundOptions)
54+
}

listener/inbound/common_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
N "github.com/metacubex/mihomo/common/net"
18+
"github.com/metacubex/mihomo/common/utils"
1819
"github.com/metacubex/mihomo/component/ca"
1920
"github.com/metacubex/mihomo/component/generater"
2021
C "github.com/metacubex/mihomo/constant"
@@ -27,6 +28,7 @@ import (
2728
var httpPath = "/inbound_test"
2829
var httpData = make([]byte, 10240)
2930
var remoteAddr = netip.MustParseAddr("1.2.3.4")
31+
var userUUID = utils.NewUUIDV4().String()
3032
var tlsCertificate, tlsPrivateKey, tlsFingerprint, _ = N.NewRandomTLSKeyPair()
3133
var tlsConfigCert, _ = tls.X509KeyPair([]byte(tlsCertificate), []byte(tlsPrivateKey))
3234
var tlsConfig = &tls.Config{Certificates: []tls.Certificate{tlsConfigCert}, NextProtos: []string{"h2", "http/1.1"}}

listener/inbound/trojan_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import (
66
"testing"
77

88
"github.com/metacubex/mihomo/adapter/outbound"
9-
"github.com/metacubex/mihomo/common/utils"
109
"github.com/metacubex/mihomo/listener/inbound"
1110
"github.com/stretchr/testify/assert"
1211
)
1312

1413
func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outboundOptions outbound.TrojanOption) {
15-
userUUID := utils.NewUUIDV4().String()
1614
inboundOptions.BaseOption = inbound.BaseOption{
1715
NameStr: "trojan_inbound",
1816
Listen: "127.0.0.1",
@@ -46,7 +44,7 @@ func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outbou
4644
tunnel.DoTest(t, out)
4745
}
4846

49-
func TestInboundTrojan_Tls(t *testing.T) {
47+
func TestInboundTrojan_TLS(t *testing.T) {
5048
inboundOptions := inbound.TrojanOption{
5149
Certificate: tlsCertificate,
5250
PrivateKey: tlsPrivateKey,
@@ -162,7 +160,7 @@ func TestInboundTrojan_Reality_Grpc(t *testing.T) {
162160
testInboundTrojan(t, inboundOptions, outboundOptions)
163161
}
164162

165-
func TestInboundTrojan_Tls_TrojanSS(t *testing.T) {
163+
func TestInboundTrojan_TLS_TrojanSS(t *testing.T) {
166164
inboundOptions := inbound.TrojanOption{
167165
Certificate: tlsCertificate,
168166
PrivateKey: tlsPrivateKey,

listener/inbound/vless_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import (
66
"testing"
77

88
"github.com/metacubex/mihomo/adapter/outbound"
9-
"github.com/metacubex/mihomo/common/utils"
109
"github.com/metacubex/mihomo/listener/inbound"
1110
"github.com/stretchr/testify/assert"
1211
)
1312

1413
func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outboundOptions outbound.VlessOption) {
15-
userUUID := utils.NewUUIDV4().String()
1614
inboundOptions.BaseOption = inbound.BaseOption{
1715
NameStr: "vless_inbound",
1816
Listen: "127.0.0.1",
@@ -46,7 +44,7 @@ func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outbound
4644
tunnel.DoTest(t, out)
4745
}
4846

49-
func TestInboundVless_Tls(t *testing.T) {
47+
func TestInboundVless_TLS(t *testing.T) {
5048
inboundOptions := inbound.VlessOption{
5149
Certificate: tlsCertificate,
5250
PrivateKey: tlsPrivateKey,

listener/inbound/vmess_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import (
66
"testing"
77

88
"github.com/metacubex/mihomo/adapter/outbound"
9-
"github.com/metacubex/mihomo/common/utils"
109
"github.com/metacubex/mihomo/listener/inbound"
1110
"github.com/stretchr/testify/assert"
1211
)
1312

1413
func testInboundVMess(t *testing.T, inboundOptions inbound.VmessOption, outboundOptions outbound.VmessOption) {
15-
userUUID := utils.NewUUIDV4().String()
1614
inboundOptions.BaseOption = inbound.BaseOption{
1715
NameStr: "vmess_inbound",
1816
Listen: "127.0.0.1",
@@ -54,7 +52,7 @@ func TestInboundVMess_Basic(t *testing.T) {
5452
testInboundVMess(t, inboundOptions, outboundOptions)
5553
}
5654

57-
func TestInboundVMess_Tls(t *testing.T) {
55+
func TestInboundVMess_TLS(t *testing.T) {
5856
inboundOptions := inbound.VmessOption{
5957
Certificate: tlsCertificate,
6058
PrivateKey: tlsPrivateKey,

0 commit comments

Comments
 (0)