Browse Source

feat: startup dhcpd service

v1.x.x
dnomd343 2 years ago
parent
commit
091ea72c7a
  1. 33
      cmd/controller.go
  2. 19
      cmd/dhcp/main.go
  3. 1
      cmd/process/daemon.go
  4. 4
      cmd/xproxy.go

33
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
}
}

19
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)
}
}

1
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)

4
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()

Loading…
Cancel
Save