-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds ability to kill a process of a userland wireguard-go
- Loading branch information
1 parent
696afeb
commit 104e72b
Showing
349 changed files
with
94,731 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,6 @@ fabric.properties | |
.idea/caches/build_file_checksums.ser | ||
|
||
# OWN | ||
assets/* | ||
dist/* | ||
bin/* | ||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PLACEHOLDER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build tools | ||
|
||
package main | ||
|
||
import ( | ||
_ "github.com/vektra/mockery/v2" | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.