Skip to content

Commit

Permalink
chore: opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed May 31, 2024
1 parent a1a65b9 commit 97c2240
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 122 deletions.
188 changes: 78 additions & 110 deletions examples/config.json
Original file line number Diff line number Diff line change
@@ -1,111 +1,79 @@
{
"web_port": 9000,
"web_token": "",
"enable_ping": false,
"log_level": "debug",
"reload_interval": 5,
"relay_configs": [
{
"listen": "127.0.0.1:1234",
"listen_type": "raw",
"transport_type": "raw",
"label": "relay1",
"tcp_remotes": [
"0.0.0.0:5201"
],
"udp_remotes": [
"0.0.0.0:5201"
]
},
{
"listen": "127.0.0.1:1235",
"listen_type": "raw",
"transport_type": "ws",
"tcp_remotes": [
"ws://0.0.0.0:2443"
],
"udp_remotes": [
"0.0.0.0:5201"
]
},
{
"listen": "127.0.0.1:1236",
"listen_type": "raw",
"transport_type": "wss",
"tcp_remotes": [
"wss://0.0.0.0:3443"
],
"udp_remotes": [
"0.0.0.0:5201"
]
},
{
"listen": "127.0.0.1:1237",
"listen_type": "raw",
"transport_type": "mwss",
"tcp_remotes": [
"wss://0.0.0.0:4443"
],
"udp_remotes": [
"0.0.0.0:5201"
]
},
{
"listen": "127.0.0.1:1238",
"listen_type": "raw",
"transport_type": "mtcp",
"tcp_remotes": [
"0.0.0.0:4444"
],
"udp_remotes": [
"0.0.0.0:5201"
]
},
{
"listen": "127.0.0.1:2443",
"listen_type": "ws",
"transport_type": "raw",
"tcp_remotes": [
"0.0.0.0:5201"
],
"udp_remotes": []
},
{
"listen": "127.0.0.1:3443",
"listen_type": "wss",
"transport_type": "raw",
"tcp_remotes": [
"0.0.0.0:5201"
],
"udp_remotes": []
},
{
"listen": "127.0.0.1:4443",
"listen_type": "mwss",
"transport_type": "raw",
"tcp_remotes": [
"0.0.0.0:5201"
],
"udp_remotes": []
},
{
"listen": "127.0.0.1:4444",
"listen_type": "mtcp",
"transport_type": "raw",
"tcp_remotes": [
"0.0.0.0:5201"
],
"udp_remotes": []
},
{
"label": "ping_test",
"listen": "127.0.0.1:8888",
"listen_type": "raw",
"transport_type": "raw",
"tcp_remotes": [
"8.8.8.8:5201",
"google.com:5201"
]
}
]
}
"web_port": 9000,
"enable_ping": false,
"log_level": "info",
"reload_interval": 60,
"relay_configs": [
{
"listen": "127.0.0.1:1234",
"listen_type": "raw",
"transport_type": "raw",
"label": "relay1",
"tcp_remotes": ["0.0.0.0:5201"],
"udp_remotes": ["0.0.0.0:5201"]
},
{
"listen": "127.0.0.1:1235",
"listen_type": "raw",
"transport_type": "ws",
"tcp_remotes": ["ws://0.0.0.0:2443"],
"udp_remotes": ["0.0.0.0:5201"]
},
{
"listen": "127.0.0.1:1236",
"listen_type": "raw",
"transport_type": "wss",
"tcp_remotes": ["wss://0.0.0.0:3443"],
"udp_remotes": ["0.0.0.0:5201"]
},
{
"listen": "127.0.0.1:1237",
"listen_type": "raw",
"transport_type": "mwss",
"tcp_remotes": ["wss://0.0.0.0:4443"],
"udp_remotes": ["0.0.0.0:5201"]
},
{
"listen": "127.0.0.1:1238",
"listen_type": "raw",
"transport_type": "mtcp",
"tcp_remotes": ["0.0.0.0:4444"],
"udp_remotes": ["0.0.0.0:5201"]
},
{
"listen": "127.0.0.1:2443",
"listen_type": "ws",
"transport_type": "raw",
"tcp_remotes": ["0.0.0.0:5201"],
"udp_remotes": []
},
{
"listen": "127.0.0.1:3443",
"listen_type": "wss",
"transport_type": "raw",
"tcp_remotes": ["0.0.0.0:5201"],
"udp_remotes": []
},
{
"listen": "127.0.0.1:4443",
"listen_type": "mwss",
"transport_type": "raw",
"tcp_remotes": ["0.0.0.0:5201"],
"udp_remotes": []
},
{
"listen": "127.0.0.1:4444",
"listen_type": "mtcp",
"transport_type": "raw",
"tcp_remotes": ["0.0.0.0:5201"],
"udp_remotes": []
},
{
"label": "ping_test",
"listen": "127.0.0.1:8888",
"listen_type": "raw",
"transport_type": "raw",
"tcp_remotes": ["8.8.8.8:5201", "google.com:5201"]
}
]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
toolchain go1.22.1

