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
|
package openvpn
import (
"bytes"
"fmt"
"strconv"
)
var eventSep = []byte(":")
var fieldSep = []byte(",")
var byteCountEventKW = []byte("BYTECOUNT")
var byteCountCliEventKW = []byte("BYTECOUNT_CLI")
var clientEventKW = []byte("CLIENT")
var echoEventKW = []byte("ECHO")
var fatalEventKW = []byte("FATAL")
var holdEventKW = []byte("HOLD")
var infoEventKW = []byte("INFO")
var logEventKW = []byte("LOG")
var needOkEventKW = []byte("NEED-OK")
var needStrEventKW = []byte("NEED-STR")
var passwordEventKW = []byte("PASSWORD")
var stateEventKW = []byte("STATE")
type Event interface {
String() string
}
// UnknownEvent represents an event of a type that this package doesn't
// know about.
//
// Future versions of this library may learn about new event types, so a
// caller should exercise caution when making use of events of this type
// to access unsupported behavior. Backward-compatibility is *not*
// guaranteed for events of this type.
type UnknownEvent struct {
keyword []byte
body []byte
}
func (e *UnknownEvent) Type() string {
return string(e.keyword)
}
func (e *UnknownEvent) Body() string {
return string(e.body)
}
func (e *UnknownEvent) String() string {
return fmt.Sprintf("%s: %s", e.keyword, e.body)
}
// MalformedEvent represents a message from the OpenVPN process that is
// presented as an event but does not comply with the expected event syntax.
//
// Events of this type should never be seen but robust callers will accept
// and ignore them, possibly generating some kind of debugging message.
//
// One reason for potentially seeing events of this type is when the target
// program is actually not an OpenVPN process at all, but in fact this client
// has been connected to a different sort of server by mistake.
type MalformedEvent struct {
raw []byte
}
func (e *MalformedEvent) String() string {
return fmt.Sprintf("Malformed Event %q", e.raw)
}
// HoldEvent is a notification that the OpenVPN process is in a management
// hold and will not continue connecting until the hold is released, e.g.
// by calling client.HoldRelease()
type HoldEvent struct {
body []byte
}
func (e *HoldEvent) String() string {
return string(e.body)
}
// StateEvent is a notification of a change of connection state. It can be
// used, for example, to detect if the OpenVPN connection has been interrupted
// and the OpenVPN process is attempting to reconnect.
type StateEvent struct {
body []byte
// bodyParts is populated only on first request, giving us the
// separate comma-separated elements of the message. Not all
// fields are populated for all states.
bodyParts [][]byte
}
func (e *StateEvent) RawTimestamp() string {
parts := e.parts()
return string(parts[0])
}
func (e *StateEvent) NewState() string {
parts := e.parts()
return string(parts[1])
}
func (e *StateEvent) Description() string {
parts := e.parts()
return string(parts[2])
}
// LocalTunnelAddr returns the IP address of the local interface within
// the tunnel, as a string that can be parsed using net.ParseIP.
//
// This field is only populated for events whose NewState returns
// either ASSIGN_IP or CONNECTED.
func (e *StateEvent) LocalTunnelAddr() string {
parts := e.parts()
return string(parts[3])
}
// RemoteAddr returns the non-tunnel IP address of the remote
// system that has connected to the local OpenVPN process.
//
// This field is only populated for events whose NewState returns
// CONNECTED.
func (e *StateEvent) RemoteAddr() string {
parts := e.parts()
return string(parts[4])
}
func (e *StateEvent) String() string {
newState := e.NewState()
switch newState {
case "ASSIGN_IP":
return fmt.Sprintf("%s: %s", newState, e.LocalTunnelAddr())
case "CONNECTED":
return fmt.Sprintf("%s: %s", newState, e.RemoteAddr())
default:
desc := e.Description()
if desc != "" {
return fmt.Sprintf("%s: %s", newState, desc)
} else {
return newState
}
}
}
func (e *StateEvent) parts() [][]byte {
if e.bodyParts == nil {
// State messages currently have only five segments, but
// we'll ask for 5 so any additional fields that might show
// up in newer versions will gather in an element we're
// not actually using.
e.bodyParts = bytes.SplitN(e.body, fieldSep, 6)
// Prevent crash if the server has sent us a malformed
// status message. This should never actually happen if
// the server is behaving itself.
if len(e.bodyParts) < 5 {
expanded := make([][]byte, 5)
copy(expanded, e.bodyParts)
e.bodyParts = expanded
}
}
return e.bodyParts
}
// EchoEvent is emitted by an OpenVPN process running in client mode when
// an "echo" command is pushed to it by the server it has connected to.
//
// The format of the echo message is free-form, since this message type is
// intended to pass application-specific data from the server-side config
// into whatever client is consuming the management prototcol.
//
// This event is emitted only if the management client has turned on events
// of this type using client.SetEchoEvents(true)
type EchoEvent struct {
body []byte
}
func (e *EchoEvent) RawTimestamp() string {
sepIndex := bytes.Index(e.body, fieldSep)
if sepIndex == -1 {
return ""
}
return string(e.body[:sepIndex])
}
func (e *EchoEvent) Message() string {
sepIndex := bytes.Index(e.body, fieldSep)
if sepIndex == -1 {
return ""
}
return string(e.body[sepIndex+1:])
}
func (e *EchoEvent) String() string {
return fmt.Sprintf("ECHO: %s", e.Message())
}
// ByteCountEvent represents a periodic snapshot of data transfer in bytes
// on a VPN connection.
//
// For OpenVPN *servers*, events are emitted for each client and the method
// ClientId identifies thet client.
//
// For other OpenVPN modes, events are emitted only once per interval for the
// single connection managed by the target process, and ClientId returns
// the empty string.
type ByteCountEvent struct {
hasClient bool
body []byte
// populated on first call to parts()
bodyParts [][]byte
}
func (e *ByteCountEvent) ClientId() string {
if !e.hasClient {
return ""
}
return string(e.parts()[0])
}
func (e *ByteCountEvent) BytesIn() int {
index := 0
if e.hasClient {
index = 1
}
str := string(e.parts()[index])
val, _ := strconv.Atoi(str)
// Ignore error, since this should never happen if OpenVPN is
// behaving itself.
return val
}
func (e *ByteCountEvent) BytesOut() int {
index := 1
if e.hasClient {
index = 2
}
str := string(e.parts()[index])
val, _ := strconv.Atoi(str)
// Ignore error, since this should never happen if OpenVPN is
// behaving itself.
return val
}
func (e *ByteCountEvent) String() string {
if e.hasClient {
return fmt.Sprintf("Client %s: %d in, %d out", e.ClientId(), e.BytesIn(), e.BytesOut())
} else {
return fmt.Sprintf("%d in, %d out", e.BytesIn(), e.BytesOut())
}
}
func (e *ByteCountEvent) parts() [][]byte {
if e.bodyParts == nil {
e.bodyParts = bytes.SplitN(e.body, fieldSep, 4)
wantCount := 2
if e.hasClient {
wantCount = 3
}
// Prevent crash if the server has sent us a malformed
// message. This should never actually happen if the
// server is behaving itself.
if len(e.bodyParts) < wantCount {
expanded := make([][]byte, wantCount)
copy(expanded, e.bodyParts)
e.bodyParts = expanded
}
}
return e.bodyParts
}
func upgradeEvent(raw []byte) Event {
splitIdx := bytes.Index(raw, eventSep)
if splitIdx == -1 {
// Should never happen, but we'll handle it robustly if it does.
return &MalformedEvent{raw}
}
keyword := raw[:splitIdx]
body := raw[splitIdx+1:]
switch {
case bytes.Equal(keyword, stateEventKW):
return &StateEvent{body: body}
case bytes.Equal(keyword, holdEventKW):
return &HoldEvent{body}
case bytes.Equal(keyword, echoEventKW):
return &EchoEvent{body}
case bytes.Equal(keyword, byteCountEventKW):
return &ByteCountEvent{hasClient: false, body: body}
case bytes.Equal(keyword, byteCountCliEventKW):
return &ByteCountEvent{hasClient: true, body: body}
default:
return &UnknownEvent{keyword, body}
}
}
|