more code cleanups

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-26 19:35:13 +00:00
parent 1ef3c8f2dd
commit 936b2d6c91
2 changed files with 58 additions and 125 deletions

View File

@ -3,7 +3,6 @@ package main
import "net/http"
import "io/ioutil"
import "log"
import "fmt"
import "bytes"
import "github.com/tidwall/gjson"
@ -42,18 +41,20 @@ func fetchJSON(GETorPOST string, token string, a string) []byte {
return body
}
func getToken(ge *pb.Event) string {
func updateToken(ge *pb.Event) bool {
if ge == nil {
log.Println("getToken() has ge = nil")
return ""
log.Println("updateToken() has ge = nil")
ge.Comment = "updateToken() has ge = nil"
return false
}
if ge.Account == nil {
log.Println("getToken() has ge.Account = nil")
return ""
log.Println("updateToken() has ge.Account = nil")
ge.Comment = "updateToken() has ge.Account = nil"
return false
}
url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
log.Println("URL =", url)
log.Println("updateToken() URL =", url)
var jsonStr = []byte(`{}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
@ -64,18 +65,27 @@ func getToken(ge *pb.Event) string {
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return ""
ge.Comment = err.Error()
return false
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
log.Println("updateToken() response Status:", resp.Status)
log.Println("updateToken() response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
log.Println("updateToken() response Body:", string(body))
json := string(body)
jwt := gjson.Get(json, "jwt")
// log.Println("TESTST SATDEA jwt =", jwt)
return jwt.String()
if (jwt.String() == "") {
log.Println("updateToken() GOT TOKEN == nil")
ge.Comment = string(body)
return false
}
if (jwt.String() != ge.Account.Token) {
log.Println("updateToken() GOT NEW TOKEN", jwt)
ge.Account.Token = jwt.String()
}
log.Println("updateToken() END")
return true
}

View File

@ -23,18 +23,17 @@ type network struct {
Subdomain string `json:"subdomain"`
}
func checkLogin(ge *pb.Event) string {
if (origLoginCheck(ge) == true) {
log.Println("origLoginCheck() LOGIN WORKED")
return ge.Account.Token
func checkLogin(ge *pb.Event) bool {
if (ge.Account == nil) {
log.Println("checkLogin() pb.Event.Account == nil ERROR")
return false
}
log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)")
log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)")
log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)")
ge.Account.URL = "http://stackapi:4000"
token := getToken(ge)
return token
if (origLoginCheck(ge) == true) {
log.Println("checkLogin() LOGIN WORKED")
return true
}
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
return updateToken(ge)
}
func fetchNetworks(token string) []network {
@ -120,18 +119,6 @@ var upgrader = websocket.Upgrader{
},
}
/*
func sendSampleProtobuf(conn *websocket.Conn) {
msg := pb.CreateSampleEvent()
msg.Name = "test echo over gorilla websocket"
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
log.Println("sendProtobuf() write:", err)
}
}
*/
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
@ -163,10 +150,15 @@ func eventHandler(w http.ResponseWriter, r *http.Request) {
}
if mytype == websocket.TextMessage {
log.Println("echoHandler() TextMessage mytype=", mytype)
// 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("echoHandler() GOT websocket.TextMessage (ERROR. NO HANDLER FOR THIS YET)")
log.Println("echoHandler() GOT websocket.TextMessage (ERROR. THIS SHOULD NEVER HAPPEN)")
log.Println("echoHandler() GOT websocket.TextMessage (ERROR. SUSPEND THIS USER)")
}
if mytype == websocket.BinaryMessage {
log.Println("echoHandler() BinaryMessage mytype=", mytype)
log.Println("echoHandler() GOT websocket.BinaryMessage")
pdata := new(pb.Event)
err = proto.Unmarshal(message, pdata)
if (err != nil) {
@ -226,12 +218,9 @@ func main() {
func processGetEvent(ge *pb.Event) *pb.Event {
log.Println("processGetEvent() START")
if (ge.Account == nil) {
log.Println("processGetEvent() ERROR ge.Account = nil")
log.Println("processGetEvent() ERROR ge.Account = nil")
log.Println("processGetEvent() ERROR ge.Account = nil")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "Account = nil"
e.Comment = "LOGIN FAILED"
return e
}
log.Println("processGetEvent() ge.Account.Token =", ge.Account.Token)
@ -273,28 +262,16 @@ func processGetEvent(ge *pb.Event) *pb.Event {
}
func processLoginEvent(ge *pb.Event) *pb.Event {
log.Println("processLoginEvent() START")
log.Println("processLoginEvent() ge.Account.Token =", ge.Account.Token)
log.Println("processLoginEvent() NEED TO LOGIN HERE OR CHECK TOKEN WORKS")
token := checkLogin(ge)
if (token == "") {
log.Println("processLoginEvent() LOGIN START")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
return e
}
log.Println("processLoginEvent() LOGIN WORKED")
log.Println("processLoginEvent() LOGIN WORKED")
log.Println("processLoginEvent() LOGIN WORKED")
e := pb.MakeOkResponse()
e.Account = ge.Account
if (e.Account.Token != token) {
log.Println("processLoginEvent() GENERATED NEW TOKEN old =", e.Account.Token)
log.Println("processLoginEvent() GENERATED NEW TOKEN new =", token)
}
e.Account.Token = token
e.Comment = "LOGIN WORKED"
return e
}
@ -306,61 +283,6 @@ func processAddEvent(ge *pb.Event) *pb.Event {
return nil
}
/*
func jsonAddEvent(ge *pb.Event) bool {
var url string
var httpType string
httpType = "GET"
if ge.Type == pb.Event_LOGIN {
url = API + "me"
} else if ge.Type == pb.Event_ADD {
url = API + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
httpType = "POST"
} else if ge.Type == pb.Event_MIGRATE {
url = API + "clusters"
} else {
// if unknown, just check login
url = API + "me"
}
log.Println("jsonAddEvent() ", httpType, url)
req, err := http.NewRequest(httpType, url, nil)
if err != nil {
log.Println("can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return false
}
log.Printf("RESPONSE =", body)
if body == nil {
log.Println("RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Username = ", ge.Username)
if (email.String() == ge.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
return true
}
}
return false
}
*/
// This is the 'orig' code
// TODO: phase this out for more common code
func origLoginCheck(ge *pb.Event) bool {
@ -372,36 +294,37 @@ func origLoginCheck(ge *pb.Event) bool {
log.Println("origLoginCheck() ", httpType, url)
req, err := http.NewRequest(httpType, url, nil)
if err != nil {
log.Println("can't make new req")
log.Println("origLoginCheck() FAILED ", "can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
log.Println("origLoginCheck() FAILED ", "Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
log.Println("origLoginCheck() FAILED ", "can't read resp")
return false
}
log.Printf("RESPONSE =", string(body))
log.Printf("origLoginCheck() ", "RESPONSE =", string(body))
if body == nil {
log.Println("RESPONSE is nil")
log.Println("origLoginCheck() FAILED ", "RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
log.Println("origLoginCheck() ", "body = ", string(body))
log.Println("origLoginCheck() ", "body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Account.Username = ", ge.Account.Username)
log.Println("origLoginCheck() ", "email = ", email.String())
log.Println("origLoginCheck() ", "ge.Account.Username = ", ge.Account.Username)
if (email.String() == ge.Account.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
log.Println("origLoginCheck() THIS TOTALLY WORKED MAN")
return true
}
}
log.Println("origLoginCheck() FAILED ")
return false
}