I have a proxy written in python with some logic that I would like to implement in a golang tool of mine, but I don’t really know how to do it.
It splits the data sent via socket (stdout) in small chunks, with a delay between each other
I have a variable which gets a random number from a list:
....
listbytes = [87, 88, 89, 90]
....
I have also a function which splits in small chunk of bytes(n) the data(lst):
............
def chunks(lst, n):
"Yield successive chunks from lst, where n is a list of possible sizes"
i = 0
while i < len(lst):
k = min(random.choice(n), len(lst) - i)
yield lst[i:i + k]
i += k
.............
Both things are executed this way:
...
# get the data
data = s.recv(BUFFER_SIZE)
if s == s_src:
d = LOCAL_DATA_HANDLER(data)
for chunk in chunks(d, listbytes):
#sleep 1.6 seconds before sending the next chunk of data
time.sleep(1.6)
#send the chunked data
s_dst.send(bytes(chunk) )
...
How do I implement the exact same logic illustrated here?
func (nObj NetObject) RunClient(cmd string) {
// Try connection
for {
conn, err := net.Dial(nObj.Type, nObj.Service)
fmt.Print("connect")
// msg := "status"
if err != nil {
fmt.Println("fail")
}
if err == nil {
fmt.Println("ok")
// defer conn.Close()
defer conn.Close()
log.Println("Connected to", conn.RemoteAddr())
handleConn(conn, cmd)
//handleConn(conn, cmd)
fmt.Println("After handle")
}
fmt.Println("Before sleep")
time.Sleep(5 * time.Second)
}
}
// Manage connection for different behavior
func handleConn(conn net.Conn, binPath string) {
msg := "Status?\n"
if binPath != "" {
// Execute command and send Standard I/O net.Conn
cmd := exec.Command(binPath)
cmd.Stdin = conn
cmd.Stdout = conn
cmd.Stderr = conn
cmd.Run()
fmt.Println("Disconnected!?")
time.Sleep(5 * time.Second)
pippo, err2 := conn.Write([]byte(msg))
if err2 != nil {
fmt.Println(pippo)
}
} else {
// Copy Standard I/O in a net.Conn
go io.Copy(os.Stderr, conn)
go io.Copy(os.Stdout, conn)
io.Copy(conn, os.Stdin)
}
}
In python it was straightforward to implement and it works like a charm.
The tool picks a random number from listbytes
every time it generates a chunk
I’m just looking for a pro ( a saint ahaha ) with some patience that provides an example of code of how it should look like to work as I wish it does.
I do not expect do make it work in first place, its almost impossible, but we all know, with some errors you get the solution before or after(:
In this case I don’t really know where to start and how to do it ( im a newbie in golang )
Can you help me please?