Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Commit

Permalink
Notifying clients about account status
Browse files Browse the repository at this point in the history
  • Loading branch information
stenya committed May 6, 2020
1 parent 6e62585 commit 976265a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200226051749-491c5fce7268 h1:fnuNgko6vrkrxuKfTMd+0eOz50ziv+Wi+t38KUT3j+E=
golang.org/x/net v0.0.0-20200226051749-491c5fce7268/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c h1:zJ0mtu4jCalhKg6Oaukv6iIkb+cOvDrajDH9DH46Q4M=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775 h1:TC0v2RSO1u2kn1ZugjrFXkRZAEaqMN/RW+OTZkBzmLE=
Expand Down
13 changes: 7 additions & 6 deletions protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ type Service interface {
err error)

SessionDelete() error
SessionStatus() (
RequestSessionStatus() (
apiCode int,
apiErrorMsg string,
sessionToken string,
accountInfo preferences.AccountStatus,
err error)

Expand Down Expand Up @@ -616,7 +617,6 @@ func (p *Protocol) processRequest(conn net.Conn, message string) {

// notify all clients about changed session status
p.notifyClients(p.createHelloResponse())

break

case "SessionDelete":
Expand All @@ -631,18 +631,19 @@ func (p *Protocol) processRequest(conn net.Conn, message string) {
p.notifyClients(p.createHelloResponse())
break

case "SessionStatus":
var resp types.SessionStatusResp
apiCode, apiErrMsg, accountInfo, err := p._service.SessionStatus()
case "AccountStatus":
var resp types.AccountStatusResp
apiCode, apiErrMsg, sessionToken, accountInfo, err := p._service.RequestSessionStatus()
if err != nil && apiCode == 0 {
// if apiCode == 0 - it is not API error. Sending error response
p.sendErrorResponse(conn, reqCmd, err)
break
}
// Sending session info
resp = types.SessionStatusResp{
resp = types.AccountStatusResp{
APIStatus: apiCode,
APIErrorMessage: apiErrMsg,
SessionToken: sessionToken,
Account: accountInfo}

// send response
Expand Down
12 changes: 12 additions & 0 deletions protocol/protocol_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"

"github.com/ivpn/desktop-app-daemon/protocol/types"
"github.com/ivpn/desktop-app-daemon/service/preferences"
"github.com/ivpn/desktop-app-daemon/version"
)

Expand All @@ -22,6 +23,17 @@ func (p *Protocol) OnServiceSessionChanged() {
p.notifyClients(&helloResp)
}

// OnAccountStatus - handler of account status info. Notifying clients.
func (p *Protocol) OnAccountStatus(sessionToken string, accountInfo preferences.AccountStatus) {
if len(sessionToken) == 0 {
return
}

p.notifyClients(&types.AccountStatusResp{
SessionToken: sessionToken,
Account: accountInfo})
}

// OnDNSChanged - DNS changed handler
func (p *Protocol) OnDNSChanged(dns net.IP) {
// notify all clients
Expand Down
4 changes: 2 additions & 2 deletions protocol/types/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ type SessionDelete struct {
CommandBase
}

// SessionStatus get session status
type SessionStatus struct {
// AccountStatus get account status
type AccountStatus struct {
CommandBase
}

Expand Down
5 changes: 3 additions & 2 deletions protocol/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ type SessionNewResp struct {
Account preferences.AccountStatus
}

// SessionStatusResp - information about created session (or error info)
type SessionStatusResp struct {
// AccountStatusResp - information about account status (or error info)
type AccountStatusResp struct {
CommandBase
APIStatus int
APIErrorMessage string
SessionToken string
Account preferences.AccountStatus
}

Expand Down
2 changes: 2 additions & 0 deletions service/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/ivpn/desktop-app-daemon/api/types"
"github.com/ivpn/desktop-app-daemon/service/preferences"
"github.com/ivpn/desktop-app-daemon/service/wgkeys"
)

Expand Down Expand Up @@ -34,6 +35,7 @@ type IWgKeysManager interface {
// IServiceEventsReceiver is the receiver for service events (normally, it is protocol object)
type IServiceEventsReceiver interface {
OnServiceSessionChanged()
OnAccountStatus(sessionToken string, account preferences.AccountStatus)
OnDNSChanged(dns net.IP)
OnKillSwitchStateChanged()
}
25 changes: 14 additions & 11 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *Service) init() error {
}

// Check session status (start as go-routine to do not block service initialisation)
go s.SessionStatus()
go s.RequestSessionStatus()
// Start session status checker
s.startSessionChecker()

Expand Down Expand Up @@ -304,7 +304,7 @@ func (s *Service) connect(vpnProc vpn.Process, manualDNS net.IP, firewallDuringC
}

// check session status each disconnection (asynchronously, in separate goroutine)
defer func() { go s.SessionStatus() }()
defer func() { go s.RequestSessionStatus() }()

s._connectMutex.Lock()
defer s._connectMutex.Unlock()
Expand Down Expand Up @@ -986,16 +986,17 @@ func (s *Service) logOut(needToDeleteOnBackend bool) error {
return nil
}

// SessionStatus receives session status
func (s *Service) SessionStatus() (
// RequestSessionStatus receives session status
func (s *Service) RequestSessionStatus() (
apiCode int,
apiErrorMsg string,
sessionToken string,
accountInfo preferences.AccountStatus,
err error) {

session := s.Preferences().Session
if session.IsLoggedIn() == false {
return apiCode, "", accountInfo, ErrorNotLoggedIn{}
return apiCode, "", "", accountInfo, ErrorNotLoggedIn{}
}

log.Info("Requesting session status...")
Expand All @@ -1007,7 +1008,7 @@ func (s *Service) SessionStatus() (
// It could happen that logout\login was performed during the session check
// Ignoring result if there is already a new session
log.Info("Ignoring requested session status result. Local session already changed.")
return apiCode, "", accountInfo, ErrorNotLoggedIn{}
return apiCode, "", "", accountInfo, ErrorNotLoggedIn{}
}

apiCode = 0
Expand All @@ -1025,22 +1026,24 @@ func (s *Service) SessionStatus() (
if err != nil {
// in case of other API error
if apiErr != nil {
return apiCode, apiErr.Message, accountInfo, err
return apiCode, apiErr.Message, "", accountInfo, err
}

// not API error
return apiCode, "", accountInfo, err
return apiCode, "", "", accountInfo, err
}

if stat == nil {
return apiCode, "", accountInfo, fmt.Errorf("unexpected error when creating requesting session status")
return apiCode, "", "", accountInfo, fmt.Errorf("unexpected error when creating requesting session status")
}

// get account status info
accountInfo = s.createAccountStatus(*stat)
// notify about account status
s._evtReceiver.OnAccountStatus(session.Session, accountInfo)

// success
return apiCode, "", accountInfo, nil
return apiCode, "", session.Session, accountInfo, nil
}

func (s *Service) createAccountStatus(apiResp types.ServiceStatusAPIResp) preferences.AccountStatus {
Expand Down Expand Up @@ -1084,7 +1087,7 @@ func (s *Service) startSessionChecker() {
}

// check status
s.SessionStatus()
s.RequestSessionStatus()

// if not logged-in - no sense to check status anymore
session := s.Preferences().Session
Expand Down

0 comments on commit 976265a

Please sign in to comment.