forked from shayne/vmproxy
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (41 loc) · 969 Bytes
/
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
package main
import (
"log"
"net"
"net/http"
"net/netip"
"os"
"tailscale.com/tsnet"
"tailscale.com/types/logger"
"tailscale.com/util/must"
)
var (
vmName string
vncAddrPort string
)
func main() {
if len(os.Args) != 3 {
log.Fatal("usage: vmproxy <vm name> <vnc addrport>")
}
vmName = os.Args[1]
vncAddrPort = os.Args[2]
must.Get(netip.ParseAddrPort(vncAddrPort))
s := &tsnet.Server{
Logf: logger.Discard,
Hostname: "vmproxy",
Dir: "./state",
}
var ln net.Listener
ln = must.Get(s.Listen("tcp", ":22"))
go sshServer(ln)
ln = must.Get(s.Listen("tcp", ":80"))
go promoteHTTPS(ln)
ln = must.Get(s.ListenTLS("tcp", ":443"))
vncProxy(s, ln)
}
func promoteHTTPS(ln net.Listener) {
log.Print("HTTP server listening on port 80 (redirecting to HTTPS)")
must.Do(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusFound)
})))
}