-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.go
241 lines (216 loc) · 6.45 KB
/
startup.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package server
import (
"crypto/x509"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"strings"
"syscall"
"time"
)
const (
nmRebuild = "rebuild"
nmPort = "port"
nmCerts = "certs"
)
var (
certs = flag.String(nmCerts, "", "certs dir")
rebuild = flag.Bool(nmRebuild, false, "rebuild certs")
port = flag.Int(nmPort, 9090, "TCP Server Port")
)
var cfg = DefaultGlobalConfig
var cpf []func() error
// Main main
func Main() {
defer func() {
if err := recover(); err != nil {
log.Printf("Unknown error: %v\r\n", err)
debug.PrintStack()
log.Printf("Stack trace: %s\r\n", string(debug.Stack()))
}
}()
flag.Parse()
cfg.Load(overrideFlags)
if *rebuild {
rebuildCertsAndKey()
os.Exit(0)
}
printInfo()
checkCertsAndKey()
registerCleanupFunc()
startupApplication()
blockingUntilTermination()
}
func printInfo() {
log.Printf("Config information: %s\r\n", cfg.String())
}
func overrideFlags(cfg *Config) {
actualFlags := make(map[string]bool)
flag.Visit(func(f *flag.Flag) {
actualFlags[f.Name] = true
})
if actualFlags[nmPort] {
cfg.System.Port = *port
}
if actualFlags[nmCerts] {
cfg.System.CertsDir = *certs
}
}
func rebuildCertsAndKey() {
if err := os.MkdirAll(cfg.System.CertsDir, 0755); err != nil {
log.Printf("Fatal: MkdirAll failure, dir: %s, nest error: %v\r\n", cfg.System.CertsDir, err)
os.Exit(1)
}
caPrivBytes, caCertBytes, err := GenerateCertificate(nil, nil, 2048, &ApplicationInformation{
CertificateConfig: &CertificateConfig{
IsCA: true,
ExpirationTime: 24 * time.Hour * 365 * 3,
},
CommonName: "z-console-server.com",
CountryName: "CN",
ProvinceName: "BeiJing",
LocalityName: "BeiJing",
OrganizationName: "Eviltomorrow Inc",
OrganizationUnitName: "Development",
})
if err != nil {
log.Printf("Error: GenerateCertificate ca cert failure, nest error: %v\r\n", err)
os.Exit(1)
}
if err := WritePKCS1PrivateKey(filepath.Join(cfg.System.CertsDir, "ca.key"), caPrivBytes); err != nil {
log.Printf("Error: WritePKCS1PrivateKey ca key failure, nest error: %v\r\n", err)
os.Exit(1)
}
if err := WriteCertificate(filepath.Join(cfg.System.CertsDir, "ca.crt"), caCertBytes); err != nil {
log.Printf("Error: WriteCertificate ca cert failure, nest error: %v\r\n", err)
os.Exit(1)
}
// server
caKey, err := x509.ParsePKCS1PrivateKey(caPrivBytes)
if err != nil {
log.Printf("Error: ParsePKCS1PrivateKey CA key failure, nest error: %v\r\n", err)
os.Exit(1)
}
caCert, err := x509.ParseCertificate(caCertBytes)
if err != nil {
log.Printf("Error: ParseCertificate CA cert failure, nest error: %v\r\n", err)
os.Exit(1)
}
getLocalIP := func() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "", err
}
defer conn.Close()
localAddr := conn.LocalAddr().String()
idx := strings.LastIndex(localAddr, ":")
return localAddr[0:idx], nil
}
localIP, err := getLocalIP()
if err != nil {
log.Printf("Error: GetLocalIP failure, nest error: %v\r\n", err)
os.Exit(1)
}
serverPrivBytes, serverCertBytes, err := GenerateCertificate(caKey, caCert, 2048, &ApplicationInformation{
CertificateConfig: &CertificateConfig{
IP: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP(localIP)},
ExpirationTime: 24 * time.Hour * 365,
},
CommonName: "localhost",
CountryName: "CN",
ProvinceName: "BeiJing",
LocalityName: "BeiJing",
OrganizationName: "Eviltomorrow Inc",
OrganizationUnitName: "Development",
})
if err != nil {
log.Printf("GenerateCertificate server cert failure, nest error: %v\r\n", err)
os.Exit(1)
}
// WritePKCS1PrivateKey(filepath.Join(cfg.System.CertsDir, "server.key"), serverPrivBytes)
if err := WritePKCS8PrivateKey(filepath.Join(cfg.System.CertsDir, "server.pem"), serverPrivBytes); err != nil {
log.Printf("Error: WritePKCS8PrivateKey server pem failure, nest error: %v\r\n", err)
os.Exit(1)
}
if err := WriteCertificate(filepath.Join(cfg.System.CertsDir, "server.crt"), serverCertBytes); err != nil {
log.Printf("Error: WritePKCS1PrivateKey server cert failure, nest error: %v\r\n", err)
os.Exit(1)
}
// client
clientPrivBytes, clientCertBytes, err := GenerateCertificate(caKey, caCert, 2048, &ApplicationInformation{
CertificateConfig: &CertificateConfig{
ExpirationTime: 24 * time.Hour * 365,
},
CommonName: "localhost",
CountryName: "CN",
ProvinceName: "BeiJing",
LocalityName: "BeiJing",
OrganizationName: "Apple Inc",
OrganizationUnitName: "Dev",
})
if err != nil {
log.Printf("Error: GenerateCertificate client key failure, nest error: %v\r\n", err)
os.Exit(1)
}
if err := WritePKCS8PrivateKey(filepath.Join(cfg.System.CertsDir, "client.pem"), clientPrivBytes); err != nil {
log.Printf("Error: WritePKCS8PrivateKey client pem failure, nest error: %v\r\n", err)
os.Exit(1)
}
if err := WriteCertificate(filepath.Join(cfg.System.CertsDir, "client.crt"), clientCertBytes); err != nil {
log.Printf("Error: WritePKCS8PrivateKey client pem failure, nest error: %v\r\n", err)
os.Exit(1)
}
}
func checkCertsAndKey() {
existfile := func(path string) error {
fi, err := os.Stat(path)
if err != nil {
return err
}
if fi.IsDir() {
return fmt.Errorf("Path is dir")
}
return nil
}
var caCert = filepath.Join(cfg.System.CertsDir, "ca.crt")
if err := existfile(caCert); err != nil {
log.Printf("Error: Ca cert is not exist, certs-dir: %s\r\n", caCert)
os.Exit(1)
}
var serverCert = filepath.Join(cfg.System.CertsDir, "server.crt")
if err := existfile(serverCert); err != nil {
log.Printf("Error: Server cert is not exist, certs-dir: %s\r\n", cfg.System.CertsDir)
os.Exit(1)
}
var serverPem = filepath.Join(cfg.System.CertsDir, "server.pem")
if err := existfile(serverPem); err != nil {
log.Printf("Server pem is not exist, certs-dir: %s\r\n", cfg.System.CertsDir)
os.Exit(1)
}
}
func registerCleanupFunc() {
}
func startupApplication() {
if err := StartupServer(); err != nil {
log.Printf("Error: startup tcp server failure, nest error: %v\r\n", err)
os.Exit(1)
}
}
func blockingUntilTermination() {
var ch = make(chan os.Signal)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
switch <-ch {
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
case syscall.SIGUSR1:
case syscall.SIGUSR2:
default:
}
for _, f := range cpf {
f()
}
}