|
|
@@ -1,141 +0,0 @@
|
|
|
-//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, "opencode-monitor") && !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, "opencode-monitor") && !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
|
|
|
-}
|