udp.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "bytes"
  22. "encoding/binary"
  23. "fmt"
  24. "io"
  25. "net"
  26. "strconv"
  27. "sync"
  28. "sync/atomic"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/monotime"
  33. )
  34. // handleUdpgwChannel implements UDP port forwarding. A single UDP
  35. // SSH channel follows the udpgw protocol, which multiplexes many
  36. // UDP port forwards.
  37. //
  38. // The udpgw protocol and original server implementation:
  39. // Copyright (c) 2009, Ambroz Bizjak <ambrop7@gmail.com>
  40. // https://github.com/ambrop72/badvpn
  41. //
  42. func (sshClient *sshClient) handleUdpgwChannel(newChannel ssh.NewChannel) {
  43. // Accept this channel immediately. This channel will replace any
  44. // previously existing udpgw channel for this client.
  45. sshChannel, requests, err := newChannel.Accept()
  46. if err != nil {
  47. if !isExpectedTunnelIOError(err) {
  48. log.WithTraceFields(LogFields{"error": err}).Warning("accept new channel failed")
  49. }
  50. return
  51. }
  52. go ssh.DiscardRequests(requests)
  53. defer sshChannel.Close()
  54. multiplexer := &udpgwPortForwardMultiplexer{
  55. sshClient: sshClient,
  56. sshChannel: sshChannel,
  57. portForwards: make(map[uint16]*udpgwPortForward),
  58. portForwardLRU: common.NewLRUConns(),
  59. relayWaitGroup: new(sync.WaitGroup),
  60. runWaitGroup: new(sync.WaitGroup),
  61. }
  62. multiplexer.runWaitGroup.Add(1)
  63. // setUdpgwChannelHandler will close any existing
  64. // udpgwPortForwardMultiplexer, waiting for all run/relayDownstream
  65. // goroutines to first terminate and all UDP socket resources to be
  66. // cleaned up.
  67. //
  68. // This synchronous shutdown also ensures that the
  69. // concurrentPortForwardCount is reduced to 0 before installing the new
  70. // udpgwPortForwardMultiplexer and its LRU object. If the older handler
  71. // were to dangle with open port forwards, and concurrentPortForwardCount
  72. // were to hit the max, the wrong LRU, the new one, would be used to
  73. // close the LRU port forward.
  74. //
  75. // Call setUdpgwHandler only after runWaitGroup is initialized, to ensure
  76. // runWaitGroup.Wait() cannot be invoked (by some subsequent new udpgw
  77. // channel) before initialized.
  78. if !sshClient.setUdpgwChannelHandler(multiplexer) {
  79. // setUdpgwChannelHandler returns false if some other SSH channel
  80. // calls setUdpgwChannelHandler in the middle of this call. In that
  81. // case, discard this channel: the client's latest udpgw channel is
  82. // retained.
  83. return
  84. }
  85. multiplexer.run()
  86. multiplexer.runWaitGroup.Done()
  87. }
  88. type udpgwPortForwardMultiplexer struct {
  89. sshClient *sshClient
  90. sshChannelWriteMutex sync.Mutex
  91. sshChannel ssh.Channel
  92. portForwardsMutex sync.Mutex
  93. portForwards map[uint16]*udpgwPortForward
  94. portForwardLRU *common.LRUConns
  95. relayWaitGroup *sync.WaitGroup
  96. runWaitGroup *sync.WaitGroup
  97. }
  98. func (mux *udpgwPortForwardMultiplexer) stop() {
  99. // udpgwPortForwardMultiplexer must be initialized by handleUdpgwChannel.
  100. //
  101. // stop closes the udpgw SSH channel, which will cause the run goroutine
  102. // to exit its message read loop and await closure of all relayDownstream
  103. // goroutines. Closing all port forward UDP conns will cause all
  104. // relayDownstream to exit.
  105. _ = mux.sshChannel.Close()
  106. mux.portForwardsMutex.Lock()
  107. for _, portForward := range mux.portForwards {
  108. _ = portForward.conn.Close()
  109. }
  110. mux.portForwardsMutex.Unlock()
  111. mux.runWaitGroup.Wait()
  112. }
  113. func (mux *udpgwPortForwardMultiplexer) run() {
  114. // In a loop, read udpgw messages from the client to this channel. Each
  115. // message contains a UDP packet to send upstream either via a new port
  116. // forward, or on an existing port forward.
  117. //
  118. // A goroutine is run to read downstream packets for each UDP port forward. All read
  119. // packets are encapsulated in udpgw protocol and sent down the channel to the client.
  120. //
  121. // When the client disconnects or the server shuts down, the channel will close and
  122. // readUdpgwMessage will exit with EOF.
  123. buffer := make([]byte, udpgwProtocolMaxMessageSize)
  124. for {
  125. // Note: message.packet points to the reusable memory in "buffer".
  126. // Each readUdpgwMessage call will overwrite the last message.packet.
  127. message, err := readUdpgwMessage(mux.sshChannel, buffer)
  128. if err != nil {
  129. if err != io.EOF {
  130. // Debug since I/O errors occur during normal operation
  131. log.WithTraceFields(LogFields{"error": err}).Debug("readUdpgwMessage failed")
  132. }
  133. break
  134. }
  135. mux.portForwardsMutex.Lock()
  136. portForward := mux.portForwards[message.connID]
  137. mux.portForwardsMutex.Unlock()
  138. // In the udpgw protocol, an existing port forward is closed when
  139. // either the discard flag is set or the remote address has changed.
  140. if portForward != nil &&
  141. (message.discardExistingConn ||
  142. !bytes.Equal(portForward.remoteIP, message.remoteIP) ||
  143. portForward.remotePort != message.remotePort) {
  144. // The port forward's goroutine will complete cleanup, including
  145. // tallying stats and calling sshClient.closedPortForward.
  146. // portForward.conn.Close() will signal this shutdown.
  147. portForward.conn.Close()
  148. // Synchronously await the termination of the relayDownstream
  149. // goroutine. This ensures that the previous goroutine won't
  150. // invoke removePortForward, with the connID that will be reused
  151. // for the new port forward, after this point.
  152. //
  153. // Limitation: this synchronous shutdown cannot prevent a "wrong
  154. // remote address" error on the badvpn udpgw client, which occurs
  155. // when the client recycles a port forward (setting discard) but
  156. // receives, from the server, a udpgw message containing the old
  157. // remote address for the previous port forward with the same
  158. // conn ID. That downstream message from the server may be in
  159. // flight in the SSH channel when the client discard message arrives.
  160. portForward.relayWaitGroup.Wait()
  161. portForward = nil
  162. }
  163. if portForward == nil {
  164. // Create a new port forward
  165. dialIP := net.IP(message.remoteIP)
  166. dialPort := int(message.remotePort)
  167. // Validate DNS packets and check the domain blocklist both when the client
  168. // indicates DNS or when DNS is _not_ indicated and the destination port is
  169. // 53.
  170. if message.forwardDNS || message.remotePort == 53 {
  171. domain, err := common.ParseDNSQuestion(message.packet)
  172. if err != nil {
  173. log.WithTraceFields(LogFields{"error": err}).Debug("ParseDNSQuestion failed")
  174. // Drop packet
  175. continue
  176. }
  177. if domain != "" {
  178. ok, _ := mux.sshClient.isDomainPermitted(domain)
  179. if !ok {
  180. // Drop packet
  181. continue
  182. }
  183. }
  184. }
  185. if message.forwardDNS {
  186. // Transparent DNS forwarding. In this case, isPortForwardPermitted
  187. // traffic rules checks are bypassed, since DNS is essential.
  188. dialIP = mux.sshClient.sshServer.support.DNSResolver.Get()
  189. dialPort = DNS_RESOLVER_PORT
  190. } else if !mux.sshClient.isPortForwardPermitted(
  191. portForwardTypeUDP, dialIP, int(message.remotePort)) {
  192. // The udpgw protocol has no error response, so
  193. // we just discard the message and read another.
  194. continue
  195. }
  196. // Note: UDP port forward counting has no dialing phase
  197. // establishedPortForward increments the concurrent UDP port
  198. // forward counter and closes the LRU existing UDP port forward
  199. // when already at the limit.
  200. mux.sshClient.establishedPortForward(portForwardTypeUDP, mux.portForwardLRU)
  201. // Can't defer sshClient.closedPortForward() here;
  202. // relayDownstream will call sshClient.closedPortForward()
  203. // Pre-check log level to avoid overhead of rendering log for
  204. // every DNS query and other UDP port forward.
  205. if IsLogLevelDebug() {
  206. log.WithTraceFields(
  207. LogFields{
  208. "remoteAddr": net.JoinHostPort(dialIP.String(), strconv.Itoa(dialPort)),
  209. "connID": message.connID}).Debug("dialing")
  210. }
  211. udpConn, err := net.DialUDP(
  212. "udp", nil, &net.UDPAddr{IP: dialIP, Port: dialPort})
  213. if err != nil {
  214. mux.sshClient.closedPortForward(portForwardTypeUDP, 0, 0)
  215. // Monitor for low resource error conditions
  216. mux.sshClient.sshServer.monitorPortForwardDialError(err)
  217. // Note: Debug level, as logMessage may contain user traffic destination address information
  218. log.WithTraceFields(LogFields{"error": err}).Debug("DialUDP failed")
  219. continue
  220. }
  221. lruEntry := mux.portForwardLRU.Add(udpConn)
  222. // Can't defer lruEntry.Remove() here;
  223. // relayDownstream will call lruEntry.Remove()
  224. // ActivityMonitoredConn monitors the TCP port forward I/O and updates
  225. // its LRU status. ActivityMonitoredConn also times out I/O on the port
  226. // forward if both reads and writes have been idle for the specified
  227. // duration.
  228. // Ensure nil interface if newClientSeedPortForward returns nil
  229. var updater common.ActivityUpdater
  230. seedUpdater := mux.sshClient.newClientSeedPortForward(dialIP)
  231. if seedUpdater != nil {
  232. updater = seedUpdater
  233. }
  234. conn, err := common.NewActivityMonitoredConn(
  235. udpConn,
  236. mux.sshClient.idleUDPPortForwardTimeout(),
  237. true,
  238. updater,
  239. lruEntry)
  240. if err != nil {
  241. lruEntry.Remove()
  242. mux.sshClient.closedPortForward(portForwardTypeUDP, 0, 0)
  243. log.WithTraceFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  244. continue
  245. }
  246. portForward = &udpgwPortForward{
  247. connID: message.connID,
  248. preambleSize: message.preambleSize,
  249. remoteIP: message.remoteIP,
  250. remotePort: message.remotePort,
  251. dialIP: dialIP,
  252. conn: conn,
  253. lruEntry: lruEntry,
  254. bytesUp: 0,
  255. bytesDown: 0,
  256. relayWaitGroup: new(sync.WaitGroup),
  257. mux: mux,
  258. }
  259. if message.forwardDNS {
  260. portForward.dnsFirstWriteTime = int64(monotime.Now())
  261. }
  262. mux.portForwardsMutex.Lock()
  263. mux.portForwards[portForward.connID] = portForward
  264. mux.portForwardsMutex.Unlock()
  265. portForward.relayWaitGroup.Add(1)
  266. mux.relayWaitGroup.Add(1)
  267. go portForward.relayDownstream()
  268. }
  269. // Note: assumes UDP writes won't block (https://golang.org/pkg/net/#UDPConn.WriteToUDP)
  270. _, err = portForward.conn.Write(message.packet)
  271. if err != nil {
  272. // Debug since errors such as "write: operation not permitted" occur during normal operation
  273. log.WithTraceFields(LogFields{"error": err}).Debug("upstream UDP relay failed")
  274. // The port forward's goroutine will complete cleanup
  275. portForward.conn.Close()
  276. }
  277. portForward.lruEntry.Touch()
  278. atomic.AddInt64(&portForward.bytesUp, int64(len(message.packet)))
  279. }
  280. // Cleanup all udpgw port forward workers when exiting
  281. mux.portForwardsMutex.Lock()
  282. for _, portForward := range mux.portForwards {
  283. // The port forward's goroutine will complete cleanup
  284. portForward.conn.Close()
  285. }
  286. mux.portForwardsMutex.Unlock()
  287. mux.relayWaitGroup.Wait()
  288. }
  289. func (mux *udpgwPortForwardMultiplexer) removePortForward(connID uint16) {
  290. mux.portForwardsMutex.Lock()
  291. delete(mux.portForwards, connID)
  292. mux.portForwardsMutex.Unlock()
  293. }
  294. type udpgwPortForward struct {
  295. // Note: 64-bit ints used with atomic operations are placed
  296. // at the start of struct to ensure 64-bit alignment.
  297. // (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
  298. dnsFirstWriteTime int64
  299. dnsFirstReadTime int64
  300. bytesUp int64
  301. bytesDown int64
  302. connID uint16
  303. preambleSize int
  304. remoteIP []byte
  305. remotePort uint16
  306. dialIP net.IP
  307. conn net.Conn
  308. lruEntry *common.LRUConnsEntry
  309. relayWaitGroup *sync.WaitGroup
  310. mux *udpgwPortForwardMultiplexer
  311. }
  312. func (portForward *udpgwPortForward) relayDownstream() {
  313. defer portForward.relayWaitGroup.Done()
  314. defer portForward.mux.relayWaitGroup.Done()
  315. // Downstream UDP packets are read into the reusable memory
  316. // in "buffer" starting at the offset past the udpgw message
  317. // header and address, leaving enough space to write the udpgw
  318. // values into the same buffer and use for writing to the ssh
  319. // channel.
  320. //
  321. // Note: there is one downstream buffer per UDP port forward,
  322. // while for upstream there is one buffer per client.
  323. // TODO: is the buffer size larger than necessary?
  324. buffer := make([]byte, udpgwProtocolMaxMessageSize)
  325. packetBuffer := buffer[portForward.preambleSize:udpgwProtocolMaxMessageSize]
  326. for {
  327. // TODO: if read buffer is too small, excess bytes are discarded?
  328. packetSize, err := portForward.conn.Read(packetBuffer)
  329. if packetSize > udpgwProtocolMaxPayloadSize {
  330. err = fmt.Errorf("unexpected packet size: %d", packetSize)
  331. }
  332. if err != nil {
  333. if err != io.EOF {
  334. // Debug since errors such as "use of closed network connection" occur during normal operation
  335. log.WithTraceFields(LogFields{"error": err}).Debug("downstream UDP relay failed")
  336. }
  337. break
  338. }
  339. if atomic.LoadInt64(&portForward.dnsFirstWriteTime) > 0 &&
  340. atomic.LoadInt64(&portForward.dnsFirstReadTime) == 0 { // Check if already set before invoking Now.
  341. atomic.CompareAndSwapInt64(&portForward.dnsFirstReadTime, 0, int64(monotime.Now()))
  342. }
  343. err = writeUdpgwPreamble(
  344. portForward.preambleSize,
  345. 0,
  346. portForward.connID,
  347. portForward.remoteIP,
  348. portForward.remotePort,
  349. uint16(packetSize),
  350. buffer)
  351. if err == nil {
  352. // ssh.Channel.Write cannot be called concurrently.
  353. // See: https://github.com/Psiphon-Inc/crypto/blob/82d98b4c7c05e81f92545f6fddb45d4541e6da00/ssh/channel.go#L272,
  354. // https://codereview.appspot.com/136420043/diff/80002/ssh/channel.go
  355. portForward.mux.sshChannelWriteMutex.Lock()
  356. _, err = portForward.mux.sshChannel.Write(buffer[0 : portForward.preambleSize+packetSize])
  357. portForward.mux.sshChannelWriteMutex.Unlock()
  358. }
  359. if err != nil {
  360. // Close the channel, which will interrupt the main loop.
  361. portForward.mux.sshChannel.Close()
  362. log.WithTraceFields(LogFields{"error": err}).Debug("downstream UDP relay failed")
  363. break
  364. }
  365. portForward.lruEntry.Touch()
  366. atomic.AddInt64(&portForward.bytesDown, int64(packetSize))
  367. }
  368. portForward.mux.removePortForward(portForward.connID)
  369. portForward.lruEntry.Remove()
  370. portForward.conn.Close()
  371. bytesUp := atomic.LoadInt64(&portForward.bytesUp)
  372. bytesDown := atomic.LoadInt64(&portForward.bytesDown)
  373. portForward.mux.sshClient.closedPortForward(portForwardTypeUDP, bytesUp, bytesDown)
  374. dnsStartTime := monotime.Time(atomic.LoadInt64(&portForward.dnsFirstWriteTime))
  375. if dnsStartTime > 0 {
  376. // Record DNS metrics using a heuristic: if a UDP packet was written and
  377. // then a packet was read, assume the DNS request successfully received a
  378. // valid response; failure occurs when the resolver fails to provide a
  379. // response; a "no such host" response is still a success. Limitations: we
  380. // assume a resolver will not respond when, e.g., rate limiting; we ignore
  381. // subsequent requests made via the same UDP port forward.
  382. dnsEndTime := monotime.Time(atomic.LoadInt64(&portForward.dnsFirstReadTime))
  383. dnsSuccess := true
  384. if dnsEndTime == 0 {
  385. dnsSuccess = false
  386. dnsEndTime = monotime.Now()
  387. }
  388. resolveElapsedTime := dnsEndTime.Sub(dnsStartTime)
  389. portForward.mux.sshClient.updateQualityMetricsWithDNSResult(
  390. dnsSuccess,
  391. resolveElapsedTime,
  392. net.IP(portForward.dialIP))
  393. }
  394. log.WithTraceFields(
  395. LogFields{
  396. "remoteAddr": net.JoinHostPort(
  397. net.IP(portForward.remoteIP).String(), strconv.Itoa(int(portForward.remotePort))),
  398. "bytesUp": bytesUp,
  399. "bytesDown": bytesDown,
  400. "connID": portForward.connID}).Debug("exiting")
  401. }
  402. // TODO: express and/or calculate udpgwProtocolMaxPayloadSize as function of MTU?
  403. const (
  404. udpgwProtocolFlagKeepalive = 1 << 0
  405. udpgwProtocolFlagRebind = 1 << 1
  406. udpgwProtocolFlagDNS = 1 << 2
  407. udpgwProtocolFlagIPv6 = 1 << 3
  408. udpgwProtocolMaxPreambleSize = 23
  409. udpgwProtocolMaxPayloadSize = 32768
  410. udpgwProtocolMaxMessageSize = udpgwProtocolMaxPreambleSize + udpgwProtocolMaxPayloadSize
  411. )
  412. type udpgwProtocolMessage struct {
  413. connID uint16
  414. preambleSize int
  415. remoteIP []byte
  416. remotePort uint16
  417. discardExistingConn bool
  418. forwardDNS bool
  419. packet []byte
  420. }
  421. func readUdpgwMessage(
  422. reader io.Reader, buffer []byte) (*udpgwProtocolMessage, error) {
  423. // udpgw message layout:
  424. //
  425. // | 2 byte size | 3 byte header | 6 or 18 byte address | variable length packet |
  426. for {
  427. // Read message
  428. _, err := io.ReadFull(reader, buffer[0:2])
  429. if err != nil {
  430. if err != io.EOF {
  431. err = errors.Trace(err)
  432. }
  433. return nil, err
  434. }
  435. size := binary.LittleEndian.Uint16(buffer[0:2])
  436. if size < 3 || int(size) > len(buffer)-2 {
  437. return nil, errors.TraceNew("invalid udpgw message size")
  438. }
  439. _, err = io.ReadFull(reader, buffer[2:2+size])
  440. if err != nil {
  441. if err != io.EOF {
  442. err = errors.Trace(err)
  443. }
  444. return nil, err
  445. }
  446. flags := buffer[2]
  447. connID := binary.LittleEndian.Uint16(buffer[3:5])
  448. // Ignore udpgw keep-alive messages -- read another message
  449. if flags&udpgwProtocolFlagKeepalive == udpgwProtocolFlagKeepalive {
  450. continue
  451. }
  452. // Read address
  453. var remoteIP []byte
  454. var remotePort uint16
  455. var packetStart, packetEnd int
  456. if flags&udpgwProtocolFlagIPv6 == udpgwProtocolFlagIPv6 {
  457. if size < 21 {
  458. return nil, errors.TraceNew("invalid udpgw message size")
  459. }
  460. remoteIP = make([]byte, 16)
  461. copy(remoteIP, buffer[5:21])
  462. remotePort = binary.BigEndian.Uint16(buffer[21:23])
  463. packetStart = 23
  464. packetEnd = 23 + int(size) - 21
  465. } else {
  466. if size < 9 {
  467. return nil, errors.TraceNew("invalid udpgw message size")
  468. }
  469. remoteIP = make([]byte, 4)
  470. copy(remoteIP, buffer[5:9])
  471. remotePort = binary.BigEndian.Uint16(buffer[9:11])
  472. packetStart = 11
  473. packetEnd = 11 + int(size) - 9
  474. }
  475. // Assemble message
  476. // Note: udpgwProtocolMessage.packet references memory in the input buffer
  477. message := &udpgwProtocolMessage{
  478. connID: connID,
  479. preambleSize: packetStart,
  480. remoteIP: remoteIP,
  481. remotePort: remotePort,
  482. discardExistingConn: flags&udpgwProtocolFlagRebind == udpgwProtocolFlagRebind,
  483. forwardDNS: flags&udpgwProtocolFlagDNS == udpgwProtocolFlagDNS,
  484. packet: buffer[packetStart:packetEnd],
  485. }
  486. return message, nil
  487. }
  488. }
  489. func writeUdpgwPreamble(
  490. preambleSize int,
  491. flags uint8,
  492. connID uint16,
  493. remoteIP []byte,
  494. remotePort uint16,
  495. packetSize uint16,
  496. buffer []byte) error {
  497. if preambleSize != 7+len(remoteIP) {
  498. return errors.TraceNew("invalid udpgw preamble size")
  499. }
  500. size := uint16(preambleSize-2) + packetSize
  501. // size
  502. binary.LittleEndian.PutUint16(buffer[0:2], size)
  503. // flags
  504. buffer[2] = flags
  505. // connID
  506. binary.LittleEndian.PutUint16(buffer[3:5], connID)
  507. // addr
  508. copy(buffer[5:5+len(remoteIP)], remoteIP)
  509. binary.BigEndian.PutUint16(buffer[5+len(remoteIP):7+len(remoteIP)], remotePort)
  510. return nil
  511. }