You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
3.6 KiB
163 lines
3.6 KiB
12 months ago
|
package wireguard
|
||
|
|
||
|
import (
|
||
|
"io/fs"
|
||
|
"strconv"
|
||
|
|
||
|
"asciigoat.org/ini/basic"
|
||
|
"darvaza.org/core"
|
||
|
)
|
||
|
|
||
|
func loadConfSection(out *Config, src *basic.Section) error {
|
||
|
switch src.Key {
|
||
|
case "Interface":
|
||
|
return loadInterfaceConfSection(out, src)
|
||
|
case "Peer":
|
||
|
return loadPeerConfSection(out, src)
|
||
|
default:
|
||
|
return core.Wrapf(fs.ErrInvalid, "unknown section %q", src.Key)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func loadInterfaceConfSection(out *Config, src *basic.Section) error {
|
||
|
var cfg InterfaceConfig
|
||
|
|
||
|
for _, field := range src.Fields {
|
||
|
if err := loadInterfaceConfField(&cfg, field); err != nil {
|
||
|
return core.Wrap(err, "Interface")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
out.Interface = cfg
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func loadPeerConfSection(out *Config, src *basic.Section) error {
|
||
|
var cfg PeerConfig
|
||
|
|
||
|
for _, field := range src.Fields {
|
||
|
if err := loadPeerConfField(&cfg, field); err != nil {
|
||
|
return core.Wrapf(err, "Peer[%v]", len(out.Peer))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
out.Peer = append(out.Peer, cfg)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// revive:disable:cyclomatic
|
||
|
// revive:disable:cognitive-complexity
|
||
|
|
||
|
func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error {
|
||
|
// revive:enable:cyclomatic
|
||
|
// revive:enable:cognitive-complexity
|
||
|
switch field.Key {
|
||
|
case "Address":
|
||
|
if !core.IsZero(cfg.Address) {
|
||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||
|
}
|
||
|
|
||
|
err := cfg.Address.UnmarshalText([]byte(field.Value))
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return core.Wrap(err, field.Key)
|
||
|
default:
|
||
|
return nil
|
||
|
}
|
||
|
case "PrivateKey":
|
||
|
if !core.IsZero(cfg.PrivateKey) {
|
||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||
|
}
|
||
|
|
||
|
err := cfg.PrivateKey.UnmarshalText([]byte(field.Value))
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return core.Wrap(err, field.Key)
|
||
|
default:
|
||
|
return nil
|
||
|
}
|
||
|
case "ListenPort":
|
||
|
if cfg.ListenPort > 0 {
|
||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||
|
}
|
||
|
|
||
|
u64, err := strconv.ParseUint(field.Value, 10, 16)
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return core.Wrap(err, field.Key)
|
||
|
case u64 == 0:
|
||
|
return core.Wrapf(fs.ErrInvalid, "invalid %q value", field.Key)
|
||
|
default:
|
||
|
cfg.ListenPort = uint16(u64)
|
||
|
return nil
|
||
|
}
|
||
|
default:
|
||
|
return core.Wrapf(fs.ErrInvalid, "unknown field %q", field.Key)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// revive:disable:cyclomatic
|
||
|
// revive:disable:cognitive-complexity
|
||
|
|
||
|
func loadPeerConfField(cfg *PeerConfig, field basic.Field) error {
|
||
|
// revive:enable:cyclomatic
|
||
|
// revive:enable:cognitive-complexity
|
||
|
|
||
|
switch field.Key {
|
||
|
case "PublicKey":
|
||
|
if !core.IsZero(cfg.PublicKey) {
|
||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||
|
}
|
||
|
|
||
|
err := cfg.PublicKey.UnmarshalText([]byte(field.Value))
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return core.Wrap(err, field.Key)
|
||
|
default:
|
||
|
return nil
|
||
|
}
|
||
|
case "Endpoint":
|
||
|
if cfg.Endpoint.String() != "" {
|
||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||
|
}
|
||
|
|
||
|
err := cfg.Endpoint.UnmarshalText([]byte(field.Value))
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return core.Wrap(err, field.Key)
|
||
|
default:
|
||
|
return nil
|
||
|
}
|
||
|
case "AllowedIPs":
|
||
|
s, err := parseAllowedIPs(field.Value)
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return core.Wrap(err, field.Key)
|
||
|
case len(s) > 0:
|
||
|
cfg.AllowedIPs = append(cfg.AllowedIPs, s...)
|
||
|
return nil
|
||
|
}
|
||
|
default:
|
||
|
return core.Wrapf(fs.ErrInvalid, "unknown field %q", field.Key)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func newConfigFromDocument(doc *basic.Document) (*Config, error) {
|
||
|
var out Config
|
||
|
|
||
|
if len(doc.Global) > 0 {
|
||
|
err := core.Wrap(fs.ErrInvalid, "fields before the first section")
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
for i := range doc.Sections {
|
||
|
src := &doc.Sections[i]
|
||
|
if err := loadConfSection(&out, src); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return &out, nil
|
||
|
}
|