-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (91 loc) · 2.71 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
package main
import (
"log"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/caarlos0/env"
"github.com/michalnicp/mole/ssh"
)
type config struct {
// ServerName is the server's fully qualified domain name (hostname).
ServerName string `env:"SERVER_NAME"`
HTTPAddr string `env:"HTTP_ADDR" envDefault:":8080"`
SSHAddr string `env:"SSH_ADDR" envDefault:":2022"`
SSHHostKey string `env:"SSH_HOST_KEY" envDefault:"ssh_host_key"`
SSHAuthorizedKeys string `env:"SSH_AUTHORIZED_KEYS" envDefault:"authorized_keys"`
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
// read config from env
var cfg config
if err := env.Parse(&cfg); err != nil {
log.Fatalf("parse environment: %v", err)
}
// create ssh server
sshServer, err := ssh.NewServer(cfg.SSHAddr)
if err != nil {
log.Fatalf("create ssh server: %v", err)
}
if err := sshServer.Start(); err != nil {
log.Fatalf("start ssh server: %v", err)
}
// create reverse proxy
domain := cfg.ServerName
proxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
subdomain := strings.TrimSuffix(req.Host, "."+domain)
id64, err := strconv.ParseUint(subdomain, 10, 64)
if err != nil {
log.Printf("parse host: %s: %v", req.Host, err)
return
}
id := uint(id64)
addr, ok := sshServer.GetForwardAddr(id)
if !ok {
log.Printf("forward address not found: %d", id)
return
}
// If there was an error above this line, the scheme will not be set.
// This results in the request failing with a 502 Bad Gateway, which
// is what we want. See proxy.ErrorHandler.
req.URL.Scheme = "http"
req.URL.Host = addr
// TODO: Should we modify req.Host as well?
// req.Host = req.URL.Host
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
if err.Error() != "unsupported protocol scheme \"\"" {
log.Printf("http: proxy error: %v", err)
}
w.WriteHeader(http.StatusBadGateway)
},
}
httpServer := http.Server{
Addr: cfg.HTTPAddr,
Handler: proxy,
}
go func() {
log.Printf("starting http server; http_addr=%s", cfg.HTTPAddr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("start http server: %v", err)
}
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
s := <-sigs
log.Printf("caught %s, shutting down", s)
if err := httpServer.Close(); err != nil {
log.Printf("close http server: %v", err)
}
if err := sshServer.Close(); err != nil {
log.Printf("close ssh server: %v", err)
}
// FIXME: get this down to 2
// time.Sleep(300 * time.Millisecond)
// pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}