Skip to content

Commit 7218a24

Browse files
authored
Proxy (#20)
* 新加连proxy服务功能 * dial proxy
1 parent 51bf907 commit 7218a24

33 files changed

+356
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*swp
2+
/.idea
23
/coverage.out
34
/cover.cov
45
autobahn-testsuite

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ quickws是一个高性能的websocket库
2222
* [配置握手时的超时时间](#配置握手时的超时时间)
2323
* [配置自动回复ping消息](#配置自动回复ping消息)
2424
* [配置socks5代理](#配置socks5代理)
25+
* [配置proxy代理](#配置proxy代理)
2526
* [服务配置参数](#服务端配置)
2627
* [配置服务自动回复ping消息](#配置服务自动回复ping消息)
2728
## 注意⚠️
@@ -210,6 +211,21 @@ func main() {
210211
}))
211212
}
212213
```
214+
#### 配置proxy代理
215+
```go
216+
import(
217+
"github.com/antlabs/quickws"
218+
)
219+
220+
func main() {
221+
222+
proxy := func(*http.Request) (*url.URL, error) {
223+
return url.Parse("http://127.0.0.1:1007")
224+
}
225+
226+
quickws.Dial("ws://127.0.0.1:12345", quickws.WithClientProxyFunc(proxy))
227+
}
228+
```
213229
### 服务端配置参数
214230
#### 配置服务自动回复ping消息
215231
```go

autobahn/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
all:
22
# mac, arm64
33
GOOS=darwin GOARCH=arm64 go build -o autobahn-server-darwin-arm64 ./autobahn-server.go
4-
# mac, arm64
5-
GOOS=darwin GOARCH=arm64 go build -tags=goexperiment.arenas -o autobahn-server-darwin-arm64-arena ./autobahn-server.go
64
# linux amd64
75
GOOS=linux GOARCH=amd64 go build -o autobahn-server-linux-amd64 ./autobahn-server.go
86

benchmark_rand_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023 antlabs. All rights reserved.
1+
// Copyright 2021-2024 antlabs. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

benchmark_read_write_message_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023 antlabs. All rights reserved.
1+
// Copyright 2021-2024 antlabs. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

callback.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023 antlabs. All rights reserved.
1+
// Copyright 2021-2024 antlabs. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

callback_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023 antlabs. All rights reserved.
1+
// Copyright 2021-2024 antlabs. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

client.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023 antlabs. All rights reserved.
1+
// Copyright 2021-2024 antlabs. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import (
2828
"github.com/antlabs/wsutil/bytespool"
2929
"github.com/antlabs/wsutil/enum"
3030
"github.com/antlabs/wsutil/fixedreader"
31+
"github.com/antlabs/wsutil/hostname"
3132
)
3233

3334
var (
@@ -46,7 +47,9 @@ type DialOption struct {
4647

4748
func ClientOptionToConf(opts ...ClientOption) *DialOption {
4849
var dial DialOption
49-
dial.defaultSetting()
50+
if err := dial.defaultSetting(); err != nil {
51+
panic(err.Error())
52+
}
5053
for _, o := range opts {
5154
o(&dial)
5255
}
@@ -82,7 +85,10 @@ func Dial(rawUrl string, opts ...ClientOption) (*Conn, error) {
8285
dial.Header = make(http.Header)
8386
}
8487

85-
dial.defaultSetting()
88+
if err := dial.defaultSetting(); err != nil {
89+
return nil, err
90+
}
91+
8692
for _, o := range opts {
8793
o(&dial)
8894
}
@@ -122,6 +128,10 @@ func (d *DialOption) handshake() (*http.Request, string, error) {
122128
d.Header.Add("Sec-WebSocket-Extensions", strExtensions)
123129
}
124130

131+
if len(d.subProtocols) > 0 {
132+
d.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.subProtocols, ", ")}
133+
}
134+
125135
req.Header = d.Header
126136
return req, secWebSocket, nil
127137
}
@@ -178,23 +188,36 @@ func (d *DialOption) tlsConn(c net.Conn) net.Conn {
178188
}
179189

180190
func (d *DialOption) Dial() (c *Conn, err error) {
191+
// scheme ws -> http
192+
// scheme wss -> https
181193
req, secWebSocket, err := d.handshake()
182194
if err != nil {
183195
return nil, err
184196
}
185197

186198
var conn net.Conn
187199
begin := time.Now()
200+
201+
hostName := hostname.GetHostName(d.u)
188202
// conn, err := net.DialTimeout("tcp", d.u.Host /* TODO 加端号*/, d.dialTimeout)
189-
if d.dialFunc == nil {
190-
conn, err = net.Dial("tcp", d.u.Host /* TODO 加端号*/)
191-
} else {
203+
dialFunc := net.Dial
204+
if d.dialFunc != nil {
192205
dialInterface, err := d.dialFunc()
193206
if err != nil {
194207
return nil, err
195208
}
196-
conn, err = dialInterface.Dial("tcp", d.u.Host)
209+
dialFunc = dialInterface.Dial
197210
}
211+
212+
if d.proxyFunc != nil {
213+
proxyURL, err := d.proxyFunc(req)
214+
if err != nil {
215+
return nil, err
216+
}
217+
dialFunc = newhttpProxy(proxyURL, dialFunc).Dial
218+
}
219+
220+
conn, err = dialFunc("tcp", hostName)
198221
if err != nil {
199222
return nil, err
200223
}

client_option_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023 antlabs. All rights reserved.
1+
// Copyright 2021-2024 antlabs. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

client_options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023 antlabs. All rights reserved.
1+
// Copyright 2021-2024 antlabs. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)