Skip to content

Commit

Permalink
Merge pull request #612 from mysteriumnetwork/cleanup-transport-on-de…
Browse files Browse the repository at this point in the history
…stroy

Added destroy callback for transport services
  • Loading branch information
soffokl authored Dec 14, 2018
2 parents 28b23e8 + 100a85a commit 7749a27
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions services/noop/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type Manager struct {
type negotiator struct {
}

func (n *negotiator) ProvideConfig(cfg json.RawMessage) (session.ServiceConfiguration, error) {
return nil, nil
func (n *negotiator) ProvideConfig(cfg json.RawMessage) (session.ServiceConfiguration, session.DestroyCallback, error) {
return nil, nil, nil
}

// Start starts service - does not block
Expand Down
2 changes: 1 addition & 1 deletion services/noop/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Test_Manager_Start(t *testing.T) {
proposal,
)

sessionConfig, err := sessionConfigProvider.ProvideConfig(nil)
sessionConfig, _, err := sessionConfigProvider.ProvideConfig(nil)
assert.NoError(t, err)
assert.Nil(t, sessionConfig)
}
Expand Down
4 changes: 2 additions & 2 deletions services/openvpn/service/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ type OpenvpnConfigNegotiator struct {
}

// ProvideConfig returns the config for user
func (ocn *OpenvpnConfigNegotiator) ProvideConfig(json.RawMessage) (session.ServiceConfiguration, error) {
return &ocn.vpnConfig, nil
func (ocn *OpenvpnConfigNegotiator) ProvideConfig(json.RawMessage) (session.ServiceConfiguration, session.DestroyCallback, error) {
return &ocn.vpnConfig, nil, nil
}

func vpnServerIP(serviceOptions Options, outboundIP, publicIP string) string {
Expand Down
23 changes: 9 additions & 14 deletions services/wireguard/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,50 +61,45 @@ type Manager struct {
}

// ProvideConfig provides the config for consumer
func (manager *Manager) ProvideConfig(publicKey json.RawMessage) (session.ServiceConfiguration, error) {
func (manager *Manager) ProvideConfig(publicKey json.RawMessage) (session.ServiceConfiguration, session.DestroyCallback, error) {
key := &wg.ConsumerConfig{}
err := json.Unmarshal(publicKey, key)
if err != nil {
return nil, err
return nil, nil, err
}

connectionEndpoint, err := manager.connectionEndpointFactory()
if err != nil {
return nil, err
return nil, nil, err
}

if err := connectionEndpoint.Start(nil); err != nil {
return nil, err
return nil, nil, err
}

if err := connectionEndpoint.AddPeer(key.PublicKey, nil); err != nil {
return nil, err
return nil, nil, err
}

config, err := connectionEndpoint.Config()
if err != nil {
return nil, err
return nil, nil, err
}

outboundIP, err := manager.ipResolver.GetOutboundIP()
if err != nil {
return nil, err
return nil, nil, err
}

manager.natService.Add(nat.RuleForwarding{
SourceAddress: config.Consumer.IPAddress.String(),
TargetIP: outboundIP,
})
if err := manager.natService.Start(); err != nil {
return nil, err
return nil, nil, err
}

return config, nil
}

// ConsumeConfig takes in the provided config and adds it to the wg device
func (manager *Manager) ConsumeConfig() error {
return nil
return config, connectionEndpoint.Stop, nil
}

// Start starts service - does not block
Expand Down
2 changes: 1 addition & 1 deletion services/wireguard/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_Manager_Start(t *testing.T) {
},
proposal,
)
sessionConfig, err := sessionConfigProvider.ProvideConfig(json.RawMessage(`{"PublicKey": "gZfkZArbw9lqfl4Yzr1Kv3nqGlhe/ynH9KKRbzPFMGk="}`))
sessionConfig, _, err := sessionConfigProvider.ProvideConfig(json.RawMessage(`{"PublicKey": "gZfkZArbw9lqfl4Yzr1Kv3nqGlhe/ynH9KKRbzPFMGk="}`))
assert.NoError(t, err)
assert.NotNil(t, sessionConfig)
}
Expand Down
6 changes: 3 additions & 3 deletions session/create_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type createConsumer struct {

// Creator defines method for session creation
type Creator interface {
Create(consumerID identity.Identity, proposalID int, config ServiceConfiguration) (Session, error)
Create(consumerID identity.Identity, proposalID int, config ServiceConfiguration, destroyCallback DestroyCallback) (Session, error)
}

// GetMessageEndpoint returns endpoint there to receive messages
Expand All @@ -51,12 +51,12 @@ func (consumer *createConsumer) NewRequest() (requestPtr interface{}) {
func (consumer *createConsumer) Consume(requestPtr interface{}) (response interface{}, err error) {
request := requestPtr.(*CreateRequest)

config, err := consumer.configProvider(request.Config)
config, destroyCallback, err := consumer.configProvider(request.Config)
if err != nil {
return responseInternalError, err
}

sessionInstance, err := consumer.sessionCreator.Create(consumer.peerID, request.ProposalId, config)
sessionInstance, err := consumer.sessionCreator.Create(consumer.peerID, request.ProposalId, config, destroyCallback)
switch err {
case nil:
return responseWithSession(sessionInstance), nil
Expand Down
6 changes: 3 additions & 3 deletions session/create_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
)

var (
mockConsumer = func(json.RawMessage) (ServiceConfiguration, error) {
return nil, nil
mockConsumer = func(json.RawMessage) (ServiceConfiguration, DestroyCallback, error) {
return nil, nil, nil
}
)

Expand Down Expand Up @@ -107,7 +107,7 @@ type managerFake struct {
}

// Create function creates and returns fake session
func (manager *managerFake) Create(consumerID identity.Identity, proposalID int, config ServiceConfiguration) (Session, error) {
func (manager *managerFake) Create(consumerID identity.Identity, proposalID int, config ServiceConfiguration, destroyCallback DestroyCallback) (Session, error) {
manager.lastConsumerID = consumerID
manager.lastProposalID = proposalID
return manager.returnSession, manager.returnError
Expand Down
7 changes: 4 additions & 3 deletions session/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ type ID string

// Session structure holds all required information about current session between service consumer and provider
type Session struct {
ID ID
Config ServiceConfiguration
ConsumerID identity.Identity
ID ID
Config ServiceConfiguration
ConsumerID identity.Identity
DestroyCallback DestroyCallback
}

// ServiceConfiguration defines service configuration from underlying transport mechanism to be passed to remote party
Expand Down
14 changes: 9 additions & 5 deletions session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ type IDGenerator func() (ID, error)

// ConfigNegotiator is able to handle config negotiations
type ConfigNegotiator interface {
ProvideConfig(consumerKey json.RawMessage) (ServiceConfiguration, error)
ProvideConfig(consumerKey json.RawMessage) (ServiceConfiguration, DestroyCallback, error)
}

// ConfigProvider provides session config for remote client
type ConfigProvider func(consumerKey json.RawMessage) (ServiceConfiguration, error)
type ConfigProvider func(consumerKey json.RawMessage) (ServiceConfiguration, DestroyCallback, error)

// SaveCallback stores newly started sessions
type SaveCallback func(Session)
// DestroyCallback cleanups session
type DestroyCallback func() error

// PromiseProcessor processes promises at provider side.
// Provider checks promises from consumer and signs them also.
Expand Down Expand Up @@ -93,7 +93,7 @@ type Manager struct {
}

// Create creates session instance. Multiple sessions per peerID is possible in case different services are used
func (manager *Manager) Create(consumerID identity.Identity, proposalID int, config ServiceConfiguration) (sessionInstance Session, err error) {
func (manager *Manager) Create(consumerID identity.Identity, proposalID int, config ServiceConfiguration, destroyCallback DestroyCallback) (sessionInstance Session, err error) {
manager.creationLock.Lock()
defer manager.creationLock.Unlock()

Expand All @@ -112,6 +112,7 @@ func (manager *Manager) Create(consumerID identity.Identity, proposalID int, con
return
}

sessionInstance.DestroyCallback = destroyCallback
manager.sessionStorage.Add(sessionInstance)
return sessionInstance, nil
}
Expand All @@ -138,6 +139,9 @@ func (manager *Manager) Destroy(consumerID identity.Identity, sessionID string)

manager.sessionStorage.Remove(ID(sessionID))

if sessionInstance.DestroyCallback != nil {
return sessionInstance.DestroyCallback()
}
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions session/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestManager_Create_StoresSession(t *testing.T) {
sessionStore := NewStorageMemory()
manager := NewManager(currentProposal, generateSessionID, sessionStore, &fakePromiseProcessor{})

sessionInstance, err := manager.Create(identity.FromAddress("deadbeef"), currentProposalID, expectedSessionConfig)
sessionInstance, err := manager.Create(identity.FromAddress("deadbeef"), currentProposalID, expectedSessionConfig, nil)
assert.NoError(t, err)
assert.Exactly(t, expectedSession, sessionInstance)
}
Expand All @@ -74,7 +74,7 @@ func TestManager_Create_RejectsUnknownProposal(t *testing.T) {
sessionStore := NewStorageMemory()
manager := NewManager(currentProposal, generateSessionID, sessionStore, &fakePromiseProcessor{})

sessionInstance, err := manager.Create(identity.FromAddress("deadbeef"), 69, expectedSessionConfig)
sessionInstance, err := manager.Create(identity.FromAddress("deadbeef"), 69, expectedSessionConfig, nil)
assert.Exactly(t, err, ErrorInvalidProposal)
assert.Exactly(t, Session{}, sessionInstance)
}
Expand All @@ -84,7 +84,7 @@ func TestManager_Create_StartsPromiseProcessor(t *testing.T) {
sessionStore := NewStorageMemory()
manager := NewManager(currentProposal, generateSessionID, sessionStore, promiseProcessor)

_, err := manager.Create(identity.FromAddress("deadbeef"), currentProposalID, expectedSessionConfig)
_, err := manager.Create(identity.FromAddress("deadbeef"), currentProposalID, expectedSessionConfig, nil)
assert.NoError(t, err)
assert.True(t, promiseProcessor.started)
assert.Exactly(t, currentProposal, promiseProcessor.proposal)
Expand Down

0 comments on commit 7749a27

Please sign in to comment.