-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwindows_netstat.go
More file actions
90 lines (83 loc) · 2.58 KB
/
Copy pathwindows_netstat.go
File metadata and controls
90 lines (83 loc) · 2.58 KB
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
package main
import (
"path"
"strconv"
"strings"
)
type windowsTCPPortProcessIDs struct {
Listening []uint32
Bound []uint32
}
func parseWindowsTCPListenerProcessIDs(output string, port uint16) []uint32 {
return parseWindowsTCPPortProcessIDs(output, port).Listening
}
func parseWindowsTCPPortProcessIDs(output string, port uint16) windowsTCPPortProcessIDs {
if port == 0 {
return windowsTCPPortProcessIDs{}
}
seenListening := map[uint32]bool{}
seenBound := map[uint32]bool{}
result := windowsTCPPortProcessIDs{}
for _, line := range strings.Split(output, "\n") {
fields := strings.Fields(line)
if len(fields) < 5 || !strings.EqualFold(fields[0], "TCP") {
continue
}
state := strings.ToUpper(fields[len(fields)-2])
if state != "LISTENING" && state != "BOUND" {
continue
}
separator := strings.LastIndex(fields[1], ":")
if separator < 0 {
continue
}
localHost := strings.TrimSpace(fields[1][:separator])
if localHost != "127.0.0.1" && localHost != "0.0.0.0" {
continue
}
localPort, err := strconv.ParseUint(fields[1][separator+1:], 10, 16)
if err != nil || uint16(localPort) != port {
continue
}
owner, err := strconv.ParseUint(fields[len(fields)-1], 10, 32)
processID := uint32(owner)
if err != nil || processID == 0 {
continue
}
if state == "LISTENING" && !seenListening[processID] {
seenListening[processID] = true
result.Listening = append(result.Listening, processID)
}
if state == "BOUND" && !seenBound[processID] {
seenBound[processID] = true
result.Bound = append(result.Bound, processID)
}
}
return result
}
func windowsRestartTargetProcessMatches(name, packageFamily, imagePath, expectedPackageFamily, expectedExecutablePath string) bool {
if !isWindowsTargetAppExecutableName(name) {
return false
}
if expectedPackageFamily != "" && strings.EqualFold(strings.TrimSpace(packageFamily), strings.TrimSpace(expectedPackageFamily)) {
return true
}
return windowsExecutablePathsEqual(imagePath, expectedExecutablePath)
}
func windowsExecutablePathsEqual(left, right string) bool {
left = normalizeWindowsExecutablePath(left)
right = normalizeWindowsExecutablePath(right)
return left != "" && right != "" && left == right
}
func normalizeWindowsExecutablePath(value string) string {
value = strings.TrimSpace(strings.ReplaceAll(value, `\`, "/"))
if strings.HasPrefix(strings.ToLower(value), "//?/unc/") {
value = "//" + value[len("//?/unc/"):]
} else if strings.HasPrefix(strings.ToLower(value), "//?/") {
value = value[len("//?/"):]
}
if value == "" {
return ""
}
return strings.ToLower(path.Clean(value))
}