discovery_darwin.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //go:build darwin
  2. package discovery
  3. import (
  4. "fmt"
  5. "os/exec"
  6. "regexp"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. )
  11. func findByPID() []int {
  12. var ports []int
  13. pidRegex := regexp.MustCompile(`\bopencode\b`)
  14. out, err := exec.Command("ps", "aux").Output()
  15. if err != nil {
  16. return ports
  17. }
  18. var pids []int
  19. for _, line := range strings.Split(string(out), "\n") {
  20. if pidRegex.MatchString(line) && !strings.Contains(line, "grep") {
  21. fields := strings.Fields(line)
  22. if len(fields) > 1 {
  23. if pid, err := strconv.Atoi(fields[1]); err == nil {
  24. pids = append(pids, pid)
  25. }
  26. }
  27. }
  28. }
  29. for _, pid := range pids {
  30. lsofOut, err := exec.Command("lsof", "-p", strconv.Itoa(pid), "-i", "TCP", "-n").Output()
  31. if err != nil {
  32. continue
  33. }
  34. for _, line := range strings.Split(string(lsofOut), "\n") {
  35. if strings.Contains(line, "LISTEN") {
  36. fields := strings.Fields(line)
  37. for _, f := range fields {
  38. if strings.Contains(f, ":") {
  39. parts := strings.Split(f, ":")
  40. if len(parts) >= 2 {
  41. if port, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
  42. if !contains(ports, port) {
  43. ports = append(ports, port)
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. netstatOut, err := exec.Command("netstat", "-an").Output()
  52. if err == nil {
  53. for _, line := range strings.Split(string(netstatOut), "\n") {
  54. if strings.Contains(line, "LISTEN") {
  55. fields := strings.Fields(line)
  56. if len(fields) >= 4 {
  57. addr := fields[3]
  58. if strings.Contains(addr, ":") {
  59. parts := strings.Split(addr, ":")
  60. if len(parts) >= 2 {
  61. if port, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
  62. if port > 1024 && port < 65535 && !contains(ports, port) {
  63. ports = append(ports, port)
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. sort.Ints(ports)
  74. return ports
  75. }
  76. func findByCmdline() []int {
  77. var ports []int
  78. out, err := exec.Command("ps", "aux").Output()
  79. if err != nil {
  80. return ports
  81. }
  82. for _, line := range strings.Split(string(out), "\n") {
  83. if strings.Contains(line, "opencode") && !strings.Contains(line, "grep") {
  84. if strings.Contains(line, "--port") {
  85. fields := strings.Fields(line)
  86. for i, f := range fields {
  87. if f == "--port" && i+1 < len(fields) {
  88. if port, err := strconv.Atoi(fields[i+1]); err == nil {
  89. if !contains(ports, port) {
  90. ports = append(ports, port)
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. sort.Ints(ports)
  99. return ports
  100. }
  101. func getListeningPorts(pid int) []int {
  102. var ports []int
  103. lsofOut, err := exec.Command("lsof", "-p", strconv.Itoa(pid), "-i", "TCP", "-n", "-P").Output()
  104. if err != nil {
  105. return ports
  106. }
  107. for _, line := range strings.Split(string(lsofOut), "\n") {
  108. if strings.Contains(line, "LISTEN") {
  109. fields := strings.Fields(line)
  110. for _, f := range fields {
  111. if strings.Contains(f, ":") {
  112. parts := strings.Split(f, ":")
  113. if len(parts) >= 2 {
  114. if port, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
  115. if !contains(ports, port) {
  116. ports = append(ports, port)
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. return ports
  125. }