sctp_proxy.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "net"
  6. "sync"
  7. "github.com/ishidawataru/sctp"
  8. )
  9. // SCTPProxy is a proxy for SCTP connections. It implements the Proxy interface to
  10. // handle SCTP traffic forwarding between the frontend and backend addresses.
  11. type SCTPProxy struct {
  12. listener *sctp.SCTPListener
  13. frontendAddr *sctp.SCTPAddr
  14. backendAddr *sctp.SCTPAddr
  15. }
  16. // NewSCTPProxy creates a new SCTPProxy.
  17. func NewSCTPProxy(frontendAddr, backendAddr *sctp.SCTPAddr) (*SCTPProxy, error) {
  18. // detect version of hostIP to bind only to correct version
  19. ipVersion := ipv4
  20. if frontendAddr.IPAddrs[0].IP.To4() == nil {
  21. ipVersion = ipv6
  22. }
  23. listener, err := sctp.ListenSCTP("sctp"+string(ipVersion), frontendAddr)
  24. if err != nil {
  25. return nil, err
  26. }
  27. // If the port in frontendAddr was 0 then ListenSCTP will have a picked
  28. // a port to listen on, hence the call to Addr to get that actual port:
  29. return &SCTPProxy{
  30. listener: listener,
  31. frontendAddr: listener.Addr().(*sctp.SCTPAddr),
  32. backendAddr: backendAddr,
  33. }, nil
  34. }
  35. func (proxy *SCTPProxy) clientLoop(client *sctp.SCTPConn, quit chan bool) {
  36. backend, err := sctp.DialSCTP("sctp", nil, proxy.backendAddr)
  37. if err != nil {
  38. log.Printf("Can't forward traffic to backend sctp/%v: %s\n", proxy.backendAddr, err)
  39. client.Close()
  40. return
  41. }
  42. clientC := sctp.NewSCTPSndRcvInfoWrappedConn(client)
  43. backendC := sctp.NewSCTPSndRcvInfoWrappedConn(backend)
  44. var wg sync.WaitGroup
  45. broker := func(to, from net.Conn) {
  46. io.Copy(to, from)
  47. from.Close()
  48. to.Close()
  49. wg.Done()
  50. }
  51. wg.Add(2)
  52. go broker(clientC, backendC)
  53. go broker(backendC, clientC)
  54. finish := make(chan struct{})
  55. go func() {
  56. wg.Wait()
  57. close(finish)
  58. }()
  59. select {
  60. case <-quit:
  61. case <-finish:
  62. }
  63. clientC.Close()
  64. backendC.Close()
  65. <-finish
  66. }
  67. // Run starts forwarding the traffic using SCTP.
  68. func (proxy *SCTPProxy) Run() {
  69. quit := make(chan bool)
  70. defer close(quit)
  71. for {
  72. client, err := proxy.listener.Accept()
  73. if err != nil {
  74. log.Printf("Stopping proxy on sctp/%v for sctp/%v (%s)", proxy.frontendAddr, proxy.backendAddr, err)
  75. return
  76. }
  77. go proxy.clientLoop(client.(*sctp.SCTPConn), quit)
  78. }
  79. }
  80. // Close stops forwarding the traffic.
  81. func (proxy *SCTPProxy) Close() { proxy.listener.Close() }
  82. // FrontendAddr returns the SCTP address on which the proxy is listening.
  83. func (proxy *SCTPProxy) FrontendAddr() net.Addr { return proxy.frontendAddr }
  84. // BackendAddr returns the SCTP proxied address.
  85. func (proxy *SCTPProxy) BackendAddr() net.Addr { return proxy.backendAddr }