package wireguard import ( "io/fs" "log" "asciigoat.org/ini/basic" "darvaza.org/core" ) type sectionHandler func(*Config, *basic.Section) error var sectionMap = map[string]func(*Config, *basic.Section) error{ "Interface": loadInterfaceConfSection, "Peer": loadPeerConfSection, } func loadConfSection(out *Config, src *basic.Section) error { h, ok := sectionMap[src.Key] if !ok { return core.Wrap(fs.ErrInvalid, "unknown section %q", src.Key) } return h(out, src) } 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.Wrap(err, "Peer[%v]", len(out.Peer)) } } out.Peer = append(out.Peer, cfg) return nil } func configFieldHandler(vi any, key, value string) error func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error { log.Printf("%s[%q] = %q", "Interface", field.Key, field.Value) key, value := field.Key, field.Value switch key { case "Address": return configFieldHandler(&cfg.Address, key, value) case "PrivateKey": return configFieldHandler(&cfg.PrivateKey, key, value) case "ListenPort": return configFieldHandler(&cfg.ListenPort, key, value) default: return core.Wrap(fs.ErrInvalid, "unknown field %q", key) } } func loadPeerConfField(cfg *PeerConfig, field basic.Field) error { log.Printf("%s[%q] = %q", "Peer", field.Key, field.Value) key, value := field.Key, field.Value switch key { case "PublicKey": return configFieldHandler(&cfg.PublicKey, key, value) case "Endpoint": return configFieldHandler(&cfg.Endpoint, key, value) case "AllowedIPs": return configFieldHandler(&cfg.AllowedIPs, key, value) default: return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key) } } 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 }