Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Adding service command #37

Merged
merged 23 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
41b50a0
feature: Adding service run command
mlsmaycon Jun 19, 2021
06c7af0
Create config dir if using default configPath
mlsmaycon Jun 20, 2021
a66cdcc
Add service controllers and installers commands
mlsmaycon Jun 20, 2021
74355a2
fix windows default config path
mlsmaycon Jun 20, 2021
8088c7a
feature: add stop handling for engine
Jun 21, 2021
a87f828
Adjust service command outputs to use cmd Print functions
mlsmaycon Jun 21, 2021
5122294
golint: properly handle defer engine stop
mlsmaycon Jun 21, 2021
52db303
Add service command tests
mlsmaycon Jun 21, 2021
bbfbf79
chore: remove os.Exit - unnecessary call
Jun 22, 2021
1a06518
Update resource file with requireAdministrator, added resources.rc an…
mlsmaycon Jun 22, 2021
20248da
Merge remote-tracking branch 'origin/add-service-command' into add-se…
mlsmaycon Jun 22, 2021
c18899d
Add windows documentation
mlsmaycon Jun 22, 2021
e553c5e
Avoid prompt admin at every execution
mlsmaycon Jun 25, 2021
ab64520
Updated documentation for Powershell as admin
mlsmaycon Jun 25, 2021
158547f
rebase
mlsmaycon Jun 25, 2021
1cf9b14
update go.mod with service command dependecies
mlsmaycon Jun 25, 2021
881f078
Removed engine.Stop
mlsmaycon Jun 25, 2021
8066504
fixed Init Execution
mlsmaycon Jun 25, 2021
ac628b6
use sudo for testing service installation
mlsmaycon Jun 25, 2021
ca59701
set config path to avoid ci/cd limitations
mlsmaycon Jun 25, 2021
8d05789
preserve env GOROOT
mlsmaycon Jun 25, 2021
cffb08a
Use go bin
mlsmaycon Jun 25, 2021
f4d7faa
debug port value
mlsmaycon Jun 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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