Skip to content

Commit

Permalink
bridge fast
Browse files Browse the repository at this point in the history
  • Loading branch information
zuiwuchang committed Jun 26, 2024
1 parent 0bee416 commit e04fa72
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
85 changes: 85 additions & 0 deletions bin/example/ws-fast.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
dialer: [
{
tag: 'tls',
timeout: '200ms',
url: 'basic+tls://example.com',
network: 'tcp',
addr: 'localhost:2443',
allowInsecure: true,
},
{
tag: 'pipe.ws',
timeout: '200ms',
url: 'ws://example.com/ws',
network: 'pipe',
addr: 'http.pipe',
fast: true,
},
{
tag: 'portal',
timeout: '200ms',
url: 'basic://',
network: 'portal',
addr: 'listener-portal-ws',
},
],
bridge: [
{
timeout: '200ms',
url: 'ws://example.com/portal',
fast: true,
network: 'pipe',
addr: 'http.pipe',
dialer: {
tag: 'tls',
close: '1s',
},
},
],
listener: [
{
network: 'tcp',
addr: ':9000',
dialer: {
tag: 'pipe.ws',
close: '1s',
},
},
{
network: 'tcp',
addr: ':9001',
dialer: {
tag: 'portal',
close: '1s',
},
},
{
network: 'pipe',
addr: 'http.pipe',
mode: 'http',
router: [
{
method: 'WS',
pattern: '/ws',
fast :true,
dialer: {
tag: 'tls',
close: '1s',
},
},
{
method: 'WS',
pattern: '/portal',
fast :true,
portal: {
tag: 'listener-portal-ws',
timeout: '200ms',
heart: '40s',
heartTimeout: '1s',
},
},
],
},
],
}
7 changes: 6 additions & 1 deletion bridge/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
Expand All @@ -21,6 +22,7 @@ import (
)

func newWebsocketBridge(nk *network.Network, log *slog.Logger, pool *pool.Pool, dialers map[string]dialer.Dialer, opts *config.Bridge, u *url.URL, secure bool) (bridge *bridge, e error) {
fmt.Println(opts)
found, ok := dialers[opts.Dialer.Tag]
if !ok {
e = errors.New(`dialer not found: ` + opts.Dialer.Tag)
Expand All @@ -45,7 +47,7 @@ func newWebsocketBridge(nk *network.Network, log *slog.Logger, pool *pool.Pool,
tag = `ws ` + network + `://` + addr
}
}
log = log.With(`bridge`, tag, `dialer`, opts.Dialer.Tag)
log = log.With(`bridge`, tag, `dialer`, opts.Dialer.Tag, `fast`, opts.Fast)
rawDialer, e := nk.Dialer(network, addr, nil)
if e != nil {
log.Error(`new dialer fail`, `error`, e)
Expand Down Expand Up @@ -99,6 +101,9 @@ func newWebsocketBridge(nk *network.Network, log *slog.Logger, pool *pool.Pool,
if e != nil {
return nil, e
}
if opts.Fast {
return ws.NetConn(), nil
}
return httpmux.NewWebsocketConn(ws), nil
}),
reverse.WithListenerSynAck(true),
Expand Down
2 changes: 2 additions & 0 deletions config/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type Bridge struct {
Addr string `json:"addr"`
// optional network
Network string `json:"network"`
// If true, only websocket handshake is used, and tcp communication is used directly after the handshake is successful.
Fast bool `json:"fast"`
// If true, do not verify whether the certificate is valid when connecting to the tls server
AllowInsecure bool `json:"allowInsecure"`
// http method, default "POST"
Expand Down
3 changes: 1 addition & 2 deletions script/conf.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Target="streamf"
Docker="king011/streamf"
Dir=$(cd "$(dirname $BASH_SOURCE)/.." && pwd)
Version="v0.0.2"
View=1
Version="v0.0.3"
Platforms=(
darwin/amd64
windows/amd64
Expand Down

0 comments on commit e04fa72

Please sign in to comment.