From 41b50a08d4b219526a495c09ed4e5f76a91d6a05 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Sat, 19 Jun 2021 14:55:45 +0200 Subject: [PATCH 01/21] feature: Adding service run command --- cmd/root.go | 2 ++ cmd/service.go | 26 +++++++++++++++++++++ cmd/service_run.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/up.go | 7 +++++- go.mod | 3 +-- go.sum | 11 +++------ 6 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 cmd/service.go create mode 100644 cmd/service_run.go diff --git a/cmd/root.go b/cmd/root.go index c499a34e33f..05918688a5d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,6 +36,8 @@ func init() { rootCmd.AddCommand(addPeerCmd) rootCmd.AddCommand(upCmd) rootCmd.AddCommand(signalCmd) + rootCmd.AddCommand(serviceCmd) + serviceCmd.AddCommand(runCmd) // run is a subcommand of service } // SetupCloseHandler handles SIGTERM signal and exits with success diff --git a/cmd/service.go b/cmd/service.go new file mode 100644 index 00000000000..397899123d1 --- /dev/null +++ b/cmd/service.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "github.com/kardianos/service" + "github.com/spf13/cobra" +) + +func newSVCConfig() *service.Config { + return &service.Config{ + Name: "wiretrustee", + DisplayName: "wiretrustee", + Description: "This is an example Go service.", + } +} + +var ( + serviceCmd = &cobra.Command{ + Use: "service", + Short: "manages wiretrustee service", + //Run: func(cmd *cobra.Command, args []string) { + //}, + } +) + +func init() { +} diff --git a/cmd/service_run.go b/cmd/service_run.go new file mode 100644 index 00000000000..b524a859476 --- /dev/null +++ b/cmd/service_run.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "github.com/kardianos/service" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +type program struct { + cmd *cobra.Command + args []string +} + +var logger service.Logger + +func (p *program) Start(s service.Service) error { + // Start should not block. Do the actual work async. + logger.Info("Starting service") //nolint + go upCmd.Run(p.cmd, p.args) + return nil +} + +func (p *program) Stop(s service.Service) error { + stopUP <- 1 + return nil +} + +var ( + runCmd = &cobra.Command{ + Use: "run", + Short: "runs wiretrustee as service", + Run: func(cmd *cobra.Command, args []string) { + + svcConfig := newSVCConfig() + + prg := &program{ + cmd: cmd, + args: args, + } + s, err := service.New(prg, svcConfig) + if err != nil { + log.Fatal(err) + } + logger, err = s.Logger(nil) + if err != nil { + log.Fatal(err) + } + + err = s.Run() + if err != nil { + logger.Error(err) //nolint + } + }, + } +) + +func init() { +} diff --git a/cmd/up.go b/cmd/up.go index bb94dbff7f5..2646c8fd30b 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -48,10 +48,15 @@ var ( //signalClient.WaitConnected() SetupCloseHandler() - select {} + <-stopUP + log.Println("Receive signal to stop running") }, } ) +// Execution control channel for stopUP signal +var stopUP chan int + func init() { + stopUP = make(chan int) } diff --git a/go.mod b/go.mod index 1b3a90f855f..0bf432b05ef 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,11 @@ go 1.16 require ( github.com/cenkalti/backoff/v4 v4.1.0 github.com/golang/protobuf v1.4.3 - github.com/google/nftables v0.0.0-20201230142148-715e31cb3c31 + github.com/kardianos/service v1.2.0 github.com/pion/ice/v2 v2.1.7 github.com/sirupsen/logrus v1.7.0 github.com/spf13/cobra v1.1.3 github.com/vishvananda/netlink v1.1.0 - github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf golang.zx2c4.com/wireguard v0.0.0-20210604143328-f9b48a961cd2 golang.zx2c4.com/wireguard/wgctrl v0.0.0-20210506160403-92e472f520a5 diff --git a/go.sum b/go.sum index 4e259b40e77..b23c3b1cc89 100644 --- a/go.sum +++ b/go.sum @@ -80,8 +80,6 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= -github.com/google/nftables v0.0.0-20201230142148-715e31cb3c31 h1:kyEB9geFhgDyawmvavtNu9iGW9ri/iq54XTSNIEeHxI= -github.com/google/nftables v0.0.0-20201230142148-715e31cb3c31/go.mod h1:cfspEyr/Ap+JDIITA+N9a0ernqG0qZ4W1aqMRgDZa1g= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= @@ -131,10 +129,10 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kardianos/service v1.2.0 h1:bGuZ/epo3vrt8IPC7mnKQolqFeYJb7Cs8Rk4PSOBB/g= +github.com/kardianos/service v1.2.0/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/koneu/natend v0.0.0-20150829182554-ec0926ea948d h1:MFX8DxRnKMY/2M3H61iSsVbo/n3h0MWGmWNN1UViOU0= -github.com/koneu/natend v0.0.0-20150829182554-ec0926ea948d/go.mod h1:QHb4k4cr1fQikUahfcRVPcEXiUgFsdIstGqlurL0XL4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -151,7 +149,6 @@ github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lk github.com/mdlayher/genetlink v1.0.0 h1:OoHN1OdyEIkScEmRgxLEe2M9U8ClMytqA5niynLtfj0= github.com/mdlayher/genetlink v1.0.0/go.mod h1:0rJ0h4itni50A86M2kHcgS85ttZazNt7a8H2a2cw0Gc= github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= -github.com/mdlayher/netlink v0.0.0-20191009155606-de872b0d824b/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.1.0/go.mod h1:H4WCitaheIsdF9yOYu8CFmCgQthAPIWZmcKp9uZHgmY= github.com/mdlayher/netlink v1.1.1/go.mod h1:WTYpFb/WTvlRJAyKhZL5/uy69TDDpHHu2VZmb2XgV7o= @@ -246,7 +243,6 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69 github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= -github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= @@ -301,7 +297,6 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191007182048-72f939374954/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= @@ -341,11 +336,11 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191029155521-f43be2a4598c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201118182958-a01c418693c7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= From 06c7af058b63cb2156632cbf70091831826e9d41 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Sun, 20 Jun 2021 23:01:12 +0200 Subject: [PATCH 02/21] Create config dir if using default configPath --- cmd/config.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd/config.go b/cmd/config.go index 4bbef1c8d7c..1754e41c801 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -6,6 +6,7 @@ import ( "github.com/wiretrustee/wiretrustee/connection" "io/ioutil" "os" + "path/filepath" ) // Config Configuration type @@ -23,6 +24,15 @@ type Config struct { //Write writes configPath to a file func (cfg *Config) Write(path string) error { + + if path == defaultConfigPath { + configDir := filepath.Dir(path) + err := os.MkdirAll(configDir, 0750) + if err != nil { + return err + } + } + bs, err := json.Marshal(cfg) if err != nil { return err From a66cdccda9c795912823fd35238a62cea72811a8 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Sun, 20 Jun 2021 23:01:44 +0200 Subject: [PATCH 03/21] Add service controllers and installers commands --- cmd/root.go | 16 +++++-- cmd/service.go | 22 +++++++++ cmd/service_controller.go | 99 +++++++++++++++++++++++++++++++++++++++ cmd/service_installer.go | 64 +++++++++++++++++++++++++ cmd/service_run.go | 58 ----------------------- 5 files changed, 196 insertions(+), 63 deletions(-) create mode 100644 cmd/service_controller.go create mode 100644 cmd/service_installer.go delete mode 100644 cmd/service_run.go diff --git a/cmd/root.go b/cmd/root.go index 05918688a5d..bb7be2afb1e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" "os" "os/signal" + "runtime" ) const ( @@ -14,8 +15,9 @@ const ( ) var ( - configPath string - logLevel string + configPath string + defaultConfigPath string + logLevel string rootCmd = &cobra.Command{ Use: "wiretrustee", @@ -28,16 +30,20 @@ var ( func Execute() error { return rootCmd.Execute() } - func init() { - rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/wiretrustee/config.json", "Wiretrustee config file location to write new config to") + defaultConfigPath = "/etc/wiretrustee/config.json" + if runtime.GOOS == "windows" { + defaultConfigPath = os.Getenv("PROGRAMDATA") + "Wiretrustee" + "config.json" + } + rootCmd.PersistentFlags().StringVar(&configPath, "config", defaultConfigPath, "Wiretrustee config file location to write new config to") rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "") rootCmd.AddCommand(initCmd) rootCmd.AddCommand(addPeerCmd) rootCmd.AddCommand(upCmd) rootCmd.AddCommand(signalCmd) rootCmd.AddCommand(serviceCmd) - serviceCmd.AddCommand(runCmd) // run is a subcommand of service + serviceCmd.AddCommand(runCmd, startCmd, stopCmd, restartCmd) // service control commands are subcommands of service + serviceCmd.AddCommand(installCmd, uninstallCmd) // service installer commands are subcommands of service } // SetupCloseHandler handles SIGTERM signal and exits with success diff --git a/cmd/service.go b/cmd/service.go index 397899123d1..2faacad115a 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -2,9 +2,17 @@ package cmd import ( "github.com/kardianos/service" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) +type program struct { + cmd *cobra.Command + args []string +} + +var logger service.Logger + func newSVCConfig() *service.Config { return &service.Config{ Name: "wiretrustee", @@ -13,6 +21,20 @@ func newSVCConfig() *service.Config { } } +func newSVC(prg *program, conf *service.Config) (service.Service, error) { + s, err := service.New(prg, conf) + if err != nil { + log.Fatal(err) + return nil, err + } + logger, err = s.Logger(nil) + if err != nil { + log.Fatal(err) + return nil, err + } + return s, nil +} + var ( serviceCmd = &cobra.Command{ Use: "service", diff --git a/cmd/service_controller.go b/cmd/service_controller.go new file mode 100644 index 00000000000..e47ba493f9a --- /dev/null +++ b/cmd/service_controller.go @@ -0,0 +1,99 @@ +package cmd + +import ( + "github.com/kardianos/service" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func (p *program) Start(s service.Service) error { + // Start should not block. Do the actual work async. + logger.Info("Starting service") //nolint + go upCmd.Run(p.cmd, p.args) + return nil +} + +func (p *program) Stop(s service.Service) error { + stopUP <- 1 + return nil +} + +var ( + runCmd = &cobra.Command{ + Use: "run", + Short: "runs wiretrustee as service", + Run: func(cmd *cobra.Command, args []string) { + + prg := &program{ + cmd: cmd, + args: args, + } + + s, err := newSVC(prg, newSVCConfig()) + if err != nil { + log.Fatal(err) + } + err = s.Run() + if err != nil { + logger.Error(err) //nolint + } + }, + } +) + +var ( + startCmd = &cobra.Command{ + Use: "start", + Short: "starts wiretrustee service", + Run: func(cmd *cobra.Command, args []string) { + + s, err := newSVC(&program{}, newSVCConfig()) + if err != nil { + log.Fatal(err) + } + err = s.Start() + if err != nil { + logger.Error(err) //nolint + } + }, + } +) + +var ( + stopCmd = &cobra.Command{ + Use: "stop", + Short: "stops wiretrustee service", + Run: func(cmd *cobra.Command, args []string) { + + s, err := newSVC(&program{}, newSVCConfig()) + if err != nil { + log.Fatal(err) + } + err = s.Stop() + if err != nil { + logger.Error(err) //nolint + } + }, + } +) + +var ( + restartCmd = &cobra.Command{ + Use: "restart", + Short: "restarts wiretrustee service", + Run: func(cmd *cobra.Command, args []string) { + + s, err := newSVC(&program{}, newSVCConfig()) + if err != nil { + log.Fatal(err) + } + err = s.Restart() + if err != nil { + logger.Error(err) //nolint + } + }, + } +) + +func init() { +} diff --git a/cmd/service_installer.go b/cmd/service_installer.go new file mode 100644 index 00000000000..b5a2aaf86ea --- /dev/null +++ b/cmd/service_installer.go @@ -0,0 +1,64 @@ +package cmd + +import ( + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "runtime" +) + +var ( + installCmd = &cobra.Command{ + Use: "install", + Short: "installs wiretrustee service", + Run: func(cmd *cobra.Command, args []string) { + + svcConfig := newSVCConfig() + + svcConfig.Arguments = []string{ + "service", + "run", + "--config", + configPath, + "--log-level", + logLevel, + } + + if runtime.GOOS == "linux" { + // Respected only by systemd systems + svcConfig.Dependencies = []string{"After=network.target syslog.target"} + } + + s, err := newSVC(&program{}, svcConfig) + if err != nil { + log.Fatal(err) + } + + err = s.Install() + if err != nil { + log.Error(err) + } + }, + } +) + +var ( + uninstallCmd = &cobra.Command{ + Use: "uninstall", + Short: "uninstalls wiretrustee service from system", + Run: func(cmd *cobra.Command, args []string) { + + s, err := newSVC(&program{}, newSVCConfig()) + if err != nil { + log.Fatal(err) + } + + err = s.Uninstall() + if err != nil { + log.Error(err) + } + }, + } +) + +func init() { +} diff --git a/cmd/service_run.go b/cmd/service_run.go deleted file mode 100644 index b524a859476..00000000000 --- a/cmd/service_run.go +++ /dev/null @@ -1,58 +0,0 @@ -package cmd - -import ( - "github.com/kardianos/service" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -type program struct { - cmd *cobra.Command - args []string -} - -var logger service.Logger - -func (p *program) Start(s service.Service) error { - // Start should not block. Do the actual work async. - logger.Info("Starting service") //nolint - go upCmd.Run(p.cmd, p.args) - return nil -} - -func (p *program) Stop(s service.Service) error { - stopUP <- 1 - return nil -} - -var ( - runCmd = &cobra.Command{ - Use: "run", - Short: "runs wiretrustee as service", - Run: func(cmd *cobra.Command, args []string) { - - svcConfig := newSVCConfig() - - prg := &program{ - cmd: cmd, - args: args, - } - s, err := service.New(prg, svcConfig) - if err != nil { - log.Fatal(err) - } - logger, err = s.Logger(nil) - if err != nil { - log.Fatal(err) - } - - err = s.Run() - if err != nil { - logger.Error(err) //nolint - } - }, - } -) - -func init() { -} From 74355a22924c377dd4f51601c786ce852ca01b94 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Sun, 20 Jun 2021 23:33:49 +0200 Subject: [PATCH 04/21] fix windows default config path --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index bb7be2afb1e..6f0c6d4621c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,7 @@ func Execute() error { func init() { defaultConfigPath = "/etc/wiretrustee/config.json" if runtime.GOOS == "windows" { - defaultConfigPath = os.Getenv("PROGRAMDATA") + "Wiretrustee" + "config.json" + defaultConfigPath = os.Getenv("PROGRAMDATA") + "\\Wiretrustee\\" + "config.json" } rootCmd.PersistentFlags().StringVar(&configPath, "config", defaultConfigPath, "Wiretrustee config file location to write new config to") rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "") From 8088c7a59172a1bfc4e3cab65fb7355cae989947 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 21 Jun 2021 11:18:03 +0200 Subject: [PATCH 05/21] feature: add stop handling for engine --- cmd/root.go | 2 +- cmd/up.go | 4 +++- connection/engine.go | 17 +++++++++++++++++ iface/iface.go | 5 +++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 6f0c6d4621c..1c7b0a472cb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,7 +53,7 @@ func SetupCloseHandler() { go func() { for range c { fmt.Println("\r- Ctrl+C pressed in Terminal") - os.Exit(0) + stopUP <- 0 } }() } diff --git a/cmd/up.go b/cmd/up.go index 2646c8fd30b..9c6003f6835 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -39,6 +39,7 @@ var ( iFaceBlackList[config.IFaceBlackList[i]] = struct{}{} } engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr, iFaceBlackList) + defer engine.Stop() err = engine.Start(myKey, config.Peers) if err != nil { @@ -48,8 +49,9 @@ var ( //signalClient.WaitConnected() SetupCloseHandler() - <-stopUP + code := <-stopUP log.Println("Receive signal to stop running") + os.Exit(code) }, } ) diff --git a/connection/engine.go b/connection/engine.go index 80928e0a35f..0f5c4841440 100644 --- a/connection/engine.go +++ b/connection/engine.go @@ -46,6 +46,23 @@ func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgA iFaceBlackList: iFaceBlackList, } } +func (e *Engine) Stop() error { + err := iface.Delete() + if err != nil { + log.Errorf("error while deleting Wireguard interface") + return err + } + + err = e.signal.Close() + if err != nil { + log.Errorf("error while closing a connection to the signal server") + return err + } + for _, c := range e.conns { + c.Close() //nolint + } + return nil +} // Start creates a new tunnel interface and listens to signals from the Signal service. // It also creates an Go routine to handle each peer communication from the config file diff --git a/iface/iface.go b/iface/iface.go index cb51e9ffdd3..e254a5cd554 100644 --- a/iface/iface.go +++ b/iface/iface.go @@ -19,6 +19,11 @@ const ( // Saves tun device object - is it required? var tunIface tun.Device +// Delete deletes an existing Wireguard interface +func Delete() error { + return tunIface.Close() +} + // Create Creates a new Wireguard interface, sets a given IP and brings it up. // Will reuse an existing one. func Create(iface string, address string) error { From a87f8288448223225371946c974b3404a69b9a9f Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Tue, 22 Jun 2021 01:07:12 +0200 Subject: [PATCH 06/21] Adjust service command outputs to use cmd Print functions --- cmd/service_controller.go | 29 ++++++++++++++++++++--------- cmd/service_installer.go | 15 ++++++++++----- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/cmd/service_controller.go b/cmd/service_controller.go index e47ba493f9a..a02a554b89f 100644 --- a/cmd/service_controller.go +++ b/cmd/service_controller.go @@ -2,7 +2,6 @@ package cmd import ( "github.com/kardianos/service" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -31,12 +30,15 @@ var ( s, err := newSVC(prg, newSVCConfig()) if err != nil { - log.Fatal(err) + cmd.PrintErrln(err) + return } err = s.Run() if err != nil { - logger.Error(err) //nolint + cmd.PrintErrln(err) + return } + cmd.Printf("Wiretrustee service is running") }, } ) @@ -49,12 +51,15 @@ var ( s, err := newSVC(&program{}, newSVCConfig()) if err != nil { - log.Fatal(err) + cmd.PrintErrln(err) + return } err = s.Start() if err != nil { - logger.Error(err) //nolint + cmd.PrintErrln(err) + return } + cmd.Printf("Wiretrustee service has been started") }, } ) @@ -67,12 +72,15 @@ var ( s, err := newSVC(&program{}, newSVCConfig()) if err != nil { - log.Fatal(err) + cmd.PrintErrln(err) + return } err = s.Stop() if err != nil { - logger.Error(err) //nolint + cmd.PrintErrln(err) + return } + cmd.Printf("Wiretrustee service has been stopped") }, } ) @@ -85,12 +93,15 @@ var ( s, err := newSVC(&program{}, newSVCConfig()) if err != nil { - log.Fatal(err) + cmd.PrintErrln(err) + return } err = s.Restart() if err != nil { - logger.Error(err) //nolint + cmd.PrintErrln(err) + return } + cmd.Printf("Wiretrustee service has been restarted") }, } ) diff --git a/cmd/service_installer.go b/cmd/service_installer.go index b5a2aaf86ea..1f289f81dd8 100644 --- a/cmd/service_installer.go +++ b/cmd/service_installer.go @@ -1,7 +1,6 @@ package cmd import ( - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "runtime" ) @@ -30,13 +29,16 @@ var ( s, err := newSVC(&program{}, svcConfig) if err != nil { - log.Fatal(err) + cmd.PrintErrln(err) + return } err = s.Install() if err != nil { - log.Error(err) + cmd.PrintErrln(err) + return } + cmd.Printf("Wiretrustee service has been installed") }, } ) @@ -49,13 +51,16 @@ var ( s, err := newSVC(&program{}, newSVCConfig()) if err != nil { - log.Fatal(err) + cmd.PrintErrln(err) + return } err = s.Uninstall() if err != nil { - log.Error(err) + cmd.PrintErrln(err) + return } + cmd.Printf("Wiretrustee has been uninstalled") }, } ) From 5122294adf1ba3e9957237ecf83448cf34b9bb15 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Tue, 22 Jun 2021 01:17:01 +0200 Subject: [PATCH 07/21] golint: properly handle defer engine stop --- cmd/up.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/up.go b/cmd/up.go index 9c6003f6835..ed8748832ed 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -39,7 +39,12 @@ var ( iFaceBlackList[config.IFaceBlackList[i]] = struct{}{} } engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr, iFaceBlackList) - defer engine.Stop() + defer func() { + err := engine.Stop() + if err != nil { + log.Fatal(err) + } + }() err = engine.Start(myKey, config.Peers) if err != nil { From 52db3031044211847b405ac35df9e3e4c719f063 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Tue, 22 Jun 2021 01:17:30 +0200 Subject: [PATCH 08/21] Add service command tests --- cmd/service_test.go | 123 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 cmd/service_test.go diff --git a/cmd/service_test.go b/cmd/service_test.go new file mode 100644 index 00000000000..2881540f2f7 --- /dev/null +++ b/cmd/service_test.go @@ -0,0 +1,123 @@ +package cmd + +import ( + "bytes" + "github.com/kardianos/service" + "io/ioutil" + "testing" +) + +func Test_ServiceInstallCMD(t *testing.T) { + b := bytes.NewBufferString("") + rootCmd.SetOut(b) + rootCmd.SetErr(b) + rootCmd.SetArgs([]string{"service", "install"}) + err := rootCmd.Execute() + if err != nil { + t.Fatal(err) + } + out, err := ioutil.ReadAll(b) + if err != nil { + t.Fatal(err) + } + expectedMSG := "Wiretrustee service has been installed" + if string(out) != expectedMSG { + t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out)) + } +} + +func Test_ServiceStartCMD(t *testing.T) { + b := bytes.NewBufferString("") + rootCmd.SetOut(b) + rootCmd.SetErr(b) + rootCmd.SetArgs([]string{"service", "start"}) + err := rootCmd.Execute() + if err != nil { + t.Fatal(err) + } + out, err := ioutil.ReadAll(b) + if err != nil { + t.Fatal(err) + } + expectedMSG := "Wiretrustee service has been started" + if string(out) != expectedMSG { + t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out)) + } +} + +func Test_ServiceRunCMD(t *testing.T) { + rootCmd.SetArgs([]string{ + "--stunURLs", + "stun:stun.wiretrustee.com:3468", + "--signalAddr", + "signal.wiretrustee.com:10000", + "--turnURLs foo:bar@turn:stun.wiretrustee.com:3468", + "--wgInterface", + "wiretrustee", + "--wgLocalAddr", + "10.100.100.1/24", + }) + rootCmd.SetArgs([]string{"init"}) + err := rootCmd.Execute() + if err != nil { + t.Fatal(err) + } + + rootCmd.ResetFlags() + rootCmd.SetArgs([]string{"service", "start"}) + err = rootCmd.Execute() + if err != nil { + t.Fatal(err) + } + s, err := newSVC(&program{}, newSVCConfig()) + if err != nil { + t.Fatal(err) + } + status, err := s.Status() + if err != nil { + t.Fatal(err) + } + + if status != service.StatusRunning { + t.Fatalf("expected running status of \"%d\" got \"%d\"", service.StatusRunning, status) + } +} + +func Test_ServiceStopCMD(t *testing.T) { + b := bytes.NewBufferString("") + rootCmd.SetOut(b) + rootCmd.SetErr(b) + rootCmd.SetArgs([]string{"service", "stop"}) + err := rootCmd.Execute() + if err != nil { + t.Fatal(err) + } + out, err := ioutil.ReadAll(b) + if err != nil { + t.Fatal(err) + } + + expectedMSG := "Wiretrustee service has been stopped" + if string(out) != expectedMSG { + t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out)) + } +} + +func Test_ServiceUninstallCMD(t *testing.T) { + b := bytes.NewBufferString("") + rootCmd.SetOut(b) + rootCmd.SetErr(b) + rootCmd.SetArgs([]string{"service", "uninstall"}) + err := rootCmd.Execute() + if err != nil { + t.Fatal(err) + } + out, err := ioutil.ReadAll(b) + if err != nil { + t.Fatal(err) + } + expectedMSG := "Wiretrustee has been uninstalled" + if string(out) != expectedMSG { + t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out)) + } +} From bbfbf797d55df2def24b9c7bab64060c96db04a8 Mon Sep 17 00:00:00 2001 From: braginini Date: Tue, 22 Jun 2021 12:11:51 +0200 Subject: [PATCH 09/21] chore: remove os.Exit - unnecessary call --- cmd/up.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/up.go b/cmd/up.go index ed8748832ed..91edf1a8d30 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -54,9 +54,8 @@ var ( //signalClient.WaitConnected() SetupCloseHandler() - code := <-stopUP + <-stopUP log.Println("Receive signal to stop running") - os.Exit(code) }, } ) From 1a06518f1b9d032de7ed68712aa61b0af3dcf0d3 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Wed, 23 Jun 2021 01:06:47 +0200 Subject: [PATCH 10/21] Update resource file with requireAdministrator, added resources.rc and manifests.xml --- cmd/service.go | 4 ++-- manifest.xml | 17 +++++++++++++++++ resources.rc | 9 +++++++++ resources_windows_amd64.syso | Bin 571204 -> 571918 bytes 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 manifest.xml create mode 100644 resources.rc diff --git a/cmd/service.go b/cmd/service.go index 2faacad115a..8cb35972736 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -16,8 +16,8 @@ var logger service.Logger func newSVCConfig() *service.Config { return &service.Config{ Name: "wiretrustee", - DisplayName: "wiretrustee", - Description: "This is an example Go service.", + DisplayName: "Wiretrustee", + Description: "A WireGuard-based mesh network that connects your devices into a single private network.", } } diff --git a/manifest.xml b/manifest.xml new file mode 100644 index 00000000000..15d9287dcd7 --- /dev/null +++ b/manifest.xml @@ -0,0 +1,17 @@ + + + + Wiretrustee application + + + + + + + + \ No newline at end of file diff --git a/resources.rc b/resources.rc new file mode 100644 index 00000000000..5ffef3cff44 --- /dev/null +++ b/resources.rc @@ -0,0 +1,9 @@ +#include + +#pragma code_page(65001) // UTF-8 + +#define STRINGIZE(x) #x +#define EXPAND(x) STRINGIZE(x) +CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST manifest.xml +wintun.dll RCDATA wintun.dll + diff --git a/resources_windows_amd64.syso b/resources_windows_amd64.syso index 20817f733ec3f110870a4a403da2eab61afda321..41caecb2b34428b503de3019a1cddd5ba7a2b34c 100644 GIT binary patch delta 690 zcmY*Xzi-qq6!zUw2w54REfSU`RD{GOM}^eENjVi&>IM=&W|KJ7QsShx19wx~A#9+# zDg6^}M{Vf_68{DpgC`~i79{MuHf_Gha^Cwsz3)BSKYpXne~=r0>;Q|uXH$3O$?VJl z*hIg~ZqYUCDe%A9|4Vd%YXS(s!5y|GTthMF7g@Fa($ zI-EEUR+_%Cq){o%oC^a8YnV8zHygeV5@*dvg6@^ZZWw2bGpPhoxj?XF!^ln?poFB7 qqVq0Ik)IRi?z5%{_S!DkYZ@}w<6YcOXdjow#Y5_WS$+T1x%VFpTf4jf delta 141 zcmeDCqIBe)5?4waBLfKNZJ)>`<@#b92ZIfer?Q;`EDDlyU@$lU=D-O?1}-35f`Op{ zE(BzN)OrBL3?>$eGbT*jDa~F06sg$8(fGRcHDl{*rqHU$eHpW@~-TzV$Un O<2J^G>F;-P$O8a$Cn2c- From c18899d13521d8d6500efe636811f2e53585d71a Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Wed, 23 Jun 2021 01:38:44 +0200 Subject: [PATCH 11/21] Add windows documentation --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b4f63b885f0..3fc090f9aa5 100644 --- a/README.md +++ b/README.md @@ -66,14 +66,28 @@ After that you may need to add /usr/local/bin in your MAC's PATH environment var ````shell export PATH=$PATH:/usr/local/bin ```` -### Client Configuration -1. Initialize Wiretrustee: -For **MACOS**, you need to create the configuration directory: +#### Windows +1. Checkout Wiretrustee [releases](https://github.com/wiretrustee/wiretrustee/releases/latest) +2. Download the latest Windows release ```wiretrustee__windows_amd64.tar.gz``` (**Switch VERSION to the latest**): +3. Decompress and move to a more fixed path in your system +4. Open Powershell +5. For Windows systems, we can use the service command to configure Wiretrustee as a service +````shell +cd C:\path\to\wiretrustee\bin +.\wiretrustee.exe service --help +.\wiretrustee.exe service install # This will prompt for administrator permissions in order to install a new service +```` +6. After installing you can follow the [Client Configuration](#Client-Configuration) steps. +7. To uninstall the service simple run the command above with the uninstall flag: ````shell -sudo mkdir /etc/wiretrustee +.\wiretrustee.exe service uninstall ```` -Then, for all systems: + +### Client Configuration +1. Initialize Wiretrustee: + +For **Unix** systems: ```shell sudo wiretrustee init \ --stunURLs stun:stun.wiretrustee.com:3468,stun:stun.l.google.com:19302 \ @@ -82,6 +96,16 @@ sudo wiretrustee init \ --wgLocalAddr 10.30.30.1/24 \ --log-level info ``` +For **Windows** systems: +```shell +.\wiretrustee.exe init ` + --stunURLs stun:stun.wiretrustee.com:3468,stun:stun.l.google.com:19302 ` + --turnURLs :@turn:stun.wiretrustee.com:3468 ` + --signalAddr signal.wiretrustee.com:10000 ` + --wgLocalAddr 10.30.30.1/24 ` + --log-level info + ``` + It is important to mention that the ```wgLocalAddr``` parameter has to be unique across your network. E.g. if you have Peer A with ```wgLocalAddr=10.30.30.1/24``` then another Peer B can have ```wgLocalAddr=10.30.30.2/24``` @@ -89,11 +113,16 @@ If for some reason, you already have a generated Wireguard key, you can specify If not specified, then a new one will be generated, and its corresponding public key will be output to the log. A new config will be generated and stored under ```/etc/wiretrustee/config.json``` -2. Add a peer to connect to. +2. Add a peer to connect to. + +For **Unix** systems: ```shell sudo wiretrustee add-peer --allowedIPs 10.30.30.2/32 --key '' ``` - +For **Windows** systems: +```shell +.\wiretrustee.exe add-peer --allowedIPs 10.30.30.2/32 --key '' +``` 3. Restart Wiretrustee to reload changes For **MACOS** you will just start the service: ````shell @@ -106,6 +135,10 @@ For **Linux** systems: sudo systemctl restart wiretrustee.service sudo systemctl status wiretrustee.service ``` +For **Windows** systems: +```shell +.\wiretrustee.exe service start +``` ### Running the Signal service After installing the application, you can run the signal using the command below: ````shell From e553c5e97e849b588d32376b9de3b7662e6beb84 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 10:28:27 +0200 Subject: [PATCH 12/21] Avoid prompt admin at every execution --- manifest.xml | 2 +- resources_windows_amd64.syso | Bin 571918 -> 571902 bytes 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.xml b/manifest.xml index 15d9287dcd7..392a6f7ff15 100644 --- a/manifest.xml +++ b/manifest.xml @@ -10,7 +10,7 @@ - + diff --git a/resources_windows_amd64.syso b/resources_windows_amd64.syso index 41caecb2b34428b503de3019a1cddd5ba7a2b34c..64d593543ceeaab4a6552c5442b95ff25b23e2a9 100644 GIT binary patch delta 81 zcmeDCqV(^x5@$*qBLfJ$n8+#3Ib$aWgAI^#WuoqG# Date: Fri, 25 Jun 2021 10:28:50 +0200 Subject: [PATCH 13/21] Updated documentation for Powershell as admin --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3fc090f9aa5..24e54dd97c0 100644 --- a/README.md +++ b/README.md @@ -72,12 +72,13 @@ export PATH=$PATH:/usr/local/bin 2. Download the latest Windows release ```wiretrustee__windows_amd64.tar.gz``` (**Switch VERSION to the latest**): 3. Decompress and move to a more fixed path in your system 4. Open Powershell -5. For Windows systems, we can use the service command to configure Wiretrustee as a service +5. For Windows systems, we can use the service command to configure Wiretrustee as a service by running the following commands in Powershell: ````shell cd C:\path\to\wiretrustee\bin .\wiretrustee.exe service --help .\wiretrustee.exe service install # This will prompt for administrator permissions in order to install a new service ```` +> You may need to run Powershell as Administrator 6. After installing you can follow the [Client Configuration](#Client-Configuration) steps. 7. To uninstall the service simple run the command above with the uninstall flag: ````shell @@ -139,6 +140,7 @@ For **Windows** systems: ```shell .\wiretrustee.exe service start ``` +> You may need to run Powershell as Administrator ### Running the Signal service After installing the application, you can run the signal using the command below: ````shell From 1cf9b143e0cc9a0a00dfd2a40def81d80154a235 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 10:40:47 +0200 Subject: [PATCH 14/21] update go.mod with service command dependecies --- go.mod | 3 +-- go.sum | 14 +++----------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 1c7bece5833..d1e55eb7091 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,13 @@ go 1.16 require ( github.com/cenkalti/backoff/v4 v4.1.0 github.com/golang/protobuf v1.5.2 - github.com/google/nftables v0.0.0-20201230142148-715e31cb3c31 + github.com/kardianos/service v1.2.0 github.com/onsi/ginkgo v1.16.4 github.com/onsi/gomega v1.13.0 github.com/pion/ice/v2 v2.1.7 github.com/sirupsen/logrus v1.7.0 github.com/spf13/cobra v1.1.3 github.com/vishvananda/netlink v1.1.0 - github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf golang.org/x/sys v0.0.0-20210510120138-977fb7262007 golang.zx2c4.com/wireguard v0.0.0-20210604143328-f9b48a961cd2 diff --git a/go.sum b/go.sum index 54f3a500e33..d5535ffb1a4 100644 --- a/go.sum +++ b/go.sum @@ -85,8 +85,6 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= -github.com/google/nftables v0.0.0-20201230142148-715e31cb3c31 h1:kyEB9geFhgDyawmvavtNu9iGW9ri/iq54XTSNIEeHxI= -github.com/google/nftables v0.0.0-20201230142148-715e31cb3c31/go.mod h1:cfspEyr/Ap+JDIITA+N9a0ernqG0qZ4W1aqMRgDZa1g= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= @@ -137,10 +135,10 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kardianos/service v1.2.0 h1:bGuZ/epo3vrt8IPC7mnKQolqFeYJb7Cs8Rk4PSOBB/g= +github.com/kardianos/service v1.2.0/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/koneu/natend v0.0.0-20150829182554-ec0926ea948d h1:MFX8DxRnKMY/2M3H61iSsVbo/n3h0MWGmWNN1UViOU0= -github.com/koneu/natend v0.0.0-20150829182554-ec0926ea948d/go.mod h1:QHb4k4cr1fQikUahfcRVPcEXiUgFsdIstGqlurL0XL4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -159,7 +157,6 @@ github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lk github.com/mdlayher/genetlink v1.0.0 h1:OoHN1OdyEIkScEmRgxLEe2M9U8ClMytqA5niynLtfj0= github.com/mdlayher/genetlink v1.0.0/go.mod h1:0rJ0h4itni50A86M2kHcgS85ttZazNt7a8H2a2cw0Gc= github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= -github.com/mdlayher/netlink v0.0.0-20191009155606-de872b0d824b/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.1.0/go.mod h1:H4WCitaheIsdF9yOYu8CFmCgQthAPIWZmcKp9uZHgmY= github.com/mdlayher/netlink v1.1.1/go.mod h1:WTYpFb/WTvlRJAyKhZL5/uy69TDDpHHu2VZmb2XgV7o= @@ -220,8 +217,6 @@ github.com/pion/udp v0.1.1 h1:8UAPvyqmsxK8oOjloDk4wUt63TzFe9WEJkg5lChlj7o= github.com/pion/udp v0.1.1/go.mod h1:6AFo+CMdKQm7UiA0eUPA8/eVCTx8jBIITLZHc9DWX5M= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= @@ -269,7 +264,6 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69 github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= -github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= @@ -328,7 +322,6 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191007182048-72f939374954/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= @@ -362,7 +355,6 @@ golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190411185658-b44545bcd369/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -376,12 +368,12 @@ golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191029155521-f43be2a4598c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201118182958-a01c418693c7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= From 881f078759a5e6a54239ed28eb8c2b0647239a05 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 10:58:42 +0200 Subject: [PATCH 15/21] Removed engine.Stop --- cmd/up.go | 6 ------ connection/engine.go | 17 ----------------- 2 files changed, 23 deletions(-) diff --git a/cmd/up.go b/cmd/up.go index 91edf1a8d30..2646c8fd30b 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -39,12 +39,6 @@ var ( iFaceBlackList[config.IFaceBlackList[i]] = struct{}{} } engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr, iFaceBlackList) - defer func() { - err := engine.Stop() - if err != nil { - log.Fatal(err) - } - }() err = engine.Start(myKey, config.Peers) if err != nil { diff --git a/connection/engine.go b/connection/engine.go index 0f5c4841440..80928e0a35f 100644 --- a/connection/engine.go +++ b/connection/engine.go @@ -46,23 +46,6 @@ func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgA iFaceBlackList: iFaceBlackList, } } -func (e *Engine) Stop() error { - err := iface.Delete() - if err != nil { - log.Errorf("error while deleting Wireguard interface") - return err - } - - err = e.signal.Close() - if err != nil { - log.Errorf("error while closing a connection to the signal server") - return err - } - for _, c := range e.conns { - c.Close() //nolint - } - return nil -} // Start creates a new tunnel interface and listens to signals from the Signal service. // It also creates an Go routine to handle each peer communication from the config file From 80665049dc8ad1350075e582d36128c155f3c4ed Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 10:59:10 +0200 Subject: [PATCH 16/21] fixed Init Execution --- cmd/service_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/service_test.go b/cmd/service_test.go index 2881540f2f7..3a2c978d6c4 100644 --- a/cmd/service_test.go +++ b/cmd/service_test.go @@ -47,17 +47,18 @@ func Test_ServiceStartCMD(t *testing.T) { func Test_ServiceRunCMD(t *testing.T) { rootCmd.SetArgs([]string{ + "init", "--stunURLs", "stun:stun.wiretrustee.com:3468", "--signalAddr", "signal.wiretrustee.com:10000", - "--turnURLs foo:bar@turn:stun.wiretrustee.com:3468", + "--turnURLs", + "foo:bar@turn:stun.wiretrustee.com:3468", "--wgInterface", - "wiretrustee", + "utun99", "--wgLocalAddr", "10.100.100.1/24", }) - rootCmd.SetArgs([]string{"init"}) err := rootCmd.Execute() if err != nil { t.Fatal(err) From ac628b6efa736c6ac60d9e1d2385544c4424067d Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 11:01:21 +0200 Subject: [PATCH 17/21] use sudo for testing service installation --- .github/workflows/golang-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golang-test.yml b/.github/workflows/golang-test.yml index df5b30b3cab..1d72d3b2ddf 100644 --- a/.github/workflows/golang-test.yml +++ b/.github/workflows/golang-test.yml @@ -18,7 +18,7 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - name: Test - run: go test ./... + run: sudo go test ./... test_build: strategy: From ca5970140fa7681862805c0b7f3b3b9539ac91f3 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 11:08:16 +0200 Subject: [PATCH 18/21] set config path to avoid ci/cd limitations --- cmd/service_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/service_test.go b/cmd/service_test.go index 3a2c978d6c4..622c4566e7c 100644 --- a/cmd/service_test.go +++ b/cmd/service_test.go @@ -11,7 +11,12 @@ func Test_ServiceInstallCMD(t *testing.T) { b := bytes.NewBufferString("") rootCmd.SetOut(b) rootCmd.SetErr(b) - rootCmd.SetArgs([]string{"service", "install"}) + rootCmd.SetArgs([]string{ + "service", + "install", + "--config", + "/tmp/config.json", + }) err := rootCmd.Execute() if err != nil { t.Fatal(err) @@ -58,6 +63,8 @@ func Test_ServiceRunCMD(t *testing.T) { "utun99", "--wgLocalAddr", "10.100.100.1/24", + "--config", + "/tmp/config.json", }) err := rootCmd.Execute() if err != nil { From 8d05789749bc2ffc7b5c1f53de9027c82219740a Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 11:18:34 +0200 Subject: [PATCH 19/21] preserve env GOROOT --- .github/workflows/golang-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golang-test.yml b/.github/workflows/golang-test.yml index 1d72d3b2ddf..cc1cfbdb9e9 100644 --- a/.github/workflows/golang-test.yml +++ b/.github/workflows/golang-test.yml @@ -18,7 +18,7 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - name: Test - run: sudo go test ./... + run: sudo --preserve-env=GOROOT go test ./... test_build: strategy: From cffb08ad23cb5d1100051fe6a20e0055e8801609 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 11:23:13 +0200 Subject: [PATCH 20/21] Use go bin --- .github/workflows/golang-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golang-test.yml b/.github/workflows/golang-test.yml index cc1cfbdb9e9..0e1ee94f8a4 100644 --- a/.github/workflows/golang-test.yml +++ b/.github/workflows/golang-test.yml @@ -18,7 +18,7 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - name: Test - run: sudo --preserve-env=GOROOT go test ./... + run: GOBIN=$(which go) && sudo --preserve-env=GOROOT $GOBIN test ./... test_build: strategy: From f4d7faaf4e1f2b3e214a668179f781de5303ec85 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Fri, 25 Jun 2021 11:49:16 +0200 Subject: [PATCH 21/21] debug port value --- iface/iface.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iface/iface.go b/iface/iface.go index 5534cd6e4de..774c6d0b629 100644 --- a/iface/iface.go +++ b/iface/iface.go @@ -113,7 +113,7 @@ func GetListenPort(iface string) (*int, error) { if err != nil { return nil, err } - log.Debugf("got Wireguard device listen port %s, %d", iface, &d.ListenPort) + log.Debugf("got Wireguard device listen port %s, %d", iface, d.ListenPort) return &d.ListenPort, nil }