/
run.go
86 lines (76 loc) · 2.11 KB
/
run.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
81
82
83
84
85
86
package main
import (
"github.com/gjbae1212/grpc-vpn/auth"
"log"
"os"
"runtime"
"github.com/gjbae1212/grpc-vpn/server"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var (
runCmd = &cobra.Command{
Use: "run",
Short: "Start vpn-server",
Long: "Start vpn-server",
PreRun: startPreRun(),
Run: startRun(),
}
)
func startPreRun() commandRun {
return func(cmd *cobra.Command, args []string) {
if runtime.GOOS != "linux" {
log.Printf(color.RedString("`vpn-server` is only to support LINUX."))
os.Exit(1)
}
if os.Getuid() != 0 {
log.Printf("%s %s", color.RedString("[RETRY][COMMAND]"),
color.CyanString("`sudo vpn-server run`"))
os.Exit(1)
}
}
}
func startRun() commandRun {
return func(cmd *cobra.Command, args []string) {
// apply default params
var opts []server.Option
if defaultConfig.SubNet != "" {
opts = append(opts, server.WithVpnSubNet(defaultConfig.SubNet))
}
if defaultConfig.Port != "" {
opts = append(opts, server.WithGrpcPort(defaultConfig.Port))
}
if defaultConfig.JwtSalt != "" {
opts = append(opts, server.WithVpnJwtSalt(defaultConfig.JwtSalt))
}
if defaultConfig.TlsCertification != "" {
opts = append(opts, server.WithGrpcTlsCertification(defaultConfig.TlsCertification))
}
if defaultConfig.TlsPem != "" {
opts = append(opts, server.WithGrpcTlsPem(defaultConfig.TlsPem))
}
if defaultConfig.JwtExpiration > 0 {
opts = append(opts, server.WithVpnJwtExpiration(defaultConfig.JwtExpiration))
}
// apply auth interceptors
var authMethods []auth.ServerAuthMethod
if auth1, ok := defaultConfig.GoogleConfig.ServerAuth(); ok {
authMethods = append(authMethods, auth1)
}
if auth2, ok := defaultConfig.AwsConfig.ServerAuth(); ok {
authMethods = append(authMethods, auth2)
}
opts = append(opts, server.WithAuthMethods(authMethods))
// create server
server, err := server.NewVpnServer(opts...)
if err != nil {
log.Panicln(color.RedString("[ERR] %s", err.Error()))
}
if err := server.Run(); err != nil {
log.Panicln(color.RedString("[ERR] %s", err.Error()))
}
}
}
func init() {
rootCmd.AddCommand(runCmd)
}