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 #6 from MysteriumNetwork/feature/traffic-stats
Traffic statistics "Data transfered" for node sessions #4
- Loading branch information
Showing
10 changed files
with
222 additions
and
139 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,75 @@ | ||
package bytescount_client | ||
|
||
import ( | ||
"fmt" | ||
"github.com/mysterium/node/openvpn" | ||
"github.com/mysterium/node/server" | ||
"github.com/mysterium/node/server/dto" | ||
"net" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type middleware struct { | ||
mysteriumClient server.Client | ||
interval time.Duration | ||
sessionId string | ||
|
||
connection net.Conn | ||
} | ||
|
||
func NewMiddleware(mysteriumClient server.Client, sessionId string, interval time.Duration) openvpn.ManagementMiddleware { | ||
return &middleware{ | ||
mysteriumClient: mysteriumClient, | ||
interval: interval, | ||
sessionId: sessionId, | ||
|
||
connection: nil, | ||
} | ||
} | ||
|
||
func (middleware *middleware) Start(connection net.Conn) error { | ||
middleware.connection = connection | ||
|
||
command := fmt.Sprintf("bytecount %d\n", int(middleware.interval.Seconds())) | ||
_, err := middleware.connection.Write([]byte(command)) | ||
|
||
return err | ||
} | ||
|
||
func (middleware *middleware) Stop() error { | ||
_, err := middleware.connection.Write([]byte("bytecount 0\n")) | ||
return err | ||
} | ||
|
||
func (middleware *middleware) ConsumeLine(line string) (consumed bool, err error) { | ||
rule, err := regexp.Compile("^>BYTECOUNT:(.*),(.*)$") | ||
if err != nil { | ||
return | ||
} | ||
|
||
match := rule.FindStringSubmatch(line) | ||
consumed = len(match) > 0 | ||
if !consumed { | ||
return | ||
} | ||
|
||
bytesIn, err := strconv.Atoi(match[1]) | ||
if err != nil { | ||
return | ||
} | ||
|
||
bytesOut, err := strconv.Atoi(match[2]) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = middleware.mysteriumClient.SessionSendStats(middleware.sessionId, dto.SessionStats{ | ||
Id: middleware.sessionId, | ||
BytesSent: bytesOut, | ||
BytesReceived: bytesIn, | ||
}) | ||
|
||
return | ||
} |
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,43 @@ | ||
package bytescount_client | ||
|
||
import ( | ||
"errors" | ||
"github.com/mysterium/node/server" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_Factory(t *testing.T) { | ||
middleware := NewMiddleware(server.NewClientFake(), "session-test", 1*time.Minute) | ||
assert.NotNil(t, middleware) | ||
} | ||
|
||
func Test_ConsumeLine(t *testing.T) { | ||
var tests = []struct { | ||
line string | ||
expectedConsumed bool | ||
expectedError error | ||
}{ | ||
{">BYTECOUNT:3018,3264", true, nil}, | ||
{">BYTECOUNT:0,3264", true, nil}, | ||
{">BYTECOUNT:3018,", true, errors.New(`strconv.ParseInt: parsing "": invalid syntax`)}, | ||
{">BYTECOUNT:,", true, errors.New(`strconv.ParseInt: parsing "": invalid syntax`)}, | ||
{"OTHER", false, nil}, | ||
{"BYTECOUNT", false, nil}, | ||
{"BYTECOUNT:", false, nil}, | ||
{"BYTECOUNT:3018,3264", false, nil}, | ||
{">BYTECOUNTT:3018,3264", false, nil}, | ||
} | ||
|
||
middleware := NewMiddleware(server.NewClientFake(), "session-test", 1*time.Minute) | ||
for _, test := range tests { | ||
consumed, err := middleware.ConsumeLine(test.line) | ||
if test.expectedError != nil { | ||
assert.Error(t, test.expectedError, err.Error(), test.line) | ||
} else { | ||
assert.NoError(t, err, test.line) | ||
} | ||
assert.Equal(t, test.expectedConsumed, consumed, test.line) | ||
} | ||
} |
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
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
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
Oops, something went wrong.