forked from microsoft/ethr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plt_windows.go
206 lines (186 loc) · 5.2 KB
/
plt_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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//-----------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license.
// See LICENSE.txt file in the project root for full license information.
//-----------------------------------------------------------------------------
package main
import (
"net"
"strings"
"syscall"
"unsafe"
tm "github.com/nsf/termbox-go"
)
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
var user32 = syscall.NewLazyDLL("user32.dll")
var iphlpapi = syscall.NewLazyDLL("iphlpapi.dll")
var (
proc_get_tcp_statistics_ex = iphlpapi.NewProc("GetTcpStatisticsEx")
proc_get_console_window = kernel32.NewProc("GetConsoleWindow")
proc_get_system_menu = user32.NewProc("GetSystemMenu")
proc_delete_menu = user32.NewProc("DeleteMenu")
proc_get_if_entry2 = iphlpapi.NewProc("GetIfEntry2")
)
type ethrNetDevInfo struct {
bytes uint64
packets uint64
drop uint64
errs uint64
}
func getNetDevStats(stats *ethrNetStat) {
ifs, err := net.Interfaces()
if err != nil {
ui.printErr("%v", err)
return
}
for _, ifi := range ifs {
if (ifi.Flags&net.FlagUp) == 0 || strings.Contains(ifi.Name, "Pseudo") {
continue
}
row := mibIfRow2{InterfaceIndex: uint32(ifi.Index)}
e := getIfEntry2(&row)
if e != nil {
ui.printErr("%v", err)
return
}
rxInfo := ethrNetDevInfo{
bytes: uint64(row.InOctets),
packets: uint64(row.InUcastPkts),
drop: uint64(row.InDiscards),
errs: uint64(row.InErrors),
}
txInfo := ethrNetDevInfo{
bytes: uint64(row.OutOctets),
packets: uint64(row.OutUcastPkts),
drop: uint64(row.OutDiscards),
errs: uint64(row.OutErrors),
}
netStats := ethrNetDevStat{
interfaceName: ifi.Name,
rxBytes: rxInfo.bytes,
txBytes: txInfo.bytes,
rxPkts: rxInfo.packets,
txPkts: txInfo.packets,
}
stats.netDevStats = append(stats.netDevStats, netStats)
}
}
type mib_tcpstats struct {
DwRtoAlgorithm uint32
DwRtoMin uint32
DwRtoMax uint32
DwMaxConn uint32
DwActiveOpens uint32
DwPassiveOpens uint32
DwAttemptFails uint32
DwEstabResets uint32
DwCurrEstab uint32
DwInSegs uint32
DwOutSegs uint32
DwRetransSegs uint32
DwInErrs uint32
DwOutRsts uint32
DwNumConns uint32
}
const (
AF_INET = 2
AF_INET6 = 23
)
func getTCPStats(stats *ethrNetStat) (errcode error) {
tcpStats := &mib_tcpstats{}
r0, _, _ := syscall.Syscall(proc_get_tcp_statistics_ex.Addr(), 2,
uintptr(unsafe.Pointer(tcpStats)),
uintptr(AF_INET),
0)
if r0 != 0 {
errcode = syscall.Errno(r0)
return
}
stats.tcpStats.segRetrans = uint64(tcpStats.DwRetransSegs)
return
}
type guid struct {
Data1 uint32
Data2 uint16
Data3 uint16
Data4 [8]byte
}
const (
MAX_STRING_SIZE = 256
MAX_PHYS_ADDRESS_LENGTH = 32
pad0for64_4for32 = 0
)
type mibIfRow2 struct {
InterfaceLuid uint64
InterfaceIndex uint32
InterfaceGuid guid
Alias [MAX_STRING_SIZE + 1]uint16
Description [MAX_STRING_SIZE + 1]uint16
PhysicalAddressLength uint32
PhysicalAddress [MAX_PHYS_ADDRESS_LENGTH]uint8
PermanentPhysicalAddress [MAX_PHYS_ADDRESS_LENGTH]uint8
Mtu uint32
Type uint32
TunnelType uint32
MediaType uint32
PhysicalMediumType uint32
AccessType uint32
DirectionType uint32
InterfaceAndOperStatusFlags uint32
OperStatus uint32
AdminStatus uint32
MediaConnectState uint32
NetworkGuid guid
ConnectionType uint32
padding1 [pad0for64_4for32]byte
TransmitLinkSpeed uint64
ReceiveLinkSpeed uint64
InOctets uint64
InUcastPkts uint64
InNUcastPkts uint64
InDiscards uint64
InErrors uint64
InUnknownProtos uint64
InUcastOctets uint64
InMulticastOctets uint64
InBroadcastOctets uint64
OutOctets uint64
OutUcastPkts uint64
OutNUcastPkts uint64
OutDiscards uint64
OutErrors uint64
OutUcastOctets uint64
OutMulticastOctets uint64
OutBroadcastOctets uint64
OutQLen uint64
}
func getIfEntry2(row *mibIfRow2) (errcode error) {
r0, _, _ := syscall.Syscall(proc_get_if_entry2.Addr(), 1,
uintptr(unsafe.Pointer(row)), 0, 0)
if r0 != 0 {
errcode = syscall.Errno(r0)
}
return
}
func hideCursor() {
tm.HideCursor()
}
const (
MF_BYCOMMAND = 0x00000000
SC_CLOSE = 0xF060
SC_MINIMIZE = 0xF020
SC_MAXIMIZE = 0xF030
SC_SIZE = 0xF000
)
func blockWindowResize() {
h, _, err := syscall.Syscall(proc_get_console_window.Addr(), 0, 0, 0, 0)
if err != 0 {
return
}
sysMenu, _, err := syscall.Syscall(proc_get_system_menu.Addr(), 2, h, 0, 0)
if err != 0 {
return
}
syscall.Syscall(proc_delete_menu.Addr(), 3, sysMenu, SC_MAXIMIZE, MF_BYCOMMAND)
syscall.Syscall(proc_delete_menu.Addr(), 3, sysMenu, SC_SIZE, MF_BYCOMMAND)
}