| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/cretz/bine/torutil" |
| 11 | ) |
| 12 | |
| 13 | // EventCode represents an asynchronous event code (ref control spec 4.1). |
| 14 | type EventCode string |
| 15 | |
| 16 | // Event codes |
| 17 | const ( |
| 18 | EventCodeAddrMap EventCode = "ADDRMAP" |
| 19 | EventCodeBandwidth EventCode = "BW" |
| 20 | EventCodeBuildTimeoutSet EventCode = "BUILDTIMEOUT_SET" |
| 21 | EventCodeCellStats EventCode = "CELL_STATS" |
| 22 | EventCodeCircuit EventCode = "CIRC" |
| 23 | EventCodeCircuitBandwidth EventCode = "CIRC_BW" |
| 24 | EventCodeCircuitMinor EventCode = "CIRC_MINOR" |
| 25 | EventCodeClientsSeen EventCode = "CLIENTS_SEEN" |
| 26 | EventCodeConfChanged EventCode = "CONF_CHANGED" |
| 27 | EventCodeConnBandwidth EventCode = "CONN_BW" |
| 28 | EventCodeDescChanged EventCode = "DESCCHANGED" |
| 29 | EventCodeGuard EventCode = "GUARD" |
| 30 | EventCodeHSDesc EventCode = "HS_DESC" |
| 31 | EventCodeHSDescContent EventCode = "HS_DESC_CONTENT" |
| 32 | EventCodeLogDebug EventCode = "DEBUG" |
| 33 | EventCodeLogErr EventCode = "ERR" |
| 34 | EventCodeLogInfo EventCode = "INFO" |
| 35 | EventCodeLogNotice EventCode = "NOTICE" |
| 36 | EventCodeLogWarn EventCode = "WARN" |
| 37 | EventCodeNetworkLiveness EventCode = "NETWORK_LIVENESS" |
| 38 | EventCodeNetworkStatus EventCode = "NS" |
| 39 | EventCodeNewConsensus EventCode = "NEWCONSENSUS" |
| 40 | EventCodeNewDesc EventCode = "NEWDESC" |
| 41 | EventCodeORConn EventCode = "ORCONN" |
| 42 | EventCodeSignal EventCode = "SIGNAL" |
| 43 | EventCodeStatusClient EventCode = "STATUS_CLIENT" |
| 44 | EventCodeStatusGeneral EventCode = "STATUS_GENERAL" |
| 45 | EventCodeStatusServer EventCode = "STATUS_SERVER" |
| 46 | EventCodeStream EventCode = "STREAM" |
| 47 | EventCodeStreamBandwidth EventCode = "STREAM_BW" |
| 48 | EventCodeTokenBucketEmpty EventCode = "TB_EMPTY" |
| 49 | EventCodeTransportLaunched EventCode = "TRANSPORT_LAUNCHED" |
| 50 | ) |
| 51 | |
| 52 | // EventCodeUnrecognized is a special event code that is only used with |
| 53 | // AddEventListener and RemoveEventListener to listen for events that are |
| 54 | // unrecognized by this library (i.e. UnrecognizedEvent). |
| 55 | var EventCodeUnrecognized EventCode = "<unrecognized>" |
| 56 | |
| 57 | var recognizedEventCodes = []EventCode{ |
| 58 | EventCodeAddrMap, |
| 59 | EventCodeBandwidth, |
| 60 | EventCodeBuildTimeoutSet, |
| 61 | EventCodeCellStats, |
| 62 | EventCodeCircuit, |
| 63 | EventCodeCircuitBandwidth, |
| 64 | EventCodeCircuitMinor, |
| 65 | EventCodeClientsSeen, |
| 66 | EventCodeConfChanged, |
| 67 | EventCodeConnBandwidth, |
| 68 | EventCodeDescChanged, |
| 69 | EventCodeGuard, |
| 70 | EventCodeHSDesc, |
| 71 | EventCodeHSDescContent, |
| 72 | EventCodeLogDebug, |
| 73 | EventCodeLogErr, |
| 74 | EventCodeLogInfo, |
| 75 | EventCodeLogNotice, |
| 76 | EventCodeLogWarn, |
| 77 | EventCodeNetworkLiveness, |
| 78 | EventCodeNetworkStatus, |
| 79 | EventCodeNewConsensus, |
| 80 | EventCodeNewDesc, |
| 81 | EventCodeORConn, |
| 82 | EventCodeSignal, |
| 83 | EventCodeStatusClient, |
| 84 | EventCodeStatusGeneral, |
| 85 | EventCodeStatusServer, |
| 86 | EventCodeStream, |
| 87 | EventCodeStreamBandwidth, |
| 88 | EventCodeTokenBucketEmpty, |
| 89 | EventCodeTransportLaunched, |
| 90 | } |
| 91 | |
| 92 | var recognizedEventCodesByCode = mapEventCodes() |
| 93 | |
| 94 | func mapEventCodes() map[EventCode]struct{} { |
| 95 | ret := make(map[EventCode]struct{}, len(recognizedEventCodes)) |
| 96 | for _, eventCode := range recognizedEventCodes { |
| 97 | ret[eventCode] = struct{}{} |
| 98 | } |
| 99 | return ret |
| 100 | } |
| 101 | |
| 102 | // EventCodes returns a new slice of all event codes that are recognized (i.e. |
| 103 | // does not include EventCodeUnrecognized). |
| 104 | func EventCodes() []EventCode { |
| 105 | ret := make([]EventCode, len(recognizedEventCodes)) |
| 106 | copy(ret, recognizedEventCodes) |
| 107 | return ret |
| 108 | } |
| 109 | |
| 110 | // ErrEventWaitSynchronousResponseOccurred is returned from EventWait (see docs) |
| 111 | var ErrEventWaitSynchronousResponseOccurred = errors.New("Synchronous event occurred during EventWait") |
| 112 | |
| 113 | // EventWait waits for the predicate to be satisified or a non-event message to |
| 114 | // come through. If a non-event comes through, the error |
| 115 | // ErrEventWaitSynchronousResponseOccurred is returned. If there is an error in |
| 116 | // the predicate or if the context completes or there is an error internally |
| 117 | // handling the event, the error is returned. Otherwise, the event that true was |
| 118 | // returned from the predicate for is returned. |
| 119 | func (c *Conn) EventWait( |
| 120 | ctx context.Context, events []EventCode, predicate func(Event) (bool, error), |
| 121 | ) (Event, error) { |
| 122 | eventCh := make(chan Event, 10) |
| 123 | defer close(eventCh) |
| 124 | if err := c.AddEventListener(eventCh, events...); err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | defer c.RemoveEventListener(eventCh, events...) |
| 128 | eventCtx, eventCancel := context.WithCancel(ctx) |
| 129 | defer eventCancel() |
| 130 | errCh := make(chan error, 1) |
| 131 | go func() { errCh <- c.HandleEvents(eventCtx) }() |
| 132 | for { |
| 133 | select { |
| 134 | case <-eventCtx.Done(): |
| 135 | return nil, eventCtx.Err() |
| 136 | case err := <-errCh: |
| 137 | return nil, err |
| 138 | case event := <-eventCh: |
| 139 | if ok, err := predicate(event); err != nil { |
| 140 | return nil, err |
| 141 | } else if ok { |
| 142 | return event, nil |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // HandleEvents loops until the context is closed dispatching async events. Can |
| 149 | // dispatch events even after context is done and of course during synchronous |
| 150 | // request. This will always end with an error, either from ctx.Done() or from |
| 151 | // an error reading/handling the event. |
| 152 | func (c *Conn) HandleEvents(ctx context.Context) error { |
| 153 | errCh := make(chan error, 1) |
| 154 | go func() { |
| 155 | for ctx.Err() == nil { |
| 156 | if err := c.HandleNextEvent(); err != nil { |
| 157 | errCh <- err |
| 158 | break |
| 159 | } |
| 160 | } |
| 161 | }() |
| 162 | select { |
| 163 | case err := <-errCh: |
| 164 | return err |
| 165 | case <-ctx.Done(): |
| 166 | return ctx.Err() |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // HandleNextEvent attempts to read and handle the next event. It will return on |
| 171 | // first message seen, event or not. Otherwise it will wait until there is a |
| 172 | // message read. |
| 173 | func (c *Conn) HandleNextEvent() error { |
| 174 | c.readLock.Lock() |
| 175 | defer c.readLock.Unlock() |
| 176 | // We'll just peek for the next 3 bytes and see if they are async |
| 177 | byts, err := c.conn.R.Peek(3) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | statusCode, err := strconv.Atoi(string(byts)) |
| 182 | if err != nil || statusCode != StatusAsyncEvent { |
| 183 | return err |
| 184 | } |
| 185 | // Read the entire thing and handle it |
| 186 | resp, err := c.ReadResponse() |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | c.relayAsyncEvents(resp) |
| 191 | return nil |
| 192 | } |
| 193 | |
| 194 | // AddEventListener adds the given channel as an event listener for the given |
| 195 | // events. Then Tor is notified about which events should be listened to. |
| 196 | // Callers are expected to call RemoveEventListener for the channel and all |
| 197 | // event codes used here before closing the channel. If no events are provided, |
| 198 | // this is essentially a no-op. The EventCodeUnrecognized event code can be used |
| 199 | // to listen for unrecognized events. |
| 200 | func (c *Conn) AddEventListener(ch chan<- Event, events ...EventCode) error { |
| 201 | c.addEventListenerToMap(ch, events...) |
| 202 | // If there is an error updating the events, remove what we just added |
| 203 | err := c.sendSetEvents() |
| 204 | if err != nil { |
| 205 | c.removeEventListenerFromMap(ch, events...) |
| 206 | } |
| 207 | return err |
| 208 | } |
| 209 | |
| 210 | // RemoveEventListener removes the given channel from being sent to by the given |
| 211 | // event codes. It is not an error to remove a channel from events |
| 212 | // AddEventListener was not called for. Tor is notified about events which may |
| 213 | // no longer be listened to. If no events are provided, this is essentially a |
| 214 | // no-op. |
| 215 | func (c *Conn) RemoveEventListener(ch chan<- Event, events ...EventCode) error { |
| 216 | c.removeEventListenerFromMap(ch, events...) |
| 217 | return c.sendSetEvents() |
| 218 | } |
| 219 | |
| 220 | func (c *Conn) addEventListenerToMap(ch chan<- Event, events ...EventCode) { |
| 221 | c.eventListenersLock.Lock() |
| 222 | defer c.eventListenersLock.Unlock() |
| 223 | for _, event := range events { |
| 224 | // Must completely replace the array, never mutate it |
| 225 | prevArr := c.eventListeners[event] |
| 226 | newArr := make([]chan<- Event, len(prevArr)+1) |
| 227 | copy(newArr, prevArr) |
| 228 | newArr[len(newArr)-1] = ch |
| 229 | c.eventListeners[event] = newArr |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | func (c *Conn) removeEventListenerFromMap(ch chan<- Event, events ...EventCode) { |
| 234 | c.eventListenersLock.Lock() |
| 235 | defer c.eventListenersLock.Unlock() |
| 236 | for _, event := range events { |
| 237 | arr := c.eventListeners[event] |
| 238 | index := -1 |
| 239 | for i, listener := range arr { |
| 240 | if listener == ch { |
| 241 | index = i |
| 242 | break |
| 243 | } |
| 244 | } |
| 245 | if index != -1 { |
| 246 | if len(arr) == 1 { |
| 247 | delete(c.eventListeners, event) |
| 248 | } else { |
| 249 | // Must completely replace the array, never mutate it |
| 250 | newArr := make([]chan<- Event, len(arr)-1) |
| 251 | copy(newArr, arr[:index]) |
| 252 | copy(newArr[index:], arr[index+1:]) |
| 253 | c.eventListeners[event] = newArr |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | func (c *Conn) sendSetEvents() error { |
| 260 | c.eventListenersLock.RLock() |
| 261 | cmd := "SETEVENTS" |
| 262 | for event := range c.eventListeners { |
| 263 | cmd += " " + string(event) |
| 264 | } |
| 265 | c.eventListenersLock.RUnlock() |
| 266 | return c.sendRequestIgnoreResponse(cmd) |
| 267 | } |
| 268 | |
| 269 | func (c *Conn) relayAsyncEvents(resp *Response) { |
| 270 | var code, data string |
| 271 | var dataArray []string |
| 272 | if len(resp.Data) == 1 { |
| 273 | // On single line, part up to space, newline, or EOL is the code, rest is data |
| 274 | if index := strings.Index(resp.Data[0], " "); index != -1 { |
| 275 | code, data = resp.Data[0][:index], resp.Data[0][index+1:] |
| 276 | } else if index := strings.Index(resp.Data[0], "\r\n"); index != -1 { |
| 277 | code, data = resp.Data[0][:index], resp.Data[0][index+2:] |
| 278 | } else { |
| 279 | code, data = resp.Data[0], "" |
| 280 | } |
| 281 | } else if len(resp.Data) > 0 { |
| 282 | // If there are multiple lines, the entire first line is the code |
| 283 | code, dataArray = resp.Data[0], resp.Data[1:] |
| 284 | } else { |
| 285 | // Otherwise, the reply line has the data |
| 286 | code, data, _ = torutil.PartitionString(resp.Reply, ' ') |
| 287 | } |
| 288 | // Only relay if there are chans |
| 289 | eventCode := EventCode(code) |
| 290 | c.eventListenersLock.RLock() |
| 291 | chans := c.eventListeners[eventCode] |
| 292 | if _, ok := recognizedEventCodesByCode[eventCode]; !ok { |
| 293 | chans = append(chans, c.eventListeners[EventCodeUnrecognized]...) |
| 294 | } |
| 295 | c.eventListenersLock.RUnlock() |
| 296 | if len(chans) == 0 { |
| 297 | return |
| 298 | } |
| 299 | // Parse the event and only send if known event |
| 300 | if event := ParseEvent(eventCode, data, dataArray); event != nil { |
| 301 | for _, ch := range chans { |
| 302 | // Just send, if closed or blocking, that's not our problem |
| 303 | ch <- event |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Zero on fail |
| 309 | func parseISOTime(str string) time.Time { |
| 310 | // Essentially time.RFC3339 but without 'T' or TZ info |
| 311 | const layout = "2006-01-02 15:04:05" |
| 312 | ret, err := time.Parse(layout, str) |
| 313 | if err != nil { |
| 314 | ret = time.Time{} |
| 315 | } |
| 316 | return ret |
| 317 | } |
| 318 | |
| 319 | // Zero on fail |
| 320 | func parseISOTime2Frac(str string) time.Time { |
| 321 | // Essentially time.RFC3339Nano but without TZ info |
| 322 | const layout = "2006-01-02T15:04:05.999999999" |
| 323 | ret, err := time.Parse(layout, str) |
| 324 | if err != nil { |
| 325 | ret = time.Time{} |
| 326 | } |
| 327 | return ret |
| 328 | } |
| 329 | |
| 330 | // Event is the base interface for all known asynchronous events. |
| 331 | type Event interface { |
| 332 | Code() EventCode |
| 333 | } |
| 334 | |
| 335 | // ParseEvent returns an Event for the given code and data info. Raw is the raw |
| 336 | // single line if it is a single-line event (even if it has newlines), dataArray |
| 337 | // is the array of lines for multi-line events. Only one of the two needs to be |
| 338 | // set. The response is never nil, but may be UnrecognizedEvent. Format errors |
| 339 | // are ignored per the Tor spec. |
| 340 | func ParseEvent(code EventCode, raw string, dataArray []string) Event { |
| 341 | switch code { |
| 342 | case EventCodeAddrMap: |
| 343 | return ParseAddrMapEvent(raw) |
| 344 | case EventCodeBandwidth: |
| 345 | return ParseBandwidthEvent(raw) |
| 346 | case EventCodeBuildTimeoutSet: |
| 347 | return ParseBuildTimeoutSetEvent(raw) |
| 348 | case EventCodeCellStats: |
| 349 | return ParseCellStatsEvent(raw) |
| 350 | case EventCodeCircuit: |
| 351 | return ParseCircuitEvent(raw) |
| 352 | case EventCodeCircuitBandwidth: |
| 353 | return ParseCircuitBandwidthEvent(raw) |
| 354 | case EventCodeCircuitMinor: |
| 355 | return ParseCircuitMinorEvent(raw) |
| 356 | case EventCodeClientsSeen: |
| 357 | return ParseClientsSeenEvent(raw) |
| 358 | case EventCodeConfChanged: |
| 359 | return ParseConfChangedEvent(dataArray) |
| 360 | case EventCodeConnBandwidth: |
| 361 | return ParseConnBandwidthEvent(raw) |
| 362 | case EventCodeDescChanged: |
| 363 | return ParseDescChangedEvent(raw) |
| 364 | case EventCodeGuard: |
| 365 | return ParseGuardEvent(raw) |
| 366 | case EventCodeHSDesc: |
| 367 | return ParseHSDescEvent(raw) |
| 368 | case EventCodeHSDescContent: |
| 369 | return ParseHSDescContentEvent(raw) |
| 370 | case EventCodeLogDebug, EventCodeLogErr, EventCodeLogInfo, EventCodeLogNotice, EventCodeLogWarn: |
| 371 | return ParseLogEvent(code, raw) |
| 372 | case EventCodeNetworkLiveness: |
| 373 | return ParseNetworkLivenessEvent(raw) |
| 374 | case EventCodeNetworkStatus: |
| 375 | return ParseNetworkStatusEvent(raw) |
| 376 | case EventCodeNewConsensus: |
| 377 | return ParseNewConsensusEvent(raw) |
| 378 | case EventCodeNewDesc: |
| 379 | return ParseNewDescEvent(raw) |
| 380 | case EventCodeORConn: |
| 381 | return ParseORConnEvent(raw) |
| 382 | case EventCodeSignal: |
| 383 | return ParseSignalEvent(raw) |
| 384 | case EventCodeStatusClient, EventCodeStatusGeneral, EventCodeStatusServer: |
| 385 | return ParseStatusEvent(code, raw) |
| 386 | case EventCodeStream: |
| 387 | return ParseStreamEvent(raw) |
| 388 | case EventCodeStreamBandwidth: |
| 389 | return ParseStreamBandwidthEvent(raw) |
| 390 | case EventCodeTokenBucketEmpty: |
| 391 | return ParseTokenBucketEmptyEvent(raw) |
| 392 | case EventCodeTransportLaunched: |
| 393 | return ParseTransportLaunchedEvent(raw) |
| 394 | default: |
| 395 | return ParseUnrecognizedEvent(code, raw, dataArray) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // CircuitEvent is CIRC in spec. |
| 400 | type CircuitEvent struct { |
| 401 | Raw string |
| 402 | CircuitID string |
| 403 | Status string |
| 404 | Path []string |
| 405 | BuildFlags []string |
| 406 | Purpose string |
| 407 | HSState string |
| 408 | RendQuery string |
| 409 | TimeCreated time.Time |
| 410 | Reason string |
| 411 | RemoteReason string |
| 412 | SocksUsername string |
| 413 | SocksPassword string |
| 414 | } |
| 415 | |
| 416 | // ParseCircuitEvent parses the event. |
| 417 | func ParseCircuitEvent(raw string) *CircuitEvent { |
| 418 | event := &CircuitEvent{Raw: raw} |
| 419 | event.CircuitID, raw, _ = torutil.PartitionString(raw, ' ') |
| 420 | var ok bool |
| 421 | event.Status, raw, ok = torutil.PartitionString(raw, ' ') |
| 422 | var attr string |
| 423 | first := true |
| 424 | for ok { |
| 425 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 426 | key, val, _ := torutil.PartitionString(attr, '=') |
| 427 | switch key { |
| 428 | case "BUILD_FLAGS": |
| 429 | event.BuildFlags = strings.Split(val, ",") |
| 430 | case "PURPOSE": |
| 431 | event.Purpose = val |
| 432 | case "HS_STATE": |
| 433 | event.HSState = val |
| 434 | case "REND_QUERY": |
| 435 | event.RendQuery = val |
| 436 | case "TIME_CREATED": |
| 437 | event.TimeCreated = parseISOTime2Frac(val) |
| 438 | case "REASON": |
| 439 | event.Reason = val |
| 440 | case "REMOTE_REASON": |
| 441 | event.RemoteReason = val |
| 442 | case "SOCKS_USERNAME": |
| 443 | event.SocksUsername = val |
| 444 | case "SOCKS_PASSWORD": |
| 445 | event.SocksPassword = val |
| 446 | default: |
| 447 | if first { |
| 448 | event.Path = strings.Split(val, ",") |
| 449 | } |
| 450 | } |
| 451 | first = false |
| 452 | } |
| 453 | return event |
| 454 | } |
| 455 | |
| 456 | // Code implements Event.Code |
| 457 | func (*CircuitEvent) Code() EventCode { return EventCodeCircuit } |
| 458 | |
| 459 | // StreamEvent is STREAM in spec. |
| 460 | type StreamEvent struct { |
| 461 | Raw string |
| 462 | StreamID string |
| 463 | Status string |
| 464 | CircuitID string |
| 465 | TargetAddress string |
| 466 | TargetPort int |
| 467 | Reason string |
| 468 | RemoteReason string |
| 469 | Source string |
| 470 | SourceAddress string |
| 471 | SourcePort int |
| 472 | Purpose string |
| 473 | } |
| 474 | |
| 475 | // ParseStreamEvent parses the event. |
| 476 | func ParseStreamEvent(raw string) *StreamEvent { |
| 477 | event := &StreamEvent{Raw: raw} |
| 478 | event.StreamID, raw, _ = torutil.PartitionString(raw, ' ') |
| 479 | event.Status, raw, _ = torutil.PartitionString(raw, ' ') |
| 480 | event.CircuitID, raw, _ = torutil.PartitionString(raw, ' ') |
| 481 | var ok bool |
| 482 | event.TargetAddress, raw, ok = torutil.PartitionString(raw, ' ') |
| 483 | if target, port, hasPort := torutil.PartitionStringFromEnd(event.TargetAddress, ':'); hasPort { |
| 484 | event.TargetAddress = target |
| 485 | event.TargetPort, _ = strconv.Atoi(port) |
| 486 | } |
| 487 | var attr string |
| 488 | for ok { |
| 489 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 490 | key, val, _ := torutil.PartitionString(attr, '=') |
| 491 | switch key { |
| 492 | case "REASON": |
| 493 | event.Reason = val |
| 494 | case "REMOTE_REASON": |
| 495 | event.RemoteReason = val |
| 496 | case "SOURCE": |
| 497 | event.Source = val |
| 498 | case "SOURCE_ADDR": |
| 499 | event.SourceAddress = val |
| 500 | if source, port, hasPort := torutil.PartitionStringFromEnd(event.SourceAddress, ':'); hasPort { |
| 501 | event.SourceAddress = source |
| 502 | event.SourcePort, _ = strconv.Atoi(port) |
| 503 | } |
| 504 | case "PURPOSE": |
| 505 | event.Purpose = val |
| 506 | } |
| 507 | } |
| 508 | return event |
| 509 | } |
| 510 | |
| 511 | // Code implements Event.Code |
| 512 | func (*StreamEvent) Code() EventCode { return EventCodeStream } |
| 513 | |
| 514 | // ORConnEvent is ORCONN in spec. |
| 515 | type ORConnEvent struct { |
| 516 | Raw string |
| 517 | Target string |
| 518 | Status string |
| 519 | Reason string |
| 520 | NumCircuits int |
| 521 | ConnID string |
| 522 | } |
| 523 | |
| 524 | // ParseORConnEvent parses the event. |
| 525 | func ParseORConnEvent(raw string) *ORConnEvent { |
| 526 | event := &ORConnEvent{Raw: raw} |
| 527 | event.Target, raw, _ = torutil.PartitionString(raw, ' ') |
| 528 | var ok bool |
| 529 | event.Status, raw, ok = torutil.PartitionString(raw, ' ') |
| 530 | var attr string |
| 531 | for ok { |
| 532 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 533 | key, val, _ := torutil.PartitionString(attr, '=') |
| 534 | switch key { |
| 535 | case "REASON": |
| 536 | event.Reason = val |
| 537 | case "NCIRCS": |
| 538 | event.NumCircuits, _ = strconv.Atoi(val) |
| 539 | case "ID": |
| 540 | event.ConnID = val |
| 541 | } |
| 542 | } |
| 543 | return event |
| 544 | } |
| 545 | |
| 546 | // Code implements Event.Code |
| 547 | func (*ORConnEvent) Code() EventCode { return EventCodeORConn } |
| 548 | |
| 549 | // BandwidthEvent is BW in spec. |
| 550 | type BandwidthEvent struct { |
| 551 | Raw string |
| 552 | BytesRead int64 |
| 553 | BytesWritten int64 |
| 554 | } |
| 555 | |
| 556 | // ParseBandwidthEvent parses the event. |
| 557 | func ParseBandwidthEvent(raw string) *BandwidthEvent { |
| 558 | event := &BandwidthEvent{Raw: raw} |
| 559 | var temp string |
| 560 | temp, raw, _ = torutil.PartitionString(raw, ' ') |
| 561 | event.BytesRead, _ = strconv.ParseInt(temp, 10, 64) |
| 562 | temp, raw, _ = torutil.PartitionString(raw, ' ') |
| 563 | event.BytesWritten, _ = strconv.ParseInt(temp, 10, 64) |
| 564 | return event |
| 565 | } |
| 566 | |
| 567 | // Code implements Event.Code |
| 568 | func (*BandwidthEvent) Code() EventCode { return EventCodeBandwidth } |
| 569 | |
| 570 | // LogEvent is DEBUG, ERR, INFO, NOTICE, and WARN in spec. |
| 571 | type LogEvent struct { |
| 572 | Severity EventCode |
| 573 | Raw string |
| 574 | } |
| 575 | |
| 576 | // ParseLogEvent parses the event. |
| 577 | func ParseLogEvent(severity EventCode, raw string) *LogEvent { |
| 578 | return &LogEvent{Severity: severity, Raw: raw} |
| 579 | } |
| 580 | |
| 581 | // Code implements Event.Code |
| 582 | func (l *LogEvent) Code() EventCode { return l.Severity } |
| 583 | |
| 584 | // NewDescEvent is NEWDESC in spec. |
| 585 | type NewDescEvent struct { |
| 586 | Raw string |
| 587 | Descs []string |
| 588 | } |
| 589 | |
| 590 | // ParseNewDescEvent parses the event. |
| 591 | func ParseNewDescEvent(raw string) *NewDescEvent { |
| 592 | return &NewDescEvent{Raw: raw, Descs: strings.Split(raw, " ")} |
| 593 | } |
| 594 | |
| 595 | // Code implements Event.Code |
| 596 | func (*NewDescEvent) Code() EventCode { return EventCodeNewDesc } |
| 597 | |
| 598 | // AddrMapEvent is ADDRMAP in spec. |
| 599 | type AddrMapEvent struct { |
| 600 | Raw string |
| 601 | Address string |
| 602 | NewAddress string |
| 603 | ErrorCode string |
| 604 | // Zero if no expire |
| 605 | Expires time.Time |
| 606 | // Sans double quotes |
| 607 | Cached string |
| 608 | } |
| 609 | |
| 610 | // ParseAddrMapEvent parses the event. |
| 611 | func ParseAddrMapEvent(raw string) *AddrMapEvent { |
| 612 | event := &AddrMapEvent{Raw: raw} |
| 613 | event.Address, raw, _ = torutil.PartitionString(raw, ' ') |
| 614 | event.NewAddress, raw, _ = torutil.PartitionString(raw, ' ') |
| 615 | var ok bool |
| 616 | // Skip local expiration, use UTC one later |
| 617 | _, raw, ok = torutil.PartitionString(raw, ' ') |
| 618 | var attr string |
| 619 | for ok { |
| 620 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 621 | key, val, _ := torutil.PartitionString(attr, '=') |
| 622 | switch key { |
| 623 | case "error": |
| 624 | event.ErrorCode = val |
| 625 | case "EXPIRES": |
| 626 | val, _ = torutil.UnescapeSimpleQuotedString(val) |
| 627 | event.Expires = parseISOTime(val) |
| 628 | case "CACHED": |
| 629 | event.Cached, _ = torutil.UnescapeSimpleQuotedStringIfNeeded(val) |
| 630 | } |
| 631 | } |
| 632 | return event |
| 633 | } |
| 634 | |
| 635 | // Code implements Event.Code |
| 636 | func (*AddrMapEvent) Code() EventCode { return EventCodeAddrMap } |
| 637 | |
| 638 | // DescChangedEvent is DESCCHANGED in spec. |
| 639 | type DescChangedEvent struct { |
| 640 | Raw string |
| 641 | } |
| 642 | |
| 643 | // ParseDescChangedEvent parses the event. |
| 644 | func ParseDescChangedEvent(raw string) *DescChangedEvent { |
| 645 | return &DescChangedEvent{Raw: raw} |
| 646 | } |
| 647 | |
| 648 | // Code implements Event.Code |
| 649 | func (*DescChangedEvent) Code() EventCode { return EventCodeDescChanged } |
| 650 | |
| 651 | // StatusEvent is STATUS_CLIENT, STATUS_GENERAL, and STATUS_SERVER in spec. |
| 652 | type StatusEvent struct { |
| 653 | Raw string |
| 654 | Type EventCode |
| 655 | Severity string |
| 656 | Action string |
| 657 | Arguments map[string]string |
| 658 | } |
| 659 | |
| 660 | // ParseStatusEvent parses the event. |
| 661 | func ParseStatusEvent(typ EventCode, raw string) *StatusEvent { |
| 662 | event := &StatusEvent{Raw: raw, Type: typ, Arguments: map[string]string{}} |
| 663 | event.Severity, raw, _ = torutil.PartitionString(raw, ' ') |
| 664 | var ok bool |
| 665 | event.Action, raw, ok = torutil.PartitionString(raw, ' ') |
| 666 | var attr string |
| 667 | for ok { |
| 668 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 669 | key, val, _ := torutil.PartitionString(attr, '=') |
| 670 | event.Arguments[key], _ = torutil.UnescapeSimpleQuotedStringIfNeeded(val) |
| 671 | } |
| 672 | return event |
| 673 | } |
| 674 | |
| 675 | // Code implements Event.Code |
| 676 | func (s *StatusEvent) Code() EventCode { return s.Type } |
| 677 | |
| 678 | // GuardEvent is GUARD in spec. |
| 679 | type GuardEvent struct { |
| 680 | Raw string |
| 681 | Type string |
| 682 | Name string |
| 683 | Status string |
| 684 | } |
| 685 | |
| 686 | // ParseGuardEvent parses the event. |
| 687 | func ParseGuardEvent(raw string) *GuardEvent { |
| 688 | event := &GuardEvent{Raw: raw} |
| 689 | event.Type, raw, _ = torutil.PartitionString(raw, ' ') |
| 690 | event.Name, raw, _ = torutil.PartitionString(raw, ' ') |
| 691 | event.Status, raw, _ = torutil.PartitionString(raw, ' ') |
| 692 | return event |
| 693 | } |
| 694 | |
| 695 | // Code implements Event.Code |
| 696 | func (*GuardEvent) Code() EventCode { return EventCodeGuard } |
| 697 | |
| 698 | // NetworkStatusEvent is NS in spec. |
| 699 | type NetworkStatusEvent struct { |
| 700 | Raw string |
| 701 | } |
| 702 | |
| 703 | // ParseNetworkStatusEvent parses the event. |
| 704 | func ParseNetworkStatusEvent(raw string) *NetworkStatusEvent { |
| 705 | return &NetworkStatusEvent{Raw: raw} |
| 706 | } |
| 707 | |
| 708 | // Code implements Event.Code |
| 709 | func (*NetworkStatusEvent) Code() EventCode { return EventCodeNetworkStatus } |
| 710 | |
| 711 | // StreamBandwidthEvent is STREAM_BW in spec. |
| 712 | type StreamBandwidthEvent struct { |
| 713 | Raw string |
| 714 | BytesRead int64 |
| 715 | BytesWritten int64 |
| 716 | Time time.Time |
| 717 | } |
| 718 | |
| 719 | // ParseStreamBandwidthEvent parses the event. |
| 720 | func ParseStreamBandwidthEvent(raw string) *StreamBandwidthEvent { |
| 721 | event := &StreamBandwidthEvent{Raw: raw} |
| 722 | var temp string |
| 723 | temp, raw, _ = torutil.PartitionString(raw, ' ') |
| 724 | event.BytesRead, _ = strconv.ParseInt(temp, 10, 64) |
| 725 | temp, raw, _ = torutil.PartitionString(raw, ' ') |
| 726 | event.BytesWritten, _ = strconv.ParseInt(temp, 10, 64) |
| 727 | temp, raw, _ = torutil.PartitionString(raw, ' ') |
| 728 | temp, _ = torutil.UnescapeSimpleQuotedString(temp) |
| 729 | event.Time = parseISOTime2Frac(temp) |
| 730 | return event |
| 731 | } |
| 732 | |
| 733 | // Code implements Event.Code |
| 734 | func (*StreamBandwidthEvent) Code() EventCode { return EventCodeStreamBandwidth } |
| 735 | |
| 736 | // ClientsSeenEvent is CLIENTS_SEEN in spec. |
| 737 | type ClientsSeenEvent struct { |
| 738 | Raw string |
| 739 | TimeStarted time.Time |
| 740 | CountrySummary map[string]int |
| 741 | IPVersions map[string]int |
| 742 | } |
| 743 | |
| 744 | // ParseClientsSeenEvent parses the event. |
| 745 | func ParseClientsSeenEvent(raw string) *ClientsSeenEvent { |
| 746 | event := &ClientsSeenEvent{Raw: raw} |
| 747 | var temp string |
| 748 | var ok bool |
| 749 | temp, raw, ok = torutil.PartitionString(raw, ' ') |
| 750 | temp, _ = torutil.UnescapeSimpleQuotedString(temp) |
| 751 | event.TimeStarted = parseISOTime(temp) |
| 752 | strToMap := func(str string) map[string]int { |
| 753 | ret := map[string]int{} |
| 754 | for _, keyVal := range strings.Split(str, ",") { |
| 755 | key, val, _ := torutil.PartitionString(keyVal, '=') |
| 756 | ret[key], _ = strconv.Atoi(val) |
| 757 | } |
| 758 | return ret |
| 759 | } |
| 760 | var attr string |
| 761 | for ok { |
| 762 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 763 | key, val, _ := torutil.PartitionString(attr, '=') |
| 764 | switch key { |
| 765 | case "CountrySummary": |
| 766 | event.CountrySummary = strToMap(val) |
| 767 | case "IPVersions": |
| 768 | event.IPVersions = strToMap(val) |
| 769 | } |
| 770 | } |
| 771 | return event |
| 772 | } |
| 773 | |
| 774 | // Code implements Event.Code |
| 775 | func (*ClientsSeenEvent) Code() EventCode { return EventCodeClientsSeen } |
| 776 | |
| 777 | // NewConsensusEvent is NEWCONSENSUS in spec. |
| 778 | type NewConsensusEvent struct { |
| 779 | Raw string |
| 780 | } |
| 781 | |
| 782 | // ParseNewConsensusEvent parses the event. |
| 783 | func ParseNewConsensusEvent(raw string) *NewConsensusEvent { |
| 784 | return &NewConsensusEvent{Raw: raw} |
| 785 | } |
| 786 | |
| 787 | // Code implements Event.Code |
| 788 | func (*NewConsensusEvent) Code() EventCode { return EventCodeNewConsensus } |
| 789 | |
| 790 | // BuildTimeoutSetEvent is BUILDTIMEOUT_SET in spec. |
| 791 | type BuildTimeoutSetEvent struct { |
| 792 | Raw string |
| 793 | Type string |
| 794 | TotalTimes int |
| 795 | Timeout time.Duration |
| 796 | Xm int |
| 797 | Alpha float32 |
| 798 | Quantile float32 |
| 799 | TimeoutRate float32 |
| 800 | CloseTimeout time.Duration |
| 801 | CloseRate float32 |
| 802 | } |
| 803 | |
| 804 | // ParseBuildTimeoutSetEvent parses the event. |
| 805 | func ParseBuildTimeoutSetEvent(raw string) *BuildTimeoutSetEvent { |
| 806 | event := &BuildTimeoutSetEvent{Raw: raw} |
| 807 | var ok bool |
| 808 | event.Type, raw, ok = torutil.PartitionString(raw, ' ') |
| 809 | _, raw, ok = torutil.PartitionString(raw, ' ') |
| 810 | var attr string |
| 811 | parseFloat := func(val string) float32 { |
| 812 | f, _ := strconv.ParseFloat(val, 32) |
| 813 | return float32(f) |
| 814 | } |
| 815 | for ok { |
| 816 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 817 | key, val, _ := torutil.PartitionString(attr, '=') |
| 818 | switch key { |
| 819 | case "TOTAL_TIMES": |
| 820 | event.TotalTimes, _ = strconv.Atoi(val) |
| 821 | case "TIMEOUT_MS": |
| 822 | if ms, err := strconv.ParseInt(val, 10, 64); err == nil { |
| 823 | event.Timeout = time.Duration(ms) * time.Millisecond |
| 824 | } |
| 825 | case "XM": |
| 826 | event.Xm, _ = strconv.Atoi(val) |
| 827 | case "ALPHA": |
| 828 | event.Alpha = parseFloat(val) |
| 829 | case "CUTOFF_QUANTILE": |
| 830 | event.Quantile = parseFloat(val) |
| 831 | case "TIMEOUT_RATE": |
| 832 | event.TimeoutRate = parseFloat(val) |
| 833 | case "CLOSE_MS": |
| 834 | if ms, err := strconv.ParseInt(val, 10, 64); err == nil { |
| 835 | event.CloseTimeout = time.Duration(ms) * time.Millisecond |
| 836 | } |
| 837 | case "CLOSE_RATE": |
| 838 | event.CloseRate = parseFloat(val) |
| 839 | } |
| 840 | } |
| 841 | return event |
| 842 | } |
| 843 | |
| 844 | // Code implements Event.Code |
| 845 | func (*BuildTimeoutSetEvent) Code() EventCode { return EventCodeBuildTimeoutSet } |
| 846 | |
| 847 | // SignalEvent is SIGNAL in spec. |
| 848 | type SignalEvent struct { |
| 849 | Raw string |
| 850 | } |
| 851 | |
| 852 | // ParseSignalEvent parses the event. |
| 853 | func ParseSignalEvent(raw string) *SignalEvent { |
| 854 | return &SignalEvent{Raw: raw} |
| 855 | } |
| 856 | |
| 857 | // Code implements Event.Code |
| 858 | func (*SignalEvent) Code() EventCode { return EventCodeSignal } |
| 859 | |
| 860 | // ConfChangedEvent is CONF_CHANGED in spec. |
| 861 | type ConfChangedEvent struct { |
| 862 | Raw []string |
| 863 | } |
| 864 | |
| 865 | // ParseConfChangedEvent parses the event. |
| 866 | func ParseConfChangedEvent(raw []string) *ConfChangedEvent { |
| 867 | // TODO: break into KeyVal and unescape strings |
| 868 | return &ConfChangedEvent{Raw: raw} |
| 869 | } |
| 870 | |
| 871 | // Code implements Event.Code |
| 872 | func (*ConfChangedEvent) Code() EventCode { return EventCodeConfChanged } |
| 873 | |
| 874 | // CircuitMinorEvent is CIRC_MINOR in spec. |
| 875 | type CircuitMinorEvent struct { |
| 876 | Raw string |
| 877 | CircuitID string |
| 878 | Event string |
| 879 | Path []string |
| 880 | BuildFlags []string |
| 881 | Purpose string |
| 882 | HSState string |
| 883 | RendQuery string |
| 884 | TimeCreated time.Time |
| 885 | OldPurpose string |
| 886 | OldHSState string |
| 887 | } |
| 888 | |
| 889 | // ParseCircuitMinorEvent parses the event. |
| 890 | func ParseCircuitMinorEvent(raw string) *CircuitMinorEvent { |
| 891 | event := &CircuitMinorEvent{Raw: raw} |
| 892 | event.CircuitID, raw, _ = torutil.PartitionString(raw, ' ') |
| 893 | var ok bool |
| 894 | event.Event, raw, ok = torutil.PartitionString(raw, ' ') |
| 895 | var attr string |
| 896 | first := true |
| 897 | for ok { |
| 898 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 899 | key, val, _ := torutil.PartitionString(attr, '=') |
| 900 | switch key { |
| 901 | case "BUILD_FLAGS": |
| 902 | event.BuildFlags = strings.Split(val, ",") |
| 903 | case "PURPOSE": |
| 904 | event.Purpose = val |
| 905 | case "HS_STATE": |
| 906 | event.HSState = val |
| 907 | case "REND_QUERY": |
| 908 | event.RendQuery = val |
| 909 | case "TIME_CREATED": |
| 910 | event.TimeCreated = parseISOTime2Frac(val) |
| 911 | case "OLD_PURPOSE": |
| 912 | event.OldPurpose = val |
| 913 | case "OLD_HS_STATE": |
| 914 | event.OldHSState = val |
| 915 | default: |
| 916 | if first { |
| 917 | event.Path = strings.Split(val, ",") |
| 918 | } |
| 919 | } |
| 920 | first = false |
| 921 | } |
| 922 | return event |
| 923 | } |
| 924 | |
| 925 | // Code implements Event.Code |
| 926 | func (*CircuitMinorEvent) Code() EventCode { return EventCodeCircuitMinor } |
| 927 | |
| 928 | // TransportLaunchedEvent is TRANSPORT_LAUNCHED in spec. |
| 929 | type TransportLaunchedEvent struct { |
| 930 | Raw string |
| 931 | Type string |
| 932 | Name string |
| 933 | Address string |
| 934 | Port int |
| 935 | } |
| 936 | |
| 937 | // ParseTransportLaunchedEvent parses the event. |
| 938 | func ParseTransportLaunchedEvent(raw string) *TransportLaunchedEvent { |
| 939 | event := &TransportLaunchedEvent{Raw: raw} |
| 940 | event.Type, raw, _ = torutil.PartitionString(raw, ' ') |
| 941 | event.Name, raw, _ = torutil.PartitionString(raw, ' ') |
| 942 | event.Address, raw, _ = torutil.PartitionString(raw, ' ') |
| 943 | var temp string |
| 944 | temp, raw, _ = torutil.PartitionString(raw, ' ') |
| 945 | event.Port, _ = strconv.Atoi(temp) |
| 946 | return event |
| 947 | } |
| 948 | |
| 949 | // Code implements Event.Code |
| 950 | func (*TransportLaunchedEvent) Code() EventCode { return EventCodeTransportLaunched } |
| 951 | |
| 952 | // ConnBandwidthEvent is CONN_BW in spec. |
| 953 | type ConnBandwidthEvent struct { |
| 954 | Raw string |
| 955 | ConnID string |
| 956 | ConnType string |
| 957 | BytesRead int64 |
| 958 | BytesWritten int64 |
| 959 | } |
| 960 | |
| 961 | // ParseConnBandwidthEvent parses the event. |
| 962 | func ParseConnBandwidthEvent(raw string) *ConnBandwidthEvent { |
| 963 | event := &ConnBandwidthEvent{Raw: raw} |
| 964 | ok := true |
| 965 | var attr string |
| 966 | for ok { |
| 967 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 968 | key, val, _ := torutil.PartitionString(attr, '=') |
| 969 | switch key { |
| 970 | case "ID": |
| 971 | event.ConnID = val |
| 972 | case "TYPE": |
| 973 | event.ConnType = val |
| 974 | case "READ": |
| 975 | event.BytesRead, _ = strconv.ParseInt(val, 10, 64) |
| 976 | case "WRITTEN": |
| 977 | event.BytesWritten, _ = strconv.ParseInt(val, 10, 64) |
| 978 | } |
| 979 | } |
| 980 | return event |
| 981 | } |
| 982 | |
| 983 | // Code implements Event.Code |
| 984 | func (*ConnBandwidthEvent) Code() EventCode { return EventCodeConnBandwidth } |
| 985 | |
| 986 | // CircuitBandwidthEvent is CIRC_BW in spec. |
| 987 | type CircuitBandwidthEvent struct { |
| 988 | Raw string |
| 989 | CircuitID string |
| 990 | BytesRead int64 |
| 991 | BytesWritten int64 |
| 992 | Time time.Time |
| 993 | } |
| 994 | |
| 995 | // ParseCircuitBandwidthEvent parses the event. |
| 996 | func ParseCircuitBandwidthEvent(raw string) *CircuitBandwidthEvent { |
| 997 | event := &CircuitBandwidthEvent{Raw: raw} |
| 998 | ok := true |
| 999 | var attr string |
| 1000 | for ok { |
| 1001 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 1002 | key, val, _ := torutil.PartitionString(attr, '=') |
| 1003 | switch key { |
| 1004 | case "ID": |
| 1005 | event.CircuitID = val |
| 1006 | case "READ": |
| 1007 | event.BytesRead, _ = strconv.ParseInt(val, 10, 64) |
| 1008 | case "WRITTEN": |
| 1009 | event.BytesWritten, _ = strconv.ParseInt(val, 10, 64) |
| 1010 | case "TIME": |
| 1011 | event.Time = parseISOTime2Frac(val) |
| 1012 | } |
| 1013 | } |
| 1014 | return event |
| 1015 | } |
| 1016 | |
| 1017 | // Code implements Event.Code |
| 1018 | func (*CircuitBandwidthEvent) Code() EventCode { return EventCodeCircuitBandwidth } |
| 1019 | |
| 1020 | // CellStatsEvent is CELL_STATS in spec. |
| 1021 | type CellStatsEvent struct { |
| 1022 | Raw string |
| 1023 | CircuitID string |
| 1024 | InboundQueueID string |
| 1025 | InboundConnID string |
| 1026 | InboundAdded map[string]int |
| 1027 | InboundRemoved map[string]int |
| 1028 | InboundTime map[string]int |
| 1029 | OutboundQueueID string |
| 1030 | OutboundConnID string |
| 1031 | OutboundAdded map[string]int |
| 1032 | OutboundRemoved map[string]int |
| 1033 | OutboundTime map[string]int |
| 1034 | } |
| 1035 | |
| 1036 | // ParseCellStatsEvent parses the event. |
| 1037 | func ParseCellStatsEvent(raw string) *CellStatsEvent { |
| 1038 | event := &CellStatsEvent{Raw: raw} |
| 1039 | ok := true |
| 1040 | var attr string |
| 1041 | toIntMap := func(val string) map[string]int { |
| 1042 | ret := map[string]int{} |
| 1043 | for _, v := range strings.Split(val, ",") { |
| 1044 | key, val, _ := torutil.PartitionString(v, ':') |
| 1045 | ret[key], _ = strconv.Atoi(val) |
| 1046 | } |
| 1047 | return ret |
| 1048 | } |
| 1049 | for ok { |
| 1050 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 1051 | key, val, _ := torutil.PartitionString(attr, '=') |
| 1052 | switch key { |
| 1053 | case "ID": |
| 1054 | event.CircuitID = val |
| 1055 | case "InboundQueue": |
| 1056 | event.InboundQueueID = val |
| 1057 | case "InboundConn": |
| 1058 | event.InboundConnID = val |
| 1059 | case "InboundAdded": |
| 1060 | event.InboundAdded = toIntMap(val) |
| 1061 | case "InboundRemoved": |
| 1062 | event.InboundRemoved = toIntMap(val) |
| 1063 | case "InboundTime": |
| 1064 | event.OutboundTime = toIntMap(val) |
| 1065 | case "OutboundQueue": |
| 1066 | event.OutboundQueueID = val |
| 1067 | case "OutboundConn": |
| 1068 | event.OutboundConnID = val |
| 1069 | case "OutboundAdded": |
| 1070 | event.OutboundAdded = toIntMap(val) |
| 1071 | case "OutboundRemoved": |
| 1072 | event.OutboundRemoved = toIntMap(val) |
| 1073 | case "OutboundTime": |
| 1074 | event.OutboundTime = toIntMap(val) |
| 1075 | } |
| 1076 | } |
| 1077 | return event |
| 1078 | } |
| 1079 | |
| 1080 | // Code implements Event.Code |
| 1081 | func (*CellStatsEvent) Code() EventCode { return EventCodeCellStats } |
| 1082 | |
| 1083 | // TokenBucketEmptyEvent is TB_EMPTY in spec. |
| 1084 | type TokenBucketEmptyEvent struct { |
| 1085 | Raw string |
| 1086 | BucketName string |
| 1087 | ConnID string |
| 1088 | ReadBucketEmpty time.Duration |
| 1089 | WriteBucketEmpty time.Duration |
| 1090 | LastRefil time.Duration |
| 1091 | } |
| 1092 | |
| 1093 | // ParseTokenBucketEmptyEvent parses the event. |
| 1094 | func ParseTokenBucketEmptyEvent(raw string) *TokenBucketEmptyEvent { |
| 1095 | event := &TokenBucketEmptyEvent{Raw: raw} |
| 1096 | var ok bool |
| 1097 | event.BucketName, raw, ok = torutil.PartitionString(raw, ' ') |
| 1098 | var attr string |
| 1099 | for ok { |
| 1100 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 1101 | key, val, _ := torutil.PartitionString(attr, '=') |
| 1102 | switch key { |
| 1103 | case "ID": |
| 1104 | event.ConnID = val |
| 1105 | case "READ": |
| 1106 | i, _ := strconv.ParseInt(val, 10, 64) |
| 1107 | event.ReadBucketEmpty = time.Duration(i) * time.Millisecond |
| 1108 | case "WRITTEN": |
| 1109 | i, _ := strconv.ParseInt(val, 10, 64) |
| 1110 | event.WriteBucketEmpty = time.Duration(i) * time.Millisecond |
| 1111 | case "LAST": |
| 1112 | i, _ := strconv.ParseInt(val, 10, 64) |
| 1113 | event.LastRefil = time.Duration(i) * time.Millisecond |
| 1114 | } |
| 1115 | } |
| 1116 | return event |
| 1117 | } |
| 1118 | |
| 1119 | // Code implements Event.Code |
| 1120 | func (*TokenBucketEmptyEvent) Code() EventCode { return EventCodeTokenBucketEmpty } |
| 1121 | |
| 1122 | // HSDescEvent is HS_DESC in spec. |
| 1123 | type HSDescEvent struct { |
| 1124 | Raw string |
| 1125 | Action string |
| 1126 | Address string |
| 1127 | AuthType string |
| 1128 | HSDir string |
| 1129 | DescID string |
| 1130 | Reason string |
| 1131 | Replica int |
| 1132 | HSDirIndex string |
| 1133 | } |
| 1134 | |
| 1135 | // ParseHSDescEvent parses the event. |
| 1136 | func ParseHSDescEvent(raw string) *HSDescEvent { |
| 1137 | event := &HSDescEvent{Raw: raw} |
| 1138 | event.Action, raw, _ = torutil.PartitionString(raw, ' ') |
| 1139 | event.Address, raw, _ = torutil.PartitionString(raw, ' ') |
| 1140 | event.AuthType, raw, _ = torutil.PartitionString(raw, ' ') |
| 1141 | var ok bool |
| 1142 | event.HSDir, raw, ok = torutil.PartitionString(raw, ' ') |
| 1143 | var attr string |
| 1144 | first := true |
| 1145 | for ok { |
| 1146 | attr, raw, ok = torutil.PartitionString(raw, ' ') |
| 1147 | key, val, valOk := torutil.PartitionString(attr, '=') |
| 1148 | switch key { |
| 1149 | case "REASON": |
| 1150 | event.Reason = val |
| 1151 | case "REPLICA": |
| 1152 | event.Replica, _ = strconv.Atoi(val) |
| 1153 | case "HSDIR_INDEX": |
| 1154 | event.HSDirIndex = val |
| 1155 | default: |
| 1156 | if first && !valOk { |
| 1157 | event.DescID = attr |
| 1158 | } |
| 1159 | } |
| 1160 | first = false |
| 1161 | } |
| 1162 | return event |
| 1163 | } |
| 1164 | |
| 1165 | // Code implements Event.Code |
| 1166 | func (*HSDescEvent) Code() EventCode { return EventCodeHSDesc } |
| 1167 | |
| 1168 | // HSDescContentEvent is HS_DESC_CONTENT in spec. |
| 1169 | type HSDescContentEvent struct { |
| 1170 | Raw string |
| 1171 | Address string |
| 1172 | DescID string |
| 1173 | HSDir string |
| 1174 | Descriptor string |
| 1175 | } |
| 1176 | |
| 1177 | // ParseHSDescContentEvent parses the event. |
| 1178 | func ParseHSDescContentEvent(raw string) *HSDescContentEvent { |
| 1179 | event := &HSDescContentEvent{Raw: raw} |
| 1180 | event.Address, raw, _ = torutil.PartitionString(raw, ' ') |
| 1181 | event.DescID, raw, _ = torutil.PartitionString(raw, ' ') |
| 1182 | newlineIndex := strings.Index(raw, "\r\n") |
| 1183 | if newlineIndex != -1 { |
| 1184 | event.HSDir, event.Descriptor = raw[:newlineIndex], raw[newlineIndex+2:] |
| 1185 | } |
| 1186 | return event |
| 1187 | } |
| 1188 | |
| 1189 | // Code implements Event.Code |
| 1190 | func (*HSDescContentEvent) Code() EventCode { return EventCodeHSDescContent } |
| 1191 | |
| 1192 | // NetworkLivenessEvent is NETWORK_LIVENESS in spec. |
| 1193 | type NetworkLivenessEvent struct { |
| 1194 | Raw string |
| 1195 | } |
| 1196 | |
| 1197 | // ParseNetworkLivenessEvent parses the event. |
| 1198 | func ParseNetworkLivenessEvent(raw string) *NetworkLivenessEvent { |
| 1199 | return &NetworkLivenessEvent{Raw: raw} |
| 1200 | } |
| 1201 | |
| 1202 | // Code implements Event.Code |
| 1203 | func (*NetworkLivenessEvent) Code() EventCode { return EventCodeNetworkLiveness } |
| 1204 | |
| 1205 | // UnrecognizedEvent is any unrecognized event code. |
| 1206 | type UnrecognizedEvent struct { |
| 1207 | EventCode EventCode |
| 1208 | RawSingleLine string |
| 1209 | RawMultiLine []string |
| 1210 | } |
| 1211 | |
| 1212 | // ParseUnrecognizedEvent creates an unrecognized event with the given values. |
| 1213 | func ParseUnrecognizedEvent(eventCode EventCode, rawSingleLine string, rawMultiLine []string) *UnrecognizedEvent { |
| 1214 | return &UnrecognizedEvent{EventCode: eventCode, RawSingleLine: rawSingleLine, RawMultiLine: rawMultiLine} |
| 1215 | } |
| 1216 | |
| 1217 | // Code implements Event.Code |
| 1218 | func (u *UnrecognizedEvent) Code() EventCode { return u.EventCode } |