-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
88 lines (81 loc) · 1.81 KB
/
core.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
package main
import (
"fmt"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/process"
"log"
)
// KillProcessByPort 根据端口号关闭进程
func KillProcessByPort(port uint32) {
entity := GetConnectionByPort(port)
if entity == nil {
return
}
pr, err := process.NewProcess(entity.Pid)
if err != nil {
log.Fatal(err)
}
err = pr.Terminate()
if err != nil {
log.Fatal(err)
}
}
// GetConnectionByPort 根据端口号获取连接
func GetConnectionByPort(port uint32) *ConnectionEntity {
connections := GetConnections()
for _, con := range connections {
if con.Port == port {
return &con
}
}
return nil
}
// GetConnections 获取处于LISTEN状态的TCP连接
func GetConnections() []ConnectionEntity {
connections, _ := net.Connections("tcp")
entities := make([]ConnectionEntity, 0)
for _, con := range connections {
if con.Status == "LISTEN" {
entities = append(entities, ConnectionEntity{
Port: con.Laddr.Port,
Pid: con.Pid,
Name: GetPidName(con.Pid),
})
}
}
return entities
}
func List(port uint32) {
connections := GetConnections()
if port == 0 {
fmt.Printf("%-10s%-10s%s\n", "port", "pid", "name")
for _, con := range connections {
fmt.Printf("%-10d%-10d%s\n", con.Port, con.Pid, con.Name)
}
} else {
for _, con := range connections {
if con.Port == port {
fmt.Printf("%-10s%-10s%s\n", "port", "pid", "name")
fmt.Printf("%-10d%-10d%s\n", con.Port, con.Pid, con.Name)
break
}
}
}
}
// GetPidName 根据PID获取进程名称
func GetPidName(pid int32) string {
pro, err := process.NewProcess(pid)
if err != nil {
log.Fatal(err)
}
var name string
if name, err = pro.Name(); err != nil {
log.Fatal(err)
}
return name
}
type ConnectionEntity struct {
Port uint32 `json:"port"`
Pid int32 `json:"pid"`
Name string `json:"name"`
}