Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] VPN stats #1026

Merged
merged 15 commits into from
May 11, 2022
Merged
40 changes: 19 additions & 21 deletions internal/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package gui

import (
"context"
"embed"
"fmt"
"io"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/skycoin/skycoin/src/util/logging"
"github.com/toqueteos/webbrowser"

"github.com/skycoin/skywire/pkg/servicedisc"
"github.com/skycoin/skywire/pkg/skyenv"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)
Expand Down Expand Up @@ -193,25 +191,25 @@ func handleVPNLinkButton(conf *visorconfig.V1) {
}

// GetAvailPublicVPNServers gets all available public VPN server from service discovery URL
func GetAvailPublicVPNServers(conf *visorconfig.V1) []string {
sdClient := servicedisc.NewClient(log, servicedisc.Config{
Type: servicedisc.ServiceTypeVPN,
PK: conf.PK,
SK: conf.SK,
DiscAddr: conf.Launcher.ServiceDisc,
})
//ctx, _ := context.WithTimeout(context.Background(), 7*time.Second)
vpnServers, err := sdClient.Services(context.Background(), 0)
if err != nil {
log.Error("Error getting public vpn servers: ", err)
return nil
}
serverAddrs := make([]string, len(vpnServers))
for idx, server := range vpnServers {
serverAddrs[idx] = server.Addr.PubKey().String()
}
return serverAddrs
}
//func GetAvailPublicVPNServers(conf *visorconfig.V1) []string {
// sdClient := servicedisc.NewClient(log, servicedisc.Config{
// Type: servicedisc.ServiceTypeVPN,
// PK: conf.PK,
// SK: conf.SK,
// DiscAddr: conf.Launcher.ServiceDisc,
// })
// //ctx, _ := context.WithTimeout(context.Background(), 7*time.Second)
// vpnServers, err := sdClient.Services(context.Background(), 0)
// if err != nil {
// log.Error("Error getting public vpn servers: ", err)
// return nil
// }
// serverAddrs := make([]string, len(vpnServers))
// for idx, server := range vpnServers {
// serverAddrs[idx] = server.Addr.PubKey().String()
// }
// return serverAddrs
//}

func initUninstallBtn() {
if !checkIsPackage() {
Expand Down
3 changes: 3 additions & 0 deletions internal/vpn/tun_device_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
func newTUNDevice() (TUNDevice, error) {
tun, err := water.New(water.Config{
DeviceType: water.TUN,
PlatformSpecificParams: water.PlatformSpecificParams{
Name: "utun4",
},
})
if err != nil {
return nil, fmt.Errorf("error allocating TUN interface: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/router/network_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type networkStats struct {

func newNetworkStats() *networkStats {
return &networkStats{
bandwidthReceivedRecStart: time.Now(),
bandwidthReceivedRecStart: time.Now().UTC(),
}
}

Expand Down Expand Up @@ -69,8 +69,8 @@ func (s *networkStats) AddBandwidthReceived(amount uint64) {

func (s *networkStats) RemoteThroughput() int64 {
s.bandwidthReceivedRecStartMu.Lock()
timePassed := time.Since(s.bandwidthReceivedRecStart)
s.bandwidthReceivedRecStart = time.Now()
timePassed := time.Now().UTC().Sub(s.bandwidthReceivedRecStart) //nolint:gosimple
s.bandwidthReceivedRecStart = time.Now().UTC()
s.bandwidthReceivedRecStartMu.Unlock()

bandwidth := atomic.SwapUint64(&s.bandwidthReceived, 0)
Expand Down
12 changes: 8 additions & 4 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (rg *RouteGroup) writePacket(ctx context.Context, tp *transport.ManagedTran
err := tp.WritePacket(ctx, packet)
// note equality here. update activity only if there was NO error
if err == nil {
if packet.Type() == routing.DataPacket {
if packet.Type() != routing.ClosePacket || packet.Type() != routing.HandshakePacket {
rg.networkStats.AddBandwidthSent(uint64(packet.Size()))
}

Expand Down Expand Up @@ -425,8 +425,7 @@ func (rg *RouteGroup) sendNetworkProbe() error {
}

throughput := rg.networkStats.RemoteThroughput()
timestamp := time.Now().UnixNano() / int64(time.Millisecond)

timestamp := time.Now().UTC().UnixNano() / int64(time.Millisecond)
rg.networkStats.SetDownloadSpeed(uint32(throughput))

packet := routing.MakeNetworkProbePacket(rule.NextRouteID(), timestamp, throughput)
Expand Down Expand Up @@ -603,6 +602,10 @@ func (rg *RouteGroup) handlePacket(packet routing.Packet) error {
})
}

if packet.Type() != routing.ClosePacket || packet.Type() != routing.HandshakePacket {
rg.networkStats.AddBandwidthReceived(uint64(packet.Size()))
}

return nil
}

Expand All @@ -615,6 +618,8 @@ func (rg *RouteGroup) handleNetworkProbePacket(packet routing.Packet) error {
ms := sentAtMs % 1000
sentAt := time.Unix(int64(sentAtMs/1000), int64(ms)*int64(time.Millisecond))

rg.logger.Debugf("Latency is around %d ms", time.Since(sentAt).Milliseconds())

rg.networkStats.SetLatency(time.Since(sentAt))
rg.networkStats.SetUploadSpeed(uint32(throughput))

Expand All @@ -629,7 +634,6 @@ func (rg *RouteGroup) handleDataPacket(packet routing.Packet) error {
if rg.isRemoteClosed() {
return nil
}
rg.networkStats.AddBandwidthReceived(uint64(packet.Size()))

select {
case <-rg.closed:
Expand Down
5 changes: 3 additions & 2 deletions pkg/router/route_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/stretchr/testify/require"

"github.com/skycoin/skywire/pkg/routing"
Expand All @@ -30,6 +31,7 @@ func TestRouteGroup_RemoteAddr(t *testing.T) {
}

func createRouteGroup(cfg *RouteGroupConfig) *RouteGroup {
l := logging.NewMasterLogger()
rt := routing.NewTable()

pk1, _ := cipher.GenerateKeyPair()
Expand All @@ -38,7 +40,6 @@ func createRouteGroup(cfg *RouteGroupConfig) *RouteGroup {
port2 := routing.Port(2)
desc := routing.NewRouteDescriptor(pk1, pk2, port1, port2)

rg := NewRouteGroup(cfg, rt, desc, nil)

rg := NewRouteGroup(cfg, rt, desc, l)
return rg
}