hub_darwin.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //go:build darwin
  2. // +build darwin
  3. package udp
  4. import (
  5. "bytes"
  6. "encoding/gob"
  7. "io"
  8. "github.com/xtls/xray-core/common/errors"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/transport/internet"
  11. )
  12. // RetrieveOriginalDest from stored laddr, caddr
  13. func RetrieveOriginalDest(oob []byte) net.Destination {
  14. dec := gob.NewDecoder(bytes.NewBuffer(oob))
  15. var la, ra net.UDPAddr
  16. dec.Decode(&la)
  17. dec.Decode(&ra)
  18. ip, port, err := internet.OriginalDst(&la, &ra)
  19. if err != nil {
  20. return net.Destination{}
  21. }
  22. return net.UDPDestination(net.IPAddress(ip), net.Port(port))
  23. }
  24. // ReadUDPMsg stores laddr, caddr for later use
  25. func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
  26. nBytes, addr, err := conn.ReadFromUDP(payload)
  27. var buf bytes.Buffer
  28. enc := gob.NewEncoder(&buf)
  29. udpAddr, ok := conn.LocalAddr().(*net.UDPAddr)
  30. if !ok {
  31. return 0, 0, 0, nil, errors.New("invalid local address")
  32. }
  33. if addr == nil {
  34. return 0, 0, 0, nil, errors.New("invalid remote address")
  35. }
  36. enc.Encode(udpAddr)
  37. enc.Encode(addr)
  38. var reader io.Reader = &buf
  39. noob, _ := reader.Read(oob)
  40. return nBytes, noob, 0, addr, err
  41. }