From b61f85caca8d01252915ac989f8dc2b66cb850ed Mon Sep 17 00:00:00 2001 From: Savolro Date: Fri, 15 Nov 2024 15:33:29 +0200 Subject: [PATCH 1/2] Upgrade moose to v14 Signed-off-by: Savolro --- cmd/daemon/events_moose.go | 7 ++- cmd/daemon/main.go | 5 -- contrib/patches/add_moose.diff | 4 +- events/moose/errors.go | 40 +++++-------- events/moose/moose.go | 104 +++++++++++---------------------- events/moose/notify.go | 2 + lib-versions.env | 4 +- third-party/moose-events | 2 +- third-party/moose-worker | 2 +- 9 files changed, 65 insertions(+), 105 deletions(-) diff --git a/cmd/daemon/events_moose.go b/cmd/daemon/events_moose.go index 0806274c..54cb21e6 100644 --- a/cmd/daemon/events_moose.go +++ b/cmd/daemon/events_moose.go @@ -3,6 +3,7 @@ package main import ( + "log" "os" "github.com/NordSecurity/nordvpn-linux/config" @@ -27,7 +28,7 @@ func newAnalytics( logLevel = "debug" } _ = os.Setenv("MOOSE_LOG", logLevel) - return &moose.Subscriber{ + sub := &moose.Subscriber{ EventsDbPath: eventsDbPath, Config: fs, Version: ver, @@ -37,4 +38,8 @@ func newAnalytics( DeviceID: id, SubscriptionAPI: subAPI, } + if err := sub.Init(); err != nil { + log.Println(internal.ErrorPrefix, "MOOSE: Initialization error:", err) + } + return sub } diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index f3b06175..c687792e 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -283,11 +283,6 @@ func main() { deviceID := fmt.Sprintf("%x", sha256.Sum256([]byte(cfg.MachineID.String()+Salt))) analytics := newAnalytics(eventsDbPath, fsystem, defaultAPI, Version, Environment, deviceID) - if cfg.Analytics.Get() { - if err := analytics.Enable(); err != nil { - log.Println(internal.WarningPrefix, err) - } - } heartBeatSubject.Subscribe(analytics.NotifyHeartBeat) daemonEvents.Subscribe(analytics) daemonEvents.Service.Connect.Subscribe(loggerSubscriber.NotifyConnect) diff --git a/contrib/patches/add_moose.diff b/contrib/patches/add_moose.diff index df16e121..87438ef0 100644 --- a/contrib/patches/add_moose.diff +++ b/contrib/patches/add_moose.diff @@ -7,8 +7,8 @@ index 129d05f..369a0f8 100644 ) +replace ( -+ moose/events => ./third-party/moose-events/nordvpnapp/moosenordvpnappgo -+ moose/worker => ./third-party/moose-worker/mooseworkergo ++ moose/events => ./third-party/moose-events/moosenordvpnappgo/v14 ++ moose/worker => ./third-party/moose-worker/mooseworkergo/v14 +) + require ( diff --git a/events/moose/errors.go b/events/moose/errors.go index 55219047..92f02110 100644 --- a/events/moose/errors.go +++ b/events/moose/errors.go @@ -12,36 +12,28 @@ import ( moose "moose/events" ) -func (s *Subscriber) PostInit(errCode uint32) *moose.ListenerError { - switch errCode { - case 13: - log.Println(internal.WarningPrefix, "moose: init internal error") - case 12: - log.Println(internal.WarningPrefix, "moose: version mismatch") - case 11: - log.Println(internal.WarningPrefix, "moose: input error") - case 3: - log.Println(internal.WarningPrefix, "moose: already initialized") - case 0: - log.Println(internal.WarningPrefix, "moose: init was successful") +func (s *Subscriber) PostInit(initResult moose.InitResult, errCode int32, errMsg string) *moose.ListenerError { + switch initResult { + case moose.InitResultOkEmptyContext, + moose.InitResultOkExistingContext, + moose.InitResultOkAlreadyStarted: + log.Println(internal.InfoPrefix, "MOOSE: Initialization OK:", initResult) default: - log.Println(internal.WarningPrefix, "moose: unexpected init error:", errCode) + log.Printf("%s MOOSE: Initialization error: %d: %d: %s\n", + internal.ErrorPrefix, + initResult, + errCode, + errMsg, + ) } - return nil } -func (s *Subscriber) OnError(level uint32, code uint32, message string) *moose.ListenerError { - var prefix string - switch level { - case 2: - prefix = internal.ErrorPrefix - case 1: - prefix = internal.WarningPrefix +func (s *Subscriber) OnError(err moose.TrackerError, level uint32, code int32, msg string) *moose.ListenerError { + if internal.IsProdEnv(s.Environment) && level < 2 { + return nil } - - log.Println(prefix, message, code) - + log.Printf("%s MOOSE: %d: %d: %s", internal.ErrorPrefix, err, code, msg) return nil } diff --git a/events/moose/moose.go b/events/moose/moose.go index 5178bd2d..a9cd9c9b 100644 --- a/events/moose/moose.go +++ b/events/moose/moose.go @@ -14,7 +14,6 @@ import "C" import ( "errors" "fmt" - "net/http" "net/url" "os/exec" "slices" @@ -28,24 +27,12 @@ import ( "github.com/NordSecurity/nordvpn-linux/distro" "github.com/NordSecurity/nordvpn-linux/events" "github.com/NordSecurity/nordvpn-linux/internal" - "github.com/NordSecurity/nordvpn-linux/request" "github.com/NordSecurity/nordvpn-linux/snapconf" moose "moose/events" worker "moose/worker" ) -const ( - workerVersion = "8.2.0" - eventEncoding = "application/json" - eventEndpoint = "/app-events" - errCodeEventSendSuccess = 0 - errCodeEventSendDisabled = 1 - errCodeRequestCreationFailed = 2 - errCodeRequestDoFailed = 3 - errCodeResponseStatus = 4 -) - // Subscriber listen events, send to moose engine type Subscriber struct { EventsDbPath string @@ -72,7 +59,7 @@ func (s *Subscriber) Enable() error { return nil } s.enabled = true - return s.mooseInit() + return s.response(moose.MooseNordvpnappSetOptIn(true)) } // Disable moose analytics engine @@ -83,10 +70,7 @@ func (s *Subscriber) Disable() error { return nil } s.enabled = false - if err := s.response(uint32(worker.Stop())); err != nil { - return err - } - return s.response(moose.MooseNordvpnappDeinit()) + return s.response(moose.MooseNordvpnappSetOptIn(false)) } func (s *Subscriber) isEnabled() bool { @@ -95,8 +79,11 @@ func (s *Subscriber) isEnabled() bool { return s.enabled } -// mooseInit initializes moose libs -func (s *Subscriber) mooseInit() error { +// Init initializes moose libs. It has to be done before usage regardless of the enabled state. +// Disabled case should be handled by `set_opt_out` value. +func (s *Subscriber) Init() error { + s.mux.Lock() + defer s.mux.Unlock() var cfg config.Config if err := s.Config.Load(&cfg); err != nil { return err @@ -134,11 +121,13 @@ func (s *Subscriber) mooseInit() error { return fmt.Errorf("starting worker: %w", err) } + s.enabled = cfg.Analytics.Get() if err := s.response(moose.MooseNordvpnappInit( s.EventsDbPath, internal.IsProdEnv(s.Environment), s, s, + s.enabled, )); err != nil { if !strings.Contains(err.Error(), "moose: already initiated") { return fmt.Errorf("starting tracker: %w", err) @@ -244,8 +233,7 @@ func (s *Subscriber) NotifyVirtualLocation(data bool) error { } func (s *Subscriber) NotifyPostquantumVpn(data bool) error { - // TODO: for now using existing field to track PQ feature. Later to be added/used dedicated field. - return s.response(moose.NordvpnappSetContextApplicationNordvpnappConfigCurrentStateTechnologyMeta(fmt.Sprintf("%t", data))) + return s.response(moose.NordvpnappSetContextApplicationNordvpnappConfigUserPreferencesPostQuantumEnabledValue(data)) } func (s *Subscriber) NotifyIpv6(data bool) error { @@ -283,6 +271,8 @@ func (s *Subscriber) NotifyLogin(data events.DataAuthorization) error { eventTrigger, eventStatus, moose.NordvpnappOptBoolNone, + -1, + nil, )); err != nil { return err } @@ -320,6 +310,8 @@ func (s *Subscriber) NotifyLogout(data events.DataAuthorization) error { eventTrigger, eventStatus, moose.NordvpnappOptBoolNone, + -1, + nil, )); err != nil { return err } @@ -344,11 +336,12 @@ func (s *Subscriber) NotifyUiItemsClick(data events.UiItemsAction) error { itemType, data.ItemValue, data.FormReference, + nil, )) } func (s *Subscriber) NotifyHeartBeat(period time.Duration) error { - if err := s.response(moose.NordvpnappSendServiceQualityStatusHeartbeat(int32(period.Minutes()))); err != nil { + if err := s.response(moose.NordvpnappSendServiceQualityStatusHeartbeat(int32(period.Minutes()), nil)); err != nil { return err } if !s.initialHeartbeatSent { @@ -468,8 +461,9 @@ func (s *Subscriber) NotifyConnect(data events.DataConnect) error { int32(data.DurationMs), eventStatus, moose.NordvpnappEventTriggerUser, - int32(-1), - int32(-1), + -1, + -1, + nil, )) } else { var threatProtection moose.NordvpnappOptBool @@ -530,10 +524,12 @@ func (s *Subscriber) NotifyConnect(data events.DataConnect) error { data.TargetServerCity, protocol, technology, + moose.NordvpnappServerTypeNone, threatProtection, - int32(-1), + -1, "", - int32(-1), + -1, + nil, )); err != nil { return err } @@ -562,11 +558,12 @@ func (s *Subscriber) NotifyDisconnect(data events.DataDisconnect) error { if s.connectionToMeshnetPeer { if err := s.response(moose.NordvpnappSendServiceQualityServersDisconnectFromMeshnetDevice( - int32(-1), + -1, eventStatus, moose.NordvpnappEventTriggerUser, connectionTime, // seconds - int32(-1), + -1, + nil, )); err != nil { return err } @@ -616,7 +613,7 @@ func (s *Subscriber) NotifyDisconnect(data events.DataDisconnect) error { } if err := s.response(moose.NordvpnappSendServiceQualityServersDisconnect( - int32(-1), + -1, eventStatus, moose.NordvpnappEventTriggerUser, moose.NordvpnappVpnConnectionTriggerNone, // pass proper trigger @@ -630,10 +627,12 @@ func (s *Subscriber) NotifyDisconnect(data events.DataDisconnect) error { "", protocol, technology, + moose.NordvpnappServerTypeNone, threatProtection, connectionTime, // seconds "", - int32(-1), + -1, + nil, )); err != nil { return err } @@ -674,46 +673,10 @@ func (s *Subscriber) NotifyRequestAPI(data events.DataRequestAPI) error { "", "", "", + nil, )) } -// sendEvent is used as a https://go.dev/ref/spec#Method_values in order be able -// to handle changing domains without involving channels. -// -// called by moose worker for each event -func (s *Subscriber) sendEvent(contentType, userAgent, requestBody string) int { - if !s.isEnabled() { - return errCodeEventSendDisabled - } - s.mux.Lock() - domain := s.currentDomain - s.mux.Unlock() - req, err := request.NewRequest( - http.MethodPost, - userAgent, - domain, - eventEndpoint, - contentType, - fmt.Sprintf("%d", len(requestBody)), - eventEncoding, - strings.NewReader(requestBody), - ) - if err != nil { - return errCodeRequestCreationFailed - } - - // Moose team requested specific timeout value - client := request.NewStdHTTP(func(c *http.Client) { c.Timeout = time.Second * 30 }) - resp, err := client.Do(req) - if err != nil { - return errCodeRequestDoFailed - } - if resp.StatusCode >= 400 { - return errCodeResponseStatus - } - return errCodeEventSendSuccess -} - func (s *Subscriber) fetchSubscriptions() error { if !s.enabled { return nil @@ -935,7 +898,10 @@ func (s *Subscriber) updateEventDomain() error { if err != nil { return err } - domainUrl.Host = s.Subdomain + "." + domainUrl.Host + // TODO: Remove subdomain handling logic as it brings no value after domain rotation removal + if s.Subdomain != "" { + domainUrl.Host = s.Subdomain + "." + domainUrl.Host + } s.currentDomain = domainUrl.String() return nil } diff --git a/events/moose/notify.go b/events/moose/notify.go index 334fa4a7..061fec2a 100644 --- a/events/moose/notify.go +++ b/events/moose/notify.go @@ -21,6 +21,7 @@ type notifyRequest func( limits string, offset string, responseSummary string, + debugJson *string, ) uint32 func noSuchEndpoint( @@ -36,6 +37,7 @@ func noSuchEndpoint( limits string, offset string, responseSummary string, + debugJson *string, ) uint32 { return 0 } diff --git a/lib-versions.env b/lib-versions.env index 459cfe51..88db8625 100644 --- a/lib-versions.env +++ b/lib-versions.env @@ -5,5 +5,5 @@ # shellcheck disable=SC2034 # used in the build process later LIBTELIO_VERSION=v5.0.0 LIBDROP_VERSION=v8.1.1 -LIBMOOSE_NORDVPNAPP_VERSION=v12.0.0-nordVpnApp -LIBMOOSE_WORKER_VERSION=v10.0.4-worker +LIBMOOSE_NORDVPNAPP_VERSION=v14.2.0-nordVpnApp +LIBMOOSE_WORKER_VERSION=v14.1.4-worker diff --git a/third-party/moose-events b/third-party/moose-events index ada82f02..4cd7282d 160000 --- a/third-party/moose-events +++ b/third-party/moose-events @@ -1 +1 @@ -Subproject commit ada82f02694e483a506d5ff3231cb04f1318dcba +Subproject commit 4cd7282df0ebcfc797dbdd6d868678d693b4bb98 diff --git a/third-party/moose-worker b/third-party/moose-worker index be862bc1..2b828b31 160000 --- a/third-party/moose-worker +++ b/third-party/moose-worker @@ -1 +1 @@ -Subproject commit be862bc19000521b8466782dc04e33bc7bf444f2 +Subproject commit 2b828b31a77dc4127e949b42aabad0b46aaaa1cc From 374b86c7152efbc47ead0124371e3c44349c48e5 Mon Sep 17 00:00:00 2001 From: Savolro Date: Fri, 15 Nov 2024 15:50:32 +0200 Subject: [PATCH 2/2] Upgrade libtelio to v5.0.3 Signed-off-by: Savolro --- contrib/patches/add_moose.diff | 19 +++++++++---------- go.mod | 4 ++-- go.sum | 6 ++---- lib-versions.env | 2 +- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/contrib/patches/add_moose.diff b/contrib/patches/add_moose.diff index 87438ef0..696da0ce 100644 --- a/contrib/patches/add_moose.diff +++ b/contrib/patches/add_moose.diff @@ -1,23 +1,22 @@ diff --git a/go.mod b/go.mod -index 129d05f..369a0f8 100644 +index e1e88ce..9b6608d 100644 --- a/go.mod +++ b/go.mod -@@ -12,6 +12,11 @@ require ( - github.com/NordSecurity/libtelio-go/v5 v5.0.0 - ) +@@ -4,12 +4,19 @@ go 1.21.1 + + toolchain go1.22.2 +replace ( + moose/events => ./third-party/moose-events/moosenordvpnappgo/v14 + moose/worker => ./third-party/moose-worker/mooseworkergo/v14 +) + + // Bindings + // NOTE: If you are chaning the binding versions here, keep in mind that you + // may also need to update versions in `./lib-versions.env` file. require ( - github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4 - github.com/NordSecurity/systray v0.0.0-20240327004800-3e3b59c1b83d -@@ -50,6 +55,8 @@ require ( - google.golang.org/grpc v1.64.1 - google.golang.org/protobuf v1.34.2 - gopkg.in/natefinch/lumberjack.v2 v2.2.1 + github.com/NordSecurity/libdrop-go/v8 v8.0.0-20241017064027-670787595588 + github.com/NordSecurity/libtelio-go/v5 v5.0.3 + moose/events v0.0.0 + moose/worker v0.0.0 ) diff --git a/go.mod b/go.mod index ca10abfb..e1e88ce4 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ toolchain go1.22.2 // may also need to update versions in `./lib-versions.env` file. require ( github.com/NordSecurity/libdrop-go/v8 v8.0.0-20241017064027-670787595588 - github.com/NordSecurity/libtelio-go/v5 v5.0.0 + github.com/NordSecurity/libtelio-go/v5 v5.0.3 ) require ( @@ -50,6 +50,7 @@ require ( google.golang.org/grpc v1.64.1 google.golang.org/protobuf v1.34.2 gopkg.in/natefinch/lumberjack.v2 v2.2.1 + gotest.tools/v3 v3.4.0 ) require ( @@ -105,5 +106,4 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - gotest.tools/v3 v3.4.0 // indirect ) diff --git a/go.sum b/go.sum index d87291a4..b6b14697 100644 --- a/go.sum +++ b/go.sum @@ -10,12 +10,10 @@ github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2y github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE= github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4 h1:2ozEjYEw4WzXAXe/t5rxnvcFytR8z/PA/Xrv3FpByng= github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4/go.mod h1:tguhorMSnkMcQExNIHWBX6TRhFeGYlERzbeAWZ4j9Uw= -github.com/NordSecurity/libdrop-go/v7 v7.0.0-rc1 h1:YnMQXlt/nG4Gy1MX7uAhU3OdaDPWW3aCiAGTkUSJ7Cc= -github.com/NordSecurity/libdrop-go/v7 v7.0.0-rc1/go.mod h1:RtQP2i5NfKk5kcTnT2kDJ7s6Um2pgIoudXFZ8SLYREc= github.com/NordSecurity/libdrop-go/v8 v8.0.0-20241017064027-670787595588 h1:L/nAbQXJGCOFqw1eTTRYEBKmiuaQQeS7b863+0Ifevw= github.com/NordSecurity/libdrop-go/v8 v8.0.0-20241017064027-670787595588/go.mod h1:SRYI0D0K6hSMBskvcB2/t/5ktSTNLPGbOvLaQ5p/sAE= -github.com/NordSecurity/libtelio-go/v5 v5.0.0 h1:MEd8XjgPp9YlTiG3ed40aiIcob2p0jaOpOTvDAWeAYc= -github.com/NordSecurity/libtelio-go/v5 v5.0.0/go.mod h1:mnoTGgXOu8dBQgPxG8MBju4d9C+ljKIT2p8OX5GFom4= +github.com/NordSecurity/libtelio-go/v5 v5.0.3 h1:ofM4yfK0kvP5Dm6rCZn9lvQyuFbBfNv6DPEgULrR8KQ= +github.com/NordSecurity/libtelio-go/v5 v5.0.3/go.mod h1:mnoTGgXOu8dBQgPxG8MBju4d9C+ljKIT2p8OX5GFom4= github.com/NordSecurity/systray v0.0.0-20240327004800-3e3b59c1b83d h1:oUEFXgFRa9Svcjr+O1stzR3vEXZ5OfQxLUcDjqFcOuo= github.com/NordSecurity/systray v0.0.0-20240327004800-3e3b59c1b83d/go.mod h1:PqJ9YgQXNUYUuoLvVrbMmZACufI5xM8JJ5aSuRX5ljg= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= diff --git a/lib-versions.env b/lib-versions.env index 88db8625..c9b298ed 100644 --- a/lib-versions.env +++ b/lib-versions.env @@ -3,7 +3,7 @@ # `./go.mod` file as well # shellcheck disable=SC2034 # used in the build process later -LIBTELIO_VERSION=v5.0.0 +LIBTELIO_VERSION=v5.0.3 LIBDROP_VERSION=v8.1.1 LIBMOOSE_NORDVPNAPP_VERSION=v14.2.0-nordVpnApp LIBMOOSE_WORKER_VERSION=v14.1.4-worker