feat: introduce config file

This commit is contained in:
braginini
2021-04-18 13:47:53 +02:00
parent 63febbde5b
commit ead16a35c9
4 changed files with 77 additions and 82 deletions
+6 -5
View File
@@ -11,17 +11,18 @@ import (
)
var (
DefaultAllowedIps = "0.0.0.0/0"
DefaultWgKeepAlive = 20 * time.Second
)
type Config 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
// Local Wireguard Interface name (e.g. wg0)
WgIface string
// Wireguard allowed IPs (e.g. 10.30.30.2/32)
WgAllowedIPs string
// Local Wireguard private key
WgKey wgtypes.Key
// Remote Wireguard public key
@@ -37,7 +38,7 @@ type IceCredentials struct {
}
type Connection struct {
Config Config
Config ConnConfig
// signalCandidate is a handler function to signal remote peer about local connection candidate
signalCandidate func(candidate ice.Candidate) error
@@ -58,7 +59,7 @@ type Connection struct {
wgConn net.Conn
}
func NewConnection(config Config,
func NewConnection(config ConnConfig,
signalCandidate func(candidate ice.Candidate) error,
signalOffer func(uFrag string, pwd string) error,
signalAnswer func(uFrag string, pwd string) error,
@@ -287,7 +288,7 @@ func (conn *Connection) createWireguardProxy() (*net.Conn, error) {
return nil, err
}
// add local proxy connection as a Wireguard peer
err = iface.UpdatePeer(conn.Config.WgIface, conn.Config.RemoteWgKey.String(), DefaultAllowedIps, DefaultWgKeepAlive,
err = iface.UpdatePeer(conn.Config.WgIface, conn.Config.RemoteWgKey.String(), conn.Config.WgAllowedIPs, DefaultWgKeepAlive,
wgConn.LocalAddr().String())
if err != nil {
log.Errorf("error while configuring Wireguard peer [%s] %s", conn.Config.RemoteWgKey.String(), err.Error())
+9 -3
View File
@@ -23,6 +23,11 @@ type Engine struct {
wgIp string
}
type Peer struct {
WgPubKey string
WgAllowedIps string
}
func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgAddr string) *Engine {
return &Engine{
stunsTurns: stunsTurns,
@@ -33,7 +38,7 @@ func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgA
}
}
func (e *Engine) Start(privateKey string, peers []string) error {
func (e *Engine) Start(privateKey string, peers []Peer) error {
// setup wireguard
myKey, err := wgtypes.ParseKey(privateKey)
@@ -65,11 +70,12 @@ func (e *Engine) Start(privateKey string, peers []string) error {
// initialize peer agents
for _, peer := range peers {
remoteKey, _ := wgtypes.ParseKey(peer)
connConfig := &Config{
remoteKey, _ := wgtypes.ParseKey(peer.WgPubKey)
connConfig := &ConnConfig{
WgListenAddr: fmt.Sprintf("127.0.0.1:%d", *wgPort),
WgPeerIp: e.wgIp,
WgIface: e.wgIface,
WgAllowedIPs: peer.WgAllowedIps,
WgKey: myKey,
RemoteWgKey: remoteKey,
StunTurnURLS: e.stunsTurns,