From d7dfc3e5b90a4d5e91c53c4c269d8221b8778fd1 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Thu, 2 Dec 2021 19:19:57 +0700 Subject: [PATCH 1/9] latency fix attempt --- pkg/router/route_group.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index 6920d996e6..5e73342b16 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -422,7 +422,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)) @@ -612,7 +612,7 @@ func (rg *RouteGroup) handleNetworkProbePacket(packet routing.Packet) error { ms := sentAtMs % 1000 sentAt := time.Unix(int64(sentAtMs/1000), int64(ms)*int64(time.Millisecond)) - rg.networkStats.SetLatency(time.Since(sentAt)) + rg.networkStats.SetLatency(time.Now().UTC().Sub(sentAt)) rg.networkStats.SetUploadSpeed(uint32(throughput)) return nil From 32bbd27c0b2a2e2f3953493ca2c88675785f4fed Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Fri, 3 Dec 2021 23:59:40 +0700 Subject: [PATCH 2/9] vpn-stats latency and throughput --- internal/gui/gui.go | 40 ++++++++++++++++++------------------- pkg/router/network_stats.go | 6 +++--- pkg/router/route_group.go | 5 +++-- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/internal/gui/gui.go b/internal/gui/gui.go index 111fe2c701..5729d66571 100644 --- a/internal/gui/gui.go +++ b/internal/gui/gui.go @@ -4,7 +4,6 @@ package gui import ( - "context" "embed" "fmt" "io" @@ -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" ) @@ -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() { diff --git a/pkg/router/network_stats.go b/pkg/router/network_stats.go index 919975503b..89be9796cf 100644 --- a/pkg/router/network_stats.go +++ b/pkg/router/network_stats.go @@ -20,7 +20,7 @@ type networkStats struct { func newNetworkStats() *networkStats { return &networkStats{ - bandwidthReceivedRecStart: time.Now(), + bandwidthReceivedRecStart: time.Now().UTC(), } } @@ -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().Sub(s.bandwidthReceivedRecStart) //nolint:gosimple + s.bandwidthReceivedRecStart = time.Now().UTC() s.bandwidthReceivedRecStartMu.Unlock() bandwidth := atomic.SwapUint64(&s.bandwidthReceived, 0) diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index 5e73342b16..3cd6b2c05d 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -610,9 +610,10 @@ func (rg *RouteGroup) handleNetworkProbePacket(packet routing.Packet) error { throughput := binary.BigEndian.Uint64(payload[8:]) ms := sentAtMs % 1000 - sentAt := time.Unix(int64(sentAtMs/1000), int64(ms)*int64(time.Millisecond)) + sentAt := time.Unix(int64(sentAtMs/1000), int64(ms)*int64(time.Millisecond)).UTC() + latency := time.Now().UTC().Sub(sentAt) - rg.networkStats.SetLatency(time.Now().UTC().Sub(sentAt)) + rg.networkStats.SetLatency(latency) rg.networkStats.SetUploadSpeed(uint32(throughput)) return nil From e245bdabddbf8771a598fa61d025ede7929f6caa Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Sun, 5 Dec 2021 20:11:33 +0700 Subject: [PATCH 3/9] unix milli --- pkg/router/route_group.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index 3cd6b2c05d..776ffc50aa 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -422,13 +422,13 @@ func (rg *RouteGroup) sendNetworkProbe() error { } throughput := rg.networkStats.RemoteThroughput() - timestamp := time.Now().UTC().UnixNano() / int64(time.Millisecond) + timestamp := time.Now().UTC().UnixMilli() rg.networkStats.SetDownloadSpeed(uint32(throughput)) packet := routing.MakeNetworkProbePacket(rule.NextRouteID(), timestamp, throughput) - return rg.writePacket(context.Background(), tp, packet, rule.KeyRouteID()) + return rg.writePacket(context.Background(), tp, packet, rule.NextRouteID()) } func (rg *RouteGroup) networkProbeServiceFn(_ time.Duration) { @@ -609,8 +609,7 @@ func (rg *RouteGroup) handleNetworkProbePacket(packet routing.Packet) error { sentAtMs := binary.BigEndian.Uint64(payload) throughput := binary.BigEndian.Uint64(payload[8:]) - ms := sentAtMs % 1000 - sentAt := time.Unix(int64(sentAtMs/1000), int64(ms)*int64(time.Millisecond)).UTC() + sentAt := time.Unix(int64(sentAtMs/1000), int64(sentAtMs)*int64(time.Millisecond)).UTC() latency := time.Now().UTC().Sub(sentAt) rg.networkStats.SetLatency(latency) From f75647b795d91b2fdc86cb9ba59aa014d0fe7455 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Fri, 10 Dec 2021 12:10:15 +0700 Subject: [PATCH 4/9] route_group update --- pkg/router/route_group.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index 776ffc50aa..d4393c02e7 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -331,7 +331,7 @@ func (rg *RouteGroup) write(data []byte, tp *transport.ManagedTransport, rule ro return 0, err } - atomic.StoreInt64(&rg.lastSent, time.Now().UnixNano()) + atomic.StoreInt64(&rg.lastSent, time.Now().UTC().UnixNano()) return len(data), nil } @@ -422,13 +422,13 @@ func (rg *RouteGroup) sendNetworkProbe() error { } throughput := rg.networkStats.RemoteThroughput() - timestamp := time.Now().UTC().UnixMilli() + timestamp := time.Now().UTC().UnixNano() / int64(time.Millisecond) rg.networkStats.SetDownloadSpeed(uint32(throughput)) packet := routing.MakeNetworkProbePacket(rule.NextRouteID(), timestamp, throughput) - return rg.writePacket(context.Background(), tp, packet, rule.NextRouteID()) + return rg.writePacket(context.Background(), tp, packet, rule.KeyRouteID()) } func (rg *RouteGroup) networkProbeServiceFn(_ time.Duration) { @@ -609,10 +609,10 @@ func (rg *RouteGroup) handleNetworkProbePacket(packet routing.Packet) error { sentAtMs := binary.BigEndian.Uint64(payload) throughput := binary.BigEndian.Uint64(payload[8:]) - sentAt := time.Unix(int64(sentAtMs/1000), int64(sentAtMs)*int64(time.Millisecond)).UTC() - latency := time.Now().UTC().Sub(sentAt) + ms := sentAtMs % 1000 + sentAt := time.Unix(int64(sentAtMs/1000), int64(ms)*int64(time.Millisecond)) - rg.networkStats.SetLatency(latency) + rg.networkStats.SetLatency(time.Since(sentAt)) rg.networkStats.SetUploadSpeed(uint32(throughput)) return nil From 8fa5e9de8bf172c8d23a4b92d52ae38ac62f1599 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Mon, 13 Dec 2021 14:31:49 +0700 Subject: [PATCH 5/9] fixes tests --- pkg/router/network_stats.go | 2 +- pkg/router/route_group.go | 15 ++++++++++----- pkg/router/route_group_test.go | 5 ++++- pkg/router/router.go | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/router/network_stats.go b/pkg/router/network_stats.go index 89be9796cf..98d0202c30 100644 --- a/pkg/router/network_stats.go +++ b/pkg/router/network_stats.go @@ -69,7 +69,7 @@ func (s *networkStats) AddBandwidthReceived(amount uint64) { func (s *networkStats) RemoteThroughput() int64 { s.bandwidthReceivedRecStartMu.Lock() - timePassed := time.Now().Sub(s.bandwidthReceivedRecStart) //nolint:gosimple + timePassed := time.Now().UTC().Sub(s.bandwidthReceivedRecStart) //nolint:gosimple s.bandwidthReceivedRecStart = time.Now().UTC() s.bandwidthReceivedRecStartMu.Unlock() diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index d4393c02e7..7d1348f2e5 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -116,14 +116,17 @@ type RouteGroup struct { } // NewRouteGroup creates a new RouteGroup. -func NewRouteGroup(cfg *RouteGroupConfig, rt routing.Table, desc routing.RouteDescriptor) *RouteGroup { +func NewRouteGroup(cfg *RouteGroupConfig, rt routing.Table, desc routing.RouteDescriptor, mLoggger *logging.MasterLogger) *RouteGroup { if cfg == nil { cfg = DefaultRouteGroupConfig() } - + logger := logging.MustGetLogger(fmt.Sprintf("RouteGroup %s", desc.String())) + if mLoggger != nil { + logger = mLoggger.PackageLogger(fmt.Sprintf("RouteGroup %s", desc.String())) + } rg := &RouteGroup{ cfg: cfg, - logger: logging.MustGetLogger(fmt.Sprintf("RouteGroup %s", desc.String())), + logger: logger, desc: desc, rt: rt, tps: make([]*transport.ManagedTransport, 0), @@ -331,7 +334,7 @@ func (rg *RouteGroup) write(data []byte, tp *transport.ManagedTransport, rule ro return 0, err } - atomic.StoreInt64(&rg.lastSent, time.Now().UTC().UnixNano()) + atomic.StoreInt64(&rg.lastSent, time.Now().UnixNano()) return len(data), nil } @@ -422,7 +425,7 @@ func (rg *RouteGroup) sendNetworkProbe() error { } throughput := rg.networkStats.RemoteThroughput() - timestamp := time.Now().UTC().UnixNano() / int64(time.Millisecond) + timestamp := time.Now().UTC().UnixMilli() rg.networkStats.SetDownloadSpeed(uint32(throughput)) @@ -612,6 +615,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)) diff --git a/pkg/router/route_group_test.go b/pkg/router/route_group_test.go index 3ad02ca34b..cc4879965d 100644 --- a/pkg/router/route_group_test.go +++ b/pkg/router/route_group_test.go @@ -3,6 +3,8 @@ package router import ( "testing" + "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/dmsg/cipher" "github.com/stretchr/testify/require" @@ -30,6 +32,7 @@ func TestRouteGroup_RemoteAddr(t *testing.T) { } func createRouteGroup(cfg *RouteGroupConfig) *RouteGroup { + l := logging.NewMasterLogger() rt := routing.NewTable() pk1, _ := cipher.GenerateKeyPair() @@ -38,7 +41,7 @@ func createRouteGroup(cfg *RouteGroupConfig) *RouteGroup { port2 := routing.Port(2) desc := routing.NewRouteDescriptor(pk1, pk2, port1, port2) - rg := NewRouteGroup(cfg, rt, desc) + rg := NewRouteGroup(cfg, rt, desc, l) return rg } diff --git a/pkg/router/router.go b/pkg/router/router.go index fb65b84c37..13af94e7e1 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -431,7 +431,7 @@ func (r *router) saveRouteGroupRules(rules routing.EdgeRules, nsConf noise.Confi nrg, ok := r.rgsNs[rules.Desc] r.logger.Infof("Creating new route group rule with desc: %s", &rules.Desc) - rg := NewRouteGroup(DefaultRouteGroupConfig(), r.rt, rules.Desc) + rg := NewRouteGroup(DefaultRouteGroupConfig(), r.rt, rules.Desc, nil) rg.appendRules(rules.Forward, rules.Reverse, r.tm.Transport(rules.Forward.NextTransportID())) // we put raw rg so it can be accessible to the router when handshake packets come in r.rgsRaw[rules.Desc] = rg From 8105e003cf0f4d6d4f6b7a90b0d2d426e4ca8df7 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Mon, 13 Dec 2021 14:32:24 +0700 Subject: [PATCH 6/9] fixes tests --- pkg/router/route_group_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/router/route_group_test.go b/pkg/router/route_group_test.go index cc4879965d..faeb2478f4 100644 --- a/pkg/router/route_group_test.go +++ b/pkg/router/route_group_test.go @@ -3,9 +3,8 @@ package router import ( "testing" - "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/require" "github.com/skycoin/skywire/pkg/routing" From 4333babcb0ba59603b646bd0009eccd6b3268ebb Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Mon, 20 Dec 2021 11:00:56 +0700 Subject: [PATCH 7/9] trying fixed interface --- internal/vpn/tun_device_unix.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/vpn/tun_device_unix.go b/internal/vpn/tun_device_unix.go index ea77020b53..423f50a136 100644 --- a/internal/vpn/tun_device_unix.go +++ b/internal/vpn/tun_device_unix.go @@ -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) From 0cd6126cdcb29b17fdd1359b0f9a3932f1d990d2 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Mon, 20 Dec 2021 17:57:23 +0700 Subject: [PATCH 8/9] add other packets as well to the overall network bandwidth meter --- pkg/router/route_group.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index 7d1348f2e5..e635838a8a 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -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())) } @@ -603,6 +603,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 } @@ -631,7 +635,6 @@ func (rg *RouteGroup) handleDataPacket(packet routing.Packet) error { if rg.isRemoteClosed() { return nil } - rg.networkStats.AddBandwidthReceived(uint64(packet.Size())) select { case <-rg.closed: From db18f8a463a1127e3f789cba0055c3589a627a73 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Sat, 29 Jan 2022 18:38:40 +0700 Subject: [PATCH 9/9] route group latency unix nano --- pkg/router/route_group.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index e635838a8a..9c120db797 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -425,8 +425,7 @@ func (rg *RouteGroup) sendNetworkProbe() error { } throughput := rg.networkStats.RemoteThroughput() - timestamp := time.Now().UTC().UnixMilli() - + timestamp := time.Now().UTC().UnixNano() / int64(time.Millisecond) rg.networkStats.SetDownloadSpeed(uint32(throughput)) packet := routing.MakeNetworkProbePacket(rule.NextRouteID(), timestamp, throughput)