-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
414 lines (353 loc) · 9.83 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"log"
"log/syslog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"sync"
"time"
"golang.org/x/crypto/acme/autocert"
)
type App struct {
Server *http.Server
Routes map[string]*Proxy
RoutesFile string
Mu sync.RWMutex
}
type DomainRoute struct {
Domain string `json:"domain"`
Port string `json:"port"`
}
type Proxy struct {
Port string
Proxy *httputil.ReverseProxy
}
type SerializableProxy struct {
Port string `json:"port"`
Domain string `json:"domain"`
}
func main() {
// setting up the logger
logger, err := syslog.NewLogger(syslog.LOG_INFO|syslog.LOG_DAEMON, log.LstdFlags)
if err != nil {
log.Fatalf("Failed to initialize syslog logger: %v", err)
return
}
log.SetOutput(logger.Writer())
routesFile := "routes.json"
// initializing a new app object
app := &App{
Routes: make(map[string]*Proxy),
RoutesFile: routesFile,
}
// load the routes we have already
loadedRoutes, err := LoadRoutes(routesFile)
if err != nil {
if !os.IsNotExist(err) {
log.Fatal(err)
}
} else {
app.Routes = loadedRoutes
}
// start the server in a goroutine
go app.startServer()
// start accepting input from the user interactively
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("> ")
if !scanner.Scan() {
break
}
line := scanner.Text()
args := strings.Fields(line)
if len(args) == 0 {
continue
}
// command handlers for the various things a user might do
switch args[0] {
case "list":
app.handleListCommand()
case "add":
if len(args) != 3 {
fmt.Println("Error: Incorrect number of arguments. Expected: add <domain> <port>")
continue
}
app.handleAddCommand(args[1], args[2])
case "remove":
if len(args) != 2 {
fmt.Println("Error: Incorrect number of arguments. Expected: remove <domain>")
continue
}
app.handleRemoveCommand(NormalizeDomain(args[1]))
case "save":
app.handleSaveCommand()
case "load":
app.handleLoadCommand()
case "help":
handleHelpCommand()
case "exit":
if app.Server != nil {
log.Println("Initiating server shutdown...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := app.Server.Shutdown(ctx); err != nil {
log.Printf("Server shutdown encountered an error: %v", err)
} else {
log.Println("Server shutdown successfully.")
}
}
log.Println("Exiting the application.")
return
default:
fmt.Println("Error: Unknown command. Type 'help' for available commands.")
}
}
if err := scanner.Err(); err != nil {
log.Printf("Error reading from stdin: %v\n", err)
}
}
// startServer sets up cerManager and an https api using a goroutine.
func (app *App) startServer() {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: func(ctx context.Context, host string) error {
app.Mu.RLock()
defer app.Mu.RUnlock()
if _, ok := app.Routes[host]; ok {
return nil
}
return fmt.Errorf("acme/autocert: host %q not configured in HostPolicy", host)
},
Cache: autocert.DirCache("tls"),
}
server := &http.Server{
Addr: ":https",
TLSConfig: certManager.TLSConfig(),
Handler: http.HandlerFunc(app.Handler()),
}
go func() {
http.HandleFunc("/", app.Handler())
// Serve on HTTP to satisfy the ACME HTTP-01 challenge and then redirect to HTTPS.
log.Fatal(http.ListenAndServe(":http", certManager.HTTPHandler(nil)))
}()
log.Fatal(server.ListenAndServeTLS("", ""))
}
// getAllDomains will make a list of all the routes for domains and apps
func (app *App) getAllDomains() []string {
app.Mu.RLock()
defer app.Mu.RUnlock()
domains := make([]string, 0, len(app.Routes))
for domain := range app.Routes {
domains = append(domains, domain)
}
return domains
}
// LoadRoutes will open the config file and add the routes found.
func LoadRoutes(file string) (map[string]*Proxy, error) {
// open the file
f, err := os.Open(file)
if err != nil {
log.Printf("Error opening file %s: %v", file, err)
return nil, err
}
defer func() {
if cerr := f.Close(); cerr != nil {
log.Printf("Error closing file %s after reading: %v", file, cerr)
}
}()
// read the file
var routes []DomainRoute
d := json.NewDecoder(f)
if err := d.Decode(&routes); err != nil {
return nil, fmt.Errorf("error decoding JSON from file %s: %w", file, err)
}
// make the routes
var failedRoutes int
rp := make(map[string]*Proxy)
for _, route := range routes {
// normalize the domains for dev sanity and wasted weekends
route.Domain = NormalizeDomain(route.Domain)
log.Println("-> route found: " + route.Domain + ":" + route.Port)
target, err := url.Parse("http://localhost:" + route.Port)
if err != nil {
log.Printf("Error parsing URL for domain %s: %v. Skipping this route.", route.Domain, err)
failedRoutes++
continue
}
// create our proxy
rp[route.Domain] = &Proxy{
Port: route.Port,
Proxy: httputil.NewSingleHostReverseProxy(target),
}
}
// else
if failedRoutes > 0 {
log.Printf("%d routes failed to load due to errors.", failedRoutes)
}
return rp, nil
}
// SaveRoutes will create and write to a config file using json
func SaveRoutes(file string, routes map[string]*Proxy) error {
// in this function we use a temporary file as a way of not clobbering
// a good config with incomplete new data like if the process fails of
// creating the new incoming config or if something else fails. this
// way we dont write a bunch of bs to the config that we have in hand.
tempFile := file + ".tmp"
// create the temporary file
f, err := os.Create(tempFile)
if err != nil {
log.Printf("Failed to create temporary file %s: %v", tempFile, err)
return err
}
var serializableRoutes []SerializableProxy
for domain, proxy := range routes {
serializableRoutes = append(serializableRoutes, SerializableProxy{
Port: proxy.Port,
Domain: domain,
})
}
// make the json with our routes
e := json.NewEncoder(f)
err = e.Encode(serializableRoutes)
if err != nil {
log.Printf("Error while encoding JSON: %v", err)
f.Close()
os.Remove(tempFile)
return err
}
// close the temp file
if err := f.Close(); err != nil {
log.Printf("Error while closing temporary file %s: %v", tempFile, err)
os.Remove(tempFile)
return err
}
// pinnochio.gif
if err := os.Rename(tempFile, file); err != nil {
log.Printf("Failed to rename temporary file %s to %s: %v", tempFile, file, err)
return err
}
return nil
}
// NormalizeDomain will solve all of your problems but it won't bring your weekend back.
func NormalizeDomain(domain string) string {
return strings.TrimPrefix(strings.ToLower(domain), "www.")
}
// Handler will receive an http request and route it appropriately
func (app *App) Handler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
app.Mu.RLock()
domain := NormalizeDomain(r.Host)
route, found := app.Routes[domain]
app.Mu.RUnlock()
if !found {
http.Error(w, "Not found", http.StatusNotFound)
return
}
// why do we even have this if we all *
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.WriteHeader(http.StatusOK)
return
}
route.Proxy.ServeHTTP(w, r)
}
}
// NewRoute should probably be part of Handler tbh
func NewRoute(routes map[string]*Proxy, domain string, port string, mu *sync.RWMutex) error {
domain = NormalizeDomain(domain)
target, err := url.Parse("http://localhost:" + port)
if err != nil {
return err
}
routes[domain] = &Proxy{
Port: port,
Proxy: httputil.NewSingleHostReverseProxy(target),
}
return nil
}
func handleHelpCommand() {
fmt.Printf(`
Commands:
- list: List all of the domain-port mappings.
- add <domain> <port>: Add a mapping for the domain on the specified port.
ex: add example.com 3000
- remove <domain>: Remove a mapping for the domain.
ex: remove example.com
- save [filepath]: Save the routes to the specified filepath or default path if not specified.
- load [filepath]: Load routes from the specified filepath or default path if not specified.
- help: Show this help.
- exit: Exit the program.
`)
}
func (app *App) handleListCommand() {
app.Mu.RLock()
defer app.Mu.RUnlock()
for domain, proxy := range app.Routes {
fmt.Printf("Domain: %s, Port: %s\n", domain, proxy.Port)
}
}
func (app *App) handleAddCommand(domain, port string) {
app.Mu.Lock()
defer app.Mu.Unlock()
err := NewRoute(app.Routes, domain, port, &app.Mu)
if err != nil {
fmt.Printf("Error adding route: %v\n", err)
return
}
err = SaveRoutes(app.RoutesFile, app.Routes)
if err != nil {
log.Println("Failed to save routes after adding:", err)
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Added new route for domain: %s on port: %s\n", domain, port)
}
func (app *App) handleRemoveCommand(domain string) {
app.Mu.Lock()
defer app.Mu.Unlock()
if _, exists := app.Routes[domain]; !exists {
fmt.Printf("Error: No such domain: %s\n", domain)
return
}
delete(app.Routes, domain)
err := SaveRoutes(app.RoutesFile, app.Routes)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Removed route for domain: %s\n", domain)
}
}
func (app *App) handleSaveCommand() {
err := SaveRoutes(app.RoutesFile, app.Routes)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Routes saved to: %s\n", app.RoutesFile)
}
}
func (app *App) handleLoadCommand() error {
routes, err := LoadRoutes(app.RoutesFile)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Error: No such file: %s\n", app.RoutesFile)
return err
} else {
fmt.Printf("Error: %v\n", err)
return err
}
}
fmt.Printf("Routes loaded from: %s\n", app.RoutesFile)
app.Mu.Lock()
app.Routes = routes
app.Mu.Unlock()
return nil
}