Skip to content

Commit

Permalink
fix doc and lint warns for connection package
Browse files Browse the repository at this point in the history
  • Loading branch information
mlsmaycon committed May 15, 2021
1 parent e6358e7 commit f1cff0e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
15 changes: 12 additions & 3 deletions connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
)

var (
// DefaultWgKeepAlive default Wireguard keep alive constant
DefaultWgKeepAlive = 20 * time.Second
)

// ConnConfig Connection configuration struct
type ConnConfig struct {
// Local Wireguard listening address e.g. 127.0.0.1:51820
WgListenAddr string
// 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)
WgIface string
// Wireguard allowed IPs (e.g. 10.30.30.2/32)
Expand All @@ -31,11 +33,13 @@ type ConnConfig struct {
StunTurnURLS []*ice.URL
}

// IceCredentials ICE protocol credentials struct
type IceCredentials struct {
uFrag string
pwd string
}

// Connection Holds information about a connection and handles signal protocol
type Connection struct {
Config ConnConfig
// signalCandidate is a handler function to signal remote peer about local connection candidate
Expand All @@ -61,6 +65,7 @@ type Connection struct {
remoteAuthCond sync.Once
}

// NewConnection Creates a new connection and sets handling functions for signal protocol
func NewConnection(config ConnConfig,
signalCandidate func(candidate ice.Candidate) error,
signalOffer func(uFrag string, pwd string) error,
Expand Down Expand Up @@ -151,6 +156,7 @@ func (conn *Connection) Open(timeout time.Duration) error {
}
}

// Close Closes a peer connection
func (conn *Connection) Close() error {
var err error
conn.closeCond.Do(func() {
Expand All @@ -176,6 +182,7 @@ func (conn *Connection) Close() error {
return err
}

// OnAnswer Handles the answer from the other peer
func (conn *Connection) OnAnswer(remoteAuth IceCredentials) error {

conn.remoteAuthCond.Do(func() {
Expand All @@ -185,23 +192,25 @@ func (conn *Connection) OnAnswer(remoteAuth IceCredentials) error {
return nil
}

// OnOffer Handles the offer from the other peer
func (conn *Connection) OnOffer(remoteAuth IceCredentials) error {

conn.remoteAuthCond.Do(func() {
log.Debugf("OnOffer from peer %s", conn.Config.RemoteWgKey.String())
conn.remoteAuthChannel <- remoteAuth
uFrag, pwd, err := conn.agent.GetLocalUserCredentials()
if err != nil {
if err != nil { //nolint
}

err = conn.signalAnswer(uFrag, pwd)
if err != nil {
if err != nil { //nolint
}
})

return nil
}

// OnRemoteCandidate Handles remote candidate provided by the peer.
func (conn *Connection) OnRemoteCandidate(candidate ice.Candidate) error {

log.Debugf("onRemoteCandidate from peer %s -> %s", conn.Config.RemoteWgKey.String(), candidate.String())
Expand Down
17 changes: 12 additions & 5 deletions connection/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"
)

// Engine is an instance of the Connection Engine
type Engine struct {
// a list of STUN and TURN servers
stunsTurns []*ice.URL
Expand All @@ -22,27 +23,31 @@ type Engine struct {
// Wireguard interface
wgIface string
// Wireguard local address
wgIp string
wgIP string
}

// Peer is an instance of the Connection Peer
type Peer struct {
WgPubKey string
WgAllowedIps string
}

// NewEngine creates a new Connection Engine
func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgAddr string) *Engine {
return &Engine{
stunsTurns: stunsTurns,
signal: signal,
wgIface: wgIface,
wgIp: wgAddr,
wgIP: wgAddr,
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 {

err := iface.Create(e.wgIface, e.wgIp)
err := iface.Create(e.wgIface, e.wgIP)
if err != nil {
log.Errorf("error while creating interface %s: [%s]", e.wgIface, err.Error())
return err
Expand Down Expand Up @@ -102,7 +107,7 @@ func (e *Engine) openPeerConnection(wgPort int, myKey wgtypes.Key, peer Peer) (*
remoteKey, _ := wgtypes.ParseKey(peer.WgPubKey)
connConfig := &ConnConfig{
WgListenAddr: fmt.Sprintf("127.0.0.1:%d", wgPort),
WgPeerIp: e.wgIp,
WgPeerIP: e.wgIP,
WgIface: e.wgIface,
WgAllowedIPs: peer.WgAllowedIps,
WgKey: myKey,
Expand Down Expand Up @@ -161,7 +166,9 @@ func signalAuth(uFrag string, pwd string, myKey wgtypes.Key, remoteKey wgtypes.K
msg, err := signal.MarshalCredential(myKey, remoteKey, &signal.Credential{
UFrag: uFrag,
Pwd: pwd}, t)

if err != nil {
return err
}
err = s.Send(msg)
if err != nil {
return err
Expand Down
8 changes: 6 additions & 2 deletions connection/wgproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
)

// WgProxy an instance of an instance of the Connection Wireguard Proxy
type WgProxy struct {
iface string
remoteKey string
Expand All @@ -16,6 +17,7 @@ type WgProxy struct {
wgConn net.Conn
}

// NewWgProxy creates a new Connection Wireguard Proxy
func NewWgProxy(iface string, remoteKey string, allowedIps string, wgAddr string) *WgProxy {
return &WgProxy{
iface: iface,
Expand All @@ -26,6 +28,7 @@ func NewWgProxy(iface string, remoteKey string, allowedIps string, wgAddr string
}
}

// Close closes the proxy
func (p *WgProxy) Close() error {

close(p.close)
Expand All @@ -39,6 +42,7 @@ func (p *WgProxy) Close() error {
return nil
}

// Start starts a new proxy using the ICE connection
func (p *WgProxy) Start(remoteConn *ice.Conn) error {

wgConn, err := net.Dial("udp", p.wgAddr)
Expand Down Expand Up @@ -78,7 +82,7 @@ func (p *WgProxy) proxyToRemotePeer(remoteConn *ice.Conn) {
continue
}

n, err = remoteConn.Write(buf[:n])
_, err = remoteConn.Write(buf[:n])
if err != nil {
//log.Warnln("failed writing to remote peer: ", err.Error())
}
Expand All @@ -102,7 +106,7 @@ func (p *WgProxy) proxyToLocalWireguard(remoteConn *ice.Conn) {
//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 {
//log.Errorf("failed writing to local Wireguard instance %s", err)
}
Expand Down

0 comments on commit f1cff0e

Please sign in to comment.