-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (120 loc) · 3.07 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
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
package main
import (
"context"
"crypto/tls"
"fmt"
"github.com/my0419/myvpn-agent/handler"
"github.com/my0419/myvpn-agent/installer"
"github.com/my0419/myvpn-agent/system"
"golang.org/x/crypto/acme/autocert"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
log.Println("Welcome to MyVPN, your reliable service for installing and configuring VPN connections.")
// debug mode
if debugLogFile := system.CreateDebugLogFile(); debugLogFile != nil {
log.SetFlags(log.Lshortfile)
log.SetOutput(debugLogFile)
}
if len(os.Getenv("ENCRYPT_KEY")) != 32 {
log.Fatal("Invalid ENCRYPT_KEY")
}
if os.Getenv("VPN_CLIENT_CONFIG_FILE") == "" {
os.Setenv("VPN_CLIENT_CONFIG_FILE", "/tmp/myvpn-client-config")
}
setup, err := installer.CreateInstaller(os.Getenv("VPN_TYPE"))
if err != nil {
log.Fatal(err)
}
go func() {
setup.RunPreStage()
}()
stopHttp := make(chan bool)
http.HandleFunc("/", handler.HandleState(setup, stopHttp, os.Getenv("ENCRYPT_KEY")))
http.HandleFunc("/debug", func(writer http.ResponseWriter, request *http.Request) {
if false == system.DebugEnabled() {
writer.WriteHeader(404)
return
}
b, err := ioutil.ReadFile(system.DEBUG_FILE)
if err != nil {
log.Println(err)
return
}
writer.Write(b)
})
ip, err := system.PublicIpAddr()
if err != nil {
log.Println("Failed to get public ip addr")
return
}
domain := system.DomainName(ip)
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(domain),
}
serveTLS := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
serverCert := &http.Server{
Addr: ":80",
Handler: certManager.HTTPHandler(nil),
}
serverHttp := &http.Server{
Addr: ":8400",
Handler: nil,
}
// serve :80 - ACME challenge
go func() {
if err = serverCert.ListenAndServe(); err != nil {
if err.Error() == "http: Server closed" {
fmt.Println("Shutdown :80 success")
} else {
fmt.Printf("Start cert serve error: %s\n", err)
}
}
}()
// serve :8400 - API
go func() {
if err = serverHttp.ListenAndServe(); err != nil {
if err.Error() == "http: Server closed" {
fmt.Println("Shutdown :8400 success")
} else {
fmt.Printf("Start cert serve error: %s\n", err)
}
}
}()
// serve :443 - API
go func() {
if err = serveTLS.ListenAndServeTLS("", ""); err != nil {
if err.Error() == "http: Server closed" {
fmt.Println("Shutdown :443 success")
} else {
fmt.Printf("Start TLS error: %s\n", err)
}
}
}()
// wait finish
for {
select {
case <-stopHttp:
if err = serverCert.Shutdown(context.Background()); err != nil {
fmt.Printf("Failed shutdown :80 error: %s\n", err.Error())
}
if err = serveTLS.Shutdown(context.Background()); err != nil {
fmt.Printf("Failed shutdown :443 error: %s\n", err.Error())
}
if err = serverHttp.Shutdown(context.Background()); err != nil {
fmt.Printf("Failed shutdown :8400 error: %s\n", err.Error())
}
setup.RunPostStage()
os.Exit(0) // turn off agent response are delivered
}
}
}