forked from winlabs/gowin32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwts.go
428 lines (362 loc) · 14.8 KB
/
wts.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright (c) 2014-2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gowin32
import (
"github.com/winlabs/gowin32/wrappers"
"fmt"
"net"
"syscall"
"time"
"unsafe"
)
// WTSConnectState enum type - Go version of WTS_CONNECTSTATE_CLASS
type WTSConnectState uint32
const (
WTSConnectStateActive WTSConnectState = wrappers.WTSActive
WTSConnectStateConnected WTSConnectState = wrappers.WTSConnected
WTSConnectStateConnectQuery WTSConnectState = wrappers.WTSConnectQuery
WTSConnectStateShadow WTSConnectState = wrappers.WTSShadow
WTSConnectStateDisconnected WTSConnectState = wrappers.WTSDisconnected
WTSConnectStateIdle WTSConnectState = wrappers.WTSIdle
WTSConnectStateListen WTSConnectState = wrappers.WTSListen
WTSConnectStateReset WTSConnectState = wrappers.WTSReset
WTSConnectStateDown WTSConnectState = wrappers.WTSDown
WTSConnectStateInit WTSConnectState = wrappers.WTSInit
)
// WTSClientProtocolType enum type go version of WTSClientProtocolType
type WTSClientProtocolType uint32
const (
WTSClientProtocolConsoleSession WTSClientProtocolType = 0
WTSClientProtocolInternal WTSClientProtocolType = 1
WTSClientProtocolRDP WTSClientProtocolType = 2
)
// WTSClientInfo - go version of WTSCLIENT structure
type WTSClientInfo struct {
ClientName string
Domain string
UserName string
WorkDirectory string
InitialProgram string
EncryptionLevel byte
ClientAddressFamily AddressFamily
clientAddress [wrappers.CLIENTADDRESS_LENGTH + 1]uint16
HRes uint
VRes uint
ColorDepth uint
ClientDirectory string
ClientBuildNumber uint
ClientHardwareId uint
ClientProductId uint
OutBufCountHost uint
OutBufCountClient uint
OutBufLength uint
DeviceID string
}
func (ci *WTSClientInfo) ClientAddressToIP() (net.IP, error) {
var buf [16]byte
if ci.ClientAddressFamily == wrappers.AF_INET {
for i := 0; i < 4; i++ {
buf[i] = byte(ci.clientAddress[i])
}
} else {
n := 0
for i := 0; i < 8; i++ {
buf[n] = byte(ci.clientAddress[i] >> 8 & 0xff)
buf[n+1] = byte(ci.clientAddress[i])
n = n + 2
}
}
return clientAddressToIP(uint32(ci.ClientAddressFamily), buf[:])
}
// WTSClientDisplay - go version of WTS_CLIENT_DISPLAY structure
type WTSClientDisplay struct {
HorizontalResolution uint
VerticalResolution uint
ColorDepth uint
}
// Info - go version of WTSINFO structure
type WTSInfo struct {
State WTSConnectState
SessionID uint
IncomingBytes uint
OutgoingBytes uint
IncomingFrames uint
OutgoingFrames uint
IncomingCompressedBytes uint
OutgoingCompressedBytes uint
WinStationName string
Domain string
UserName string
ConnectTime time.Time
DisconnectTime time.Time
LastInputTime time.Time
LogonTime time.Time
CurrentTime time.Time
}
// WTSSessionInfo - go version of WTS_SESSION_INFO structure
type WTSSessionInfo struct {
SessionID uint
WinStationName string
State WTSConnectState
}
type WTSServer struct {
handle syscall.Handle
}
func OpenWTSServer(serverName string) *WTSServer {
result := WTSServer{}
if serverName != "" {
result.handle = wrappers.WTSOpenServer(syscall.StringToUTF16Ptr(serverName))
}
return &result
}
func (wts *WTSServer) Close() {
if wts.handle != 0 {
wrappers.WTSCloseServer(wts.handle)
wts.handle = 0
}
}
func (wts *WTSServer) EnumerateSessions() ([]WTSSessionInfo, error) {
var sessionInfo *wrappers.WTS_SESSION_INFO
var count uint32
if err := wrappers.WTSEnumerateSessions(wts.handle, 0, 1, &sessionInfo, &count); err != nil {
return nil, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(sessionInfo)))
si := sessionInfo
result := make([]WTSSessionInfo, count)
for i := uint32(0); i < count; i++ {
result[i] = WTSSessionInfo{SessionID: uint(si.SessionId),
WinStationName: LpstrToString(si.WinStationName),
State: WTSConnectState(si.State)}
si = (*wrappers.WTS_SESSION_INFO)(unsafe.Pointer(uintptr(unsafe.Pointer(si)) + unsafe.Sizeof(*si)))
}
return result, nil
}
func (wts *WTSServer) LogoffSession(sessionID uint, wait bool) error {
return wrappers.WTSLogoffSession(wts.handle, uint32(sessionID), wait)
}
func (wts *WTSServer) QuerySessionInitialProgram(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSInitialProgram)
}
func (wts *WTSServer) QuerySessionApplicationName(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSApplicationName)
}
func (wts *WTSServer) QuerySessionWorkingDirectory(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSWorkingDirectory)
}
func (wts *WTSServer) QuerySessionID(sessionID uint) (uint, error) {
r1, err := wts.querySessionInformationAsUint32(sessionID, wrappers.WTSSessionId)
return uint(r1), err
}
func (wts *WTSServer) QuerySessionUserName(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSUserName)
}
func (wts *WTSServer) QuerySessionWinStationName(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSWinStationName)
}
func (wts *WTSServer) QuerySessionDomainName(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSDomainName)
}
func (wts *WTSServer) QuerySessionConnectState(sessionID uint) (WTSConnectState, error) {
r1, err := wts.querySessionInformationAsUint32(sessionID, wrappers.WTSConnectState)
return WTSConnectState(r1), err
}
func (wts *WTSServer) QuerySessionClientBuildNumber(sessionID uint) (uint32, error) {
return wts.querySessionInformationAsUint32(sessionID, wrappers.WTSClientBuildNumber)
}
func (wts *WTSServer) QuerySessionClientName(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSClientName)
}
func (wts *WTSServer) QuerySessionClientDirectory(sessionID uint) (string, error) {
return wts.querySessionInformationAsString(sessionID, wrappers.WTSClientDirectory)
}
func (wts *WTSServer) QuerySessionClientProductId(sessionID uint) (uint16, error) {
return wts.querySessionInformationAsUint16(sessionID, wrappers.WTSClientProductId)
}
func (wts *WTSServer) QuerySessionClientHardwareId(sessionID uint) (uint32, error) {
return wts.querySessionInformationAsUint32(sessionID, wrappers.WTSClientHardwareId)
}
func (wts *WTSServer) QuerySessionClientAddress(sessionID uint) (net.IP, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), wrappers.WTSClientAddress, &buffer, &bytesReturned); err != nil {
return net.IP{}, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
// MS doc: The IP address is offset by two bytes from the start of the Address member of the WTS_CLIENT_ADDRESS structure.
// https://msdn.microsoft.com/en-us/library/aa383861%28v=vs.85%29.aspx
a := *(*wrappers.WTS_CLIENT_ADDRESS)(unsafe.Pointer(buffer))
return clientAddressToIP(a.AddressFamily, a.Address[2:])
}
func (wts *WTSServer) QuerySessionClientDisplay(sessionID uint) (WTSClientDisplay, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), wrappers.WTSClientDisplay, &buffer, &bytesReturned); err != nil {
return WTSClientDisplay{}, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
cd := *(*wrappers.WTS_CLIENT_DISPLAY)(unsafe.Pointer(buffer))
return WTSClientDisplay{
HorizontalResolution: uint(cd.HorizontalResolution),
VerticalResolution: uint(cd.HorizontalResolution),
ColorDepth: uint(cd.ColorDepth)}, nil
}
func (wts *WTSServer) QuerySessionClientProtocolType(sessionID uint) (WTSClientProtocolType, error) {
r1, err := wts.querySessionInformationAsUint16(sessionID, wrappers.WTSClientProtocolType)
return WTSClientProtocolType(r1), err
}
func (wts *WTSServer) QuerySessionClientInfo(sessionID uint) (WTSClientInfo, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), wrappers.WTSClientInfo, &buffer, &bytesReturned); err != nil {
return WTSClientInfo{}, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
c := *(*wrappers.WTSCLIENT)(unsafe.Pointer(buffer))
return WTSClientInfo{
ClientName: syscall.UTF16ToString(c.ClientName[:]),
Domain: syscall.UTF16ToString(c.Domain[:]),
UserName: syscall.UTF16ToString(c.UserName[:]),
WorkDirectory: syscall.UTF16ToString(c.WorkDirectory[:]),
InitialProgram: syscall.UTF16ToString(c.InitialProgram[:]),
EncryptionLevel: c.EncryptionLevel,
ClientAddressFamily: AddressFamily(c.ClientAddressFamily),
clientAddress: c.ClientAddress,
HRes: uint(c.HRes),
VRes: uint(c.VRes),
ColorDepth: uint(c.ColorDepth),
ClientDirectory: syscall.UTF16ToString(c.ClientDirectory[:]),
ClientBuildNumber: uint(c.ClientBuildNumber),
ClientHardwareId: uint(c.ClientHardwareId),
ClientProductId: uint(c.ClientProductId),
OutBufCountHost: uint(c.OutBufCountHost),
OutBufCountClient: uint(c.OutBufCountClient),
OutBufLength: uint(c.OutBufLength),
DeviceID: syscall.UTF16ToString(c.DeviceId[:]),
}, nil
}
func (wts *WTSServer) QuerySessionSesionInfo(sessionID uint) (WTSInfo, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), wrappers.WTSSessionInfo, &buffer, &bytesReturned); err != nil {
return WTSInfo{}, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
i := *(*wrappers.WTSINFO)(unsafe.Pointer(buffer))
return WTSInfo{
State: WTSConnectState(i.State),
SessionID: uint(i.SessionId),
IncomingBytes: uint(i.IncomingBytes),
OutgoingBytes: uint(i.OutgoingBytes),
IncomingFrames: uint(i.IncomingFrames),
OutgoingFrames: uint(i.OutgoingFrames),
IncomingCompressedBytes: uint(i.IncomingCompressedBytes),
OutgoingCompressedBytes: uint(i.OutgoingCompressedBytes),
WinStationName: syscall.UTF16ToString(i.WinStationName[:]),
Domain: syscall.UTF16ToString(i.Domain[:]),
UserName: syscall.UTF16ToString(i.UserName[:]),
ConnectTime: windowsFileTimeToTime(i.ConnectTime),
DisconnectTime: windowsFileTimeToTime(i.DisconnectTime),
LastInputTime: windowsFileTimeToTime(i.LastInputTime),
LogonTime: windowsFileTimeToTime(i.LogonTime),
CurrentTime: windowsFileTimeToTime(i.CurrentTime)}, nil
}
func (wts *WTSServer) QuerySessionAddressV4(sessionID uint) (wrappers.WTS_CLIENT_ADDRESS, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), wrappers.WTSSessionAddressV4, &buffer, &bytesReturned); err != nil {
return wrappers.WTS_CLIENT_ADDRESS{}, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
return *(*wrappers.WTS_CLIENT_ADDRESS)(unsafe.Pointer(buffer)), nil
}
func (wts *WTSServer) QuerySessionIsRemoteSession(sessionID uint) (bool, error) {
return wts.querySessionInformationAsBool(sessionID, wrappers.WTSIsRemoteSession)
}
func (wts *WTSServer) QueryUserToken(sessionID uint) (*Token, error) {
var handle syscall.Handle
if err := wrappers.WTSQueryUserToken(uint32(sessionID), &handle); err != nil {
return nil, err
}
return &Token{handle: handle}, nil
}
func (wts *WTSServer) querySessionInformationAsBool(sessionID uint, infoClass uint32) (bool, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), infoClass, &buffer, &bytesReturned); err != nil {
return false, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
if bytesReturned != 1 {
return false, buferSizeError(1, bytesReturned)
}
return *(*byte)(unsafe.Pointer(buffer)) != 0, nil
}
func (wts *WTSServer) querySessionInformationAsString(sessionID uint, infoClass uint32) (string, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), infoClass, &buffer, &bytesReturned); err != nil {
return "", err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
return LpstrToString(buffer), nil
}
func (wts *WTSServer) querySessionInformationAsUint16(sessionID uint, infoClass uint32) (uint16, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), infoClass, &buffer, &bytesReturned); err != nil {
return 0, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
if bytesReturned != 2 {
return 0, buferSizeError(2, bytesReturned)
}
return *(*uint16)(unsafe.Pointer(buffer)), nil
}
func (wts *WTSServer) querySessionInformationAsUint32(sessionID uint, infoClass uint32) (uint32, error) {
var buffer *uint16
var bytesReturned uint32
if err := wrappers.WTSQuerySessionInformation(wts.handle, uint32(sessionID), infoClass, &buffer, &bytesReturned); err != nil {
return 0, err
}
defer wrappers.WTSFreeMemory((*byte)(unsafe.Pointer(buffer)))
if bytesReturned != 4 {
return 0, buferSizeError(4, bytesReturned)
}
return *(*uint32)(unsafe.Pointer(buffer)), nil
}
func buferSizeError(excpected, returned uint32) error {
return fmt.Errorf("Invalid buffer size. Expected: %d returned: %d", excpected, returned)
}
func clientAddressToIP(addressFamily uint32, address []byte) (net.IP, error) {
switch addressFamily {
case wrappers.AF_INET:
if len(address) >= 4 {
return net.IPv4(address[0], address[1], address[2], address[3]), nil
}
case wrappers.AF_INET6:
if len(address) >= 16 {
return net.IP(address[:16]), nil
}
}
return nil, fmt.Errorf("Unknown addressFamily: %v", addressFamily)
}
func windowsFileTimeToTime(fileTime int64) time.Time {
const TicksPerSecond = 10000000
const EpochDifference = 11644473600
// we also can use win32 api FileTimeToSystemTime
return time.Unix((fileTime/TicksPerSecond)-EpochDifference, 0)
}