From 4c51cd861a8861866c57bdb24813525190602475 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Fri, 13 Nov 2020 20:13:13 -0500 Subject: [PATCH] basic linting --- client/searchctl.go | 21 ++++++++++++++++----- poll/cli.go | 22 +++++++++++++++++++--- poll/headers.go | 1 - poll/instance.go | 9 +++++---- poll/main.go | 4 ++++ 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/client/searchctl.go b/client/searchctl.go index c237cab..6186bea 100644 --- a/client/searchctl.go +++ b/client/searchctl.go @@ -45,11 +45,11 @@ func main() { var commandMap CommandMap var responseback ResponseBack - if *shutdownPtr == true { + if *shutdownPtr { totalflags++ commandMap.Type = "shutdown" } - if *statusPtr == true { + if *statusPtr { totalflags++ commandMap.Type = "status" } @@ -89,8 +89,16 @@ func main() { // Send the request binary.LittleEndian.PutUint32(sizebytes, uint32(b)) - c.Write(sizebytes) - c.Write(commandByte) + _, err = c.Write(sizebytes) + if err != nil { + fmt.Println(err) + return + } + _, err = c.Write(commandByte) + if err != nil { + fmt.Println(err) + return + } // Read the response n, err := io.ReadFull(c, sizebytes) @@ -99,7 +107,10 @@ func main() { } jsonsize := int(binary.LittleEndian.Uint32(sizebytes)) responsebytes := make([]byte, jsonsize) - n, err = io.ReadFull(c, responsebytes) + _, err = io.ReadFull(c, responsebytes) + if err != nil { + fmt.Println("Read Error", err) + } err = json.Unmarshal(responsebytes, &responseback) if err != nil { fmt.Println("Unmarshal error", err) diff --git a/poll/cli.go b/poll/cli.go index bb361a2..21291b6 100644 --- a/poll/cli.go +++ b/poll/cli.go @@ -16,6 +16,10 @@ func handleClient(commandClient net.Conn, runninginstances *[]RunningInstance, i var commandmap CommandMap var responseback ResponseBack n, err := io.ReadFull(commandClient, sizebyte) + if err != nil { + fmt.Println("Read error", err) + os.Exit(1) + } if n != 4 { fmt.Println("Did not read 4 bytes, failure.") os.Exit(1) @@ -23,8 +27,12 @@ func handleClient(commandClient net.Conn, runninginstances *[]RunningInstance, i jsonsize := int(binary.LittleEndian.Uint32(sizebyte)) jsonbyte := make([]byte, jsonsize) n, err = io.ReadFull(commandClient, jsonbyte) + if err != nil { + fmt.Println("Unable to unmarshal") + os.Exit(1) + } if n != jsonsize { - fmt.Println("Failued to read json size of ", n) + fmt.Println("Failed to read json size of ", n) os.Exit(1) } @@ -67,7 +75,11 @@ func handleClient(commandClient net.Conn, runninginstances *[]RunningInstance, i n = len(responsebytes) binary.LittleEndian.PutUint32(sizebyte, uint32(n)) - commandClient.Write(sizebyte) + _, err = commandClient.Write(sizebyte) + if err != nil { + fmt.Println("Error on write:", err) + os.Exit(1) + } responsebyte, err := json.Marshal(responseback) if err != nil { @@ -75,6 +87,10 @@ func handleClient(commandClient net.Conn, runninginstances *[]RunningInstance, i os.Exit(1) } - commandClient.Write(responsebyte) + _, err = commandClient.Write(responsebyte) + if err != nil { + fmt.Println("Error on write:", err) + os.Exit(1) + } commandClient.Close() } diff --git a/poll/headers.go b/poll/headers.go index 177297d..848e928 100644 --- a/poll/headers.go +++ b/poll/headers.go @@ -7,7 +7,6 @@ const ( CLIENT_ISSUE = 600 ONION_PROTOCOL = 601 BAD_RESPONSE = 602 - NO_CONNECTION = 603 BAD_NODEINFO = 604 ) diff --git a/poll/instance.go b/poll/instance.go index f37d161..51e1943 100644 --- a/poll/instance.go +++ b/poll/instance.go @@ -37,7 +37,7 @@ func StartInstancePoll(instancereport InstanceReport, reportPostChan chan Report // Only placing this here to later have the option of using // an HTTP client via a SOCKS5 Tor proxy - if strings.Contains(instancereport.endpoint, ".onion") == true { + if strings.Contains(instancereport.endpoint, ".onion") { instanceReportChan <- InstanceReport{instancereport.endpoint, ONION_PROTOCOL, "", 0} return } @@ -48,11 +48,13 @@ func StartInstancePoll(instancereport InstanceReport, reportPostChan chan Report instanceReportChan <- InstanceReport{instancereport.endpoint, CLIENT_ISSUE, "", 0} return } - body, err := ioutil.ReadAll(resp.Body) + if err != nil { + instanceReportChan <- InstanceReport{instancereport.endpoint, BAD_RESPONSE, "", 0} + return + } err = json.Unmarshal(body, &newposts) if err != nil { -// instanceReportChan <- InstanceReport{instancereport.endpoint, resp.StatusCode, "", 0} instanceReportChan <- InstanceReport{instancereport.endpoint, BAD_RESPONSE, "", 0} return } @@ -147,7 +149,6 @@ func NewInstance(endpoint string, runninginstances *[]RunningInstance, instanceR q.endpoint = endpoint q.status = BAD_NODEINFO instanceReportChan <- q - return }() } diff --git a/poll/main.go b/poll/main.go index 3a1cf74..b177c63 100644 --- a/poll/main.go +++ b/poll/main.go @@ -90,6 +90,8 @@ func engine() { } defer l.Close() + x := 0 + commandClient := make(chan net.Conn) go func(l net.Listener) { @@ -112,6 +114,8 @@ func engine() { go writePost(pool, v) case w := <-instanceReportChan: // Start or suspend instance if w.status == NEW_INSTANCE { + fmt.Println("The instance: ", x, ", ", len(runninginstances)) + x = x + 1 NewInstance(w.endpoint, &runninginstances, instanceReportChan, reportPostChan) } else if w.status == RUNNING || w.status == TOOMANYREQUESTS { for i, runninginstance := range runninginstances {