Skip to content

Commit

Permalink
Feat: Full UDP support
Browse files Browse the repository at this point in the history
  • Loading branch information
Musixal committed Oct 16, 2024
1 parent 6bc4aea commit b8bb625
Show file tree
Hide file tree
Showing 7 changed files with 998 additions and 275 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Welcome to the **`Backhaul`** project! This project provides a high-performance
- [Detailed Configuration](#detailed-configuration)
- [TCP Configuration](#tcp-configuration)
- [TCP Multiplexing Configuration](#tcp-multiplexing-configuration)
- [UDP Configuration](#udp-configuration)
- [WebSocket Configuration](#websocket-configuration)
- [Secure WebSocket Configuration](#secure-websocket-configuration)
- [WS Multiplexing Configuration](#ws-multiplexing-configuration)
Expand Down Expand Up @@ -84,7 +85,7 @@ To start using the solution, you'll need to configure both server and client com
[server]# Local, IRAN
bind_addr = "0.0.0.0:3080" # Address and port for the server to listen on (mandatory).
transport = "tcp" # Protocol to use ("tcp", "tcpmux", "ws", "wss", "wsmux", "wssmux". mandatory).
accept_udp = true # Enable transferring UDP connections over TCP transport. (optional, default: false)
accept_udp = false # Enable transferring UDP connections over TCP transport. (optional, default: false)
token = "your_token" # Authentication token for secure communication (optional).
keepalive_period = 75 # Interval in seconds to send keep-alive packets.(optional, default: 75s)
nodelay = false # Enable TCP_NODELAY (optional, default: false).
Expand Down Expand Up @@ -154,6 +155,7 @@ To start using the solution, you'll need to configure both server and client com
[server]
bind_addr = "0.0.0.0:3080"
transport = "tcp"
accept_udp = false
token = "your_token"
keepalive_period = 75
nodelay = true
Expand Down Expand Up @@ -195,6 +197,38 @@ To start using the solution, you'll need to configure both server and client com
`nodelay`: Refers to a TCP socket option (TCP_NODELAY) that improve the latency but decrease the bandwidth
#### UDP Configuration
* **Server**:
```toml
[server]
bind_addr = "0.0.0.0:3080"
transport = "udp"
token = "your_token"
heartbeat = 20
channel_size = 2048
sniffer = false
web_port = 2060
sniffer_log = "/root/backhaul.json"
log_level = "info"
ports = []
```
* **Client**:
```toml
[client]
remote_addr = "0.0.0.0:3080"
transport = "udp"
token = "your_token"
connection_pool = 8
retry_interval = 3
sniffer = false
web_port = 2060
sniffer_log = "/root/backhaul.json"
log_level = "info"
```
#### TCP Multiplexing Configuration
* **Server**:
Expand Down
64 changes: 41 additions & 23 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ func (c *Client) Start() {

if c.config.Transport == config.TCP {
tcpConfig := &transport.TcpConfig{
RemoteAddr: c.config.RemoteAddr,
Nodelay: c.config.Nodelay,
KeepAlive: time.Duration(c.config.Keepalive) * time.Second,
RetryInterval: time.Duration(c.config.RetryInterval) * time.Second,
DialTimeOut: time.Duration(c.config.DialTimeout) * time.Second,
ConnPoolSize: c.config.ConnectionPool,
Token: c.config.Token,
Sniffer: c.config.Sniffer,
WebPort: c.config.WebPort,
SnifferLog: c.config.SnifferLog,
RemoteAddr: c.config.RemoteAddr,
Nodelay: c.config.Nodelay,
KeepAlive: time.Duration(c.config.Keepalive) * time.Second,
RetryInterval: time.Duration(c.config.RetryInterval) * time.Second,
DialTimeOut: time.Duration(c.config.DialTimeout) * time.Second,
ConnPoolSize: c.config.ConnectionPool,
Token: c.config.Token,
Sniffer: c.config.Sniffer,
WebPort: c.config.WebPort,
SnifferLog: c.config.SnifferLog,
}
tcpClient := transport.NewTCPClient(c.ctx, tcpConfig, c.logger)
go tcpClient.Start()
Expand All @@ -69,7 +69,7 @@ func (c *Client) Start() {
KeepAlive: time.Duration(c.config.Keepalive) * time.Second,
RetryInterval: time.Duration(c.config.RetryInterval) * time.Second,
DialTimeOut: time.Duration(c.config.DialTimeout) * time.Second,
ConnPoolSize: c.config.ConnectionPool,
ConnPoolSize: c.config.ConnectionPool,
Token: c.config.Token,
MuxVersion: c.config.MuxVersion,
MaxFrameSize: c.config.MaxFrameSize,
Expand All @@ -84,17 +84,17 @@ func (c *Client) Start() {

} else if c.config.Transport == config.WS || c.config.Transport == config.WSS {
WsConfig := &transport.WsConfig{
RemoteAddr: c.config.RemoteAddr,
Nodelay: c.config.Nodelay,
KeepAlive: time.Duration(c.config.Keepalive) * time.Second,
RetryInterval: time.Duration(c.config.RetryInterval) * time.Second,
DialTimeOut: time.Duration(c.config.DialTimeout) * time.Second,
ConnPoolSize: c.config.ConnectionPool,
Token: c.config.Token,
Sniffer: c.config.Sniffer,
WebPort: c.config.WebPort,
SnifferLog: c.config.SnifferLog,
Mode: c.config.Transport,
RemoteAddr: c.config.RemoteAddr,
Nodelay: c.config.Nodelay,
KeepAlive: time.Duration(c.config.Keepalive) * time.Second,
RetryInterval: time.Duration(c.config.RetryInterval) * time.Second,
DialTimeOut: time.Duration(c.config.DialTimeout) * time.Second,
ConnPoolSize: c.config.ConnectionPool,
Token: c.config.Token,
Sniffer: c.config.Sniffer,
WebPort: c.config.WebPort,
SnifferLog: c.config.SnifferLog,
Mode: c.config.Transport,
}
WsClient := transport.NewWSClient(c.ctx, WsConfig, c.logger)
go WsClient.Start()
Expand All @@ -106,7 +106,7 @@ func (c *Client) Start() {
KeepAlive: time.Duration(c.config.Keepalive) * time.Second,
RetryInterval: time.Duration(c.config.RetryInterval) * time.Second,
DialTimeOut: time.Duration(c.config.DialTimeout) * time.Second,
ConnPoolSize: c.config.ConnectionPool,
ConnPoolSize: c.config.ConnectionPool,
Token: c.config.Token,
MuxVersion: c.config.MuxVersion,
MaxFrameSize: c.config.MaxFrameSize,
Expand Down Expand Up @@ -136,13 +136,31 @@ func (c *Client) Start() {
quicClient := transport.NewQuicClient(c.ctx, quicConfig, c.logger)
go quicClient.ChannelDialer(true)

} else if c.config.Transport == config.UDP {
udpConfig := &transport.UdpConfig{
RemoteAddr: c.config.RemoteAddr,
RetryInterval: time.Duration(c.config.RetryInterval) * time.Second,
DialTimeOut: time.Duration(c.config.DialTimeout) * time.Second,
ConnPoolSize: c.config.ConnectionPool,
Token: c.config.Token,
Sniffer: c.config.Sniffer,
WebPort: c.config.WebPort,
SnifferLog: c.config.SnifferLog,
}
udpClient := transport.NewUDPClient(c.ctx, udpConfig, c.logger)
go udpClient.Start()

} else {
c.logger.Fatal("invalid transport type: ", c.config.Transport)
}

<-c.ctx.Done()

c.logger.Info("all workers stopped successfully")

// supress other logs
c.logger.SetLevel(logrus.FatalLevel)

}
func (c *Client) Stop() {
if c.cancel != nil {
Expand Down
Loading

0 comments on commit b8bb625

Please sign in to comment.