-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from mysteriumnetwork/server-byte-count
Added a middleware for server side statistics for sessions
- Loading branch information
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (C) 2019 The "MysteriumNetwork/go-openvpn" Authors. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bytecount | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/mysteriumnetwork/go-openvpn/openvpn/management" | ||
) | ||
|
||
var rule = regexp.MustCompile("^>BYTECOUNT_CLI:([0-9]*),([0-9]*),([0-9]*)$") | ||
|
||
// SessionByteChangeHandler is the callback we call with the session byte count | ||
type SessionByteChangeHandler func(SessionByteCount) | ||
|
||
// SessionByteCount represents | ||
type SessionByteCount struct { | ||
ClientID, BytesIn, BytesOut int | ||
} | ||
|
||
// Middleware reports the different session byte counts | ||
type Middleware struct { | ||
handler SessionByteChangeHandler | ||
updateInterval int | ||
} | ||
|
||
// NewMiddleware returns a new instance of the middleware | ||
func NewMiddleware(h SessionByteChangeHandler, updateInterval int) *Middleware { | ||
return &Middleware{ | ||
handler: h, | ||
updateInterval: updateInterval, | ||
} | ||
} | ||
|
||
// Start starts the middleware | ||
func (m *Middleware) Start(cw management.CommandWriter) error { | ||
_, err := cw.SingleLineCommand("bytecount %v", m.updateInterval) | ||
return err | ||
} | ||
|
||
// Stop stops the middleware | ||
func (m *Middleware) Stop(cw management.CommandWriter) error { | ||
_, err := cw.SingleLineCommand("bytecount %v", 0) | ||
return err | ||
} | ||
|
||
// ConsumeLine handles the given openvpn management line | ||
func (m *Middleware) ConsumeLine(line string) (consumed bool, err error) { | ||
if !rule.MatchString(line) { | ||
return false, nil | ||
} | ||
|
||
match := rule.FindStringSubmatch(line) | ||
|
||
if len(match) != 4 { | ||
return false, fmt.Errorf("wrong match length for %q. got len = %v, expected %v", line, len(match), 4) | ||
} | ||
|
||
clientID, err := strconv.Atoi(match[1]) | ||
if err != nil { | ||
return false, fmt.Errorf("could not parse clientID from match[1]: %q", match[1]) | ||
} | ||
|
||
bytesIn, err := strconv.Atoi(match[2]) | ||
if err != nil { | ||
return false, fmt.Errorf("could not parse clientID from match[2]: %q", match[2]) | ||
} | ||
|
||
bytesOut, err := strconv.Atoi(match[3]) | ||
if err != nil { | ||
return false, fmt.Errorf("could not parse clientID from match[3]: %q", match[3]) | ||
} | ||
|
||
m.handler(SessionByteCount{ | ||
ClientID: clientID, | ||
BytesIn: bytesIn, | ||
BytesOut: bytesOut, | ||
}) | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) 2019 The "MysteriumNetwork/go-openvpn" Authors. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bytecount | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockHandler struct { | ||
sbc SessionByteCount | ||
} | ||
|
||
func (mh *mockHandler) Handle(s SessionByteCount) { | ||
mh.sbc = s | ||
} | ||
|
||
func Test_ConsumesOKLine(t *testing.T) { | ||
statsRecorder := &mockHandler{} | ||
middleware := NewMiddleware(statsRecorder.Handle, 1) | ||
consumed, err := middleware.ConsumeLine(">BYTECOUNT_CLI:1,2,3") | ||
assert.Nil(t, err) | ||
assert.True(t, consumed) | ||
assert.Equal(t, 1, statsRecorder.sbc.ClientID) | ||
assert.Equal(t, 2, statsRecorder.sbc.BytesIn) | ||
assert.Equal(t, 3, statsRecorder.sbc.BytesOut) | ||
} | ||
|
||
func Test_IgnoresMalformedLines(t *testing.T) { | ||
badLines := []string{ | ||
">BYTECOUNT_CLI:asdasd,2,3", | ||
">BYTECOUNT_CLI", | ||
"whatever", | ||
"BYTECOUNT_CLI:1,2,3", | ||
} | ||
for _, v := range badLines { | ||
statsRecorder := &mockHandler{} | ||
middleware := NewMiddleware(statsRecorder.Handle, 1) | ||
consumed, err := middleware.ConsumeLine(v) | ||
assert.Nil(t, err) | ||
assert.False(t, consumed) | ||
assert.EqualValues(t, SessionByteCount{}, statsRecorder.sbc) | ||
} | ||
} |