Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package websocket_test
 
import (
    "context"
    "runtime"
    "testing"
    "time"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/common/protocol/tls/cert"
    "github.com/v2fly/v2ray-core/v5/transport/internet"
    "github.com/v2fly/v2ray-core/v5/transport/internet/tls"
    . "github.com/v2fly/v2ray-core/v5/transport/internet/websocket"
)
 
func Test_listenWSAndDial(t *testing.T) {
    listen, err := ListenWS(context.Background(), net.LocalHostIP, 13146, &internet.MemoryStreamConfig{
        ProtocolName: "websocket",
        ProtocolSettings: &Config{
            Path: "ws",
        },
    }, func(conn internet.Connection) {
        go func(c internet.Connection) {
            defer c.Close()
 
            var b [1024]byte
            _, err := c.Read(b[:])
            if err != nil {
                return
            }
 
            common.Must2(c.Write([]byte("Response")))
        }(conn)
    })
    common.Must(err)
 
    ctx := context.Background()
    streamSettings := &internet.MemoryStreamConfig{
        ProtocolName:     "websocket",
        ProtocolSettings: &Config{Path: "ws"},
    }
    conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146), streamSettings)
 
    common.Must(err)
    _, err = conn.Write([]byte("Test connection 1"))
    common.Must(err)
 
    var b [1024]byte
    n, err := conn.Read(b[:])
    common.Must(err)
    if string(b[:n]) != "Response" {
        t.Error("response: ", string(b[:n]))
    }
 
    common.Must(conn.Close())
    <-time.After(time.Second * 5)
    conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146), streamSettings)
    common.Must(err)
    _, err = conn.Write([]byte("Test connection 2"))
    common.Must(err)
    n, err = conn.Read(b[:])
    common.Must(err)
    if string(b[:n]) != "Response" {
        t.Error("response: ", string(b[:n]))
    }
    common.Must(conn.Close())
 
    common.Must(listen.Close())
}
 
func TestDialWithRemoteAddr(t *testing.T) {
    listen, err := ListenWS(context.Background(), net.LocalHostIP, 13148, &internet.MemoryStreamConfig{
        ProtocolName: "websocket",
        ProtocolSettings: &Config{
            Path: "ws",
        },
    }, func(conn internet.Connection) {
        go func(c internet.Connection) {
            defer c.Close()
 
            var b [1024]byte
            _, err := c.Read(b[:])
            // common.Must(err)
            if err != nil {
                return
            }
 
            _, err = c.Write([]byte("Response"))
            common.Must(err)
        }(conn)
    })
    common.Must(err)
 
    conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), 13148), &internet.MemoryStreamConfig{
        ProtocolName:     "websocket",
        ProtocolSettings: &Config{Path: "ws", Header: []*Header{{Key: "X-Forwarded-For", Value: "1.1.1.1"}}},
    })
 
    common.Must(err)
    _, err = conn.Write([]byte("Test connection 1"))
    common.Must(err)
 
    var b [1024]byte
    n, err := conn.Read(b[:])
    common.Must(err)
    if string(b[:n]) != "Response" {
        t.Error("response: ", string(b[:n]))
    }
 
    common.Must(listen.Close())
}
 
func Test_listenWSAndDial_TLS(t *testing.T) {
    if runtime.GOARCH == "arm64" {
        return
    }
 
    start := time.Now()
 
    streamSettings := &internet.MemoryStreamConfig{
        ProtocolName: "websocket",
        ProtocolSettings: &Config{
            Path: "wss",
        },
        SecurityType: "tls",
        SecuritySettings: &tls.Config{
            AllowInsecure: true,
            Certificate:   []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
        },
    }
    listen, err := ListenWS(context.Background(), net.LocalHostIP, 13143, streamSettings, func(conn internet.Connection) {
        go func() {
            _ = conn.Close()
        }()
    })
    common.Must(err)
    defer listen.Close()
 
    conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), 13143), streamSettings)
    common.Must(err)
    _ = conn.Close()
 
    end := time.Now()
    if !end.Before(start.Add(time.Second * 5)) {
        t.Error("end: ", end, " start: ", start)
    }
}