From 00d19a95daadd41c986424577b403f063860c52a Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 29 May 2019 08:55:41 +0000 Subject: [PATCH] move gorilla websocket functions into their own file Signed-off-by: Jeff Carr --- gorilla-server/apiJSON.go | 27 --------- gorilla-server/main.go | 117 ------------------------------------- gorilla-server/pbsocket.go | 111 +++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 144 deletions(-) create mode 100644 gorilla-server/pbsocket.go diff --git a/gorilla-server/apiJSON.go b/gorilla-server/apiJSON.go index 666a3e5..7e656d6 100644 --- a/gorilla-server/apiJSON.go +++ b/gorilla-server/apiJSON.go @@ -7,33 +7,6 @@ import "io/ioutil" import "github.com/tidwall/gjson" import pb "git.wit.com/wit/witProtobuf" -/* -func main() { - ge := pb.MakeOkResponse() - c := pb.MakeDefaultConfig() - ge.Account = c.Accounts[0] - ge.Account.Email = "jcarr@wit.com" - ge.Account.Password = "yowzayowza" - ge.Account.URL = "http://stackapi:4000/" - // ge.Account.Token = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6Ijloa0VYa2M0YURxTlVqSElGU2VJYUJoOCIsImV4cCI6MTU1OTY3OTAxMywiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.eCfhg3_VYBZh1ve69p0Op4U9L2T9CAZY5qPtx7vNswe3TB-y1nz2X2vw4AzPwfy4fACKeQGkJmWGedt1NKQP-WaZNVtGpgX9g0yvloCob2OMQwsLhq3X7e83weiK_Jlk" - ge.Account.Token = "badtoken" - - junkEvent := processLoginEvent(ge) - log.Println("processAddEvent() junkEvent =", junkEvent) - log.Println("processAddEvent() START ge.Account =", ge.Account) - - tmp := string(GETbody(ge, "http://stackapi:4000/me")) - log.Println("processAddEvent() recieved json=", tmp) - - tmp = string(GETbody(ge, "http://stackapi:4000/clusters")) - log.Println("processAddEvent() recieved json=", tmp) - - url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25" - newVM := string(POSTbody(ge, url)) - log.Println("processAddEvent() recieved newVM=", newVM) -} -*/ - // check if a user can login here // tries generates a new TOKEN if the old one doesn't work // fails with the appropriate protobuf response to send diff --git a/gorilla-server/main.go b/gorilla-server/main.go index 03b5f5a..f394da5 100644 --- a/gorilla-server/main.go +++ b/gorilla-server/main.go @@ -1,39 +1,19 @@ package main import "flag" -import "io" import "log" import "net/http" import "encoding/json" import "io/ioutil" -import "github.com/golang/protobuf/proto" import pb "git.wit.com/wit/witProtobuf" -import "github.com/gorilla/websocket" import "github.com/davecgh/go-spew/spew" import "github.com/tidwall/gjson" -// const API = "http://stackapi:4000/" - type network struct { Subdomain string `json:"subdomain"` } -/* -func checkLogin(ge *pb.Event) bool { - if (ge.Account == nil) { - log.Println("checkLogin() pb.Event.Account == nil ERROR") - return false - } - if (origLoginCheck(ge) == true) { - log.Println("checkLogin() LOGIN WORKED") - return true - } - log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)") - return updateToken(ge) -} -*/ - func fetchNetworks(ge *pb.Event) []network { var networks []network req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil) @@ -117,103 +97,6 @@ func fetchVMs(ge *pb.Event, network string) []vm { return vms } -// Also potentially look at: -// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/ - -var upgrader = websocket.Upgrader{ - ReadBufferSize: 4096, - WriteBufferSize: 4096, - EnableCompression: true, - CheckOrigin: func(r *http.Request) bool { - return true - }, -} - -func sendProtobuf(conn *websocket.Conn, msg *pb.Event) { - log.Println("sendProtobuf() START") - data, _ := proto.Marshal(msg) - err := conn.WriteMessage(websocket.BinaryMessage, data) - if err != nil { - log.Println("sendProtobuf() write:", err) - } - log.Println("sendProtobuf() END") -} - -func eventHandler(w http.ResponseWriter, r *http.Request) { - log.Println("echoHandler() START") - log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent")) - log.Println("echoHandler() ATTEMPT r.Header", r.Header) - for key, value := range r.Header { - log.Println("echoHandler() HEADER key, value =", key, value) - } - - conn, err := upgrader.Upgrade(w, r, nil) - if err != nil { - log.Println("Upgrade:", err) - return - } - defer conn.Close() - - for { - mytype, message, err := conn.ReadMessage() - if err != nil { - log.Println("read:", err) - return - } - - if mytype == websocket.TextMessage { - // probably this should never happen since the client socket connection should have - // already been dropped. If a user is doing this, the account should probably be - // terminated as abuse - log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. NO HANDLER FOR THIS YET)") - log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. THIS SHOULD NEVER HAPPEN)") - log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. SUSPEND THIS USER)") - } - if mytype == websocket.BinaryMessage { - log.Println("\teventHandler() GOT websocket.BinaryMessage") - pdata := new(pb.Event) - var e *pb.Event - e = new(pb.Event) - err = proto.Unmarshal(message, pdata) - if (err != nil) { - log.Printf("readConn() something fucked up happened in Unmarshal") - } - log.Printf("\teventHandler() pdata.Type =", pdata.Type) - log.Printf("\teventHandler() recv binary: %s", pdata) - if pdata.Type == pb.Event_GET { - log.Printf("\teventHandler() DOING Event_GET") - e = processGetEvent(pdata) - } else if pdata.Type == pb.Event_LOGIN { - log.Printf("\teventHandler() DOING Event_LOGIN") - e = processLoginEvent(pdata) - } else if pdata.Type == pb.Event_ADD { - log.Printf("\teventHandler() DOING Event_ADD") - e = processAddEvent(pdata) - } - if (e == nil) { - e = pb.MakeFailResponse() - e.Comment = "websocket.BinaryMessage LOGIN FAILED" - } - e.Account = pdata.Account - log.Println("\teventHandler() e.Comment =", e.Comment) - sendProtobuf(conn, e) - } - } -} - -func serveHome(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - http.Error(w, "Not found.", http.StatusNotFound) - return - } - if r.Method != "GET" { - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) - return - } - w.Header().Set("Content-Type", "text/html; charset=utf-8") - io.WriteString(w, "Echo Server") -} - var addr = flag.String("addr", ":9000", "http service address") func main() { diff --git a/gorilla-server/pbsocket.go b/gorilla-server/pbsocket.go new file mode 100644 index 0000000..f347c15 --- /dev/null +++ b/gorilla-server/pbsocket.go @@ -0,0 +1,111 @@ +package main + +// These routines just recieve protobufs and pass protobufs back over +// a websocket (in this current case, a 'gorilla/websocket' +// +// These routines should probably be kept pretty generic in that regard + +import "io" +import "log" +import "net/http" + +import "github.com/golang/protobuf/proto" +import pb "git.wit.com/wit/witProtobuf" +import "github.com/gorilla/websocket" + +// Also potentially look at: +// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/ + +var upgrader = websocket.Upgrader{ + ReadBufferSize: 4096, + WriteBufferSize: 4096, + EnableCompression: true, + CheckOrigin: func(r *http.Request) bool { + return true + }, +} + +func sendProtobuf(conn *websocket.Conn, msg *pb.Event) { + log.Println("sendProtobuf() START") + data, _ := proto.Marshal(msg) + err := conn.WriteMessage(websocket.BinaryMessage, data) + if err != nil { + log.Println("sendProtobuf() write:", err) + } + log.Println("sendProtobuf() END") +} + +func eventHandler(w http.ResponseWriter, r *http.Request) { + log.Println("echoHandler() START") + log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent")) + log.Println("echoHandler() ATTEMPT r.Header", r.Header) + for key, value := range r.Header { + log.Println("echoHandler() HEADER key, value =", key, value) + } + + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + log.Println("Upgrade:", err) + return + } + defer conn.Close() + + for { + mytype, message, err := conn.ReadMessage() + if err != nil { + log.Println("read:", err) + return + } + + if mytype == websocket.TextMessage { + // probably this should never happen since the client socket connection should have + // already been dropped. If a user is doing this, the account should probably be + // terminated as abuse + log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. NO HANDLER FOR THIS YET)") + log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. THIS SHOULD NEVER HAPPEN)") + log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. SUSPEND THIS USER)") + } + if mytype == websocket.BinaryMessage { + log.Println("\teventHandler() GOT websocket.BinaryMessage") + pdata := new(pb.Event) + var e *pb.Event + e = new(pb.Event) + err = proto.Unmarshal(message, pdata) + if (err != nil) { + log.Printf("readConn() something fucked up happened in Unmarshal") + } + log.Printf("\teventHandler() pdata.Type =", pdata.Type) + log.Printf("\teventHandler() recv binary: %s", pdata) + if pdata.Type == pb.Event_GET { + log.Printf("\teventHandler() DOING Event_GET") + e = processGetEvent(pdata) + } else if pdata.Type == pb.Event_LOGIN { + log.Printf("\teventHandler() DOING Event_LOGIN") + e = processLoginEvent(pdata) + } else if pdata.Type == pb.Event_ADD { + log.Printf("\teventHandler() DOING Event_ADD") + e = processAddEvent(pdata) + } + if (e == nil) { + e = pb.MakeFailResponse() + e.Comment = "websocket.BinaryMessage LOGIN FAILED" + } + e.Account = pdata.Account + log.Println("\teventHandler() e.Comment =", e.Comment) + sendProtobuf(conn, e) + } + } +} + +func serveHome(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.Error(w, "Not found.", http.StatusNotFound) + return + } + if r.Method != "GET" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + io.WriteString(w, "Echo Server") +}