sing-box/test/hysteria2_test.go

209 lines
5.6 KiB
Go
Raw Normal View History

2023-08-31 20:07:32 +08:00
package main
import (
"net/netip"
"testing"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
2023-09-16 21:36:06 +08:00
"github.com/sagernet/sing-quic/hysteria2"
2024-11-07 21:44:04 +08:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/json/badoption"
2023-08-31 20:07:32 +08:00
)
func TestHysteria2Self(t *testing.T) {
t.Run("self", func(t *testing.T) {
testHysteria2Self(t, "")
})
t.Run("self-salamander", func(t *testing.T) {
testHysteria2Self(t, "password")
})
}
func testHysteria2Self(t *testing.T, salamanderPassword string) {
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
var obfs *option.Hysteria2Obfs
if salamanderPassword != "" {
obfs = &option.Hysteria2Obfs{
Type: hysteria2.ObfsTypeSalamander,
Password: salamanderPassword,
}
}
startInstance(t, option.Options{
2024-11-07 21:44:04 +08:00
LegacyInbounds: []option.LegacyInbound{
2023-08-31 20:07:32 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2024-11-07 21:44:04 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2023-08-31 20:07:32 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeHysteria2,
Hysteria2Options: option.Hysteria2InboundOptions{
ListenOptions: option.ListenOptions{
2024-11-07 21:44:04 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2023-08-31 20:07:32 +08:00
ListenPort: serverPort,
},
2023-09-17 00:25:53 +08:00
UpMbps: 100,
DownMbps: 100,
Obfs: obfs,
2023-08-31 20:07:32 +08:00
Users: []option.Hysteria2User{{
Password: "password",
}},
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2023-08-31 20:07:32 +08:00
},
},
},
},
2024-11-02 00:39:02 +08:00
LegacyOutbounds: []option.LegacyOutbound{
2023-08-31 20:07:32 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeHysteria2,
Tag: "hy2-out",
Hysteria2Options: option.Hysteria2OutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
2023-09-17 00:25:53 +08:00
UpMbps: 100,
DownMbps: 100,
2023-08-31 20:07:32 +08:00
Obfs: obfs,
Password: "password",
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2023-08-31 20:07:32 +08:00
},
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
Type: C.RuleTypeDefault,
DefaultOptions: option.DefaultRule{
2024-10-21 23:38:34 +08:00
RawDefaultRule: option.RawDefaultRule{
Inbound: []string{"mixed-in"},
},
RuleAction: option.RuleAction{
Action: C.RuleActionTypeRoute,
RouteOptions: option.RouteActionOptions{
Outbound: "hy2-out",
},
},
2023-08-31 20:07:32 +08:00
},
},
},
},
})
2023-10-25 12:00:00 +08:00
testSuitLargeUDP(t, clientPort, testPort)
2023-08-31 20:07:32 +08:00
}
func TestHysteria2Inbound(t *testing.T) {
caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
startInstance(t, option.Options{
2024-11-07 21:44:04 +08:00
LegacyInbounds: []option.LegacyInbound{
2023-08-31 20:07:32 +08:00
{
Type: C.TypeHysteria2,
Hysteria2Options: option.Hysteria2InboundOptions{
ListenOptions: option.ListenOptions{
2024-11-07 21:44:04 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2023-08-31 20:07:32 +08:00
ListenPort: serverPort,
},
Obfs: &option.Hysteria2Obfs{
Type: hysteria2.ObfsTypeSalamander,
Password: "cry_me_a_r1ver",
},
Users: []option.Hysteria2User{{
Password: "password",
}},
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2023-08-31 20:07:32 +08:00
},
},
},
},
})
startDockerContainer(t, DockerOptions{
Image: ImageHysteria2,
Ports: []uint16{serverPort, clientPort},
Cmd: []string{"client", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
Bind: map[string]string{
"hysteria2-client.yml": "/etc/hysteria/config.yml",
caPem: "/etc/hysteria/ca.pem",
},
})
testSuit(t, clientPort, testPort)
}
func TestHysteria2Outbound(t *testing.T) {
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
startDockerContainer(t, DockerOptions{
Image: ImageHysteria2,
Ports: []uint16{testPort},
Cmd: []string{"server", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
Bind: map[string]string{
"hysteria2-server.yml": "/etc/hysteria/config.yml",
certPem: "/etc/hysteria/cert.pem",
keyPem: "/etc/hysteria/key.pem",
},
})
startInstance(t, option.Options{
2024-11-07 21:44:04 +08:00
LegacyInbounds: []option.LegacyInbound{
2023-08-31 20:07:32 +08:00
{
Type: C.TypeMixed,
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2024-11-07 21:44:04 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2023-08-31 20:07:32 +08:00
ListenPort: clientPort,
},
},
},
},
2024-11-02 00:39:02 +08:00
LegacyOutbounds: []option.LegacyOutbound{
2023-08-31 20:07:32 +08:00
{
Type: C.TypeHysteria2,
Hysteria2Options: option.Hysteria2OutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Obfs: &option.Hysteria2Obfs{
Type: hysteria2.ObfsTypeSalamander,
Password: "cry_me_a_r1ver",
},
Password: "password",
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2023-08-31 20:07:32 +08:00
},
},
},
},
})
testSuitSimple1(t, clientPort, testPort)
}