From 091ea72c7a9231113c0c941279473e0ac90a33b1 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Fri, 2 Sep 2022 10:59:49 +0800 Subject: [PATCH] feat: startup dhcpd service --- cmd/controller.go | 33 +++++++++++++++++++++++++++++++++ cmd/dhcp/main.go | 19 +++++++++++++++++++ cmd/process/daemon.go | 1 + cmd/xproxy.go | 4 ++-- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/cmd/controller.go b/cmd/controller.go index 949b71d..56022ed 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -2,7 +2,9 @@ package main import ( "XProxy/cmd/asset" + "XProxy/cmd/common" "XProxy/cmd/config" + "XProxy/cmd/dhcp" "XProxy/cmd/network" "XProxy/cmd/process" "XProxy/cmd/proxy" @@ -13,6 +15,7 @@ import ( "path" "strconv" "syscall" + "time" ) func runProcess(env []string, command ...string) { @@ -36,6 +39,14 @@ func loadRadvd(settings *config.Config) { } } +func loadDhcp(settings *config.Config) { + common.CreateFolder(dhcp.WorkDir) + if settings.DHCP.IPv4.Enable || settings.DHCP.IPv6.Enable { + common.CreateFolder(path.Join(exposeDir, "dhcp")) + dhcp.Load(&settings.DHCP) + } +} + func loadAsset(settings *config.Config) { if settings.Asset.Disable { log.Infof("Skip loading asset") @@ -84,3 +95,25 @@ func runRadvd(settings *config.Config) { log.Infof("Skip running radvd") } } + +func runDhcp(settings *config.Config) { + leaseDir := path.Join(exposeDir, "dhcp") + if settings.DHCP.IPv4.Enable { + v4Leases := path.Join(leaseDir, "dhcp4.leases") + v4Config := path.Join(dhcp.WorkDir, "dhcp4.conf") + if !common.IsFileExist(v4Leases) { + common.WriteFile(v4Leases, "", true) + } + runProcess(nil, "dhcpd", "-4", "-f", "-cf", v4Config, "-lf", v4Leases) + time.Sleep(time.Second) // wait 1s for avoid cluttered output + } + if settings.DHCP.IPv6.Enable { + v6Leases := path.Join(leaseDir, "dhcp6.leases") + v6Config := path.Join(dhcp.WorkDir, "dhcp6.conf") + if !common.IsFileExist(v6Leases) { + common.WriteFile(v6Leases, "", true) + } + runProcess(nil, "dhcpd", "-6", "-f", "-cf", v6Config, "-lf", v6Leases) + time.Sleep(time.Second) // wait 1s for avoid cluttered output + } +} diff --git a/cmd/dhcp/main.go b/cmd/dhcp/main.go index 24874a6..d9a6472 100644 --- a/cmd/dhcp/main.go +++ b/cmd/dhcp/main.go @@ -1,5 +1,13 @@ package dhcp +import ( + "XProxy/cmd/common" + log "github.com/sirupsen/logrus" + "path" +) + +var WorkDir = "/etc/dhcp" + type dhcpConfig struct { Enable bool `yaml:"enable" json:"enable"` Configure string `yaml:"config" json:"config"` @@ -9,3 +17,14 @@ type Config struct { IPv4 dhcpConfig `yaml:"ipv4" json:"ipv4"` IPv6 dhcpConfig `yaml:"ipv6" json:"ipv6"` } + +func Load(config *Config) { + if config.IPv4.Enable { + log.Infof("Load DHCPv4 configure") + common.WriteFile(path.Join(WorkDir, "dhcp4.conf"), config.IPv4.Configure, true) + } + if config.IPv6.Enable { + log.Infof("Load DHCPv6 configure") + common.WriteFile(path.Join(WorkDir, "dhcp6.conf"), config.IPv6.Configure, true) + } +} diff --git a/cmd/process/daemon.go b/cmd/process/daemon.go index d1275f1..d3c8953 100644 --- a/cmd/process/daemon.go +++ b/cmd/process/daemon.go @@ -12,6 +12,7 @@ func daemonSub(sub *Process) { log.Warningf("Catch process %s exit", sub.name) time.Sleep(5 * time.Second) // delay 3s -> try to restart if !exitFlag { + log.Debugf("Process %s restart -> %v", sub.name, sub.command) sub.Run(true, sub.env) log.Infof("Process %s restart success", sub.name) daemonSub(sub) diff --git a/cmd/xproxy.go b/cmd/xproxy.go index 4f9cadf..1f97902 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -91,14 +91,14 @@ func main() { xproxyInit() var settings config.Config log.Infof("XProxy %s start (%s)", version, goVersion) - // TODO: load dhcp configure config.Load(configFile, &settings) loadNetwork(&settings) loadProxy(&settings) loadAsset(&settings) loadRadvd(&settings) + loadDhcp(&settings) custom.RunPreScript(&settings.Custom) - // TODO: run dhcp service + runDhcp(&settings) runRadvd(&settings) runProxy(&settings) blockWait()