-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (125 loc) · 3.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/feiandxs/port-releaser/i18n"
"golang.org/x/text/language"
)
// detectLanguage 检测系统语言
func detectLanguage() string {
langEnv := os.Getenv("LANG")
langTag, err := language.Parse(langEnv)
if err != nil {
return "en" // 默认为英文
}
base, _ := langTag.Base()
langCode := base.String()
// 检查语言是否支持,如果不支持则返回英文
if _, ok := i18n.Messages[langCode]; !ok {
return "en"
}
return langCode
}
// findProcessWindows 在Windows上查找占用特定端口的进程
func findProcessWindows(port string) ([]string, error) {
cmd := exec.Command("cmd", "/c", "netstat -ano | findstr :"+port)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
var processes []string
for _, line := range strings.Split(string(output), "\n") {
if strings.Contains(line, "LISTENING") {
processes = append(processes, line)
}
}
return processes, nil
}
// findProcessUnix 在Unix-like系统(macOS和Linux)上查找占用特定端口的进程
func findProcessUnix(port string) ([]string, error) {
cmd := exec.Command("lsof", "-i", "tcp:"+port)
output, err := cmd.CombinedOutput()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == 1 {
return []string{}, nil
}
}
return nil, err
}
var processes []string
for _, line := range strings.Split(string(output), "\n") {
if strings.Contains(line, "LISTEN") {
processes = append(processes, line)
}
}
return processes, nil
}
// killProcess 使用适当的命令终止指定PID的进程
func killProcess(pid string) error {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("taskkill", "/f", "/pid", pid)
} else {
cmd = exec.Command("kill", "-9", pid)
}
return cmd.Run()
}
// confirmAndKill 显示进程信息,并询问用户是否终止进程
func confirmAndKill(processes []string, lang string) {
reader := bufio.NewReader(os.Stdin)
for _, process := range processes {
fmt.Printf(i18n.Messages[lang]["process"], process)
fields := strings.Fields(process)
var pid string
if runtime.GOOS == "windows" {
pid = fields[len(fields)-1]
} else {
pid = fields[1]
}
fmt.Print(i18n.Messages[lang]["terminateProcess"])
response, _ := reader.ReadString('\n')
if strings.TrimSpace(response) == "y" {
err := killProcess(pid)
if err != nil {
fmt.Printf(i18n.Messages[lang]["unableToTerminate"], pid, err)
} else {
fmt.Printf(i18n.Messages[lang]["processTerminated"], pid)
}
}
}
}
func main() {
lang := detectLanguage()
flag.Parse()
port := flag.Arg(0)
if port == "" {
fmt.Println(i18n.Messages[lang]["specifyPort"])
return
}
var processes []string
var err error
switch runtime.GOOS {
case "windows":
processes, err = findProcessWindows(port)
case "darwin", "linux":
processes, err = findProcessUnix(port)
default:
fmt.Printf(i18n.Messages[lang]["unsupportedOS"], runtime.GOOS)
return
}
if err != nil {
fmt.Printf(i18n.Messages[lang]["errorFindingPort"], err)
return
}
if len(processes) == 0 {
fmt.Printf(i18n.Messages[lang]["noProcessFound"], port)
return
}
confirmAndKill(processes, lang)
}