how to inspect net.Conn content?

Written by
Link to Post

https://www.reddit.com/r/golang/comments/aeyv64/how_to_inspect_netconn_content/

Hi, I am a new gopher here, I would like to make a port forward, but I don’t know how to intercept the traffic please point me how to fix the code?

package main import ( "fmt" "io" "net" "os" //"strings" "bytes" ) func main() { fmt.Println(os.Args) if len(os.Args) != 4 { fmt.Println("prog listen_port remote_host, remote_port") os.Exit(3) } listen_port := os.Args[1] rmt_host := os.Args[2] rmt_port := os.Args[3] fmt.Println("localhost:" + listen_port + " -> " + rmt_host+":"+rmt_port) ln, err := net.Listen("tcp", ":"+listen_port) if err != nil { panic(err) } for { conn, err := ln.Accept() if err != nil { panic(err) } go portFWD(conn, rmt_host, rmt_port) } } func portFWD(conn net.Conn, rmt_host string, rmt_port string) { proxy, err := net.Dial("tcp", rmt_host+":"+rmt_port) if err != nil { panic(err) } go func(){ defer conn.Close() defer proxy.Close() //experiment part var buf bytes.Buffer io.Copy(&buf, proxy) fmt.Println("total size:", buf.Len()) _, err = conn.Write(buf.Bytes()) //io.Copy(conn,proxy) //working part }() go func(){ defer conn.Close() defer proxy.Close() io.Copy(proxy, conn) }() } 

run by: go run portfwd.go 8888 192.168.1.1 1077

If I enable the experiment part and comment the “working part”, the code would fail to forward port, anyone knows how to properly inspect the length or fmt.Print() the content?

submitted by /u/c2h2pro
[link] [comments]

Article Tags:
· ·
Article Categories:
reddit

Leave a Reply