Browse Source

windows监控不到的bug

moki 3 weeks ago
parent
commit
230ce76a1e
2 changed files with 34 additions and 12 deletions
  1. 1 0
      .idea/AI-Status-Light.iml
  2. 33 12
      internal/discovery/discovery_windows.go

+ 1 - 0
.idea/AI-Status-Light.iml

@@ -5,6 +5,7 @@
     <content url="file://$MODULE_DIR$">
       <excludeFolder url="file://$MODULE_DIR$/bin" />
       <excludeFolder url="file://$MODULE_DIR$/data" />
+      <excludeFolder url="file://$MODULE_DIR$/dist" />
     </content>
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />

+ 33 - 12
internal/discovery/discovery_windows.go

@@ -12,7 +12,7 @@ import (
 
 func findByPID() []int {
 	var ports []int
-	pidRegex := regexp.MustCompile(`(?i)opencode`)
+	pidRegex := regexp.MustCompile(`(?i)(opencode|bun)`)
 
 	tasklistOut, err := exec.Command("tasklist", "/FO", "CSV", "/NH").Output()
 	if err != nil {
@@ -68,19 +68,39 @@ func findByPID() []int {
 func findByCmdline() []int {
 	var ports []int
 
+	// 查找 opencode 进程
 	wmicOut, err := exec.Command("wmic", "process", "where", "name like '%opencode%'", "get", "commandline", "/FORMAT:LIST").Output()
-	if err != nil {
-		return ports
+	if err == nil {
+		for _, line := range strings.Split(string(wmicOut), "\n") {
+			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)
+							}
+						}
+					}
+				}
+			}
+		}
 	}
 
-	for _, line := range strings.Split(string(wmicOut), "\n") {
-		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)
+	// 查找 bun 进程运行的 opencode
+	if len(ports) == 0 {
+		bunOut, err := exec.Command("wmic", "process", "where", "name like '%bun%'", "get", "commandline", "/FORMAT:LIST").Output()
+		if err == nil {
+			for _, line := range strings.Split(string(bunOut), "\n") {
+				if strings.Contains(line, "opencode") && 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)
+								}
+							}
 						}
 					}
 				}
@@ -88,8 +108,9 @@ func findByCmdline() []int {
 		}
 	}
 
+	// 使用 PowerShell 作为后备方案
 	if len(ports) == 0 {
-		powershellOut, err := exec.Command("powershell", "-Command", "Get-CimInstance Win32_Process | Where-Object {$_.Name -like '*opencode*'} | Select-Object CommandLine").Output()
+		powershellOut, err := exec.Command("powershell", "-Command", "Get-CimInstance Win32_Process | Where-Object {$_.Name -like '*opencode*' -or ($_.Name -like '*bun*' -and $_.CommandLine -like '*opencode*')} | Select-Object CommandLine").Output()
 		if err == nil {
 			for _, line := range strings.Split(string(powershellOut), "\n") {
 				if strings.Contains(line, "--port") {