mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-06-03 02:49:47 +00:00
refactor: align tunnel runtime terminology
This commit is contained in:
@@ -29,8 +29,8 @@ var (
|
||||
ErrKeyStringLength = errors.New("key string length must be 32")
|
||||
// ErrInvalidSocks5 is returned when the SOCKS version is not 5.
|
||||
ErrInvalidSocks5 = errors.New("invalid SOCKS5 version")
|
||||
// ErrNoPeers is returned when no peers are available for sending.
|
||||
ErrNoPeers = errors.New("no peers available")
|
||||
// ErrNoLinks is returned when no links are available for sending.
|
||||
ErrNoLinks = errors.New("no links available")
|
||||
// ErrEncryptFailed is returned when encryption fails.
|
||||
ErrEncryptFailed = errors.New("encrypt failed")
|
||||
// ErrUnsupportedSocksCommand is returned when a SOCKS5 command is not supported.
|
||||
@@ -41,14 +41,14 @@ var (
|
||||
ErrTunnelSetupFailed = errors.New("tunnel setup failed")
|
||||
)
|
||||
|
||||
// Client handles local SOCKS5 connections and tunnels them via WebRTC.
|
||||
// Client handles local SOCKS5 connections and tunnels them through the selected runtime stack.
|
||||
type Client struct {
|
||||
links []link.Link
|
||||
cipher *crypto.Cipher
|
||||
mux *mux.Multiplexer
|
||||
connections map[uint16]net.Conn
|
||||
connMu sync.RWMutex
|
||||
peerIdx atomic.Uint32
|
||||
linkIdx atomic.Uint32
|
||||
clientID uint32
|
||||
activeClients atomic.Int32
|
||||
wg sync.WaitGroup
|
||||
@@ -60,7 +60,7 @@ func Run(
|
||||
ctx context.Context,
|
||||
linkName,
|
||||
transportName,
|
||||
providerName,
|
||||
carrierName,
|
||||
roomURL,
|
||||
keyHex string,
|
||||
localAddr string,
|
||||
@@ -68,7 +68,7 @@ func Run(
|
||||
socksUser string,
|
||||
socksPass string,
|
||||
) error {
|
||||
return RunWithReady(ctx, linkName, transportName, providerName, roomURL, keyHex, localAddr, dnsServer, socksUser, socksPass, nil)
|
||||
return RunWithReady(ctx, linkName, transportName, carrierName, roomURL, keyHex, localAddr, dnsServer, socksUser, socksPass, nil)
|
||||
}
|
||||
|
||||
// RunWithReady is like Run but accepts a callback that is called when the client is ready.
|
||||
@@ -76,7 +76,7 @@ func RunWithReady(
|
||||
ctx context.Context,
|
||||
linkName,
|
||||
transportName,
|
||||
providerName,
|
||||
carrierName,
|
||||
roomURL,
|
||||
keyHex string,
|
||||
localAddr string,
|
||||
@@ -109,10 +109,10 @@ func RunWithReady(
|
||||
|
||||
c.setupMux()
|
||||
|
||||
const peerCount = 1
|
||||
for i := range peerCount {
|
||||
if err := c.addTransport(runCtx, linkName, transportName, providerName, roomURL, i, cancel, dnsServer, "", 0); err != nil {
|
||||
return fmt.Errorf("addTransport failed: %w", err)
|
||||
const linkCount = 1
|
||||
for i := range linkCount {
|
||||
if err := c.addLink(runCtx, linkName, transportName, carrierName, roomURL, i, cancel, dnsServer, "", 0); err != nil {
|
||||
return fmt.Errorf("addLink failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,20 +180,20 @@ func (c *Client) setupMux() {
|
||||
return fmt.Errorf("%w: %w", ErrEncryptFailed, err)
|
||||
}
|
||||
if len(c.links) == 0 {
|
||||
return ErrNoPeers
|
||||
return ErrNoLinks
|
||||
}
|
||||
idx := c.peerIdx.Add(1) % uint32(len(c.links)) //nolint:gosec
|
||||
idx := c.linkIdx.Add(1) % uint32(len(c.links)) //nolint:gosec
|
||||
return c.links[idx].Send(encrypted)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) addTransport(
|
||||
func (c *Client) addLink(
|
||||
ctx context.Context,
|
||||
linkName,
|
||||
transportName,
|
||||
providerName,
|
||||
carrierName,
|
||||
roomURL string,
|
||||
peerID int,
|
||||
linkID int,
|
||||
cancel context.CancelFunc,
|
||||
dnsServer,
|
||||
socksProxyAddr string,
|
||||
@@ -201,7 +201,7 @@ func (c *Client) addTransport(
|
||||
) error {
|
||||
ln, err := link.New(ctx, linkName, link.Config{
|
||||
Transport: transportName,
|
||||
Carrier: providerName,
|
||||
Carrier: carrierName,
|
||||
RoomURL: roomURL,
|
||||
Name: names.Generate(),
|
||||
OnData: c.onData,
|
||||
@@ -214,20 +214,20 @@ func (c *Client) addTransport(
|
||||
}
|
||||
|
||||
ln.SetEndedCallback(func(reason string) {
|
||||
logger.Infof("Client transport %d reported conference end: %s", peerID, reason)
|
||||
logger.Infof("Client link %d reported conference end: %s", linkID, reason)
|
||||
cancel()
|
||||
})
|
||||
c.links = append(c.links, ln)
|
||||
|
||||
ln.SetReconnectCallback(func() {
|
||||
c.handleTransportReconnect(peerID)
|
||||
c.handleLinkReconnect(linkID)
|
||||
})
|
||||
|
||||
logger.Infof("Connecting transport %d via %s/%s...", peerID, transportName, providerName)
|
||||
logger.Infof("Connecting link %d via %s/%s/%s...", linkID, linkName, transportName, carrierName)
|
||||
if err := ln.Connect(ctx); err != nil {
|
||||
return fmt.Errorf("failed to connect transport: %w", err)
|
||||
return fmt.Errorf("failed to connect link: %w", err)
|
||||
}
|
||||
logger.Infof("Transport %d connected", peerID)
|
||||
logger.Infof("Link %d connected", linkID)
|
||||
|
||||
c.wg.Add(1)
|
||||
go func() {
|
||||
@@ -243,8 +243,8 @@ func (c *Client) addTransport(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) handleTransportReconnect(peerID int) {
|
||||
logger.Infof("transport %d reconnect event", peerID)
|
||||
func (c *Client) handleLinkReconnect(linkID int) {
|
||||
logger.Infof("link %d reconnect event", linkID)
|
||||
|
||||
c.connMu.Lock()
|
||||
for sid, conn := range c.connections {
|
||||
@@ -261,9 +261,9 @@ func (c *Client) handleTransportReconnect(peerID int) {
|
||||
return fmt.Errorf("%w: %w", ErrEncryptFailed, err)
|
||||
}
|
||||
if len(c.links) == 0 {
|
||||
return ErrNoPeers
|
||||
return ErrNoLinks
|
||||
}
|
||||
idx := c.peerIdx.Add(1) % uint32(len(c.links)) //nolint:gosec
|
||||
idx := c.linkIdx.Add(1) % uint32(len(c.links)) //nolint:gosec
|
||||
return c.links[idx].Send(encrypted)
|
||||
})
|
||||
c.mux.Reset()
|
||||
@@ -448,7 +448,7 @@ func (c *Client) shutdown() {
|
||||
c.connMu.Unlock()
|
||||
|
||||
for i, tr := range c.links {
|
||||
logger.Infof("closing transport %d", i)
|
||||
logger.Infof("closing link %d", i)
|
||||
_ = tr.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,15 +32,15 @@ var (
|
||||
ErrSocks5AuthFailed = errors.New("SOCKS5 auth failed")
|
||||
// ErrSocks5ConnectFailed is returned when SOCKS5 connection fails.
|
||||
ErrSocks5ConnectFailed = errors.New("SOCKS5 connect failed")
|
||||
// ErrNoPeers is returned when no peers are available.
|
||||
ErrNoPeers = errors.New("no peers available")
|
||||
// ErrNoLinks is returned when no links are available.
|
||||
ErrNoLinks = errors.New("no links available")
|
||||
// ErrDialProxy is returned when dialing the proxy fails.
|
||||
ErrDialProxy = errors.New("failed to dial proxy")
|
||||
// ErrEncryptFailed is returned when encryption fails.
|
||||
ErrEncryptFailed = errors.New("encrypt failed")
|
||||
)
|
||||
|
||||
// Server handles incoming WebRTC connections and proxies their traffic.
|
||||
// Server handles incoming tunnel connections and proxies their traffic.
|
||||
type Server struct {
|
||||
links []link.Link
|
||||
cipher *crypto.Cipher
|
||||
@@ -49,7 +49,7 @@ type Server struct {
|
||||
connMu sync.RWMutex
|
||||
streamPumps map[uint16]net.Conn
|
||||
pumpMu sync.Mutex
|
||||
peerIdx atomic.Uint32
|
||||
linkIdx atomic.Uint32
|
||||
activeClients atomic.Int32
|
||||
wg sync.WaitGroup
|
||||
dnsServer string
|
||||
@@ -70,7 +70,7 @@ func Run(
|
||||
ctx context.Context,
|
||||
linkName,
|
||||
transportName,
|
||||
providerName,
|
||||
carrierName,
|
||||
roomURL,
|
||||
keyHex string,
|
||||
dnsServer,
|
||||
@@ -102,10 +102,10 @@ func Run(
|
||||
s.setupResolver()
|
||||
s.setupMux()
|
||||
|
||||
const peerCount = 1
|
||||
for i := range peerCount {
|
||||
if err := s.addTransport(runCtx, linkName, transportName, providerName, roomURL, i, cancel); err != nil {
|
||||
return fmt.Errorf("addTransport failed: %w", err)
|
||||
const linkCount = 1
|
||||
for i := range linkCount {
|
||||
if err := s.addLink(runCtx, linkName, transportName, carrierName, roomURL, i, cancel); err != nil {
|
||||
return fmt.Errorf("addLink failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,25 +179,25 @@ func (s *Server) setupMux() {
|
||||
return fmt.Errorf("%w: %w", ErrEncryptFailed, err)
|
||||
}
|
||||
if len(s.links) == 0 {
|
||||
return ErrNoPeers
|
||||
return ErrNoLinks
|
||||
}
|
||||
idx := s.peerIdx.Add(1) % uint32(len(s.links)) //nolint:gosec
|
||||
idx := s.linkIdx.Add(1) % uint32(len(s.links)) //nolint:gosec
|
||||
return s.links[idx].Send(encrypted)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) addTransport(
|
||||
func (s *Server) addLink(
|
||||
ctx context.Context,
|
||||
linkName,
|
||||
transportName,
|
||||
providerName,
|
||||
carrierName,
|
||||
roomURL string,
|
||||
peerID int,
|
||||
linkID int,
|
||||
cancel context.CancelFunc,
|
||||
) error {
|
||||
ln, err := link.New(ctx, linkName, link.Config{
|
||||
Transport: transportName,
|
||||
Carrier: providerName,
|
||||
Carrier: carrierName,
|
||||
RoomURL: roomURL,
|
||||
Name: names.Generate(),
|
||||
OnData: s.onData,
|
||||
@@ -210,20 +210,20 @@ func (s *Server) addTransport(
|
||||
}
|
||||
|
||||
ln.SetEndedCallback(func(reason string) {
|
||||
logger.Infof("Server transport %d reported conference end: %s", peerID, reason)
|
||||
logger.Infof("Server link %d reported conference end: %s", linkID, reason)
|
||||
cancel()
|
||||
})
|
||||
s.links = append(s.links, ln)
|
||||
|
||||
ln.SetReconnectCallback(func() {
|
||||
s.handleTransportReconnect(peerID)
|
||||
s.handleLinkReconnect(linkID)
|
||||
})
|
||||
|
||||
logger.Infof("Connecting transport %d via %s/%s...", peerID, transportName, providerName)
|
||||
logger.Infof("Connecting link %d via %s/%s/%s...", linkID, linkName, transportName, carrierName)
|
||||
if err := ln.Connect(ctx); err != nil {
|
||||
return fmt.Errorf("failed to connect transport: %w", err)
|
||||
return fmt.Errorf("failed to connect link: %w", err)
|
||||
}
|
||||
logger.Infof("Transport %d connected", peerID)
|
||||
logger.Infof("Link %d connected", linkID)
|
||||
|
||||
s.wg.Add(1)
|
||||
go func() {
|
||||
@@ -233,8 +233,8 @@ func (s *Server) addTransport(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) handleTransportReconnect(peerID int) {
|
||||
logger.Infof("transport %d reconnect event", peerID)
|
||||
func (s *Server) handleLinkReconnect(linkID int) {
|
||||
logger.Infof("link %d reconnect event", linkID)
|
||||
|
||||
s.connMu.Lock()
|
||||
for sid, conn := range s.connections {
|
||||
@@ -251,9 +251,9 @@ func (s *Server) handleTransportReconnect(peerID int) {
|
||||
return fmt.Errorf("%w: %w", ErrEncryptFailed, err)
|
||||
}
|
||||
if len(s.links) == 0 {
|
||||
return ErrNoPeers
|
||||
return ErrNoLinks
|
||||
}
|
||||
idx := s.peerIdx.Add(1) % uint32(len(s.links)) //nolint:gosec
|
||||
idx := s.linkIdx.Add(1) % uint32(len(s.links)) //nolint:gosec
|
||||
return s.links[idx].Send(encrypted)
|
||||
})
|
||||
s.mux.Reset()
|
||||
@@ -353,7 +353,7 @@ func (s *Server) shutdown() {
|
||||
s.connMu.Unlock()
|
||||
|
||||
for i, tr := range s.links {
|
||||
logger.Infof("closing transport %d", i)
|
||||
logger.Infof("closing link %d", i)
|
||||
_ = tr.Close()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user