mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-13 13:38:06 +08:00
chore: add inbound test for anytls
This commit is contained in:
parent
3d806b5e4c
commit
345d3d7052
@ -86,6 +86,11 @@ func New(config LC.AnyTLSServer, tunnel C.Tunnel, additions ...inbound.Addition)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if len(tlsConfig.Certificates) > 0 {
|
||||||
|
l = tls.NewListener(l, tlsConfig)
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("disallow using AnyTLS without certificates config")
|
||||||
|
}
|
||||||
sl.listeners = append(sl.listeners, l)
|
sl.listeners = append(sl.listeners, l)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -130,8 +135,6 @@ func (l *Listener) AddrList() (addrList []net.Addr) {
|
|||||||
|
|
||||||
func (l *Listener) HandleConn(conn net.Conn, h *sing.ListenerHandler) {
|
func (l *Listener) HandleConn(conn net.Conn, h *sing.ListenerHandler) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
conn = tls.Server(conn, l.tlsConfig)
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
b := buf.NewPacket()
|
b := buf.NewPacket()
|
||||||
|
54
listener/inbound/anytls_test.go
Normal file
54
listener/inbound/anytls_test.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package inbound_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
|
"github.com/metacubex/mihomo/listener/inbound"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testInboundAnyTLS(t *testing.T, inboundOptions inbound.AnyTLSOption, outboundOptions outbound.AnyTLSOption) {
|
||||||
|
inboundOptions.BaseOption = inbound.BaseOption{
|
||||||
|
NameStr: "anytls_inbound",
|
||||||
|
Listen: "127.0.0.1",
|
||||||
|
Port: "0",
|
||||||
|
}
|
||||||
|
inboundOptions.Users = map[string]string{"test": userUUID}
|
||||||
|
in, err := inbound.NewAnyTLS(&inboundOptions)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
tunnel := NewHttpTestTunnel()
|
||||||
|
defer tunnel.Close()
|
||||||
|
|
||||||
|
err = in.Listen(tunnel)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer in.Close()
|
||||||
|
|
||||||
|
addrPort, err := netip.ParseAddrPort(in.Address())
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
outboundOptions.Name = "anytls_outbound"
|
||||||
|
outboundOptions.Server = addrPort.Addr().String()
|
||||||
|
outboundOptions.Port = int(addrPort.Port())
|
||||||
|
outboundOptions.Password = userUUID
|
||||||
|
|
||||||
|
out, err := outbound.NewAnyTLS(outboundOptions)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
tunnel.DoTest(t, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInboundAnyTLS_TLS(t *testing.T) {
|
||||||
|
inboundOptions := inbound.AnyTLSOption{
|
||||||
|
Certificate: tlsCertificate,
|
||||||
|
PrivateKey: tlsPrivateKey,
|
||||||
|
}
|
||||||
|
outboundOptions := outbound.AnyTLSOption{
|
||||||
|
Fingerprint: tlsFingerprint,
|
||||||
|
}
|
||||||
|
testInboundAnyTLS(t, inboundOptions, outboundOptions)
|
||||||
|
}
|
@ -15,6 +15,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/generater"
|
"github.com/metacubex/mihomo/component/generater"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
@ -27,6 +28,7 @@ import (
|
|||||||
var httpPath = "/inbound_test"
|
var httpPath = "/inbound_test"
|
||||||
var httpData = make([]byte, 10240)
|
var httpData = make([]byte, 10240)
|
||||||
var remoteAddr = netip.MustParseAddr("1.2.3.4")
|
var remoteAddr = netip.MustParseAddr("1.2.3.4")
|
||||||
|
var userUUID = utils.NewUUIDV4().String()
|
||||||
var tlsCertificate, tlsPrivateKey, tlsFingerprint, _ = N.NewRandomTLSKeyPair()
|
var tlsCertificate, tlsPrivateKey, tlsFingerprint, _ = N.NewRandomTLSKeyPair()
|
||||||
var tlsConfigCert, _ = tls.X509KeyPair([]byte(tlsCertificate), []byte(tlsPrivateKey))
|
var tlsConfigCert, _ = tls.X509KeyPair([]byte(tlsCertificate), []byte(tlsPrivateKey))
|
||||||
var tlsConfig = &tls.Config{Certificates: []tls.Certificate{tlsConfigCert}, NextProtos: []string{"h2", "http/1.1"}}
|
var tlsConfig = &tls.Config{Certificates: []tls.Certificate{tlsConfigCert}, NextProtos: []string{"h2", "http/1.1"}}
|
||||||
|
@ -6,13 +6,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
|
||||||
"github.com/metacubex/mihomo/listener/inbound"
|
"github.com/metacubex/mihomo/listener/inbound"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outboundOptions outbound.TrojanOption) {
|
func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outboundOptions outbound.TrojanOption) {
|
||||||
userUUID := utils.NewUUIDV4().String()
|
|
||||||
inboundOptions.BaseOption = inbound.BaseOption{
|
inboundOptions.BaseOption = inbound.BaseOption{
|
||||||
NameStr: "trojan_inbound",
|
NameStr: "trojan_inbound",
|
||||||
Listen: "127.0.0.1",
|
Listen: "127.0.0.1",
|
||||||
@ -46,7 +44,7 @@ func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outbou
|
|||||||
tunnel.DoTest(t, out)
|
tunnel.DoTest(t, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInboundTrojan_Tls(t *testing.T) {
|
func TestInboundTrojan_TLS(t *testing.T) {
|
||||||
inboundOptions := inbound.TrojanOption{
|
inboundOptions := inbound.TrojanOption{
|
||||||
Certificate: tlsCertificate,
|
Certificate: tlsCertificate,
|
||||||
PrivateKey: tlsPrivateKey,
|
PrivateKey: tlsPrivateKey,
|
||||||
@ -162,7 +160,7 @@ func TestInboundTrojan_Reality_Grpc(t *testing.T) {
|
|||||||
testInboundTrojan(t, inboundOptions, outboundOptions)
|
testInboundTrojan(t, inboundOptions, outboundOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInboundTrojan_Tls_TrojanSS(t *testing.T) {
|
func TestInboundTrojan_TLS_TrojanSS(t *testing.T) {
|
||||||
inboundOptions := inbound.TrojanOption{
|
inboundOptions := inbound.TrojanOption{
|
||||||
Certificate: tlsCertificate,
|
Certificate: tlsCertificate,
|
||||||
PrivateKey: tlsPrivateKey,
|
PrivateKey: tlsPrivateKey,
|
||||||
|
@ -6,13 +6,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
|
||||||
"github.com/metacubex/mihomo/listener/inbound"
|
"github.com/metacubex/mihomo/listener/inbound"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outboundOptions outbound.VlessOption) {
|
func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outboundOptions outbound.VlessOption) {
|
||||||
userUUID := utils.NewUUIDV4().String()
|
|
||||||
inboundOptions.BaseOption = inbound.BaseOption{
|
inboundOptions.BaseOption = inbound.BaseOption{
|
||||||
NameStr: "vless_inbound",
|
NameStr: "vless_inbound",
|
||||||
Listen: "127.0.0.1",
|
Listen: "127.0.0.1",
|
||||||
@ -46,7 +44,7 @@ func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outbound
|
|||||||
tunnel.DoTest(t, out)
|
tunnel.DoTest(t, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInboundVless_Tls(t *testing.T) {
|
func TestInboundVless_TLS(t *testing.T) {
|
||||||
inboundOptions := inbound.VlessOption{
|
inboundOptions := inbound.VlessOption{
|
||||||
Certificate: tlsCertificate,
|
Certificate: tlsCertificate,
|
||||||
PrivateKey: tlsPrivateKey,
|
PrivateKey: tlsPrivateKey,
|
||||||
|
@ -6,13 +6,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
|
||||||
"github.com/metacubex/mihomo/listener/inbound"
|
"github.com/metacubex/mihomo/listener/inbound"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testInboundVMess(t *testing.T, inboundOptions inbound.VmessOption, outboundOptions outbound.VmessOption) {
|
func testInboundVMess(t *testing.T, inboundOptions inbound.VmessOption, outboundOptions outbound.VmessOption) {
|
||||||
userUUID := utils.NewUUIDV4().String()
|
|
||||||
inboundOptions.BaseOption = inbound.BaseOption{
|
inboundOptions.BaseOption = inbound.BaseOption{
|
||||||
NameStr: "vmess_inbound",
|
NameStr: "vmess_inbound",
|
||||||
Listen: "127.0.0.1",
|
Listen: "127.0.0.1",
|
||||||
@ -54,7 +52,7 @@ func TestInboundVMess_Basic(t *testing.T) {
|
|||||||
testInboundVMess(t, inboundOptions, outboundOptions)
|
testInboundVMess(t, inboundOptions, outboundOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInboundVMess_Tls(t *testing.T) {
|
func TestInboundVMess_TLS(t *testing.T) {
|
||||||
inboundOptions := inbound.VmessOption{
|
inboundOptions := inbound.VmessOption{
|
||||||
Certificate: tlsCertificate,
|
Certificate: tlsCertificate,
|
||||||
PrivateKey: tlsPrivateKey,
|
PrivateKey: tlsPrivateKey,
|
||||||
|
Loading…
Reference in New Issue
Block a user