proxy.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // docker-proxy provides a network Proxy interface and implementations for TCP
  2. // and UDP.
  3. package main
  4. import (
  5. "net"
  6. "github.com/ishidawataru/sctp"
  7. )
  8. // ipVersion refers to IP version - v4 or v6
  9. type ipVersion string
  10. const (
  11. // IPv4 is version 4
  12. ipv4 ipVersion = "4"
  13. // IPv4 is version 6
  14. ipv6 ipVersion = "6"
  15. )
  16. // Proxy defines the behavior of a proxy. It forwards traffic back and forth
  17. // between two endpoints : the frontend and the backend.
  18. // It can be used to do software port-mapping between two addresses.
  19. // e.g. forward all traffic between the frontend (host) 127.0.0.1:3000
  20. // to the backend (container) at 172.17.42.108:4000.
  21. type Proxy interface {
  22. // Run starts forwarding traffic back and forth between the front
  23. // and back-end addresses.
  24. Run()
  25. // Close stops forwarding traffic and close both ends of the Proxy.
  26. Close()
  27. // FrontendAddr returns the address on which the proxy is listening.
  28. FrontendAddr() net.Addr
  29. // BackendAddr returns the proxied address.
  30. BackendAddr() net.Addr
  31. }
  32. // NewProxy creates a Proxy according to the specified frontendAddr and backendAddr.
  33. func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
  34. switch frontendAddr.(type) {
  35. case *net.UDPAddr:
  36. return NewUDPProxy(frontendAddr.(*net.UDPAddr), backendAddr.(*net.UDPAddr))
  37. case *net.TCPAddr:
  38. return NewTCPProxy(frontendAddr.(*net.TCPAddr), backendAddr.(*net.TCPAddr))
  39. case *sctp.SCTPAddr:
  40. return NewSCTPProxy(frontendAddr.(*sctp.SCTPAddr), backendAddr.(*sctp.SCTPAddr))
  41. default:
  42. panic("Unsupported protocol")
  43. }
  44. }