|
|
|
@ -2,7 +2,7 @@ package wireguard
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"io/fs" |
|
|
|
|
"strconv" |
|
|
|
|
"log" |
|
|
|
|
|
|
|
|
|
"asciigoat.org/ini/basic" |
|
|
|
|
"darvaza.org/core" |
|
|
|
@ -50,104 +50,40 @@ func loadPeerConfSection(out *Config, src *basic.Section) error {
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// revive:disable:cyclomatic
|
|
|
|
|
// revive:disable:cognitive-complexity
|
|
|
|
|
func configFieldHandler(vi any, key, value string) error |
|
|
|
|
|
|
|
|
|
func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error { |
|
|
|
|
// revive:enable:cyclomatic
|
|
|
|
|
// revive:enable:cognitive-complexity
|
|
|
|
|
log.Printf("%s[%q] = %q", "Interface", field.Key, field.Value) |
|
|
|
|
|
|
|
|
|
// TODO: refactor when asciigoat's ini parser learns to do reflection
|
|
|
|
|
switch field.Key { |
|
|
|
|
case "Address": |
|
|
|
|
if !core.IsZero(cfg.Address) { |
|
|
|
|
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key) |
|
|
|
|
} |
|
|
|
|
key, value := field.Key, field.Value |
|
|
|
|
|
|
|
|
|
err := cfg.Address.UnmarshalText([]byte(field.Value)) |
|
|
|
|
switch { |
|
|
|
|
case err != nil: |
|
|
|
|
return core.Wrap(err, field.Key) |
|
|
|
|
default: |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
switch key { |
|
|
|
|
case "Address": |
|
|
|
|
return configFieldHandler(&cfg.Address, key, value) |
|
|
|
|
case "PrivateKey": |
|
|
|
|
if !core.IsZero(cfg.PrivateKey) { |
|
|
|
|
return core.Wrap(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 |
|
|
|
|
} |
|
|
|
|
return configFieldHandler(&cfg.PrivateKey, key, value) |
|
|
|
|
case "ListenPort": |
|
|
|
|
if cfg.ListenPort > 0 { |
|
|
|
|
return core.Wrap(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.Wrap(fs.ErrInvalid, "invalid %q value", field.Key) |
|
|
|
|
default: |
|
|
|
|
cfg.ListenPort = uint16(u64) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
return configFieldHandler(&cfg.ListenPort, key, value) |
|
|
|
|
default: |
|
|
|
|
return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key) |
|
|
|
|
return core.Wrap(fs.ErrInvalid, "unknown field %q", key) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// revive:disable:cyclomatic
|
|
|
|
|
// revive:disable:cognitive-complexity
|
|
|
|
|
|
|
|
|
|
func loadPeerConfField(cfg *PeerConfig, field basic.Field) error { |
|
|
|
|
// revive:enable:cyclomatic
|
|
|
|
|
// revive:enable:cognitive-complexity
|
|
|
|
|
log.Printf("%s[%q] = %q", "Peer", field.Key, field.Value) |
|
|
|
|
|
|
|
|
|
switch field.Key { |
|
|
|
|
case "PublicKey": |
|
|
|
|
if !core.IsZero(cfg.PublicKey) { |
|
|
|
|
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key) |
|
|
|
|
} |
|
|
|
|
key, value := field.Key, field.Value |
|
|
|
|
|
|
|
|
|
err := cfg.PublicKey.UnmarshalText([]byte(field.Value)) |
|
|
|
|
switch { |
|
|
|
|
case err != nil: |
|
|
|
|
return core.Wrap(err, field.Key) |
|
|
|
|
default: |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
switch key { |
|
|
|
|
case "PublicKey": |
|
|
|
|
return configFieldHandler(&cfg.PublicKey, key, value) |
|
|
|
|
case "Endpoint": |
|
|
|
|
if cfg.Endpoint.String() != "" { |
|
|
|
|
return core.Wrap(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 |
|
|
|
|
} |
|
|
|
|
return configFieldHandler(&cfg.Endpoint, key, value) |
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
return configFieldHandler(&cfg.AllowedIPs, key, value) |
|
|
|
|
default: |
|
|
|
|
return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key) |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newConfigFromDocument(doc *basic.Document) (*Config, error) { |
|
|
|
|