This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory_test.go
More file actions
134 lines (123 loc) · 4.07 KB
/
factory_test.go
File metadata and controls
134 lines (123 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package singproxy
import (
"context"
"crypto/tls"
"io"
"net"
"net/http"
"net/url"
"testing"
"time"
"github.com/sagernet/sing-box/option"
)
func TestFromURL(t *testing.T) {
realityPublicKey := "zpbDgfQxvlM2vbx3M1yM4fNC525q_g8yHiTPikDqjhs"
realityShortID := "a1b2c3d4e5f6a7b8"
wgPrivateKey := "gCrpA4g8MvjGn85nslmf8Uv25soA9j+R5f6vOa3a41E="
wgPublicKey := "w9q0T7aiJ27v39yO85yD5jY3kQ1Oa2u5b8a/cDef3gY="
testCases := []struct {
name string
url string
shouldErr bool
validate func(t *testing.T, p Proxy)
}{
{
name: "Schemaless HTTP",
url: "user:pass@schemaless.example.com:8080",
validate: func(t *testing.T, p Proxy) {
opts := p.(*SingBoxProxy).options.(*option.HTTPOutboundOptions)
if opts.Server != "schemaless.example.com" || opts.ServerPort != 8080 || opts.Username != "user" || opts.Password != "pass" {
t.Errorf("Schemaless HTTP parsing failed. Got %+v", opts)
}
},
},
{
name: "VLESS with Reality",
url: "vless://a-vless-uuid@reality.example.com:443?security=reality&sni=sni.example.com&fp=chrome&pbk=" + realityPublicKey + "&sid=" + realityShortID + "&type=tcp#VLESS-Reality",
validate: func(t *testing.T, p Proxy) {
opts := p.(*SingBoxProxy).options.(*option.VLESSOutboundOptions)
if opts.Server != "reality.example.com" || opts.UUID != "a-vless-uuid" || !opts.TLS.Enabled || !opts.TLS.Reality.Enabled || opts.TLS.Reality.PublicKey != realityPublicKey || opts.TLS.ServerName != "sni.example.com" {
t.Errorf("VLESS+Reality parsing failed. Got %+v", opts)
}
},
},
{
name: "WireGuard",
url: "wireguard://" + url.PathEscape(wgPrivateKey) + "@wg.example.com:51820?publickey=" + url.PathEscape(wgPublicKey) + "&address=192.168.1.1/32&address=fd00::1/128",
validate: func(t *testing.T, p Proxy) {
opts := p.(*SingBoxProxy).options.(*option.LegacyWireGuardOutboundOptions)
expectedAddrs := []string{"192.168.1.1/32", "fd00::1/128"}
if len(opts.LocalAddress) != len(expectedAddrs) {
t.Fatalf("WireGuard parsing failed. Expected %d addresses, got %d", len(expectedAddrs), len(opts.LocalAddress))
}
for i := range opts.LocalAddress {
if opts.LocalAddress[i].String() != expectedAddrs[i] {
t.Errorf("WireGuard address mismatch. Got %s, expected %s", opts.LocalAddress[i].String(), expectedAddrs[i])
}
}
},
},
{
name: "Invalid Scheme",
url: "invalid-scheme://whatever",
shouldErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
proxy, err := FromURL(time.Second*8, tc.url)
if tc.shouldErr {
if err == nil {
t.Errorf("Expected an error, but got none")
}
return
}
if err != nil {
t.Fatalf("Did not expect an error, but got: %v", err)
}
if proxy == nil {
t.Fatal("Expected a proxy instance, but got nil")
}
if tc.validate != nil {
tc.validate(t, proxy)
}
})
}
}
func TestDirectConnection(t *testing.T) {
if testing.Short() {
t.Skip("Skipping live network test in short mode")
}
proxy, err := FromURL(time.Second*8, "direct")
if err != nil {
t.Fatalf("Failed to create direct proxy: %v", err)
}
client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
tcpAddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
return proxy.DialContext(ctx, network, tcpAddr)
},
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
req, err := http.NewRequestWithContext(context.Background(), "GET", "https://httpbun.com/get", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("User-Agent", "singproxy-Test-Client")
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
t.Errorf("Expected status code 200, but got %d. Body: %s", resp.StatusCode, string(bodyBytes))
}
t.Log("Successfully connected to httpbin.org through direct proxy.")
}