-
Notifications
You must be signed in to change notification settings - Fork 63
/
conn_windows.go
67 lines (54 loc) · 1.32 KB
/
conn_windows.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
//go:build windows
// +build windows
package main
import (
"path/filepath"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/process"
)
type psutilConn struct{}
func (ps *psutilConn) GetOpenSockets() (OpenSockets, error) {
openSockets := make(OpenSockets)
if err := ps.getConnections(ProtoTCP, openSockets); err != nil {
return nil, err
}
if err := ps.getConnections(ProtoUDP, openSockets); err != nil {
return nil, err
}
return openSockets, nil
}
func (ps *psutilConn) getProcName(pid int32) ProcessInfo {
procInfo := ProcessInfo{Name: unknownProcessName}
proc, err := process.NewProcess(pid)
if err != nil {
return procInfo
}
exe, err := proc.Exe()
if err != nil {
return procInfo
}
procInfo.Pid = int(pid)
procInfo.Name = filepath.Base(exe)
return procInfo
}
func (ps *psutilConn) getConnections(proto Protocol, openSockets OpenSockets) error {
connections, err := net.Connections(string(proto))
if err != nil {
return err
}
for _, conn := range connections {
if proto == ProtoTCP && conn.Status != "ESTABLISHED" {
continue
}
localSocket := LocalSocket{
IP: conn.Laddr.IP,
Port: uint16(conn.Laddr.Port),
Protocol: proto,
}
openSockets[localSocket] = ps.getProcName(conn.Pid)
}
return nil
}
func GetSocketFetcher() SocketFetcher {
return &psutilConn{}
}