| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //go:build darwin
- package discovery
- import (
- "os/exec"
- "regexp"
- "sort"
- "strconv"
- "strings"
- )
- func findByPID() []int {
- var ports []int
- pidRegex := regexp.MustCompile(`\bopencode\b`)
- out, err := exec.Command("ps", "aux").Output()
- if err != nil {
- return ports
- }
- var pids []int
- for _, line := range strings.Split(string(out), "\n") {
- if pidRegex.MatchString(line) && !strings.Contains(line, "grep") {
- fields := strings.Fields(line)
- if len(fields) > 1 {
- if pid, err := strconv.Atoi(fields[1]); err == nil {
- pids = append(pids, pid)
- }
- }
- }
- }
- for _, pid := range pids {
- lsofOut, err := exec.Command("lsof", "-p", strconv.Itoa(pid), "-i", "TCP", "-n").Output()
- if err != nil {
- continue
- }
- for _, line := range strings.Split(string(lsofOut), "\n") {
- if strings.Contains(line, "LISTEN") {
- fields := strings.Fields(line)
- for _, f := range fields {
- if strings.Contains(f, ":") {
- parts := strings.Split(f, ":")
- if len(parts) >= 2 {
- if port, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
- if !contains(ports, port) {
- ports = append(ports, port)
- }
- }
- }
- }
- }
- }
- }
- netstatOut, err := exec.Command("netstat", "-an").Output()
- if err == nil {
- for _, line := range strings.Split(string(netstatOut), "\n") {
- if strings.Contains(line, "LISTEN") {
- fields := strings.Fields(line)
- if len(fields) >= 4 {
- addr := fields[3]
- if strings.Contains(addr, ":") {
- parts := strings.Split(addr, ":")
- if len(parts) >= 2 {
- if port, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
- if port > 1024 && port < 65535 && !contains(ports, port) {
- ports = append(ports, port)
- }
- }
- }
- }
- }
- }
- }
- }
- }
- sort.Ints(ports)
- return ports
- }
- func findByCmdline() []int {
- var ports []int
- out, err := exec.Command("ps", "aux").Output()
- if err != nil {
- return ports
- }
- for _, line := range strings.Split(string(out), "\n") {
- if strings.Contains(line, "opencode") && !strings.Contains(line, "grep") {
- if strings.Contains(line, "--port") {
- fields := strings.Fields(line)
- for i, f := range fields {
- if f == "--port" && i+1 < len(fields) {
- if port, err := strconv.Atoi(fields[i+1]); err == nil {
- if !contains(ports, port) {
- ports = append(ports, port)
- }
- }
- }
- }
- }
- }
- }
- sort.Ints(ports)
- return ports
- }
- func getListeningPorts(pid int) []int {
- var ports []int
- lsofOut, err := exec.Command("lsof", "-p", strconv.Itoa(pid), "-i", "TCP", "-n", "-P").Output()
- if err != nil {
- return ports
- }
- for _, line := range strings.Split(string(lsofOut), "\n") {
- if strings.Contains(line, "LISTEN") {
- fields := strings.Fields(line)
- for _, f := range fields {
- if strings.Contains(f, ":") {
- parts := strings.Split(f, ":")
- if len(parts) >= 2 {
- if port, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
- if !contains(ports, port) {
- ports = append(ports, port)
- }
- }
- }
- }
- }
- }
- }
- return ports
- }
|