-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use openvpn3 reconnect #590
Changes from 3 commits
c8b12a2
e9948d7
13ab5f6
4b2c9cf
afb684c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,11 @@ | |
package mysterium | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/cihub/seelog" | ||
"github.com/mysteriumnetwork/go-openvpn/openvpn3" | ||
|
||
"github.com/mysteriumnetwork/node/consumer" | ||
"github.com/mysteriumnetwork/node/core/connection" | ||
"github.com/mysteriumnetwork/node/services/openvpn" | ||
|
@@ -85,8 +87,46 @@ func (adapter channelToCallbacksAdapter) OnStats(openvpnStats openvpn3.Statistic | |
// 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) { | ||
// ReconnectableSession interface exposing reconnect for an active session | ||
type ReconnectableSession interface { | ||
Reconnect(afterSeconds int) error | ||
} | ||
|
||
type sessionTracker struct { | ||
session *openvpn3.Session | ||
mux sync.Mutex | ||
} | ||
|
||
func (st *sessionTracker) sessionCreated(s *openvpn3.Session) { | ||
st.mux.Lock() | ||
st.session = s | ||
st.mux.Unlock() | ||
} | ||
|
||
// Reconnect reconnects active session after given time | ||
func (st *sessionTracker) Reconnect(afterSeconds int) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can use here golang native There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C.reconnect accepts int, so no conversion is needed. From mobile frameworks, more straight type is int too. |
||
if st.session == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we protect the reading of
zolia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errors.New("session not created yet") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we return here(when the st.session is null, for example), the st.mux will never unlock now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point. Fixed. |
||
} | ||
|
||
return st.session.Reconnect(afterSeconds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this can be run in a multithread way, |
||
} | ||
|
||
func (st *sessionTracker) handleState(stateEvent connection.StateEvent) { | ||
// On disconnected - remove session | ||
if stateEvent.State == connection.Disconnecting { | ||
st.session = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we protect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrapped
zolia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
// OverrideOpenvpnConnection replaces default openvpn connection factory with mobile related one returning session that can be reconnected | ||
func (mobNode *MobileNode) OverrideOpenvpnConnection(tunnelSetup Openvpn3TunnelSetup) ReconnectableSession { | ||
openvpn.Bootstrap() | ||
|
||
zolia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
st := &sessionTracker{} | ||
|
||
mobNode.di.EventBus.Subscribe(connection.StateEventTopic, st.handleState) | ||
|
||
mobNode.di.ConnectionRegistry.Register("openvpn", func(options connection.ConnectOptions, stateChannel connection.StateChannel, statisticsChannel connection.StatisticsChannel) (connection.Connection, error) { | ||
|
||
vpnClientConfig, err := openvpn.NewClientConfigFromSession(options.SessionConfig, "", "") | ||
|
@@ -117,9 +157,11 @@ func (mobNode *MobileNode) OverrideOpenvpnConnection(tunnelSetup Openvpn3TunnelS | |
} | ||
|
||
session := openvpn3.NewMobileSession(config, credentials, channelToCallbacks(stateChannel, statisticsChannel), tunnelSetup) | ||
st.sessionCreated(session) | ||
|
||
return &sessionWrapper{ | ||
session: session, | ||
}, nil | ||
}) | ||
return st | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reference is being set/read from possibly different threads. Some kind of sync mechanism is needed, like Mutex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, added mutex