package main import ( "fmt" "io" "os" "proxy/cmd" "unicode" ) // Start the two plugs and run two concurrent forward methods func main() { c1 := cmd.New("vde_plug", "/run/vde/sw_main.sock") c2 := cmd.New("vde_plug", "/run/vde/sw_proxy.sock") c1.Execute() c2.Execute() go pipeForward(c1.OutReader, c2.InWriter, cmd.In) go pipeForward(c2.OutReader, c1.InWriter, cmd.Out) c1.WaitH() c2.WaitH() } // Reads from an input and writes to and output, // do things to the content in between. // For now only output it in xxd format. // Is meant to be run concurrently with "go pipeForward(...)" func pipeForward(reader io.Reader, writer io.Writer, prefix string) { i := 0 for { bytes := make([]byte, 16) bytesReadable := make([]byte, 16) _, err := reader.Read(bytes) if err == io.EOF { break } for i, ch := range bytes { if ch > unicode.MaxASCII || ch < '\u0020' { bytesReadable[i] = '\u002E' } else { bytesReadable[i] = ch } } xxdString := fmt.Sprintf("%s%08x: %04x %04x %04x %04x %04x %04x %04x %04x %s\n", prefix, i, bytes[0:1], bytes[2:3], bytes[4:5], bytes[6:7], bytes[8:9], bytes[10:11], bytes[12:13], bytes[14:15], bytesReadable) os.Stdout.WriteString(xxdString) writer.Write(bytes) i += 16 } }