Skip to content

Commit

Permalink
Proxy (#20)
Browse files Browse the repository at this point in the history
* 新加连proxy服务功能

* dial proxy
  • Loading branch information
guonaihong authored Dec 15, 2023
1 parent 51bf907 commit 7218a24
Show file tree
Hide file tree
Showing 33 changed files with 356 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*swp
/.idea
/coverage.out
/cover.cov
autobahn-testsuite
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ quickws是一个高性能的websocket库
* [配置握手时的超时时间](#配置握手时的超时时间)
* [配置自动回复ping消息](#配置自动回复ping消息)
* [配置socks5代理](#配置socks5代理)
* [配置proxy代理](#配置proxy代理)
* [服务配置参数](#服务端配置)
* [配置服务自动回复ping消息](#配置服务自动回复ping消息)
## 注意⚠️
Expand Down Expand Up @@ -210,6 +211,21 @@ func main() {
}))
}
```
#### 配置proxy代理
```go
import(
"github.com/antlabs/quickws"
)

func main() {

proxy := func(*http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:1007")
}

quickws.Dial("ws://127.0.0.1:12345", quickws.WithClientProxyFunc(proxy))
}
```
### 服务端配置参数
#### 配置服务自动回复ping消息
```go
Expand Down
2 changes: 0 additions & 2 deletions autobahn/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
all:
# mac, arm64
GOOS=darwin GOARCH=arm64 go build -o autobahn-server-darwin-arm64 ./autobahn-server.go
# mac, arm64
GOOS=darwin GOARCH=arm64 go build -tags=goexperiment.arenas -o autobahn-server-darwin-arm64-arena ./autobahn-server.go
# linux amd64
GOOS=linux GOARCH=amd64 go build -o autobahn-server-linux-amd64 ./autobahn-server.go

Expand Down
2 changes: 1 addition & 1 deletion benchmark_rand_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion benchmark_read_write_message_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion callback.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion callback_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
37 changes: 30 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@ import (
"github.com/antlabs/wsutil/bytespool"
"github.com/antlabs/wsutil/enum"
"github.com/antlabs/wsutil/fixedreader"
"github.com/antlabs/wsutil/hostname"
)

var (
Expand All @@ -46,7 +47,9 @@ type DialOption struct {

func ClientOptionToConf(opts ...ClientOption) *DialOption {
var dial DialOption
dial.defaultSetting()
if err := dial.defaultSetting(); err != nil {
panic(err.Error())
}
for _, o := range opts {
o(&dial)
}
Expand Down Expand Up @@ -82,7 +85,10 @@ func Dial(rawUrl string, opts ...ClientOption) (*Conn, error) {
dial.Header = make(http.Header)
}

dial.defaultSetting()
if err := dial.defaultSetting(); err != nil {
return nil, err
}

for _, o := range opts {
o(&dial)
}
Expand Down Expand Up @@ -122,6 +128,10 @@ func (d *DialOption) handshake() (*http.Request, string, error) {
d.Header.Add("Sec-WebSocket-Extensions", strExtensions)
}

if len(d.subProtocols) > 0 {
d.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.subProtocols, ", ")}
}

req.Header = d.Header
return req, secWebSocket, nil
}
Expand Down Expand Up @@ -178,23 +188,36 @@ func (d *DialOption) tlsConn(c net.Conn) net.Conn {
}

func (d *DialOption) Dial() (c *Conn, err error) {
// scheme ws -> http
// scheme wss -> https
req, secWebSocket, err := d.handshake()
if err != nil {
return nil, err
}

var conn net.Conn
begin := time.Now()

hostName := hostname.GetHostName(d.u)
// conn, err := net.DialTimeout("tcp", d.u.Host /* TODO 加端号*/, d.dialTimeout)
if d.dialFunc == nil {
conn, err = net.Dial("tcp", d.u.Host /* TODO 加端号*/)
} else {
dialFunc := net.Dial
if d.dialFunc != nil {
dialInterface, err := d.dialFunc()
if err != nil {
return nil, err
}
conn, err = dialInterface.Dial("tcp", d.u.Host)
dialFunc = dialInterface.Dial
}

if d.proxyFunc != nil {
proxyURL, err := d.proxyFunc(req)
if err != nil {
return nil, err
}
dialFunc = newhttpProxy(proxyURL, dialFunc).Dial
}

conn, err = dialFunc("tcp", hostName)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion client_option_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client_options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
28 changes: 26 additions & 2 deletions common_options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,8 @@
package quickws

import (
"net/http"
"net/url"
"time"
"unicode/utf8"
)
Expand Down Expand Up @@ -301,9 +303,31 @@ func WithClientOnCloseFunc(onClose func(c *Conn, err error)) ClientOption {
}
}

// 18. 配置新的dial函数
// 18. 配置新的dial函数, 这里可以配置socks5代理地址
func WithClientDialFunc(dialFunc func() (Dialer, error)) ClientOption {
return func(o *DialOption) {
o.dialFunc = dialFunc
}
}

// 19. 配置proxy地址
func WithClientProxyFunc(proxyFunc func(*http.Request) (*url.URL, error)) ClientOption {
return func(o *DialOption) {
o.proxyFunc = proxyFunc
}
}

// 20. 设置支持的子协议
// 20.1 设置客户端支持的子协议
func WithClientSubprotocols(subprotocols []string) ClientOption {
return func(o *DialOption) {
o.subProtocols = subprotocols
}
}

// 20.2 设置服务端支持的子协议
func WithServerSubprotocols(subprotocols []string) ServerOption {
return func(o *ConnOption) {
o.subProtocols = subprotocols
}
}
2 changes: 1 addition & 1 deletion common_options_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
15 changes: 13 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,12 +15,17 @@
package quickws

import (
"errors"
"net"
"net/http"
"net/url"
"time"

"github.com/antlabs/wsutil/enum"
)

var ErrDialFuncAndProxyFunc = errors.New("dialFunc and proxyFunc can't be set at the same time")

type Dialer interface {
Dial(network, addr string) (c net.Conn, err error)
}
Expand All @@ -43,14 +48,15 @@ type Config struct {
maxDelayWriteDuration time.Duration // 最大延迟时间, 默认值是10ms
subProtocols []string // 设置支持的子协议
dialFunc func() (Dialer, error)
proxyFunc func(*http.Request) (*url.URL, error) //
}

func (c *Config) initPayloadSize() int {
return int((1024.0 + float32(enum.MaxFrameHeaderSize)) * c.windowsMultipleTimesPayloadSize)
}

// 默认设置
func (c *Config) defaultSetting() {
func (c *Config) defaultSetting() error {
c.Callback = &DefCallback{}
c.maxDelayWriteNum = 10
c.windowsMultipleTimesPayloadSize = 1.0
Expand All @@ -60,4 +66,9 @@ func (c *Config) defaultSetting() {
c.parseMode = ParseModeWindows
// 对于text消息,默认不检查text是utf8字符
c.utf8Check = func(b []byte) bool { return true }

if c.dialFunc != nil && c.proxyFunc != nil {
return ErrDialFuncAndProxyFunc
}
return nil
}
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion err.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/antlabs/quickws
go 1.20

require (
github.com/antlabs/wsutil v0.1.2
github.com/antlabs/wsutil v0.1.6
golang.org/x/net v0.19.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/antlabs/wsutil v0.1.2 h1:8H6E0eMJ2Wp0qi9YGDeyG3DlIfIZncw2NSScC5bYSBQ=
github.com/antlabs/wsutil v0.1.2/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=
github.com/antlabs/wsutil v0.1.6 h1:K7wR+EvqQT1Nn7jAKs3dKsGtUykPD2OYlCicv4/tUf8=
github.com/antlabs/wsutil v0.1.6/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
2 changes: 1 addition & 1 deletion opcode.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion parse_mode.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 antlabs. All rights reserved.
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 7218a24

Please sign in to comment.