2024-05-23 13:24:02 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-29 16:40:26 +02:00
|
|
|
"fmt"
|
2024-05-26 22:14:33 +02:00
|
|
|
"net"
|
2024-05-23 13:24:02 +02:00
|
|
|
)
|
|
|
|
|
2024-05-29 16:40:26 +02:00
|
|
|
// Manager todo: thread safe
|
2024-05-23 13:24:02 +02:00
|
|
|
type Manager struct {
|
|
|
|
ctx context.Context
|
|
|
|
srvAddress string
|
|
|
|
peerID string
|
|
|
|
|
2024-05-29 16:40:26 +02:00
|
|
|
relayClient *Client
|
2024-05-23 13:24:02 +02:00
|
|
|
|
2024-05-29 16:40:26 +02:00
|
|
|
relayClients map[string]*Client
|
2024-05-23 13:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager(ctx context.Context, serverAddress string, peerID string) *Manager {
|
|
|
|
return &Manager{
|
2024-05-29 16:40:26 +02:00
|
|
|
ctx: ctx,
|
|
|
|
srvAddress: serverAddress,
|
|
|
|
peerID: peerID,
|
|
|
|
relayClients: make(map[string]*Client),
|
2024-05-23 13:24:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 16:40:26 +02:00
|
|
|
func (m *Manager) Serve() error {
|
|
|
|
m.relayClient = NewClient(m.ctx, m.srvAddress, m.peerID)
|
|
|
|
err := m.relayClient.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-05-23 13:24:02 +02:00
|
|
|
}
|
2024-05-29 16:40:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
2024-05-23 13:24:02 +02:00
|
|
|
|
2024-05-29 16:40:26 +02:00
|
|
|
func (m *Manager) RelayAddress() (net.Addr, error) {
|
|
|
|
if m.relayClient == nil {
|
|
|
|
return nil, fmt.Errorf("relay client not connected")
|
|
|
|
}
|
|
|
|
return m.relayClient.RelayRemoteAddress()
|
2024-05-26 22:14:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) OpenConn(peerKey string) (net.Conn, error) {
|
2024-05-29 16:40:26 +02:00
|
|
|
if m.relayClient == nil {
|
|
|
|
return nil, fmt.Errorf("relay client not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
rAddr, err := m.relayClient.RelayRemoteAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("relay client not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.OpenConnTo(rAddr.String(), peerKey)
|
2024-05-26 22:14:33 +02:00
|
|
|
}
|
|
|
|
|
2024-05-29 16:40:26 +02:00
|
|
|
func (m *Manager) OpenConnTo(serverAddress, peerKey string) (net.Conn, error) {
|
|
|
|
if m.relayClient == nil {
|
|
|
|
return nil, fmt.Errorf("relay client not connected")
|
|
|
|
}
|
|
|
|
rAddr, err := m.relayClient.RelayRemoteAddress()
|
2024-05-26 22:14:33 +02:00
|
|
|
if err != nil {
|
2024-05-29 16:40:26 +02:00
|
|
|
return nil, fmt.Errorf("relay client not connected")
|
2024-05-26 22:14:33 +02:00
|
|
|
}
|
2024-05-29 16:40:26 +02:00
|
|
|
|
|
|
|
if rAddr.String() == serverAddress {
|
|
|
|
return m.relayClient.OpenConn(peerKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
relayClient, ok := m.relayClients[serverAddress]
|
|
|
|
if ok {
|
|
|
|
return relayClient.OpenConn(peerKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
relayClient = NewClient(m.ctx, serverAddress, m.peerID)
|
|
|
|
err = relayClient.ConnectWithoutReconnect()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := relayClient.OpenConn(peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.relayClients[serverAddress] = relayClient
|
|
|
|
return conn, nil
|
2024-05-23 13:24:02 +02:00
|
|
|
}
|