Skip to content

Commit

Permalink
Merge pull request #37 from wiretrustee/add-service-command
Browse files Browse the repository at this point in the history
feature: Adding service command
  • Loading branch information
braginini authored Jun 27, 2021
2 parents 3041ff4 + f4d7faa commit bb24774
Show file tree
Hide file tree
Showing 15 changed files with 461 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golang-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
run: GOBIN=$(which go) && sudo --preserve-env=GOROOT $GOBIN test ./...

test_build:
strategy:
Expand Down
49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,29 @@ 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_<VERSION>_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 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
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 \
Expand All @@ -82,18 +97,33 @@ 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 User>:<TURN password>@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```

If for some reason, you already have a generated Wireguard key, you can specify it with the ```--wgKey``` parameter.
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 '<REMOTE PEER WIREUARD PUBLIC KEY>'
```

For **Windows** systems:
```shell
.\wiretrustee.exe add-peer --allowedIPs 10.30.30.2/32 --key '<REMOTE PEER WIREUARD PUBLIC KEY>'
```
3. Restart Wiretrustee to reload changes
For **MACOS** you will just start the service:
````shell
Expand All @@ -106,6 +136,11 @@ For **Linux** systems:
sudo systemctl restart wiretrustee.service
sudo systemctl status wiretrustee.service
```
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
Expand Down
10 changes: 10 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/wiretrustee/wiretrustee/connection"
"io/ioutil"
"os"
"path/filepath"
)

// Config Configuration type
Expand All @@ -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
Expand Down
18 changes: 13 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
"os"
"os/signal"
"runtime"
)

const (
Expand All @@ -14,8 +15,9 @@ const (
)

var (
configPath string
logLevel string
configPath string
defaultConfigPath string
logLevel string

rootCmd = &cobra.Command{
Use: "wiretrustee",
Expand All @@ -28,14 +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, 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
Expand All @@ -45,7 +53,7 @@ func SetupCloseHandler() {
go func() {
for range c {
fmt.Println("\r- Ctrl+C pressed in Terminal")
os.Exit(0)
stopUP <- 0
}
}()
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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",
DisplayName: "Wiretrustee",
Description: "A WireGuard-based mesh network that connects your devices into a single private network.",
}
}

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",
Short: "manages wiretrustee service",
//Run: func(cmd *cobra.Command, args []string) {
//},
}
)

func init() {
}
110 changes: 110 additions & 0 deletions cmd/service_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cmd

import (
"github.com/kardianos/service"
"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 {
cmd.PrintErrln(err)
return
}
err = s.Run()
if err != nil {
cmd.PrintErrln(err)
return
}
cmd.Printf("Wiretrustee service is running")
},
}
)

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 {
cmd.PrintErrln(err)
return
}
err = s.Start()
if err != nil {
cmd.PrintErrln(err)
return
}
cmd.Printf("Wiretrustee service has been started")
},
}
)

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 {
cmd.PrintErrln(err)
return
}
err = s.Stop()
if err != nil {
cmd.PrintErrln(err)
return
}
cmd.Printf("Wiretrustee service has been stopped")
},
}
)

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 {
cmd.PrintErrln(err)
return
}
err = s.Restart()
if err != nil {
cmd.PrintErrln(err)
return
}
cmd.Printf("Wiretrustee service has been restarted")
},
}
)

func init() {
}
69 changes: 69 additions & 0 deletions cmd/service_installer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"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 {
cmd.PrintErrln(err)
return
}

err = s.Install()
if err != nil {
cmd.PrintErrln(err)
return
}
cmd.Printf("Wiretrustee service has been installed")
},
}
)

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 {
cmd.PrintErrln(err)
return
}

err = s.Uninstall()
if err != nil {
cmd.PrintErrln(err)
return
}
cmd.Printf("Wiretrustee has been uninstalled")
},
}
)

func init() {
}
Loading

0 comments on commit bb24774

Please sign in to comment.