diff --git a/mobile/mysterium/entrypoint.go b/mobile/mysterium/entrypoint.go index fa8cb3a8fc..e4be12a8b4 100644 --- a/mobile/mysterium/entrypoint.go +++ b/mobile/mysterium/entrypoint.go @@ -21,13 +21,9 @@ import ( "path/filepath" "github.com/mitchellh/go-homedir" - "github.com/mysteriumnetwork/go-openvpn/openvpn3" "github.com/mysteriumnetwork/node/cmd" - "github.com/mysteriumnetwork/node/core/connection" "github.com/mysteriumnetwork/node/core/node" "github.com/mysteriumnetwork/node/metadata" - "github.com/mysteriumnetwork/node/services/openvpn" - "github.com/mysteriumnetwork/node/services/openvpn/session" ) // MobileNode represents node object tuned for mobile devices @@ -38,11 +34,8 @@ type MobileNode struct { // MobileNetworkOptions alias for node.OptionsNetwork to be visible from mobile framework type MobileNetworkOptions node.OptionsNetwork -// Openvpn3TunnelSetup is alias for openvpn3 tunnel setup interface exposed to Android/iOS interop -type Openvpn3TunnelSetup openvpn3.TunnelSetup - // NewNode function creates new Node -func NewNode(appPath string, optionsNetwork *MobileNetworkOptions, tunnelSetup Openvpn3TunnelSetup) (*MobileNode, error) { +func NewNode(appPath string, optionsNetwork *MobileNetworkOptions) (*MobileNode, error) { var di cmd.Dependencies var dataDir, currentDir string @@ -81,41 +74,6 @@ func NewNode(appPath string, optionsNetwork *MobileNetworkOptions, tunnelSetup O return nil, err } - di.ConnectionRegistry.Register("openvpn", func(options connection.ConnectOptions, channel connection.StateChannel) (connection.Connection, error) { - - vpnClientConfig, err := openvpn.NewClientConfigFromSession(options.SessionConfig, "", "") - if err != nil { - return nil, err - } - - profileContent, err := vpnClientConfig.ToConfigFileContent() - if err != nil { - return nil, err - } - - config := openvpn3.NewConfig(profileContent) - config.GuiVersion = "govpn 0.1" - config.CompressionMode = "asym" - - signer := di.SignerFactory(options.ConsumerID) - - username, password, err := session.SignatureCredentialsProvider(options.SessionID, signer)() - if err != nil { - return nil, err - } - - credentials := openvpn3.UserCredentials{ - Username: username, - Password: password, - } - - session := openvpn3.NewMobileSession(config, credentials, channelToCallbacks(channel, di.StatsKeeper), tunnelSetup) - - return &sessionWrapper{ - session: session, - }, nil - }) - return &MobileNode{di: di}, nil } diff --git a/mobile/mysterium/callbacks_adapter.go b/mobile/mysterium/openvpn_connection_setup.go similarity index 52% rename from mobile/mysterium/callbacks_adapter.go rename to mobile/mysterium/openvpn_connection_setup.go index 5ca0e228a1..4edc5627bc 100644 --- a/mobile/mysterium/callbacks_adapter.go +++ b/mobile/mysterium/openvpn_connection_setup.go @@ -22,8 +22,28 @@ import ( "github.com/mysteriumnetwork/go-openvpn/openvpn3" "github.com/mysteriumnetwork/node/client/stats" "github.com/mysteriumnetwork/node/core/connection" + "github.com/mysteriumnetwork/node/services/openvpn" + "github.com/mysteriumnetwork/node/services/openvpn/session" ) +type sessionWrapper struct { + session *openvpn3.Session +} + +func (wrapper *sessionWrapper) Start() error { + + wrapper.session.Start() + return nil +} + +func (wrapper *sessionWrapper) Stop() { + wrapper.session.Stop() +} + +func (wrapper *sessionWrapper) Wait() error { + return wrapper.session.Wait() +} + type statsUpdater interface { Save(stats stats.SessionStats) } @@ -65,3 +85,45 @@ func (adapter channelToCallbacksAdapter) OnStats(openvpnStats openvpn3.Statistic BytesReceived: uint64(openvpnStats.BytesIn), }) } + +// Openvpn3TunnelSetup is alias for openvpn3 tunnel setup interface exposed to Android/iOS interop +type Openvpn3TunnelSetup openvpn3.TunnelSetup + +// OverrideOpenvpnConnection replaces default openvpn connection factory with mobile related one +func (mobNode *MobileNode) OverrideOpenvpnConnection(tunnelSetup Openvpn3TunnelSetup) { + mobNode.di.ConnectionRegistry.Register("openvpn", func(options connection.ConnectOptions, channel connection.StateChannel) (connection.Connection, error) { + + vpnClientConfig, err := openvpn.NewClientConfigFromSession(options.SessionConfig, "", "") + if err != nil { + return nil, err + } + + profileContent, err := vpnClientConfig.ToConfigFileContent() + if err != nil { + return nil, err + } + + config := openvpn3.NewConfig(profileContent) + config.GuiVersion = "govpn 0.1" + config.CompressionMode = "asym" + config.ConnTimeout = 0 //essentially means - reconnect forever (but can always stopped with disconnect) + + signer := mobNode.di.SignerFactory(options.ConsumerID) + + username, password, err := session.SignatureCredentialsProvider(options.SessionID, signer)() + if err != nil { + return nil, err + } + + credentials := openvpn3.UserCredentials{ + Username: username, + Password: password, + } + + session := openvpn3.NewMobileSession(config, credentials, channelToCallbacks(channel, mobNode.di.StatsKeeper), tunnelSetup) + + return &sessionWrapper{ + session: session, + }, nil + }) +} diff --git a/mobile/mysterium/wireguard_connection_setup.go b/mobile/mysterium/wireguard_connection_setup.go new file mode 100644 index 0000000000..6abb745c28 --- /dev/null +++ b/mobile/mysterium/wireguard_connection_setup.go @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2018 The "MysteriumNetwork/node" Authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package mysterium + +import ( + "errors" + + "github.com/mysteriumnetwork/node/core/connection" +) + +// TODO this probably can be aligned with openvpn3 setup interface as its very close to android api, but for sake of +// package independencies we are not reusing the same interface although implementation is rougly the same +type tunnSetupPlaceholder interface { + Establish() int +} + +// WireguardTunnelSetup represents interface for setuping tunnel on caller side (i.e. android api) +type WireguardTunnelSetup tunnSetupPlaceholder + +// OverrideWireguardConnection overrides default wireguard connection implementation to more mobile adapted one +func (mobNode *MobileNode) OverrideWireguardConnection(wgTunnelSetup WireguardTunnelSetup) { + mobNode.di.ConnectionRegistry.Register("wireguard", func(options connection.ConnectOptions, channel connection.StateChannel) (connection.Connection, error) { + return nil, errors.New("not implemented yet") + }) +} diff --git a/mobile/mysterium/wrapper.go b/mobile/mysterium/wrapper.go deleted file mode 100644 index 4c261fcb3b..0000000000 --- a/mobile/mysterium/wrapper.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2018 The "MysteriumNetwork/node" Authors. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package mysterium - -import "github.com/mysteriumnetwork/go-openvpn/openvpn3" - -type sessionWrapper struct { - session *openvpn3.Session -} - -func (wrapper *sessionWrapper) Start() error { - - wrapper.session.Start() - return nil -} - -func (wrapper *sessionWrapper) Stop() { - wrapper.session.Stop() -} - -func (wrapper *sessionWrapper) Wait() error { - return wrapper.session.Wait() -}