procfs.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package procfs
  2. import (
  3. "bufio"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "fmt"
  7. "net"
  8. "net/netip"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "unsafe"
  13. N "github.com/sagernet/sing/common/network"
  14. )
  15. var (
  16. netIndexOfLocal = -1
  17. netIndexOfUid = -1
  18. nativeEndian binary.ByteOrder
  19. )
  20. func init() {
  21. var x uint32 = 0x01020304
  22. if *(*byte)(unsafe.Pointer(&x)) == 0x01 {
  23. nativeEndian = binary.BigEndian
  24. } else {
  25. nativeEndian = binary.LittleEndian
  26. }
  27. }
  28. func ResolveSocketByProcSearch(network string, source, _ netip.AddrPort) int32 {
  29. if netIndexOfLocal < 0 || netIndexOfUid < 0 {
  30. return -1
  31. }
  32. path := "/proc/net/"
  33. if network == N.NetworkTCP {
  34. path += "tcp"
  35. } else {
  36. path += "udp"
  37. }
  38. if source.Addr().Is6() {
  39. path += "6"
  40. }
  41. sIP := source.Addr().AsSlice()
  42. if len(sIP) == 0 {
  43. return -1
  44. }
  45. var bytes [2]byte
  46. binary.BigEndian.PutUint16(bytes[:], source.Port())
  47. local := fmt.Sprintf("%s:%s", hex.EncodeToString(nativeEndianIP(sIP)), hex.EncodeToString(bytes[:]))
  48. file, err := os.Open(path)
  49. if err != nil {
  50. return -1
  51. }
  52. defer file.Close()
  53. reader := bufio.NewReader(file)
  54. for {
  55. row, _, err := reader.ReadLine()
  56. if err != nil {
  57. return -1
  58. }
  59. fields := strings.Fields(string(row))
  60. if len(fields) <= netIndexOfLocal || len(fields) <= netIndexOfUid {
  61. continue
  62. }
  63. if strings.EqualFold(local, fields[netIndexOfLocal]) {
  64. uid, err := strconv.Atoi(fields[netIndexOfUid])
  65. if err != nil {
  66. return -1
  67. }
  68. return int32(uid)
  69. }
  70. }
  71. }
  72. func nativeEndianIP(ip net.IP) []byte {
  73. result := make([]byte, len(ip))
  74. for i := 0; i < len(ip); i += 4 {
  75. value := binary.BigEndian.Uint32(ip[i:])
  76. nativeEndian.PutUint32(result[i:], value)
  77. }
  78. return result
  79. }
  80. func init() {
  81. file, err := os.Open("/proc/net/tcp")
  82. if err != nil {
  83. return
  84. }
  85. defer file.Close()
  86. reader := bufio.NewReader(file)
  87. header, _, err := reader.ReadLine()
  88. if err != nil {
  89. return
  90. }
  91. columns := strings.Fields(string(header))
  92. var txQueue, rxQueue, tr, tmWhen bool
  93. for idx, col := range columns {
  94. offset := 0
  95. if txQueue && rxQueue {
  96. offset--
  97. }
  98. if tr && tmWhen {
  99. offset--
  100. }
  101. switch col {
  102. case "tx_queue":
  103. txQueue = true
  104. case "rx_queue":
  105. rxQueue = true
  106. case "tr":
  107. tr = true
  108. case "tm->when":
  109. tmWhen = true
  110. case "local_address":
  111. netIndexOfLocal = idx + offset
  112. case "uid":
  113. netIndexOfUid = idx + offset
  114. }
  115. }
  116. }