packet.go 850 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package packet
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "log"
  6. )
  7. var udpHeaderLen = 8
  8. func ProcessPacketBeforeSending(buf []byte, n, dstPort int) ([]byte, int, error) {
  9. log.Println("@###### DST Port: ", dstPort)
  10. portbuf := new(bytes.Buffer)
  11. binary.Write(portbuf, binary.BigEndian, uint16(dstPort))
  12. if n > len(buf)-2 {
  13. buf = append(buf, portbuf.Bytes()[0])
  14. buf = append(buf, portbuf.Bytes()[1])
  15. } else {
  16. buf[n] = portbuf.Bytes()[0]
  17. buf[n+1] = portbuf.Bytes()[1]
  18. }
  19. n += 2
  20. return buf, n, nil
  21. }
  22. func ExtractInfo(buffer []byte, n int) (int, int, error) {
  23. data := buffer[:n]
  24. var localWgPort uint16
  25. portBuf := data[n-2 : n+1]
  26. reader := bytes.NewReader(portBuf)
  27. err := binary.Read(reader, binary.BigEndian, &localWgPort)
  28. if err != nil {
  29. log.Println("Failed to read port buffer: ", err)
  30. }
  31. n -= 2
  32. return int(localWgPort), n, err
  33. }