@@ -30,6 +30,7 @@ public class Tun2Socks extends CordovaPlugin {
30
30
public static final int RESULT_OK = -1 ;
31
31
32
32
private String m_socksServerAddress ;
33
+ private CallbackContext m_onStartCallback = null ;
33
34
private CallbackContext m_onDisconnectCallback = null ;
34
35
35
36
@ Override
@@ -48,7 +49,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
48
49
// from onActivityResult.
49
50
m_socksServerAddress = args .getString (0 );
50
51
Log .i (LOG_TAG , "Got socks server address: " + m_socksServerAddress );
51
- prepareAndStartTunnelService (callbackContext );
52
+ m_onStartCallback = callbackContext ;
53
+ prepareAndStartTunnelService ();
52
54
}
53
55
return true ;
54
56
} else if (action .equals (STOP_ACTION )) {
@@ -76,10 +78,12 @@ protected void pluginInitialize() {
76
78
return ;
77
79
}
78
80
81
+ IntentFilter broadcastFilter =
82
+ new IntentFilter (TunnelVpnService .TUNNEL_VPN_DISCONNECT_BROADCAST );
83
+ broadcastFilter .addAction (TunnelVpnService .TUNNEL_VPN_START_BROADCAST );
84
+
79
85
LocalBroadcastManager .getInstance (getBaseContext ())
80
- .registerReceiver (
81
- m_disconnectBroadcastReceiver ,
82
- new IntentFilter (TunnelVpnService .TUNNEL_VPN_DISCONNECT_BROADCAST ));
86
+ .registerReceiver (m_vpnTunnelBroadcastReceiver , broadcastFilter );
83
87
}
84
88
85
89
@ Override
@@ -89,16 +93,15 @@ public void onDestroy() {
89
93
stopTunnelService ();
90
94
}
91
95
92
- protected void prepareAndStartTunnelService (CallbackContext callbackContext ) {
96
+ protected void prepareAndStartTunnelService () {
93
97
Log .d (LOG_TAG , "Starting tun2socks..." );
94
98
if (hasVpnService ()) {
95
99
if (prepareVpnService ()) {
96
100
startTunnelService (getBaseContext ());
97
101
}
98
- callbackContext .success ("Started tun2socks" );
99
102
} else {
100
103
Log .e (LOG_TAG , "Device does not support whole device VPN mode." );
101
- callbackContext .error ("Failed to start tun2socks" );
104
+ m_onStartCallback .error ("Failed to start tun2socks" );
102
105
}
103
106
}
104
107
@@ -127,13 +130,16 @@ protected boolean prepareVpnService() throws ActivityNotFoundException {
127
130
public void onActivityResult (int request , int result , Intent data ) {
128
131
if (request == REQUEST_CODE_PREPARE_VPN && result == RESULT_OK ) {
129
132
startTunnelService (getBaseContext ());
133
+ } else {
134
+ Log .e (LOG_TAG , "failed to prepare VPN" );
135
+ m_onStartCallback .error ("Failed to start tun2socks" );
130
136
}
131
137
}
132
138
133
139
protected void startTunnelService (Context context ) {
134
140
Log .i (LOG_TAG , "starting tunnel service" );
135
141
if (isServiceRunning ()) {
136
- Log .w (LOG_TAG , "already running service" );
142
+ Log .d (LOG_TAG , "already running service" );
137
143
TunnelManager tunnelManager = TunnelState .getTunnelState ().getTunnelManager ();
138
144
if (tunnelManager != null ) {
139
145
tunnelManager .restartTunnel (m_socksServerAddress );
@@ -172,6 +178,19 @@ private Context getBaseContext() {
172
178
return this .cordova .getActivity ().getApplicationContext ();
173
179
}
174
180
181
+ public void onStartVpn (boolean success ) {
182
+ if (m_onStartCallback == null || m_onStartCallback .isFinished ()) {
183
+ Log .e (LOG_TAG , "failed to call on start callback" );
184
+ return ;
185
+ }
186
+
187
+ if (success ) {
188
+ m_onStartCallback .success ("Started tun2socks" );
189
+ } else {
190
+ m_onStartCallback .error ("Failed to start tun2socks" );
191
+ }
192
+ }
193
+
175
194
public void onDisconnect () {
176
195
if (m_onDisconnectCallback != null ) {
177
196
PluginResult result = new PluginResult (PluginResult .Status .OK );
@@ -180,20 +199,27 @@ public void onDisconnect() {
180
199
}
181
200
}
182
201
183
- private DisconnectBroadcastReceiver m_disconnectBroadcastReceiver =
184
- new DisconnectBroadcastReceiver (Tun2Socks .this );
202
+ private VpnTunnelBroadcastReceiver m_vpnTunnelBroadcastReceiver =
203
+ new VpnTunnelBroadcastReceiver (Tun2Socks .this );
185
204
186
- private class DisconnectBroadcastReceiver extends BroadcastReceiver {
205
+ private class VpnTunnelBroadcastReceiver extends BroadcastReceiver {
187
206
private Tun2Socks m_handler ;
188
207
189
- public DisconnectBroadcastReceiver (Tun2Socks handler ) {
208
+ public VpnTunnelBroadcastReceiver (Tun2Socks handler ) {
190
209
m_handler = handler ;
191
210
}
192
211
193
212
@ Override
194
213
public void onReceive (Context context , Intent intent ) {
195
- // Callback into handler so we can communicate the disconnect event to js.
196
- m_handler .onDisconnect ();
214
+ final String action = intent .getAction ();
215
+ if (TunnelVpnService .TUNNEL_VPN_START_BROADCAST .equals (action )) {
216
+ boolean startSuccess = intent .getBooleanExtra (
217
+ TunnelVpnService .TUNNEL_VPN_START_SUCCESS_EXTRA , true );
218
+ m_handler .onStartVpn (startSuccess );
219
+
220
+ } else if (TunnelVpnService .TUNNEL_VPN_DISCONNECT_BROADCAST .equals (action )) {
221
+ m_handler .onDisconnect ();
222
+ }
197
223
}
198
224
};
199
225
}
0 commit comments