forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
131 lines (109 loc) · 4.48 KB
/
api.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
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"time"
gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/miekg/dns"
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/handlers"
"github.com/play-with-docker/play-with-docker/templates"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
)
func main() {
config.ParseFlags()
handlers.Bootstrap()
bypassCaptcha := len(os.Getenv("GOOGLE_RECAPTCHA_DISABLED")) > 0
// Start the DNS server
dns.HandleFunc(".", handlers.DnsRequest)
udpDnsServer := &dns.Server{Addr: ":53", Net: "udp"}
go func() {
err := udpDnsServer.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
tcpDnsServer := &dns.Server{Addr: ":53", Net: "tcp"}
go func() {
err := tcpDnsServer.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
server := handlers.Broadcast.GetHandler()
r := mux.NewRouter()
corsRouter := mux.NewRouter()
// Reverse proxy (needs to be the first route, to make sure it is the first thing we check)
//proxyHandler := handlers.NewMultipleHostReverseProxy()
//websocketProxyHandler := handlers.NewMultipleHostWebsocketReverseProxy()
tcpHandler := handlers.NewTCPProxy()
corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"}))
cmd := exec.Command("docker", "ps", "--filter label=com.docker.swarm.service.name=pwd", "--format '{{.Names}}'")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal("exec.Command of docker ps failed")
log.Fatal(err)
}
pwd := out.String()
fmt.Printf("pwd container name: %q\n", out.String())
// Specific routes
r.Host(fmt.Sprintf("{subdomain:.*}{%s}{node:%s}-{port:%s}.{tld:.*}", pwd, config.PWDHostnameRegex, config.PortRegex)).Handler(tcpHandler)
r.Host(fmt.Sprintf("{subdomain:.*}{%s}{node:%s}.{tld:.*}", pwd, config.PWDHostnameRegex)).Handler(tcpHandler)
r.Host(fmt.Sprintf("{%s}{alias:%s}-{session:%s}-{port:%s}.{tld:.*}", pwd, config.AliasnameRegex, config.AliasSessionRegex, config.PortRegex)).Handler(tcpHandler)
r.Host(fmt.Sprintf("{%s}{alias:%s}-{session:%s}.{tld:.*}", pwd, config.AliasnameRegex, config.AliasSessionRegex)).Handler(tcpHandler)
r.HandleFunc("/ping", handlers.Ping).Methods("GET")
corsRouter.HandleFunc("/instances/images", handlers.GetInstanceImages).Methods("GET")
corsRouter.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")
corsRouter.HandleFunc("/sessions/{sessionId}/setup", handlers.SessionSetup).Methods("POST")
corsRouter.HandleFunc("/sessions/{sessionId}/instances", handlers.NewInstance).Methods("POST")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/uploads", handlers.FileUpload).Methods("POST")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", handlers.DeleteInstance).Methods("DELETE")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/exec", handlers.Exec).Methods("POST")
r.HandleFunc("/p/{sessionId}", handlers.Home).Methods("GET")
r.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./www")))
r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/robots.txt")
})
r.HandleFunc("/sdk.js", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/sdk.js")
})
corsRouter.Handle("/sessions/{sessionId}/ws/", server)
r.Handle("/metrics", promhttp.Handler())
// Generic routes
r.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
if bypassCaptcha {
http.ServeFile(rw, r, "./www/bypass.html")
} else {
welcome, tmplErr := templates.GetWelcomeTemplate()
if tmplErr != nil {
log.Fatal(tmplErr)
}
rw.Write(welcome)
}
}).Methods("GET")
corsRouter.HandleFunc("/", handlers.NewSession).Methods("POST")
n := negroni.Classic()
r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter))))
n.UseHandler(r)
httpServer := http.Server{
Addr: "0.0.0.0:" + config.PortNumber,
Handler: n,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
}
go func() {
log.Println("Listening on port " + config.PortNumber)
log.Fatal(httpServer.ListenAndServe())
}()
go handlers.ListenSSHProxy("0.0.0.0:1022")
// Now listen for TLS connections that need to be proxied
handlers.StartTLSProxy(config.SSLPortNumber)
}