From 6e0afd6e2a1c8a2848b39c9e2ac4ec50ffcc4187 Mon Sep 17 00:00:00 2001 From: deranjer Date: Thu, 1 Mar 2018 15:31:11 -0500 Subject: [PATCH] Fixing some API issues, adding a few API responses --- config.toml | 8 ++++---- engine/clientStructs.go | 6 ++++++ main.go | 28 ++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/config.toml b/config.toml index dc614d96..2fd387b3 100644 --- a/config.toml +++ b/config.toml @@ -8,7 +8,7 @@ SeedRatioStop = 1.50 #automatically stops the torrent after it reaches this seeding ratio #Relative or absolute path accepted, the server will convert any relative path to an absolute path. - DefaultMoveFolder = 'Z:\downloads' #default path that a finished torrent is symlinked to after completion. Torrents added via RSS will default here + DefaultMoveFolder = 'downloads' #default path that a finished torrent is symlinked to after completion. Torrents added via RSS will default here TorrentWatchFolder = 'torrentUpload' #folder path that is watched for .torrent files and adds them automatically every 5 minutes #Limits your upload and download speed globally, all are averages and not burst protected (usually burst on start). @@ -25,13 +25,13 @@ [notifications] - PushBulletToken = "o.8sUHemPkTCaty3u7KnyvEBN19EkeT63g" #add your pushbullet api token here to notify of torrent completion to pushbullet + PushBulletToken = "" #add your pushbullet api token here to notify of torrent completion to pushbullet [reverseProxy] #This is for setting up goTorrent behind a reverse Proxy (with SSL, reverse proxy with no SSL will require editing the WSS connection to a WS connection manually) - ProxyEnabled = true #bool, either false or true + ProxyEnabled = false #bool, either false or true #URL is CASE SENSITIVE - BaseURL = "derajnet.duckdns.org/gopher/" # MUST be in the format (if you have a subdomain, and must have trailing slash) "yoursubdomain.domain.org/subroute/" + BaseURL = "domain.com/subroute/" # MUST be in the format (if you have a subdomain, and must have trailing slash) "yoursubdomain.domain.org/subroute/" [EncryptionPolicy] diff --git a/engine/clientStructs.go b/engine/clientStructs.go index 97617183..cbbef9dc 100644 --- a/engine/clientStructs.go +++ b/engine/clientStructs.go @@ -19,6 +19,12 @@ type Message struct { //Next are the messages the server sends to the client +//AuthResponse is sent when the client fails to perform authentication correctly +type AuthResponse struct { + MessageType string + Payload string +} + //ServerPushMessage is information (usually logs and status messages) that the server pushes to the client type ServerPushMessage struct { MessageType string diff --git a/main.go b/main.go index 04274993..4cfbe8bd 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,9 @@ var ( var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + return true + }, } func serveHome(w http.ResponseWriter, r *http.Request) { @@ -48,8 +51,16 @@ func serveHome(w http.ResponseWriter, r *http.Request) { func handleAuthentication(conn *websocket.Conn, db *storm.DB) { msg := Engine.Message{} err := conn.ReadJSON(&msg) - payloadData := msg.Payload.(map[string]interface{}) - clientAuthToken := payloadData["ClientAuthString"].(string) + conn.WriteJSON(msg) //TODO just for testing, remove + payloadData, ok := msg.Payload.(map[string]interface{}) + clientAuthToken, tokenOk := payloadData["ClientAuthString"].(string) + fmt.Println("ClientAuthToken:", clientAuthToken, "TokenOkay", tokenOk, "PayloadData", payloadData, "PayloadData Okay?", ok) + if ok == false || tokenOk == false { + authFail := Engine.AuthResponse{MessageType: "authResponse", Payload: "Message Payload in AuthRequest was malformed, closing connection"} + conn.WriteJSON(authFail) + conn.Close() + return + } if err != nil { Logger.WithFields(logrus.Fields{"error": err, "SuppliedToken": clientAuthToken}).Error("Unable to read authentication message") } @@ -63,10 +74,15 @@ func handleAuthentication(conn *websocket.Conn, db *storm.DB) { return singingKey, nil }) if err != nil { + authFail := Engine.AuthResponse{MessageType: "authResponse", Payload: "Parsing of Token failed, ensure you have the correct token! Closing Connection"} + conn.WriteJSON(authFail) Logger.WithFields(logrus.Fields{"error": err, "SuppliedToken": token}).Error("Unable to parse token!") conn.Close() + return } if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { + authTrue := Engine.AuthResponse{MessageType: "authResponse", Payload: "Authentication Verified, proceed with commands."} + conn.WriteJSON(authTrue) fmt.Println("Claims", claims["ClientName"], claims["Issuer"]) Authenticated = true } else { @@ -197,7 +213,9 @@ func main() { router.HandleFunc("/websocket", func(w http.ResponseWriter, r *http.Request) { //websocket is the main data pipe to the frontend conn, err := upgrader.Upgrade(w, r, nil) - fmt.Println("Websocket connection here") + fmt.Println("Websocket connection established, awaiting authentication") + connResponse := Engine.ServerPushMessage{MessageType: "connectResponse", MessageLevel: "Message", Payload: "Websocket Connection Established, awaiting Authentication"} + conn.WriteJSON(&connResponse) defer conn.Close() //defer closing the websocket until done. if err != nil { Logger.WithFields(logrus.Fields{"error": err}).Fatal("Unable to create websocket!") @@ -206,6 +224,8 @@ func main() { if Authenticated != true { handleAuthentication(conn, db) } else { //If we are authenticated inject the connection into the other packages + connResponse := Engine.ServerPushMessage{MessageType: "authResponse", MessageLevel: "Message", Payload: "Already Authenticated... Awaiting Commands"} + conn.WriteJSON(&connResponse) Logger.Info("Authenticated, websocket connection available!") } Engine.Conn = conn @@ -222,7 +242,7 @@ func main() { break MessageLoop } var payloadData map[string]interface{} - if msg.Payload != nil { + if msg.Payload != nil && msg.Payload != "" { payloadData = msg.Payload.(map[string]interface{}) } Logger.WithFields(logrus.Fields{"message": msg}).Debug("Message From Client")