dispatch/server/server.go

166 lines
3.7 KiB
Go
Raw Normal View History

package server
import (
2017-04-15 02:48:24 +00:00
"crypto/tls"
"log"
2016-01-04 18:26:32 +00:00
"net"
"net/http"
2016-01-04 18:26:32 +00:00
"net/http/httputil"
"net/url"
"strings"
2016-03-01 00:51:26 +00:00
"github.com/gorilla/websocket"
"github.com/spf13/viper"
2016-01-04 18:26:32 +00:00
"github.com/khlieng/dispatch/letsencrypt"
2015-12-11 03:35:48 +00:00
"github.com/khlieng/dispatch/storage"
)
var (
sessions *sessionStore
channelStore *storage.ChannelStore
2015-05-01 22:20:22 +00:00
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
2016-01-04 18:26:32 +00:00
func Run() {
sessions = newSessionStore()
channelStore = storage.NewChannelStore()
if viper.GetBool("dev") {
upgrader.CheckOrigin = func(r *http.Request) bool {
return true
}
}
2016-01-04 18:26:32 +00:00
reconnectIRC()
initAuth()
initFileServer()
2016-01-04 18:26:32 +00:00
startHTTP()
}
2016-01-04 18:26:32 +00:00
func startHTTP() {
port := viper.GetString("port")
2016-01-04 18:26:32 +00:00
if viper.GetBool("https.enabled") {
portHTTPS := viper.GetString("https.port")
redirect := viper.GetBool("https.redirect")
if redirect {
2016-01-04 18:26:32 +00:00
log.Println("[HTTP] Listening on port", port, "(HTTPS Redirect)")
go http.ListenAndServe(":"+port, createHTTPSRedirect(portHTTPS))
}
server := &http.Server{
Addr: ":" + portHTTPS,
Handler: http.HandlerFunc(serve),
}
2016-01-04 18:26:32 +00:00
if certExists() {
log.Println("[HTTPS] Listening on port", portHTTPS)
server.ListenAndServeTLS(viper.GetString("https.cert"), viper.GetString("https.key"))
2016-01-04 18:26:32 +00:00
} else if domain := viper.GetString("letsencrypt.domain"); domain != "" {
dir := storage.Path.LetsEncrypt()
email := viper.GetString("letsencrypt.email")
lePort := viper.GetString("letsencrypt.port")
if viper.GetBool("letsencrypt.proxy") && lePort != "" && (port != "80" || !redirect) {
log.Println("[HTTP] Listening on port 80 (Let's Encrypt Proxy))")
go http.ListenAndServe(":80", http.HandlerFunc(letsEncryptProxy))
}
2016-01-15 18:48:03 +00:00
letsEncrypt, err := letsencrypt.Run(dir, domain, email, ":"+lePort)
2016-01-04 18:26:32 +00:00
if err != nil {
log.Fatal(err)
}
2017-04-15 02:48:24 +00:00
server.TLSConfig = &tls.Config{
GetCertificate: letsEncrypt.GetCertificate,
}
log.Println("[HTTPS] Listening on port", portHTTPS)
2017-04-15 02:48:24 +00:00
log.Fatal(server.ListenAndServeTLS("", ""))
2016-01-04 18:26:32 +00:00
} else {
log.Fatal("Could not locate SSL certificate or private key")
}
} else {
if viper.GetBool("dev") {
// The node dev server will proxy index page requests and
// websocket connections to this port
port = "1337"
}
2016-01-04 18:26:32 +00:00
log.Println("[HTTP] Listening on port", port)
log.Fatal(http.ListenAndServe(":"+port, http.HandlerFunc(serve)))
}
}
func serve(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(404)
return
}
if r.URL.Path == "/ws" {
session := handleAuth(w, r)
if session == nil {
log.Println("[Auth] No session")
w.WriteHeader(500)
return
}
upgradeWS(w, r, session)
} else {
serveFiles(w, r)
}
}
func upgradeWS(w http.ResponseWriter, r *http.Request, session *Session) {
conn, err := upgrader.Upgrade(w, r, w.Header())
2015-05-01 22:20:22 +00:00
if err != nil {
log.Println(err)
return
}
newWSHandler(conn, session, r).run()
2015-05-01 22:20:22 +00:00
}
2016-01-04 18:26:32 +00:00
func createHTTPSRedirect(portHTTPS string) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/.well-known/acme-challenge") {
letsEncryptProxy(w, r)
return
}
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}
2016-01-04 18:26:32 +00:00
u := url.URL{
Scheme: "https",
Host: net.JoinHostPort(host, portHTTPS),
Path: r.RequestURI,
}
w.Header().Set("Location", u.String())
w.WriteHeader(http.StatusMovedPermanently)
})
}
func letsEncryptProxy(w http.ResponseWriter, r *http.Request) {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}
2016-01-04 18:26:32 +00:00
upstream := &url.URL{
Scheme: "http",
Host: net.JoinHostPort(host, viper.GetString("letsencrypt.port")),
}
httputil.NewSingleHostReverseProxy(upstream).ServeHTTP(w, r)
}