require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/getsentry/sentry-go v0.27.0
github.com/go-ping/ping v1.1.0
github.com/gobwas/ws v1.3.2
Expand All @@ -27,7 +28,6 @@ require (
)

require (
github.com/alecthomas/kingpin/v2 v2.4.0 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/beevik/ntp v1.4.2 // indirect
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions internal/transporter/ws_conn.go → internal/conn/ws_conn.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package transporter
package conn

import (
"fmt"
Expand All @@ -9,6 +9,7 @@ import (
"github.com/Ehco1996/ehco/pkg/buffer"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"go.uber.org/zap"
)

type wsConn struct {
Expand All @@ -17,7 +18,7 @@ type wsConn struct {
buf []byte
}

func newWsConn(conn net.Conn, isServer bool) *wsConn {
func NewWSConn(conn net.Conn, isServer bool) *wsConn {
return &wsConn{conn: conn, isServer: isServer, buf: buffer.BufferPool.Get()}
}

Expand All @@ -27,6 +28,7 @@ func (c *wsConn) Read(b []byte) (n int, err error) {
return 0, err
}
if header.Length > int64(cap(c.buf)) {
zap.S().Warnf("ws payload size:%d is larger than buffer size:%d", header.Length, cap(c.buf))
c.buf = make([]byte, header.Length)
}
payload := c.buf[:header.Length]
Expand All @@ -38,7 +40,7 @@ func (c *wsConn) Read(b []byte) (n int, err error) {
ws.Cipher(payload, header.Mask, 0)
}
if len(payload) > len(b) {
return 0, fmt.Errorf("buffer too small to transport ws msg")
return 0, fmt.Errorf("buffer size:%d too small to transport ws payload size:%d", len(b), len(payload))
}
copy(b, payload)
return len(payload), nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package transporter
package conn

import (
"context"
Expand All @@ -23,7 +23,7 @@ func TestClientConn_ReadWrite(t *testing.T) {
}
go func() {
defer conn.Close()
wsc := newWsConn(conn, true)
wsc := NewWSConn(conn, true)

buf := make([]byte, 1024)
for {
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestClientConn_ReadWrite(t *testing.T) {
}
defer conn.Close()

wsClientConn := newWsConn(conn, false)
wsClientConn := NewWSConn(conn, false)
for i := 0; i < 3; i++ {
// test write
n, err := wsClientConn.Write(data)
Expand Down
7 changes: 4 additions & 3 deletions internal/transporter/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gobwas/ws"
"github.com/labstack/echo/v4"

"github.com/Ehco1996/ehco/internal/conn"
"github.com/Ehco1996/ehco/internal/constant"
"github.com/Ehco1996/ehco/internal/metrics"
"github.com/Ehco1996/ehco/internal/web"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (s *WsClient) TCPHandShake(remote *lb.Node) (net.Conn, error) {
latency := time.Since(t1)
metrics.HandShakeDuration.WithLabelValues(remote.Label).Observe(float64(latency.Milliseconds()))
remote.HandShakeDuration = latency
c := newWsConn(wsc, false)
c := conn.NewWSConn(wsc, false)
return c, nil
}

Expand Down Expand Up @@ -91,8 +92,8 @@ func (s *WsServer) HandleRequest(w http.ResponseWriter, req *http.Request) {
if err != nil {
return
}
c := newWsConn(wsc, true)
if err := s.RelayTCPConn(c, s.relayer.TCPHandShake); err != nil {

if err := s.RelayTCPConn(conn.NewWSConn(wsc, true), s.relayer.TCPHandShake); err != nil {
s.l.Errorf("RelayTCPConn error: %s", err.Error())
}
}
7 changes: 5 additions & 2 deletions internal/transporter/wss_mux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// NOTE CAN NOT use real ws frame to transport smux frame
// err: accept stream err: buffer size:8 too small to transport ws payload size:45
// so this transport just use ws protocol to handshake and then use smux protocol to transport
package transporter

import (
Expand Down Expand Up @@ -101,12 +104,12 @@ func (s *MwssServer) ListenAndServe() error {
}

func (s *MwssServer) HandleRequest(w http.ResponseWriter, r *http.Request) {
conn, _, _, err := ws.UpgradeHTTP(r, w)
c, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
s.l.Error(err)
return
}
s.mux(conn)
s.mux(c)
}

func (s *MwssServer) Close() error {
Expand Down

0 comments on commit 97c2240

Please sign in to comment.