-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
80 lines (75 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"io/fs"
"os"
"os/signal"
"path/filepath"
"github.com/spf13/pflag"
)
var exit bool
func main() {
confPath := pflag.StringP("wg-conf", "c", "", "wireguard.conf")
driverName := pflag.StringP("driver-name", "n", "netfilter2", "driver name")
processPath := pflag.StringSliceP("file", "f", nil, "multi process files or single directory")
pflag.Parse()
if len(*confPath) == 0 || len(*processPath) == 0 {
pflag.Usage()
return
}
wgConf, err := ParseWgConf(*confPath)
if err != nil {
panic(err)
}
if len(*processPath) == 0 {
panic("no process file provided")
}
var processFiles []string
switch {
case len(*processPath) == 1:
file := (*processPath)[0]
fi, err := os.Stat(file)
if os.IsNotExist(err) {
// if path not exist, we treat it as process name
processFiles = append(processFiles, file)
break
}
if err != nil {
panic(err)
}
if fi.IsDir() {
fmt.Println("Provided path is a directory, try to get exe files under it")
err := filepath.WalkDir(file, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
if filepath.Ext(d.Name()) == ".exe" {
processFiles = append(processFiles, d.Name())
}
}
return nil
})
if err != nil {
return
}
} else {
processFiles = append(processFiles, file)
}
default:
for _, s := range *processPath {
processFiles = append(processFiles, s)
}
}
fmt.Printf("filter names: %v\n", processFiles)
conf := &Conf{
WgConf: wgConf,
processNames: processFiles,
}
nf := NewNetFilter(*driverName, conf)
err = nf.Start()
if err != nil {
panic(err)
}
defer nf.Stop()
stopChan := make(chan os.Signal)
signal.Notify(stopChan, os.Interrupt, os.Kill)
<-stopChan
}