mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 10:36:12 +00:00
Allow reading data of any size through socket
This commit is contained in:
+42
-42
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -80,24 +81,22 @@ func main() {
|
|||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a buffer for incoming data.
|
|
||||||
buf := make([]byte, 4096)
|
|
||||||
|
|
||||||
// Read data from the connection.
|
// Read data from the connection.
|
||||||
n, err := conn.Read(buf)
|
data, err := readAllConn(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print json data if flag is set
|
// Print json data if flag is set
|
||||||
if *printJson {
|
if *printJson {
|
||||||
fmt.Println(string(buf[:n]))
|
fmt.Println(string(data))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decoode JSON data
|
// Decoode JSON data
|
||||||
var returnedJsonData map[string]any
|
var returnedJsonData map[string]any
|
||||||
err = json.Unmarshal(buf[:n], &returnedJsonData)
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
}
|
}
|
||||||
@@ -138,27 +137,22 @@ func main() {
|
|||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a buffer for incoming data.
|
|
||||||
buf := make([]byte, 4096)
|
|
||||||
|
|
||||||
// Read data from the connection.
|
// Read data from the connection.
|
||||||
n, err := conn.Read(buf)
|
data, err := readAllConn(conn)
|
||||||
if err == io.EOF {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print json data if flag is set
|
// Print json data if flag is set
|
||||||
if *printJson {
|
if *printJson {
|
||||||
fmt.Println(string(buf[:n]))
|
fmt.Println(string(data))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decoode JSON data
|
// Decoode JSON data
|
||||||
var returnedJsonData map[string]any
|
var returnedJsonData map[string]any
|
||||||
err = json.Unmarshal(buf[:n], &returnedJsonData)
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
}
|
}
|
||||||
@@ -214,27 +208,22 @@ func main() {
|
|||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a buffer for incoming data.
|
|
||||||
buf := make([]byte, 4096)
|
|
||||||
|
|
||||||
// Read data from the connection.
|
// Read data from the connection.
|
||||||
n, err := conn.Read(buf)
|
data, err := readAllConn(conn)
|
||||||
if err == io.EOF {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print json data if flag is set
|
// Print json data if flag is set
|
||||||
if *printJson {
|
if *printJson {
|
||||||
fmt.Println(string(buf[:n]))
|
fmt.Println(string(data))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decoode JSON data
|
// Decoode JSON data
|
||||||
var returnedJsonData map[string]any
|
var returnedJsonData map[string]any
|
||||||
err = json.Unmarshal(buf[:n], &returnedJsonData)
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
}
|
}
|
||||||
@@ -275,27 +264,22 @@ func main() {
|
|||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a buffer for incoming data.
|
|
||||||
buf := make([]byte, 4096)
|
|
||||||
|
|
||||||
// Read data from the connection.
|
// Read data from the connection.
|
||||||
n, err := conn.Read(buf)
|
data, err := readAllConn(conn)
|
||||||
if err == io.EOF {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print json data if flag is set
|
// Print json data if flag is set
|
||||||
if *printJson {
|
if *printJson {
|
||||||
fmt.Println(string(buf[:n]))
|
fmt.Println(string(data))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decoode JSON data
|
// Decoode JSON data
|
||||||
var returnedJsonData map[string]any
|
var returnedJsonData map[string]any
|
||||||
err = json.Unmarshal(buf[:n], &returnedJsonData)
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
}
|
}
|
||||||
@@ -342,27 +326,22 @@ func main() {
|
|||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a buffer for incoming data.
|
|
||||||
buf := make([]byte, 4096)
|
|
||||||
|
|
||||||
// Read data from the connection.
|
// Read data from the connection.
|
||||||
n, err := conn.Read(buf)
|
data, err := readAllConn(conn)
|
||||||
if err == io.EOF {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print json data if flag is set
|
// Print json data if flag is set
|
||||||
if *printJson {
|
if *printJson {
|
||||||
fmt.Println(string(buf[:n]))
|
fmt.Println(string(data))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decoode JSON data
|
// Decoode JSON data
|
||||||
var returnedJsonData map[string]any
|
var returnedJsonData map[string]any
|
||||||
err = json.Unmarshal(buf[:n], &returnedJsonData)
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
}
|
}
|
||||||
@@ -430,3 +409,24 @@ func dialSocket() {
|
|||||||
log.Fatalf("Failed to set write deadline! Error: %s\n", err)
|
log.Fatalf("Failed to set write deadline! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readAllConn(conn net.Conn) ([]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
for {
|
||||||
|
dataChunk := make([]byte, 1024)
|
||||||
|
|
||||||
|
n, err := conn.Read(dataChunk)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Write(dataChunk[:n])
|
||||||
|
|
||||||
|
if n < 1024 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|||||||
+26
-8
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -32,27 +33,23 @@ func listenToSocket() {
|
|||||||
conn, err := socket.Accept()
|
conn, err := socket.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Println("Could not accept socket connection!")
|
logger.Println("Could not accept socket connection!")
|
||||||
panic(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the connection in a separate goroutine.
|
// Handle the connection in a separate goroutine.
|
||||||
go func(conn net.Conn) {
|
go func(conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
// Create a buffer for incoming data.
|
|
||||||
buf := make([]byte, 4096)
|
|
||||||
|
|
||||||
// Read data from the connection.
|
// Read data from the connection.
|
||||||
n, err := conn.Read(buf)
|
data, err := readAllConn(conn)
|
||||||
if err == io.EOF {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decoode JSON data
|
// Decoode JSON data
|
||||||
var jsonData map[string]any
|
var jsonData map[string]any
|
||||||
err = json.Unmarshal(buf[:n], &jsonData)
|
err = json.Unmarshal(data, &jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Write(wrapErrorInJson(fmt.Errorf("Invalid JSON")))
|
conn.Write(wrapErrorInJson(fmt.Errorf("Invalid JSON")))
|
||||||
return
|
return
|
||||||
@@ -303,3 +300,24 @@ func wrapSuccessMsgInJson(msg string) []byte {
|
|||||||
}
|
}
|
||||||
return jsonData
|
return jsonData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readAllConn(conn net.Conn) ([]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
for {
|
||||||
|
dataChunk := make([]byte, 1024)
|
||||||
|
|
||||||
|
n, err := conn.Read(dataChunk)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Write(dataChunk[:n])
|
||||||
|
|
||||||
|
if n < 1024 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user