From b7d902261e4858f49251cea38a8779c65d6fad8d Mon Sep 17 00:00:00 2001 From: lonord Date: Mon, 18 Jun 2018 20:56:25 +0800 Subject: [PATCH] bugs fix --- app/webservice.go | 2 +- base/config.go | 2 +- base/util.go | 8 ++++++++ bridge/bridge.go | 3 +++ build.sh | 15 ++++++--------- client/clientutil.go | 6 +++++- dnsmasq/dnsmasq.go | 43 ++++++++++++++++++++++++++++++++++++++++--- forward/forward.go | 18 ++++++++++-------- main.go | 1 + 9 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 base/util.go diff --git a/app/webservice.go b/app/webservice.go index 8cfdeb9..001444d 100644 --- a/app/webservice.go +++ b/app/webservice.go @@ -29,7 +29,7 @@ func NewWebService(act *MainAction, cfg *ba.Config) *WebService { } func (s *WebService) Start() { - addr := fmt.Sprintf(s.cfg.RPCHost, ":", s.cfg.RPCPort) + addr := fmt.Sprintf("%s:%d", s.cfg.RPCHost, s.cfg.RPCPort) go func() { log.Println("server listens at http://", addr) if err := s.e.Start(addr); err != nil { diff --git a/base/config.go b/base/config.go index d55cd8c..2c23de5 100644 --- a/base/config.go +++ b/base/config.go @@ -34,6 +34,6 @@ func ReadConfig(readConfig ConfigReaderFn) (*Config, error) { if cfg.RPCPort == 0 { cfg.RPCPort = 2018 } - log.Println("config readed: ", cfg) + log.Printf("config readed: %+v", cfg) return &cfg, nil } diff --git a/base/util.go b/base/util.go new file mode 100644 index 0000000..10c0ee4 --- /dev/null +++ b/base/util.go @@ -0,0 +1,8 @@ +package ba + +import "strings" + +func GetSubnetPrefix(ipAddr string) string { + ipChunks := strings.Split(ipAddr, ".") + return strings.Join([]string{ipChunks[0], ipChunks[1], ipChunks[2]}, ".") +} diff --git a/bridge/bridge.go b/bridge/bridge.go index bd54d82..5219859 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -2,6 +2,7 @@ package bridge import ( "fmt" + "log" "regexp" "strings" @@ -36,6 +37,7 @@ func (b *Bridge) SetupBridge() error { if err != nil { return err } + log.Println("bridge setted up") return nil } @@ -48,6 +50,7 @@ func (b *Bridge) ClearBridge() error { if bridge != nil { dealDeleteBridge(b.execFn, bridge) } + log.Println("bridge cleared") return nil } diff --git a/build.sh b/build.sh index 95dd25a..bd7a572 100755 --- a/build.sh +++ b/build.sh @@ -19,23 +19,20 @@ gobuild() { } build_deb() { - fpm -s dir -t deb -p $1 -n router-service -v ${VERSION} -d "bridge-utils (> 0)" -d "iptables (> 0)" -d "dnsmasq (> 0)" -d "net-tools (> 0)" -d "net-tools (> 0)" -C dist/tmp . + gobuild + rm -rf dist/linux/$1 + mkdir -p dist/linux/$1 + fpm -s dir -t deb -a $1 -p dist/linux/$1/router-service_v${VERSION}_$1.deb -n router-service -v ${VERSION} -d "bridge-utils (> 0)" -d "iptables (> 0)" -d "dnsmasq (> 0)" -d "net-tools (> 0)" -C dist/tmp . } # build deb x64 export CGO_ENABLED=0 export GOOS=linux export GOARCH=amd64 -gobuild -rm -rf dist/linux/x64 -mkdir -p dist/linux/x64 -build_deb dist/linux/x64/router-service_v${VERSION}_amd64.deb +build_deb amd64 # build deb armhf export CGO_ENABLED=0 export GOOS=linux export GOARCH=arm -gobuild -rm -rf dist/linux/armhf -mkdir -p dist/linux/armhf -build_deb dist/linux/armhf/router-service_v${VERSION}_armhf.deb +build_deb armhf diff --git a/client/clientutil.go b/client/clientutil.go index 919cdc8..f28b0a8 100644 --- a/client/clientutil.go +++ b/client/clientutil.go @@ -34,9 +34,13 @@ func ReadClients(execFn ba.CmdExecutorFn, cfg *ba.Config) ([]ClientInfo, error) if !strings.Contains(chunks[3], ":") { continue } + ip := chunks[1][1 : len(chunks[1])-1] + if !strings.HasPrefix(ip, ba.GetSubnetPrefix(cfg.BridgeAddr)) { + continue + } client := ClientInfo{ HostName: chunks[0], - IPAddr: chunks[1][1 : len(chunks[1])-1], + IPAddr: ip, MACAddr: chunks[3], } clients = append(clients, client) diff --git a/dnsmasq/dnsmasq.go b/dnsmasq/dnsmasq.go index a00e1e5..7af3951 100644 --- a/dnsmasq/dnsmasq.go +++ b/dnsmasq/dnsmasq.go @@ -2,8 +2,11 @@ package dnsmasq import ( "fmt" + "log" "os/exec" "strings" + "sync" + "syscall" "../base" ) @@ -12,27 +15,50 @@ type DnsmasqProcess struct { cfg *ba.Config proc *exec.Cmd fileReaderFn ba.FileReaderFn + statusLock sync.RWMutex + running bool + stopChan chan bool } func NewDnsmasqProcess(reader ba.FileReaderFn, c *ba.Config) *DnsmasqProcess { return &DnsmasqProcess{ cfg: c, fileReaderFn: reader, + running: false, } } func (p *DnsmasqProcess) Start() error { if !p.isRunning() { + p.stopChan = make(chan bool) internalArgs := collectInternalArgs(p.fileReaderFn, p.cfg) args := make([]string, len(internalArgs)+len(p.cfg.DnsmasqArgs)) - copy(internalArgs, args) - copy(p.cfg.DnsmasqArgs, args[len(internalArgs):]) + copy(args, internalArgs) + copy(args[len(internalArgs):], p.cfg.DnsmasqArgs) + log.Printf("run dnsmasq with args: %v", args) cmd := exec.Command("dnsmasq", args...) err := ba.ExecPipeCmd(cmd) if err != nil { return err } p.proc = cmd + p.setRunning(true) + log.Println("dnsmasq process started") + go func() { + err := cmd.Wait() + code := cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() + if code != 0 { + if err != nil { + log.Printf("dnsmasq process exited %d with error: %v", code, err) + } else { + log.Printf("dnsmasq process exited with code %d", code) + } + } else { + log.Printf("dnsmasq process exited") + } + p.setRunning(false) + close(p.stopChan) + }() } return nil } @@ -44,6 +70,9 @@ func (p *DnsmasqProcess) Stop() error { return err } p.proc = nil + if p.stopChan != nil { + <-p.stopChan + } } return nil } @@ -61,7 +90,15 @@ func (p *DnsmasqProcess) Restart() error { } func (p *DnsmasqProcess) isRunning() bool { - return p.proc != nil && !p.proc.ProcessState.Exited() + p.statusLock.RLock() + defer p.statusLock.RUnlock() + return p.running +} + +func (p *DnsmasqProcess) setRunning(r bool) { + p.statusLock.Lock() + defer p.statusLock.Unlock() + p.running = r } func collectInternalArgs(fileReaderFn ba.FileReaderFn, c *ba.Config) []string { diff --git a/forward/forward.go b/forward/forward.go index 0b3b090..408ddaa 100644 --- a/forward/forward.go +++ b/forward/forward.go @@ -2,6 +2,7 @@ package forward import ( "fmt" + "log" "strings" "../base" @@ -26,6 +27,7 @@ func (f *Forward) SetupForward() error { if err != nil { return err } + log.Println("iptables rules setted up") return nil } @@ -34,13 +36,14 @@ func (f *Forward) ClearForward() error { if err != nil { return err } + log.Println("iptables rules cleared") return nil } func addIptablesRule(execFn ba.CmdExecutorFn, c *ba.Config) error { - _, err1 := execFn(fmt.Sprint("iptables -t nat -A ", generateNatRule(c))) - _, err2 := execFn(fmt.Sprint("iptables -I ", generateForwardSourceRule(c))) - _, err3 := execFn(fmt.Sprint("iptables -I ", generateForwardDestinationRule(c))) + _, err1 := execFn(fmt.Sprint("iptables -t nat -A", generateNatRule(c))) + _, err2 := execFn(fmt.Sprint("iptables -I", generateForwardSourceRule(c))) + _, err3 := execFn(fmt.Sprint("iptables -I", generateForwardDestinationRule(c))) if err1 != nil { return err1 } @@ -54,9 +57,9 @@ func addIptablesRule(execFn ba.CmdExecutorFn, c *ba.Config) error { } func deleteIptablesRule(execFn ba.CmdExecutorFn, c *ba.Config) error { - _, err1 := execFn(fmt.Sprint("iptables -t nat -D ", generateNatRule(c))) - _, err2 := execFn(fmt.Sprint("iptables -D ", generateForwardSourceRule(c))) - _, err3 := execFn(fmt.Sprint("iptables -D ", generateForwardDestinationRule(c))) + _, err1 := execFn(fmt.Sprint("iptables -t nat -D", generateNatRule(c))) + _, err2 := execFn(fmt.Sprint("iptables -D", generateForwardSourceRule(c))) + _, err3 := execFn(fmt.Sprint("iptables -D", generateForwardDestinationRule(c))) if err1 != nil { return err1 } @@ -82,6 +85,5 @@ func generateForwardDestinationRule(c *ba.Config) string { } func dealWithIPNetmask(ip string) string { - ipChunks := strings.Split(ip, ".") - return strings.Join([]string{ipChunks[0], ipChunks[1], ipChunks[2], "0/24"}, ".") + return strings.Join([]string{ba.GetSubnetPrefix(ip), ".0/24"}, "") } diff --git a/main.go b/main.go index d7b9925..3768ebc 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,7 @@ func run() error { case syscall.SIGTERM: log.Println("got signal ", sig.String()) stopService(ctx) + return nil } } }