netbird/cmd/config.go

58 lines
940 B
Go
Raw Normal View History

package cmd
2021-04-14 14:20:25 +02:00
2021-04-18 13:47:53 +02:00
import (
"encoding/json"
"github.com/pion/ice/v2"
"github.com/wiretrustee/wiretrustee/connection"
"io/ioutil"
"os"
)
2021-04-14 14:20:25 +02:00
type Config struct {
// Wireguard private key of local peer
2021-04-18 15:54:10 +02:00
PrivateKey string
2021-04-18 13:47:53 +02:00
Peers []connection.Peer
StunTurnURLs []*ice.URL
2021-04-14 14:20:25 +02:00
// host:port of the signal server
SignalAddr string
WgAddr string
WgIface string
2021-04-14 14:20:25 +02:00
}
2021-04-18 13:47:53 +02:00
//Write writes configPath to a file
func (cfg *Config) Write(path string) error {
bs, err := json.Marshal(cfg)
if err != nil {
return err
}
err = ioutil.WriteFile(path, bs, 0600)
if err != nil {
return err
}
return nil
}
//Read reads configPath from a file
func Read(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
bs, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var cfg Config
err = json.Unmarshal(bs, &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}