-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
93 lines (79 loc) · 1.78 KB
/
node.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
87
88
89
90
91
92
93
package main
import (
"io/ioutil"
"net"
"os"
"strings"
"github.com/rs/zerolog"
)
type nodeConfig struct {
id string
tlsVersion string
token string
port uint16
logLevel string
dockerPath string
mangementIPs []net.IP
logger *zerolog.Logger
}
func (c *nodeConfig) getLogger() *zerolog.Logger {
if c.logger == nil {
// logger config
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.WarnLevel)
logConfigLevel, errLogLevel := zerolog.ParseLevel(c.logLevel)
if errLogLevel == nil {
zerolog.SetGlobalLevel(logConfigLevel)
}
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
c.logger = &logger
}
return c.logger
}
func (c *nodeConfig) setMangementIPs(ips string) {
var lst []net.IP
for _, ip := range strings.Split(ips, ",") {
i := net.ParseIP(strings.TrimSpace(ip))
if i != nil {
lst = append(lst, net.ParseIP(ip))
}
}
c.mangementIPs = lst
}
func (c *nodeConfig) canBlockIP(ipString string) bool {
ip := net.ParseIP(ipString)
if ip != nil {
for _, mip := range c.mangementIPs {
if mip.Equal(ip) {
return false
}
}
}
return ip != nil
}
func (c *nodeConfig) canBlockCIDR(cidr string) bool {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return false
}
for _, mip := range c.mangementIPs {
if ipNet.Contains(mip) {
return false
}
}
return true
}
func (c *nodeConfig) writeEnv() error {
envSample, err1 := ioutil.ReadFile(c.dockerPath + "/.env.2.ready")
if err1 != nil {
return err1
}
envSampleString := string(envSample)
addons := []string{
"ASM_NODE_ID=" + c.id,
"ASM_SSL_PROFILE=" + c.tlsVersion,
"",
}
envSampleString += "\n" + strings.Join(addons, "\n")
return ioutil.WriteFile(c.dockerPath+"/.env", []byte(envSampleString), 0644)
}