Skip to content

Commit

Permalink
Upgrade internal libraries (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
keliramu authored Nov 18, 2024
2 parents a9922f6 + 374b86c commit 2928bf1
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 122 deletions.
7 changes: 6 additions & 1 deletion cmd/daemon/events_moose.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"log"
"os"

"github.com/NordSecurity/nordvpn-linux/config"
Expand All @@ -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,
Expand All @@ -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
}
5 changes: 0 additions & 5 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 11 additions & 12 deletions contrib/patches/add_moose.diff
Original file line number Diff line number Diff line change
@@ -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/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
+)
+
// 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
)
Expand Down
40 changes: 16 additions & 24 deletions events/moose/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
104 changes: 35 additions & 69 deletions events/moose/moose.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "C"
import (
"errors"
"fmt"
"net/http"
"net/url"
"os/exec"
"slices"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -283,6 +271,8 @@ func (s *Subscriber) NotifyLogin(data events.DataAuthorization) error {
eventTrigger,
eventStatus,
moose.NordvpnappOptBoolNone,
-1,
nil,
)); err != nil {
return err
}
Expand Down Expand Up @@ -320,6 +310,8 @@ func (s *Subscriber) NotifyLogout(data events.DataAuthorization) error {
eventTrigger,
eventStatus,
moose.NordvpnappOptBoolNone,
-1,
nil,
)); err != nil {
return err
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions events/moose/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type notifyRequest func(
limits string,
offset string,
responseSummary string,
debugJson *string,
) uint32

func noSuchEndpoint(
Expand All @@ -36,6 +37,7 @@ func noSuchEndpoint(
limits string,
offset string,
responseSummary string,
debugJson *string,
) uint32 {
return 0
}
Expand Down
Loading

0 comments on commit 2928bf1

Please sign in to comment.