package main import ( "errors" "fmt" "log" "net" "os" "p4go/rpc" "strings" "time" ) var proxy = "8193" var p4 = "8192" var EOS = errors.New("EOS") func connSetup() (net.Conn, net.Conn, *net.TCPListener) { ips, err := net.LookupIP("localhost") checkError(err) for _, ip := range ips { fmt.Println("Resolved addr ", ip) } proxyAddr := strings.Join([]string{ips[0].String(), proxy}, ":") p4Addr := strings.Join([]string{ips[0].String(), p4}, ":") proxyTcpAddr, err := net.ResolveTCPAddr("tcp", proxyAddr) checkError(err) p4TcpAddr, err := net.ResolveTCPAddr("tcp", p4Addr) checkError(err) proxyListener, err := net.ListenTCP("tcp", proxyTcpAddr) checkError(err) proxyConn, err := proxyListener.Accept() checkError(err) p4Conn, err := net.DialTCP("tcp", nil, p4TcpAddr) checkError(err) return proxyConn, p4Conn, proxyListener } func main() { p4Conn, proxyConn, proxyListener := connSetup() defer proxyListener.Close() defer p4Conn.Close() proxyCommand(p4Conn, proxyConn) } func checkError(err error) { if err != nil { fmt.Fprintf(os.Stderr, "Fatal error: %s\n", err.Error()) os.Exit(1) } } func proxyCommand(p4_c net.Conn, proxy_c net.Conn) { p4_c.SetReadDeadline(time.Now().Add(time.Second)) proxy_c.SetReadDeadline(time.Now().Add(time.Second)) f_out, err := os.Create("incoming.dec.txt") checkError(err) proxy_dec := rpc.NewDecoder(proxy_c) proxy_enc := rpc.NewEncoder(proxy_c) p4_dec := rpc.NewDecoder(p4_c) p4_enc := rpc.NewEncoder(p4_c) eCh := make(chan error) go ProcInput(proxy_dec, p4_enc, eCh, f_out) go ProcInput(p4_dec, proxy_enc, eCh, f_out) err = <-eCh if err == EOS { log.Println("Reached the end of command") } else if err != nil { log.Panic(<-eCh) } } func ProcInput(dec *rpc.Decoder, enc *rpc.Encoder, eCh chan error, f_out *os.File) { for { vs := rpc.NewVars() err := dec.Decode(vs) if err != nil { log.Println("Error from dec") eCh <- err } val, ok := vs["func"] if ok && (val == "release" || val == "release2") { eCh <- EOS } f_out.WriteString(fmt.Sprintf("Decoding:\n\t%+v\n", vs)) enc.Encode(vs) } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 14966 | Brett Bates |
A way to look into what is being sent. Not exactly practical. Doesn't work for long messages. |