dump.go 735 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // This program listens to the host and port specified by the -listen flag and
  2. // dumps any incoming data to standard output.
  3. package main
  4. import (
  5. "flag"
  6. "fmt"
  7. "io"
  8. "log"
  9. "net"
  10. "os"
  11. )
  12. var addr = flag.String("listen", "localhost:8000", "server listen address")
  13. type dumpWriter struct {
  14. c net.Conn
  15. w io.Writer
  16. }
  17. func (w dumpWriter) Write(v []byte) (int, error) {
  18. fmt.Fprintf(w.w, "[%v->%v] ", w.c.RemoteAddr(), w.c.LocalAddr())
  19. return w.w.Write(v)
  20. }
  21. func main() {
  22. flag.Parse()
  23. l, err := net.Listen("tcp", *addr)
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. log.Println("Listening on", l.Addr())
  28. for {
  29. c, err := l.Accept()
  30. if err != nil {
  31. log.Println(err)
  32. continue
  33. }
  34. go io.Copy(dumpWriter{c, os.Stdout}, c)
  35. }
  36. }