Skip to content

Commit

Permalink
adds ability to kill a process of a userland wireguard-go
Browse files Browse the repository at this point in the history
  • Loading branch information
xsteadfastx committed Apr 16, 2021
1 parent 696afeb commit 104e72b
Show file tree
Hide file tree
Showing 349 changed files with 94,731 additions and 41 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ fabric.properties
.idea/caches/build_file_checksums.ser

# OWN
assets/*
dist/*
bin/*
.envrc
16 changes: 8 additions & 8 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ builds:
- "-X go.xsfx.dev/wg-quicker/cmd.date={{.Date}}"
hooks:
pre:
- make generate GOARCH={{.Arch}}
- make wireguard-go GOARCH={{.Arch}}

- id: arm64
env:
Expand All @@ -36,7 +36,7 @@ builds:
- "-X go.xsfx.dev/wg-quicker/cmd.date={{.Date}}"
hooks:
pre:
- make generate GOARCH={{.Arch}}
- make wireguard-go GOARCH={{.Arch}}

- id: 386
env:
Expand All @@ -54,7 +54,7 @@ builds:
- "-X go.xsfx.dev/wg-quicker/cmd.date={{.Date}}"
hooks:
pre:
- make generate GOARCH={{.Arch}}
- make wireguard-go GOARCH={{.Arch}}

- id: arm-5
env:
Expand All @@ -74,8 +74,7 @@ builds:
- "-X go.xsfx.dev/wg-quicker/cmd.date={{.Date}}"
hooks:
pre:
- make clean
- make generate GOARCH={{.Arch}} GOARM=5
- make wireguard-go GOARCH={{.Arch}} GOARM=5

- id: arm-6
env:
Expand All @@ -95,7 +94,7 @@ builds:
- "-X go.xsfx.dev/wg-quicker/cmd.date={{.Date}}"
hooks:
pre:
- make generate GOARCH={{.Arch}} GOARM=6
- make wireguard-go GOARCH={{.Arch}} GOARM=6

- id: arm-7
env:
Expand All @@ -115,7 +114,7 @@ builds:
- "-X go.xsfx.dev/wg-quicker/cmd.date={{.Date}}"
hooks:
pre:
- make generate GOARCH={{.Arch}} GOARM=7
- make wireguard-go GOARCH={{.Arch}} GOARM=7

archives:
- replacements:
Expand All @@ -133,7 +132,8 @@ changelog:
exclude:
- "^docs:"
- "^test:"
- "^lint"
- "^ci:"
- "^lint:"
- "^happy linting"

release:
Expand Down
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ all: clean wg-quicker
.PHONY: clean
clean:
rm wg-quicker || true
rm -rf assets || true
rm third_party/wireguard-go/wireguard-go || true
rm assets/wireguard-go/wireguard-go || true

.PHONY: wireguard-go
wireguard-go: clean
cd third_party/wireguard-go; \
GOOS=linux GOARCH=$(GOARCH) $(GOARMLINE) $(GO) build -v -o wireguard-go $(GOFLAGS) .
cp third_party/wireguard-go/wireguard-go assets/wireguard-go

.PHONY: generate
generate: wireguard-go
go generate -v ./...
generate:
go generate ./...

wg-quicker: generate
wg-quicker: wireguard-go
$(GO) build -v $(GOFLAGS) -o "$@" cmd/wg-quicker/main.go

.PHONY: build
Expand All @@ -46,3 +47,7 @@ test:
.PHONY: lint
lint:
golangci-lint run --enable-all --disable gomnd --disable godox --disable exhaustivestruct --timeout 5m

.PHONY: install-tools
install-tools:
@cat tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % go install -v %
7 changes: 7 additions & 0 deletions assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// nolint:gochecknoglobals,golint,stylecheck
package assets

import _ "embed"

//go:embed wireguard-go/wireguard-go
var WGO []byte
1 change: 1 addition & 0 deletions assets/wireguard-go/wireguard-go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PLACEHOLDER
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var downCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
c, log := loadConfig(args[0])
if err := wgquick.Down(c, iface, userspace, log); err != nil {
if err := wgquick.Down(c, iface, log); err != nil {
logrus.WithError(err).Errorln("cannot down interface")
}
},
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module go.xsfx.dev/wg-quicker

go 1.12
go 1.16

require (
github.com/getlantern/byteexec v0.0.0-20170405023437-4cfb26ec74f4
github.com/getlantern/filepersist v0.0.0-20160317154340-c5f0cd24e799 // indirect
github.com/getlantern/golog v0.0.0-20201105130739-9586b8bde3a9 // indirect
github.com/rs/zerolog v1.20.0 // indirect
github.com/sirupsen/logrus v1.4.0
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.5.1
github.com/vektra/mockery/v2 v2.7.4
github.com/vishvananda/netlink v1.0.0
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc // indirect
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527
Expand Down
57 changes: 48 additions & 9 deletions go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build tools

package main

import (
_ "github.com/vektra/mockery/v2"
)
33 changes: 33 additions & 0 deletions tools/pidof/mock_Commander.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions tools/pidof/pidof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package pidof

import (
"errors"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"

log "github.com/sirupsen/logrus"
)

//go:generate mockery --name Commander --inpackage --output .

var ErrPIDNotFound = fmt.Errorf("could not find pid")

type Commander interface {
Output(cmd string) ([]byte, error)
}

type Cdr struct{}

func (c Cdr) Output(cmd string) ([]byte, error) {
cm := exec.Command("/bin/sh", "-c", cmd)

log.WithField("cmd", cm.String()).Debug("command to run")

// nolint:wrapcheck
return cm.Output()
}

func Pidof(name string, cdr Commander) (int, error) {
p, err := cdr.Output(fmt.Sprintf("ps a|grep '%s'", name))
if err != nil {
log.WithField("output", p).Debug(p)

return 0, fmt.Errorf("could not execute ps command: %w", err)
}

for _, l := range strings.Split(string(p), "\n") {
if strings.Contains(l, name) {
// nolint:gocritic
rePID, err := regexp.Compile(`(\d+)\s.+`)
if err != nil {
return 0, fmt.Errorf("could not compile re: %w", err)
}

matches := rePID.FindStringSubmatch(l)
// nolint: gomnd
if len(matches) == 2 {
pid, err := strconv.Atoi(matches[1])
if err != nil {
return 0, fmt.Errorf("could not convert string to int: %w", err)
}

return pid, nil
}

break
}
}

return 0, ErrPIDNotFound
}

func Pkill(name string) error {
c := Cdr{}

pid, err := Pidof(name, c)
if err != nil {
if errors.Is(err, ErrPIDNotFound) {
log.Debug("looks like the process is not running")

return nil
}

return fmt.Errorf("could not get pid: %w", err)
}

if err := syscall.Kill(pid, 9); err != nil {
return fmt.Errorf("could not kill process: %w", err)
}

return nil
}
75 changes: 75 additions & 0 deletions tools/pidof/pidof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// nolint:gochecknoglobals,paralleltest,goerr113
package pidof_test

import (
"embed"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"go.xsfx.dev/wg-quicker/tools/pidof"
)

//go:embed testdata/*
var testdata embed.FS

func TestPidof(t *testing.T) {
assert := assert.New(t)

tables := []struct {
name string
data string
pid int
err error
}{
{
"/home/linuxbrew/.linuxbrew/bin/tmux new-session -t local",
"psa_ubuntu_1.txt",
20814,
nil,
},
{
"/root/.byteexec/wireguard-go w1nd50r",
"psa_alpine_1.txt",
2360,
nil,
},
{
".byteexec/wireguard-go w1nd50r",
"psa_alpine_1.txt",
2360,
nil,
},
{
"/root/.byteexec/wireguard-go w2nd50r",
"psa_alpine_1.txt",
0,
fmt.Errorf("could not find pid"),
},
{
"/root/.byteexec/wireguard-go w1nd50r",
"psa_alpine_2.txt",
2501,
nil,
},
}

for _, table := range tables {
out, err := testdata.ReadFile("testdata/" + table.data)
assert.NoError(err)

mock := new(pidof.MockCommander)
mock.On("Output", fmt.Sprintf("ps a|grep '%s'", table.name)).Return(out, table.err)
pid, err := pidof.Pidof(table.name, mock)

if table.err != nil {
assert.Error(table.err, err)
} else {
assert.NoError(err)
}

assert.Equal(table.pid, pid)

mock.AssertExpectations(t)
}
}

0 comments on commit 104e72b

Please sign in to comment.