From 9b55383b19439619708a904b7d82aef706f2340f Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 20 Aug 2022 14:11:13 +0800 Subject: [PATCH] feat: log level option for radvd --- cmd/config/decode.go | 1 + cmd/controller.go | 11 ++++++++++- cmd/process/daemon.go | 4 ++-- cmd/radvd/radvd.go | 27 ++++++++++++++------------- cmd/xproxy.go | 2 +- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/cmd/config/decode.go b/cmd/config/decode.go index e8318ce..98ddbec 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -106,6 +106,7 @@ func decodeProxy(rawConfig *yamlConfig, config *Config) { func decodeRadvd(rawConfig *yamlConfig, config *Config) { config.Radvd = rawConfig.Radvd + log.Debugf("Radvd log level -> %d", config.Radvd.Log) log.Debugf("Radvd enable -> %t", config.Radvd.Enable) log.Debugf("Radvd options -> %v", config.Radvd.Option) log.Debugf("Radvd prefix -> %v", config.Radvd.Prefix) diff --git a/cmd/controller.go b/cmd/controller.go index a5685f9..f693a73 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -12,7 +12,9 @@ import ( "os" "os/signal" "path" + "strconv" "syscall" + "time" ) func runProcess(command ...string) { @@ -72,6 +74,13 @@ func runProxy(settings *config.Config) { func runRadvd(settings *config.Config) { if settings.Radvd.Enable { - runProcess("radvd", "-n", "-m", "logfile", "-l", path.Join(exposeDir, "log/radvd.log")) + radvdCmd := []string{"radvd", "--nodaemon"} + if settings.Radvd.Log > 0 { // with log option + radvdCmd = append(radvdCmd, "--logmethod", "logfile") + radvdCmd = append(radvdCmd, "--logfile", path.Join(exposeDir, "log/radvd.log")) + radvdCmd = append(radvdCmd, "--debug", strconv.Itoa(settings.Radvd.Log)) + time.Sleep(time.Second) // radvd will crash on first boot without delay (enable debug), why??? + } + runProcess(radvdCmd...) } } diff --git a/cmd/process/daemon.go b/cmd/process/daemon.go index 19fe324..a05d6da 100644 --- a/cmd/process/daemon.go +++ b/cmd/process/daemon.go @@ -10,8 +10,8 @@ func daemonSub(sub *Process) { sub.Wait() } log.Warningf("Catch process %s exit", sub.name) - time.Sleep(3 * time.Second) // delay 3s - if !exitFlag { // not in exit process + time.Sleep(3 * time.Second) // delay 3s -> try to restart + if !exitFlag { sub.Run(true) log.Infof("Process %s restart success", sub.name) daemonSub(sub) diff --git a/cmd/radvd/radvd.go b/cmd/radvd/radvd.go index eb394c1..921cbe3 100644 --- a/cmd/radvd/radvd.go +++ b/cmd/radvd/radvd.go @@ -7,6 +7,7 @@ import ( ) type Config struct { + Log int `yaml:"log" json:"log"` Enable bool `yaml:"enable" json:"enable"` Client []string `yaml:"client" json:"client"` Option map[string]string `yaml:"option" json:"option"` @@ -40,9 +41,9 @@ func loadOption(options map[string]string, intend int) string { // load options return ret } -func loadClient(clients []string) string { - if len(clients) == 0 { // without client settings - return "" +func loadClient(clients []string) string { // load radvd client configure + if len(clients) == 0 { + return "" // without client settings } ret := genSpace(4) + "clients {\n" for _, client := range clients { @@ -52,32 +53,32 @@ func loadClient(clients []string) string { } func loadPrefix(prefix string, option map[string]string) string { // load radvd prefix configure - if prefix == "" { // without prefix settings - return "" + if prefix == "" { + return "" // without prefix settings } header := genSpace(4) + "prefix " + prefix + " {\n" return header + loadOption(option, 8) + genSpace(4) + "};\n" } func loadRoute(cidr string, option map[string]string) string { // load radvd route configure - if cidr == "" { // without route settings - return "" + if cidr == "" { + return "" // without route settings } header := genSpace(4) + "route " + cidr + " {\n" return header + loadOption(option, 8) + genSpace(4) + "};\n" } -func loadRdnss(ip []string, option map[string]string) string { - if len(ip) == 0 { // without rdnss settings - return "" +func loadRdnss(ip []string, option map[string]string) string { // load radvd RDNSS configure + if len(ip) == 0 { + return "" // without rdnss settings } header := genSpace(4) + "RDNSS " + strings.Join(ip, " ") + " {\n" return header + loadOption(option, 8) + genSpace(4) + "};\n" } -func loadDnssl(suffix []string, option map[string]string) string { - if len(suffix) == 0 { // without dnssl settings - return "" +func loadDnssl(suffix []string, option map[string]string) string { // load radvd DNSSL configure + if len(suffix) == 0 { + return "" // without dnssl settings } header := genSpace(4) + "DNSSL " + strings.Join(suffix, " ") + " {\n" return header + loadOption(option, 8) + genSpace(4) + "};\n" diff --git a/cmd/xproxy.go b/cmd/xproxy.go index 72129cf..103d04e 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -79,8 +79,8 @@ func main() { loadRadvd(&settings) runScript(&settings) - runProxy(&settings) runRadvd(&settings) + runProxy(&settings) blockWait() process.Exit(subProcess...) }