proxy.go 716 B

1234567891011121314151617181920212223242526272829
  1. package proxy
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. type Proxy interface {
  7. // Start forwarding traffic back and forth the front and back-end
  8. // addresses.
  9. Run()
  10. // Stop forwarding traffic and close both ends of the Proxy.
  11. Close()
  12. // Return the address on which the proxy is listening.
  13. FrontendAddr() net.Addr
  14. // Return the proxied address.
  15. BackendAddr() net.Addr
  16. }
  17. func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
  18. switch frontendAddr.(type) {
  19. case *net.UDPAddr:
  20. return NewUDPProxy(frontendAddr.(*net.UDPAddr), backendAddr.(*net.UDPAddr))
  21. case *net.TCPAddr:
  22. return NewTCPProxy(frontendAddr.(*net.TCPAddr), backendAddr.(*net.TCPAddr))
  23. default:
  24. panic(fmt.Errorf("Unsupported protocol"))
  25. }
  26. }