-
Notifications
You must be signed in to change notification settings - Fork 20
/
event.go
303 lines (270 loc) · 9.15 KB
/
event.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
// +build windows
package winlog
/*
#cgo LDFLAGS: -l wevtapi
#include "event.h"
*/
import "C"
import (
"errors"
"fmt"
"time"
"unsafe"
)
type EVT_SUBSCRIBE_FLAGS int
const (
_ = iota
EvtSubscribeToFutureEvents
EvtSubscribeStartAtOldestRecord
EvtSubscribeStartAfterBookmark
)
type EVT_VARIANT_TYPE int
const (
EvtVarTypeNull = iota
EvtVarTypeString
EvtVarTypeAnsiString
EvtVarTypeSByte
EvtVarTypeByte
EvtVarTypeInt16
EvtVarTypeUInt16
EvtVarTypeInt32
EvtVarTypeUInt32
EvtVarTypeInt64
EvtVarTypeUInt64
EvtVarTypeSingle
EvtVarTypeDouble
EvtVarTypeBoolean
EvtVarTypeBinary
EvtVarTypeGuid
EvtVarTypeSizeT
EvtVarTypeFileTime
EvtVarTypeSysTime
EvtVarTypeSid
EvtVarTypeHexInt32
EvtVarTypeHexInt64
EvtVarTypeEvtHandle
EvtVarTypeEvtXml
)
/* Fields that can be rendered with GetRendered*Value */
type EVT_SYSTEM_PROPERTY_ID int
const (
EvtSystemProviderName = iota
EvtSystemProviderGuid
EvtSystemEventID
EvtSystemQualifiers
EvtSystemLevel
EvtSystemTask
EvtSystemOpcode
EvtSystemKeywords
EvtSystemTimeCreated
EvtSystemEventRecordId
EvtSystemActivityID
EvtSystemRelatedActivityID
EvtSystemProcessID
EvtSystemThreadID
EvtSystemChannel
EvtSystemComputer
EvtSystemUserID
EvtSystemVersion
)
/* Formatting modes for GetFormattedMessage */
type EVT_FORMAT_MESSAGE_FLAGS int
const (
_ = iota
EvtFormatMessageEvent
EvtFormatMessageLevel
EvtFormatMessageTask
EvtFormatMessageOpcode
EvtFormatMessageKeyword
EvtFormatMessageChannel
EvtFormatMessageProvider
EvtFormatMessageId
EvtFormatMessageXml
)
// Get a handle to a render context which will render properties from the System element.
// Wraps EvtCreateRenderContext() with Flags = EvtRenderContextSystem. The resulting
// handle must be closed with CloseEventHandle.
func GetSystemRenderContext() (SysRenderContext, error) {
context := SysRenderContext(C.CreateSystemRenderContext())
if context == 0 {
return 0, GetLastError()
}
return context, nil
}
// Get a handle for a event log subscription on the given channel.
// `query` is an XPath expression to filter the events on the channel - "*" allows all events.
// The resulting handle must be closed with CloseEventHandle.
func CreateListener(channel, query string, startpos EVT_SUBSCRIBE_FLAGS, watcher *LogEventCallbackWrapper) (ListenerHandle, error) {
cChan := C.CString(channel)
cQuery := C.CString(query)
listenerHandle := C.CreateListener(cChan, cQuery, C.int(startpos), C.PVOID(watcher))
C.free(unsafe.Pointer(cChan))
C.free(unsafe.Pointer(cQuery))
if listenerHandle == 0 {
return 0, GetLastError()
}
return ListenerHandle(listenerHandle), nil
}
// Get a handle for an event log subscription on the given channel. Will begin at the
// bookmarked event, or the closest possible event if the log has been truncated.
// `query` is an XPath expression to filter the events on the channel - "*" allows all events.
// The resulting handle must be closed with CloseEventHandle.
func CreateListenerFromBookmark(channel, query string, watcher *LogEventCallbackWrapper, bookmarkHandle BookmarkHandle) (ListenerHandle, error) {
cChan := C.CString(channel)
cQuery := C.CString(query)
listenerHandle := C.CreateListenerFromBookmark(cChan, cQuery, C.PVOID(watcher), C.ULONGLONG(bookmarkHandle))
C.free(unsafe.Pointer(cChan))
C.free(unsafe.Pointer(cQuery))
if listenerHandle == 0 {
return 0, GetLastError()
}
return ListenerHandle(listenerHandle), nil
}
// Get the Go string for the field at the given index. Returns
// false if the type of the field isn't EvtVarTypeString.
func RenderStringField(fields RenderedFields, fieldIndex EVT_SYSTEM_PROPERTY_ID) (string, bool) {
fieldType := C.GetRenderedValueType(C.PVOID(fields), C.int(fieldIndex))
if fieldType != EvtVarTypeString {
return "", false
}
cString := C.GetRenderedStringValue(C.PVOID(fields), C.int(fieldIndex))
if cString == nil {
return "", false
}
value := C.GoString(cString)
C.free(unsafe.Pointer(cString))
return value, true
}
// Get the timestamp of the field at the given index. Returns false if the
// type of the field isn't EvtVarTypeFileTime.
func RenderFileTimeField(fields RenderedFields, fieldIndex EVT_SYSTEM_PROPERTY_ID) (time.Time, bool) {
fieldType := C.GetRenderedValueType(C.PVOID(fields), C.int(fieldIndex))
if fieldType != EvtVarTypeFileTime {
return time.Time{}, false
}
field := C.GetRenderedFileTimeValue(C.PVOID(fields), C.int(fieldIndex))
return time.Unix(int64(field), 0), true
}
// Get the unsigned integer at the given index. Returns false if the field
// type isn't EvtVarTypeByte, EvtVarTypeUInt16, EvtVarTypeUInt32, or EvtVarTypeUInt64.
func RenderUIntField(fields RenderedFields, fieldIndex EVT_SYSTEM_PROPERTY_ID) (uint64, bool) {
var field C.ULONGLONG
fieldType := C.GetRenderedValueType(C.PVOID(fields), C.int(fieldIndex))
switch fieldType {
case EvtVarTypeByte:
field = C.GetRenderedByteValue(C.PVOID(fields), C.int(fieldIndex))
case EvtVarTypeUInt16:
field = C.GetRenderedUInt16Value(C.PVOID(fields), C.int(fieldIndex))
case EvtVarTypeUInt32:
field = C.GetRenderedUInt32Value(C.PVOID(fields), C.int(fieldIndex))
case EvtVarTypeUInt64:
field = C.GetRenderedUInt64Value(C.PVOID(fields), C.int(fieldIndex))
default:
return 0, false
}
return uint64(field), true
}
// Get the signed integer at the given index. Returns false if the type of
// the field isn't EvtVarTypeSByte, EvtVarTypeInt16, EvtVarTypeInt32, EvtVarTypeInt64.
func RenderIntField(fields RenderedFields, fieldIndex EVT_SYSTEM_PROPERTY_ID) (int64, bool) {
var field C.LONGLONG
fieldType := C.GetRenderedValueType(C.PVOID(fields), C.int(fieldIndex))
switch fieldType {
case EvtVarTypeSByte:
field = C.GetRenderedSByteValue(C.PVOID(fields), C.int(fieldIndex))
case EvtVarTypeInt16:
field = C.GetRenderedInt16Value(C.PVOID(fields), C.int(fieldIndex))
case EvtVarTypeInt32:
field = C.GetRenderedInt32Value(C.PVOID(fields), C.int(fieldIndex))
case EvtVarTypeInt64:
field = C.GetRenderedInt64Value(C.PVOID(fields), C.int(fieldIndex))
default:
return 0, false
}
return int64(field), true
}
// Get the formatted string that represents this message. This method wraps EvtFormatMessage.
func FormatMessage(eventPublisherHandle PublisherHandle, eventHandle EventHandle, format EVT_FORMAT_MESSAGE_FLAGS) (string, error) {
cString := C.GetFormattedMessage(C.ULONGLONG(eventPublisherHandle), C.ULONGLONG(eventHandle), C.int(format))
if cString == nil {
return "", GetLastError()
}
value := C.GoString(cString)
C.free(unsafe.Pointer(cString))
return value, nil
}
// Get the formatted string for the last error which occurred. Wraps GetLastError and FormatMessage.
func GetLastError() error {
errStr := C.GetLastErrorString()
err := errors.New(C.GoString(errStr))
C.LocalFree(C.HLOCAL(errStr))
return err
}
// Render the system properties from the event and returns an array of properties.
// Properties can be accessed using RenderStringField, RenderIntField, RenderFileTimeField,
// or RenderUIntField depending on type. This buffer must be freed after use.
func RenderEventValues(renderContext SysRenderContext, eventHandle EventHandle) (RenderedFields, error) {
values := RenderedFields(C.RenderEventValues(C.ULONGLONG(renderContext), C.ULONGLONG(eventHandle)))
if values == nil {
return nil, GetLastError()
}
return values, nil
}
// Render the event as XML.
func RenderEventXML(eventHandle EventHandle) (string, error) {
xml := C.RenderEventXML(C.ULONGLONG(eventHandle))
if xml == nil {
return "", GetLastError()
}
xmlString := C.GoString(xml)
C.free(unsafe.Pointer(xml))
return xmlString, nil
}
// Get a handle that represents the publisher of the event, given the rendered event values.
func GetEventPublisherHandle(renderedFields RenderedFields) (PublisherHandle, error) {
handle := PublisherHandle(C.GetEventPublisherHandle(C.PVOID(renderedFields)))
if handle == 0 {
return 0, GetLastError()
}
return handle, nil
}
// Close an event handle.
func CloseEventHandle(handle uint64) error {
if C.CloseEvtHandle(C.ULONGLONG(handle)) != 1 {
return GetLastError()
}
return nil
}
// Cancel pending actions on the event handle.
func CancelEventHandle(handle uint64) error {
if C.CancelEvtHandle(C.ULONGLONG(handle)) != 1 {
return GetLastError()
}
return nil
}
func Free(ptr unsafe.Pointer) {
C.free(ptr)
}
/* Get the first event in the log, for testing */
func getTestEventHandle() (EventHandle, error) {
handle := C.GetTestEventHandle()
if handle == 0 {
return 0, GetLastError()
}
return EventHandle(handle), nil
}
/* These are entry points for the callback to hand the pointer to Go-land.
Note: handles are only valid within the callback. Don't pass them out. */
//export eventCallbackError
func eventCallbackError(errCode C.ULONGLONG, logWatcher unsafe.Pointer) {
watcher := (*LogEventCallbackWrapper)(logWatcher).callback
// The provided errCode can be looked up in the Microsoft System Error Code table:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
watcher.PublishError(fmt.Errorf("Event log callback got error code: %v", errCode))
}
//export eventCallback
func eventCallback(handle C.ULONGLONG, logWatcher unsafe.Pointer) {
wrapper := (*LogEventCallbackWrapper)(logWatcher)
watcher := wrapper.callback
watcher.PublishEvent(EventHandle(handle), wrapper.subscribedChannel)
}