mirror of
https://github.com/netbirdio/netbird.git
synced 2025-02-02 11:29:46 +01:00
fix doc and lint warns for connection package
This commit is contained in:
parent
e6358e7bb2
commit
f1cff0e13a
@ -11,14 +11,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// DefaultWgKeepAlive default Wireguard keep alive constant
|
||||||
DefaultWgKeepAlive = 20 * time.Second
|
DefaultWgKeepAlive = 20 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConnConfig Connection configuration struct
|
||||||
type ConnConfig struct {
|
type ConnConfig struct {
|
||||||
// Local Wireguard listening address e.g. 127.0.0.1:51820
|
// Local Wireguard listening address e.g. 127.0.0.1:51820
|
||||||
WgListenAddr string
|
WgListenAddr string
|
||||||
// A Local Wireguard Peer IP address in CIDR notation e.g. 10.30.30.1/24
|
// A Local Wireguard Peer IP address in CIDR notation e.g. 10.30.30.1/24
|
||||||
WgPeerIp string
|
WgPeerIP string
|
||||||
// Local Wireguard Interface name (e.g. wg0)
|
// Local Wireguard Interface name (e.g. wg0)
|
||||||
WgIface string
|
WgIface string
|
||||||
// Wireguard allowed IPs (e.g. 10.30.30.2/32)
|
// Wireguard allowed IPs (e.g. 10.30.30.2/32)
|
||||||
@ -31,11 +33,13 @@ type ConnConfig struct {
|
|||||||
StunTurnURLS []*ice.URL
|
StunTurnURLS []*ice.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IceCredentials ICE protocol credentials struct
|
||||||
type IceCredentials struct {
|
type IceCredentials struct {
|
||||||
uFrag string
|
uFrag string
|
||||||
pwd string
|
pwd string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connection Holds information about a connection and handles signal protocol
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
Config ConnConfig
|
Config ConnConfig
|
||||||
// signalCandidate is a handler function to signal remote peer about local connection candidate
|
// signalCandidate is a handler function to signal remote peer about local connection candidate
|
||||||
@ -61,6 +65,7 @@ type Connection struct {
|
|||||||
remoteAuthCond sync.Once
|
remoteAuthCond sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewConnection Creates a new connection and sets handling functions for signal protocol
|
||||||
func NewConnection(config ConnConfig,
|
func NewConnection(config ConnConfig,
|
||||||
signalCandidate func(candidate ice.Candidate) error,
|
signalCandidate func(candidate ice.Candidate) error,
|
||||||
signalOffer func(uFrag string, pwd string) error,
|
signalOffer func(uFrag string, pwd string) error,
|
||||||
@ -151,6 +156,7 @@ func (conn *Connection) Open(timeout time.Duration) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close Closes a peer connection
|
||||||
func (conn *Connection) Close() error {
|
func (conn *Connection) Close() error {
|
||||||
var err error
|
var err error
|
||||||
conn.closeCond.Do(func() {
|
conn.closeCond.Do(func() {
|
||||||
@ -176,6 +182,7 @@ func (conn *Connection) Close() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnAnswer Handles the answer from the other peer
|
||||||
func (conn *Connection) OnAnswer(remoteAuth IceCredentials) error {
|
func (conn *Connection) OnAnswer(remoteAuth IceCredentials) error {
|
||||||
|
|
||||||
conn.remoteAuthCond.Do(func() {
|
conn.remoteAuthCond.Do(func() {
|
||||||
@ -185,23 +192,25 @@ func (conn *Connection) OnAnswer(remoteAuth IceCredentials) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnOffer Handles the offer from the other peer
|
||||||
func (conn *Connection) OnOffer(remoteAuth IceCredentials) error {
|
func (conn *Connection) OnOffer(remoteAuth IceCredentials) error {
|
||||||
|
|
||||||
conn.remoteAuthCond.Do(func() {
|
conn.remoteAuthCond.Do(func() {
|
||||||
log.Debugf("OnOffer from peer %s", conn.Config.RemoteWgKey.String())
|
log.Debugf("OnOffer from peer %s", conn.Config.RemoteWgKey.String())
|
||||||
conn.remoteAuthChannel <- remoteAuth
|
conn.remoteAuthChannel <- remoteAuth
|
||||||
uFrag, pwd, err := conn.agent.GetLocalUserCredentials()
|
uFrag, pwd, err := conn.agent.GetLocalUserCredentials()
|
||||||
if err != nil {
|
if err != nil { //nolint
|
||||||
}
|
}
|
||||||
|
|
||||||
err = conn.signalAnswer(uFrag, pwd)
|
err = conn.signalAnswer(uFrag, pwd)
|
||||||
if err != nil {
|
if err != nil { //nolint
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnRemoteCandidate Handles remote candidate provided by the peer.
|
||||||
func (conn *Connection) OnRemoteCandidate(candidate ice.Candidate) error {
|
func (conn *Connection) OnRemoteCandidate(candidate ice.Candidate) error {
|
||||||
|
|
||||||
log.Debugf("onRemoteCandidate from peer %s -> %s", conn.Config.RemoteWgKey.String(), candidate.String())
|
log.Debugf("onRemoteCandidate from peer %s -> %s", conn.Config.RemoteWgKey.String(), candidate.String())
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Engine is an instance of the Connection Engine
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
// a list of STUN and TURN servers
|
// a list of STUN and TURN servers
|
||||||
stunsTurns []*ice.URL
|
stunsTurns []*ice.URL
|
||||||
@ -22,27 +23,31 @@ type Engine struct {
|
|||||||
// Wireguard interface
|
// Wireguard interface
|
||||||
wgIface string
|
wgIface string
|
||||||
// Wireguard local address
|
// Wireguard local address
|
||||||
wgIp string
|
wgIP string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Peer is an instance of the Connection Peer
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
WgPubKey string
|
WgPubKey string
|
||||||
WgAllowedIps string
|
WgAllowedIps string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewEngine creates a new Connection Engine
|
||||||
func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgAddr string) *Engine {
|
func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgAddr string) *Engine {
|
||||||
return &Engine{
|
return &Engine{
|
||||||
stunsTurns: stunsTurns,
|
stunsTurns: stunsTurns,
|
||||||
signal: signal,
|
signal: signal,
|
||||||
wgIface: wgIface,
|
wgIface: wgIface,
|
||||||
wgIp: wgAddr,
|
wgIP: wgAddr,
|
||||||
conns: map[string]*Connection{},
|
conns: map[string]*Connection{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start creates a new tunnel interface and listens to signals from the Signal service.
|
||||||
|
// It also creates an Go routine to handle each peer communication from the config file
|
||||||
func (e *Engine) Start(myKey wgtypes.Key, peers []Peer) error {
|
func (e *Engine) Start(myKey wgtypes.Key, peers []Peer) error {
|
||||||
|
|
||||||
err := iface.Create(e.wgIface, e.wgIp)
|
err := iface.Create(e.wgIface, e.wgIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error while creating interface %s: [%s]", e.wgIface, err.Error())
|
log.Errorf("error while creating interface %s: [%s]", e.wgIface, err.Error())
|
||||||
return err
|
return err
|
||||||
@ -102,7 +107,7 @@ func (e *Engine) openPeerConnection(wgPort int, myKey wgtypes.Key, peer Peer) (*
|
|||||||
remoteKey, _ := wgtypes.ParseKey(peer.WgPubKey)
|
remoteKey, _ := wgtypes.ParseKey(peer.WgPubKey)
|
||||||
connConfig := &ConnConfig{
|
connConfig := &ConnConfig{
|
||||||
WgListenAddr: fmt.Sprintf("127.0.0.1:%d", wgPort),
|
WgListenAddr: fmt.Sprintf("127.0.0.1:%d", wgPort),
|
||||||
WgPeerIp: e.wgIp,
|
WgPeerIP: e.wgIP,
|
||||||
WgIface: e.wgIface,
|
WgIface: e.wgIface,
|
||||||
WgAllowedIPs: peer.WgAllowedIps,
|
WgAllowedIPs: peer.WgAllowedIps,
|
||||||
WgKey: myKey,
|
WgKey: myKey,
|
||||||
@ -161,7 +166,9 @@ func signalAuth(uFrag string, pwd string, myKey wgtypes.Key, remoteKey wgtypes.K
|
|||||||
msg, err := signal.MarshalCredential(myKey, remoteKey, &signal.Credential{
|
msg, err := signal.MarshalCredential(myKey, remoteKey, &signal.Credential{
|
||||||
UFrag: uFrag,
|
UFrag: uFrag,
|
||||||
Pwd: pwd}, t)
|
Pwd: pwd}, t)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
err = s.Send(msg)
|
err = s.Send(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WgProxy an instance of an instance of the Connection Wireguard Proxy
|
||||||
type WgProxy struct {
|
type WgProxy struct {
|
||||||
iface string
|
iface string
|
||||||
remoteKey string
|
remoteKey string
|
||||||
@ -16,6 +17,7 @@ type WgProxy struct {
|
|||||||
wgConn net.Conn
|
wgConn net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewWgProxy creates a new Connection Wireguard Proxy
|
||||||
func NewWgProxy(iface string, remoteKey string, allowedIps string, wgAddr string) *WgProxy {
|
func NewWgProxy(iface string, remoteKey string, allowedIps string, wgAddr string) *WgProxy {
|
||||||
return &WgProxy{
|
return &WgProxy{
|
||||||
iface: iface,
|
iface: iface,
|
||||||
@ -26,6 +28,7 @@ func NewWgProxy(iface string, remoteKey string, allowedIps string, wgAddr string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes the proxy
|
||||||
func (p *WgProxy) Close() error {
|
func (p *WgProxy) Close() error {
|
||||||
|
|
||||||
close(p.close)
|
close(p.close)
|
||||||
@ -39,6 +42,7 @@ func (p *WgProxy) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start starts a new proxy using the ICE connection
|
||||||
func (p *WgProxy) Start(remoteConn *ice.Conn) error {
|
func (p *WgProxy) Start(remoteConn *ice.Conn) error {
|
||||||
|
|
||||||
wgConn, err := net.Dial("udp", p.wgAddr)
|
wgConn, err := net.Dial("udp", p.wgAddr)
|
||||||
@ -78,7 +82,7 @@ func (p *WgProxy) proxyToRemotePeer(remoteConn *ice.Conn) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err = remoteConn.Write(buf[:n])
|
_, err = remoteConn.Write(buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//log.Warnln("failed writing to remote peer: ", err.Error())
|
//log.Warnln("failed writing to remote peer: ", err.Error())
|
||||||
}
|
}
|
||||||
@ -102,7 +106,7 @@ func (p *WgProxy) proxyToLocalWireguard(remoteConn *ice.Conn) {
|
|||||||
//log.Errorf("failed reading from remote connection %s", err)
|
//log.Errorf("failed reading from remote connection %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err = p.wgConn.Write(buf[:n])
|
_, err = p.wgConn.Write(buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//log.Errorf("failed writing to local Wireguard instance %s", err)
|
//log.Errorf("failed writing to local Wireguard instance %s", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user