-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e61267
commit af610d8
Showing
13 changed files
with
230 additions
and
16 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 |
---|---|---|
@@ -1 +1 @@ | ||
v1.1.0 | ||
v1.1.1beta1 |
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
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
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,106 @@ | ||
package rest | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"path" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/in4it/wireguard-server/pkg/wireguard" | ||
) | ||
|
||
func (c *Context) userStatsHandler(w http.ResponseWriter, r *http.Request) { | ||
var userStatsResponse UserStatsResponse | ||
statsFile := c.Storage.Client.ConfigPath(path.Join(wireguard.VPN_STATS_DIR, "user-"+time.Now().Format("2006-01-02")) + ".log") | ||
if !c.Storage.Client.FileExists(statsFile) { // file does not exist so just return empty response | ||
out, err := json.Marshal(userStatsResponse) | ||
if err != nil { | ||
c.returnError(w, fmt.Errorf("user stats response marshal error: %s", err), http.StatusBadRequest) | ||
return | ||
} | ||
c.write(w, out) | ||
return | ||
} | ||
logData, err := c.Storage.Client.ReadFile(statsFile) | ||
if err != nil { | ||
c.returnError(w, fmt.Errorf("readfile error: %s", err), http.StatusBadRequest) | ||
return | ||
} | ||
scanner := bufio.NewScanner(bytes.NewReader(logData)) | ||
|
||
receiveBytesLast := make(map[string]int64) | ||
transmitBytesLast := make(map[string]int64) | ||
receiveBytesData := make(map[string][]UserStatsDataPoint) | ||
transmitBytesData := make(map[string][]UserStatsDataPoint) | ||
for scanner.Scan() { // all other entries | ||
inputSplit := strings.Split(scanner.Text(), ",") | ||
userID := inputSplit[1] | ||
if _, ok := receiveBytesLast[userID]; !ok { | ||
val, err := strconv.ParseInt(inputSplit[3], 10, 64) | ||
if err == nil { | ||
receiveBytesLast[userID] = val | ||
} else { | ||
receiveBytesLast[userID] = 0 | ||
} | ||
} | ||
if _, ok := transmitBytesLast[userID]; !ok { | ||
val, err := strconv.ParseInt(inputSplit[4], 10, 64) | ||
if err == nil { | ||
transmitBytesLast[userID] = val | ||
} else { | ||
transmitBytesLast[userID] = 0 | ||
} | ||
} | ||
receiveBytes, err := strconv.ParseInt(inputSplit[3], 10, 64) | ||
if err == nil { | ||
if _, ok := receiveBytesData[userID]; !ok { | ||
receiveBytesData[userID] = []UserStatsDataPoint{} | ||
} | ||
receiveBytesData[userID] = append(receiveBytesData[userID], UserStatsDataPoint{X: inputSplit[0], Y: receiveBytes - receiveBytesLast[userID]}) | ||
} | ||
transmitBytes, err := strconv.ParseInt(inputSplit[4], 10, 64) | ||
if err == nil { | ||
if _, ok := transmitBytesData[userID]; !ok { | ||
transmitBytesData[userID] = []UserStatsDataPoint{} | ||
} | ||
transmitBytesData[userID] = append(transmitBytesData[userID], UserStatsDataPoint{X: inputSplit[0], Y: transmitBytes - transmitBytesLast[userID]}) | ||
} | ||
receiveBytesLast[userID] = receiveBytes | ||
transmitBytesLast[userID] = transmitBytes | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
c.returnError(w, fmt.Errorf("log file read (scanner) error: %s", err), http.StatusBadRequest) | ||
return | ||
} | ||
userStatsResponse.ReceiveBytes = UserStatsData{ | ||
Datasets: []UserStatsDataset{}, | ||
} | ||
userStatsResponse.TransmitBytes = UserStatsData{ | ||
Datasets: []UserStatsDataset{}, | ||
} | ||
for userID, data := range receiveBytesData { | ||
userStatsResponse.ReceiveBytes.Datasets = append(userStatsResponse.ReceiveBytes.Datasets, UserStatsDataset{ | ||
Label: userID, | ||
Data: data, | ||
}) | ||
} | ||
for userID, data := range transmitBytesData { | ||
userStatsResponse.TransmitBytes.Datasets = append(userStatsResponse.TransmitBytes.Datasets, UserStatsDataset{ | ||
Label: userID, | ||
Data: data, | ||
}) | ||
} | ||
|
||
out, err := json.Marshal(userStatsResponse) | ||
if err != nil { | ||
c.returnError(w, fmt.Errorf("user stats response marshal error: %s", err), http.StatusBadRequest) | ||
return | ||
} | ||
c.write(w, out) | ||
} |
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,64 @@ | ||
package rest | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http/httptest" | ||
"path" | ||
"testing" | ||
"time" | ||
|
||
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks" | ||
"github.com/in4it/wireguard-server/pkg/wireguard" | ||
) | ||
|
||
func TestUserStatsHandler(t *testing.T) { | ||
|
||
storage := &testingmocks.MockMemoryStorage{} | ||
|
||
c, err := newContext(storage, SERVER_TYPE_VPN) | ||
if err != nil { | ||
t.Fatalf("Cannot create context") | ||
} | ||
testData := `2024-08-23T19:29:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,12729136,24348520,2024-08-23T18:30:42 | ||
2024-08-23T19:34:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,13391716,25162108,2024-08-23T19:33:38 | ||
2024-08-23T19:39:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,14419152,27496068,2024-08-23T19:37:39 | ||
2024-08-23T19:44:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,16003988,30865740,2024-08-23T19:42:51 | ||
2024-08-23T19:49:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,19777928,57367624,2024-08-23T19:48:51 | ||
2024-08-23T19:54:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,23772276,75895264,2024-08-23T19:52:51 | ||
2024-08-23T19:59:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,25443216,81496940,2024-08-23T19:58:52 | ||
2024-08-23T20:04:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,26574324,83886164,2024-08-23T20:02:53 | ||
2024-08-23T20:09:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,39928520,85171728,2024-08-23T20:08:54` | ||
|
||
statsFile := c.Storage.Client.ConfigPath(path.Join(wireguard.VPN_STATS_DIR, "user-"+time.Now().Format("2006-01-02")) + ".log") | ||
err = c.Storage.Client.WriteFile(statsFile, []byte(testData)) | ||
if err != nil { | ||
t.Fatalf("Cannot write test file") | ||
} | ||
|
||
req := httptest.NewRequest("GET", "http://example.com/stats/user", nil) | ||
w := httptest.NewRecorder() | ||
c.userStatsHandler(w, req) | ||
|
||
resp := w.Result() | ||
|
||
if resp.StatusCode != 200 { | ||
t.Fatalf("status code is not 200: %d", resp.StatusCode) | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
var userStatsResponse UserStatsResponse | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&userStatsResponse) | ||
if err != nil { | ||
t.Fatalf("Cannot decode response from create user: %s", err) | ||
} | ||
|
||
if userStatsResponse.ReceiveBytes.Datasets[0].Data[1].Y != 662580 { | ||
t.Fatalf("unexpected data: %d", userStatsResponse.ReceiveBytes.Datasets[0].Data[1].Y) | ||
} | ||
if userStatsResponse.TransmitBytes.Datasets[0].Data[1].Y != 813588 { | ||
t.Fatalf("unexpected data: %d", userStatsResponse.TransmitBytes.Datasets[0].Data[1].Y) | ||
} | ||
|
||
} |
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
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