basic UDP server bound to an interface
This commit is contained in:
15
config/BUILD.bazel
Normal file
15
config/BUILD.bazel
Normal file
@@ -0,0 +1,15 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "config",
|
||||
srcs = [
|
||||
"config.go",
|
||||
"path.go",
|
||||
],
|
||||
importpath = "git.wntrmute.dev/kyle/kdhcp/config",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//log",
|
||||
"@in_gopkg_yaml_v2//:yaml_v2",
|
||||
],
|
||||
)
|
||||
@@ -1,9 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"git.wntrmute.dev/kyle/kdhcp/log"
|
||||
"gopkg.in/yaml.v2"
|
||||
@@ -78,13 +80,15 @@ type ConfigFile struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Version int `yaml:"version"`
|
||||
Interface string `yaml:"interface"`
|
||||
Version int `yaml:"version"`
|
||||
Interface string `yaml:"interface"`
|
||||
Address string `yaml:"address"`
|
||||
IP net.IP
|
||||
Port int
|
||||
LeaseFile string `yaml:"lease_file"`
|
||||
Network *Network `yaml:"network"`
|
||||
Pools map[string]*IPRange `yaml:"pools"`
|
||||
Statics map[string]net.IP `yaml:"statics"`
|
||||
Device *net.Interface
|
||||
}
|
||||
|
||||
func (cfg *Config) process() (err error) {
|
||||
@@ -95,11 +99,31 @@ func (cfg *Config) process() (err error) {
|
||||
log.Warningf("config: Version is greater than the current version %d. The config may not behave as expected.", CurrentVersion)
|
||||
}
|
||||
|
||||
cfg.Device, err = net.InterfaceByName(cfg.Interface)
|
||||
_, err = net.InterfaceByName(cfg.Interface)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config: while looking up interface %s: %w", cfg.Interface, err)
|
||||
}
|
||||
|
||||
ip, port, err := net.SplitHostPort(cfg.Address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg.IP = net.ParseIP(ip)
|
||||
if cfg.IP == nil {
|
||||
return fmt.Errorf("config: parsing IP from address %s: %w", cfg.Address, err)
|
||||
}
|
||||
|
||||
cfg.IP, err = ensureV4(cfg.IP)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config: address %w", err)
|
||||
}
|
||||
|
||||
cfg.Port, err = strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config: invalid port %s: %w", port, err)
|
||||
}
|
||||
|
||||
err = cfg.Network.ensureV4()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -124,6 +148,14 @@ func (cfg *Config) process() (err error) {
|
||||
}
|
||||
|
||||
func Load(path string) (*Config, error) {
|
||||
if path == "" {
|
||||
path = FindConfigPath()
|
||||
}
|
||||
|
||||
if path == "" {
|
||||
return nil, errors.New("config: no config file path specified and couldn't find a valid config file path")
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config: loading %s: %w", path, err)
|
||||
@@ -143,5 +175,6 @@ func Load(path string) (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Debugf("config: read configuration from %s", path)
|
||||
return config, nil
|
||||
}
|
||||
|
||||
37
config/path.go
Normal file
37
config/path.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
|
||||
"git.wntrmute.dev/kyle/kdhcp/log"
|
||||
)
|
||||
|
||||
func FindConfigPath() string {
|
||||
paths := []string{"/home/kyle/code/kdhcp/kdhcpd.yaml"}
|
||||
user, err := user.Current()
|
||||
if err == nil {
|
||||
if homedir := user.HomeDir; homedir != "" {
|
||||
paths = append(paths, filepath.Join(homedir, ".config", "kdhcp", "kdhcpd.yaml"))
|
||||
}
|
||||
}
|
||||
|
||||
paths = append(paths, "/etc/kdhcp/kdhcpd.yaml")
|
||||
|
||||
for _, path := range paths {
|
||||
_, err = os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Debugf("config: while trying to stat config file at '%s': %s", path, err)
|
||||
continue
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user