|
| 1 | +package turnconn |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/pion/logging" |
| 9 | + "github.com/pion/turn/v2" |
| 10 | + "github.com/pion/webrtc/v3" |
| 11 | + "golang.org/x/net/proxy" |
| 12 | + "golang.org/x/xerrors" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + reservedHost = "coder" |
| 17 | + |
| 18 | + // Proxy is a an ICE Server that uses a special hostname |
| 19 | + // to indicate traffic should be proxied. |
| 20 | + Proxy = webrtc.ICEServer{ |
| 21 | + URLs: []string{"turns:" + reservedHost}, |
| 22 | + Username: "coder", |
| 23 | + Credential: "coder", |
| 24 | + } |
| 25 | +) |
| 26 | + |
| 27 | +// New constructs a new TURN server binding to the relay address provided. |
| 28 | +// The relay address is used to broadcast the location of an accepted connection. |
| 29 | +func New(relayAddress *turn.RelayAddressGeneratorStatic) (*Server, error) { |
| 30 | + if relayAddress == nil { |
| 31 | + // Default to localhost. |
| 32 | + relayAddress = &turn.RelayAddressGeneratorStatic{ |
| 33 | + RelayAddress: net.IP{127, 0, 0, 1}, |
| 34 | + Address: "127.0.0.1", |
| 35 | + } |
| 36 | + } |
| 37 | + logger := logging.NewDefaultLoggerFactory() |
| 38 | + logger.DefaultLogLevel = logging.LogLevelDisabled |
| 39 | + server := &Server{ |
| 40 | + conns: make(chan net.Conn, 1), |
| 41 | + closed: make(chan struct{}), |
| 42 | + } |
| 43 | + server.listener = &listener{ |
| 44 | + srv: server, |
| 45 | + } |
| 46 | + var err error |
| 47 | + server.turn, err = turn.NewServer(turn.ServerConfig{ |
| 48 | + AuthHandler: func(username, realm string, srcAddr net.Addr) (key []byte, ok bool) { |
| 49 | + return turn.GenerateAuthKey(Proxy.Username, "", Proxy.Credential.(string)), true |
| 50 | + }, |
| 51 | + ListenerConfigs: []turn.ListenerConfig{{ |
| 52 | + Listener: server.listener, |
| 53 | + RelayAddressGenerator: relayAddress, |
| 54 | + }}, |
| 55 | + LoggerFactory: logger, |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + return nil, xerrors.Errorf("create server: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + return server, nil |
| 62 | +} |
| 63 | + |
| 64 | +// ProxyDialer accepts a proxy function that's called when the connection |
| 65 | +// address matches the reserved host in the "Proxy" ICE server. |
| 66 | +// |
| 67 | +// This should be passed to WebRTC connections as an ICE dialer. |
| 68 | +func ProxyDialer(proxyFunc func() (c net.Conn, err error)) proxy.Dialer { |
| 69 | + return dialer(func(network, addr string) (net.Conn, error) { |
| 70 | + host, _, err := net.SplitHostPort(addr) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + if host != reservedHost { |
| 75 | + return proxy.Direct.Dial(network, addr) |
| 76 | + } |
| 77 | + netConn, err := proxyFunc() |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + return &conn{ |
| 82 | + localAddress: &net.TCPAddr{ |
| 83 | + IP: net.IPv4(127, 0, 0, 1), |
| 84 | + }, |
| 85 | + Conn: netConn, |
| 86 | + }, nil |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +// Server accepts and connects TURN allocations. |
| 91 | +// |
| 92 | +// This is a thin wrapper around pion/turn that pipes |
| 93 | +// connections directly to the in-memory handler. |
| 94 | +type Server struct { |
| 95 | + listener *listener |
| 96 | + turn *turn.Server |
| 97 | + |
| 98 | + closeMutex sync.Mutex |
| 99 | + closed chan (struct{}) |
| 100 | + conns chan (net.Conn) |
| 101 | +} |
| 102 | + |
| 103 | +// Accept consumes a new connection into the TURN server. |
| 104 | +// A unique remote address must exist per-connection. |
| 105 | +// pion/turn indexes allocations based on the address. |
| 106 | +func (s *Server) Accept(nc net.Conn, remoteAddress *net.TCPAddr) { |
| 107 | + s.conns <- &conn{ |
| 108 | + Conn: nc, |
| 109 | + remoteAddress: remoteAddress, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Close ends the TURN server. |
| 114 | +func (s *Server) Close() error { |
| 115 | + s.closeMutex.Lock() |
| 116 | + defer s.closeMutex.Unlock() |
| 117 | + if s.isClosed() { |
| 118 | + return nil |
| 119 | + } |
| 120 | + defer close(s.closed) |
| 121 | + close(s.conns) |
| 122 | + return s.turn.Close() |
| 123 | +} |
| 124 | + |
| 125 | +func (s *Server) isClosed() bool { |
| 126 | + select { |
| 127 | + case <-s.closed: |
| 128 | + return true |
| 129 | + default: |
| 130 | + return false |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// listener implements net.Listener for the TURN |
| 135 | +// server to consume. |
| 136 | +type listener struct { |
| 137 | + srv *Server |
| 138 | +} |
| 139 | + |
| 140 | +func (l *listener) Accept() (net.Conn, error) { |
| 141 | + conn, ok := <-l.srv.conns |
| 142 | + if !ok { |
| 143 | + return nil, io.EOF |
| 144 | + } |
| 145 | + return conn, nil |
| 146 | +} |
| 147 | + |
| 148 | +func (*listener) Close() error { |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func (*listener) Addr() net.Addr { |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +type conn struct { |
| 157 | + net.Conn |
| 158 | + localAddress *net.TCPAddr |
| 159 | + remoteAddress *net.TCPAddr |
| 160 | +} |
| 161 | + |
| 162 | +func (t *conn) LocalAddr() net.Addr { |
| 163 | + return t.localAddress |
| 164 | +} |
| 165 | + |
| 166 | +func (t *conn) RemoteAddr() net.Addr { |
| 167 | + return t.remoteAddress |
| 168 | +} |
| 169 | + |
| 170 | +type dialer func(network, addr string) (c net.Conn, err error) |
| 171 | + |
| 172 | +func (d dialer) Dial(network, addr string) (c net.Conn, err error) { |
| 173 | + return d(network, addr) |
| 174 | +} |
0 commit comments