2017-02-06 03:19:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-02-09 03:08:53 +00:00
|
|
|
|
|
|
|
jwt "github.com/dgrijalva/jwt-go"
|
2017-02-06 03:19:04 +00:00
|
|
|
)
|
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
//launchClientListener - starts up http listeners and handles various URI paths
|
2017-02-06 03:19:04 +00:00
|
|
|
func launchClientListener() {
|
2017-02-09 03:08:53 +00:00
|
|
|
loginfo.Println("starting WebRequestExternal Listener ", *argServerBinding)
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch url := r.URL.Path; url {
|
|
|
|
case "/":
|
|
|
|
handleConnectionWebSocket(connectionTable, w, r, false)
|
|
|
|
|
|
|
|
default:
|
|
|
|
http.Error(w, "Not Found", 404)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
s := &http.Server{
|
|
|
|
Addr: *argServerBinding,
|
|
|
|
Handler: mux,
|
|
|
|
}
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
err := s.ListenAndServeTLS("certs/fullchain.pem", "certs/privkey.pem")
|
2017-02-06 03:19:04 +00:00
|
|
|
if err != nil {
|
2017-02-09 03:08:53 +00:00
|
|
|
logfatal.Println("ListenAndServeTLS: ", err)
|
2017-02-06 03:19:04 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
// handleConnectionWebSocket handles websocket requests from the peer.
|
|
|
|
func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.ResponseWriter, r *http.Request, admin bool) {
|
|
|
|
loginfo.Println("websocket opening ", r.RemoteAddr, " ", r.Host)
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
tokenString := r.URL.Query().Get("access_token")
|
|
|
|
result, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return []byte(secretKey), nil
|
|
|
|
})
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
if err != nil || !result.Valid {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte("Not Authorized"))
|
|
|
|
loginfo.Println("access_token invalid...closing connection")
|
|
|
|
return
|
|
|
|
}
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
loginfo.Println("access_token valid")
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
claims := result.Claims.(jwt.MapClaims)
|
|
|
|
loginfo.Println("processing domains", claims["domains"])
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-09 03:08:53 +00:00
|
|
|
if admin == true {
|
|
|
|
loginfo.Println("Recognized Admin connection, waiting authentication")
|
|
|
|
} else {
|
|
|
|
loginfo.Println("Recognized connection, waiting authentication")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
loginfo.Println("WebSocket upgrade failed", err)
|
|
|
|
return
|
2017-02-06 03:19:04 +00:00
|
|
|
}
|
2017-02-09 03:08:53 +00:00
|
|
|
loginfo.Println("before connection table")
|
|
|
|
connection := &Connection{connectionTable: connectionTable, conn: conn, send: make(chan []byte, 256), source: r.RemoteAddr, admin: admin}
|
|
|
|
connection.connectionTable.register <- connection
|
|
|
|
go connection.writer()
|
|
|
|
//go connection.sender()
|
|
|
|
connection.reader()
|
2017-02-06 03:19:04 +00:00
|
|
|
}
|