Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "flag"
  5. "io"
  6. "log"
  7. "net"
  8. )
  9.  
  10. var (
  11. addr = flag.String("addr", ":8100", "Local proxy address")
  12. raddr = flag.String("raddr", "localhost:10000", "Remove endpoint address")
  13. )
  14.  
  15. func main() {
  16. flag.Parse()
  17.  
  18. list, err := net.Listen("tcp", *addr)
  19. if err != nil {
  20. log.Fatalf("cannot listen to %s: %v", *addr, err)
  21. }
  22.  
  23. for {
  24. conn, err := list.Accept()
  25. if err != nil {
  26. log.Fatalf("cannot accept conn: %v", err)
  27. }
  28.  
  29. go proxy(conn)
  30. }
  31. }
  32.  
  33. // proxy proxies connection to remote service. Not that this is not production ready
  34. // as it is missing timeouts, slow client atacks etc.
  35. func proxy(conn net.Conn) {
  36. log.Printf("proxy %s: %s\n", conn.LocalAddr().String(), conn.RemoteAddr().String())
  37. remote, err := net.Dial("tcp", *raddr)
  38. if err != nil {
  39. log.Fatalf("cannot dial to remote: %v", err)
  40. }
  41. defer remote.Close()
  42.  
  43. go io.Copy(remote, conn)
  44. io.Copy(conn, remote)
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement