-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (163 loc) · 3.97 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
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
package main
import (
"flag"
"fmt"
"github.com/lextoumbourou/goodhosts"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"os/user"
"path"
"strconv"
"strings"
"syscall"
"time"
)
var childProcesses = make([]*os.Process, 0)
var verbose = false
func StartKubectlProxy(svc *Service) error {
if verbose {
log.Printf("Starting %s (%s) proxy at *:%d", svc.Target, svc.Namespace, svc.LocalPort)
}
cmd := exec.Command(
"kubectl",
"port-forward",
"--namespace",
svc.Namespace,
svc.Target,
fmt.Sprintf("%d:%d", svc.LocalPort, svc.Port),
)
err := cmd.Start()
if err == nil {
childProcesses = append(childProcesses, cmd.Process)
}
return err
}
func GetManifestFilename() string {
if flag.NArg() > 0 {
return flag.Arg(0)
}
tryFiles := []string{"proxy.yaml", "proxy.yml"}
if usr, err := user.Current(); err == nil {
tryFiles = append(
tryFiles,
path.Join(usr.HomeDir, "proxy.yaml"),
path.Join(usr.HomeDir, ".proxy.yaml"),
path.Join(usr.HomeDir, "proxy.yml"),
path.Join(usr.HomeDir, ".proxy.yml"),
)
}
for _, filename := range tryFiles {
if _, err := os.Stat(filename); err == nil {
return filename
}
}
log.Println("Manifest file is not defined.")
Exit(1)
return ""
}
func Exit(code int) {
for _, proc := range childProcesses {
if err := proc.Kill(); err != nil {
log.Println(err)
}
}
os.Exit(code)
}
func ListenSignals() {
channel := make(chan os.Signal, 1)
signal.Notify(channel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGQUIT)
go func() {
s := <-channel
log.Println("Got signal", s)
Exit(1)
}()
}
func main() {
ListenSignals()
noHosts := false
listen := ""
insecure := false
flag.BoolVar(&noHosts, "no-hosts", false, "Disable hosts file changes")
flag.StringVar(&listen, "listen", "", "Listen address")
flag.BoolVar(&insecure, "insecure", false, "Disable SSL certificate verification")
flag.BoolVar(&verbose, "verbose", false, "Print detailed logs")
flag.Parse()
manifest, err := ReadManifest(GetManifestFilename()); if err != nil {
panic(err)
}
hosts, err := goodhosts.NewHosts(); if err != nil {
panic(err)
}
rules := make(map[string]string, len(manifest.Services))
hostsModified := false
if listen == "" {
listen = manifest.Listen
}
port, err := strconv.ParseInt(strings.Split(listen, ":")[1], 10, 32); if err != nil {
log.Println("Invalid port number")
Exit(1)
}
println("Proxyctl will redirect requests from those domains:")
for _, svc := range manifest.Services {
// Update proxy rule
rules[svc.Name] = fmt.Sprintf("%s://%s:%d", svc.Protocol, svc.Host, svc.LocalPort)
// Start kubectl port-forward proxy process
if err := StartKubectlProxy(&svc); err != nil {
log.Println(err)
Exit(2)
}
// Add entry to hosts file
if !noHosts && !hosts.Has("127.0.0.1", svc.Name) {
hostsModified = true
hosts.Lines = append(hosts.Lines, goodhosts.HostsLine{
IP: "127.0.0.1",
Hosts: []string{svc.Name},
Raw: fmt.Sprintf("127.0.0.1 %s", svc.Name),
})
}
// Print URL
if manifest.TLS.Enabled {
if port == 443 {
fmt.Printf("https://%s/\n", svc.Name)
} else {
fmt.Printf("https://%s:%d/\n", svc.Name, port)
}
} else {
if port == 80 {
fmt.Printf("http://%s/\n", svc.Name)
} else {
fmt.Printf("http://%s:%d/\n", svc.Name, port)
}
}
}
if hostsModified {
if err := hosts.Flush(); err != nil {
log.Println("Failed to write hosts file.", err)
Exit(2)
}
}
// Skip SSL verification if --insecure flag passed
if insecure || manifest.Insecure {
DisableSSL()
}
// Wait a seconds for kubectl proxies to fire up
time.Sleep(1 * time.Second)
// Create handler
handler := ProxyHandler{rules}
// Listen and serve
log.Printf("Listening at %s\n", listen)
if manifest.TLS.Enabled {
if err := http.ListenAndServeTLS(listen, manifest.TLS.Cert, manifest.TLS.Key, handler); err != nil {
log.Println(err)
Exit(2)
}
} else {
if err := http.ListenAndServe(listen, handler); err != nil {
log.Println(err)
Exit(2)
}
}
}