-
Notifications
You must be signed in to change notification settings - Fork 1
/
wit.go
60 lines (51 loc) · 1.28 KB
/
wit.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
package main
import (
"context"
"github.com/hbahadorzadeh/wit/model"
"github.com/hbahadorzadeh/wit/service"
"github.com/janeczku/go-ipset/ipset"
"go.uber.org/dig"
"log"
"os"
"os/exec"
)
func BuildContainer(args []string) *dig.Container {
container := dig.New()
//Config
container.Provide(func() model.Config {
return model.BuildConfigs(args)
})
//IptablesService
container.Provide(func(config model.Config) *service.IpTables {
return service.GetIptablesService(config)
})
//IpsetService
ipsetServiceInstance := service.IpsetService{}
container.Provide(func(config model.Config, ipt *service.IpTables) *ipset.IPSet {
return ipsetServiceInstance.GetInstance(config, ipt)
})
//WebService
container.Provide(func(config model.Config, ipset *ipset.IPSet) *service.WebService {
return service.GetWebService(config, ipset)
})
return container
}
func main() {
if os.Geteuid() != 0 {
log.Println("You need root permission!")
cmd := exec.CommandContext(context.Background(), "/usr/bin/sudo", os.Args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
os.Exit(0)
} else {
container := BuildContainer(os.Args[1:])
err := container.Invoke(func(webService *service.WebService) {
webService.Start()
})
if err != nil {
panic(err)
}
}
os.Exit(0)
}