@@ -30,6 +30,7 @@ public class Tun2Socks extends CordovaPlugin {
3030 public static final int RESULT_OK = -1 ;
3131
3232 private String m_socksServerAddress ;
33+ private CallbackContext m_onStartCallback = null ;
3334 private CallbackContext m_onDisconnectCallback = null ;
3435
3536 @ Override
@@ -48,7 +49,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
4849 // from onActivityResult.
4950 m_socksServerAddress = args .getString (0 );
5051 Log .i (LOG_TAG , "Got socks server address: " + m_socksServerAddress );
51- prepareAndStartTunnelService (callbackContext );
52+ m_onStartCallback = callbackContext ;
53+ prepareAndStartTunnelService ();
5254 }
5355 return true ;
5456 } else if (action .equals (STOP_ACTION )) {
@@ -76,10 +78,12 @@ protected void pluginInitialize() {
7678 return ;
7779 }
7880
81+ IntentFilter broadcastFilter =
82+ new IntentFilter (TunnelVpnService .TUNNEL_VPN_DISCONNECT_BROADCAST );
83+ broadcastFilter .addAction (TunnelVpnService .TUNNEL_VPN_START_BROADCAST );
84+
7985 LocalBroadcastManager .getInstance (getBaseContext ())
80- .registerReceiver (
81- m_disconnectBroadcastReceiver ,
82- new IntentFilter (TunnelVpnService .TUNNEL_VPN_DISCONNECT_BROADCAST ));
86+ .registerReceiver (m_vpnTunnelBroadcastReceiver , broadcastFilter );
8387 }
8488
8589 @ Override
@@ -89,16 +93,15 @@ public void onDestroy() {
8993 stopTunnelService ();
9094 }
9195
92- protected void prepareAndStartTunnelService (CallbackContext callbackContext ) {
96+ protected void prepareAndStartTunnelService () {
9397 Log .d (LOG_TAG , "Starting tun2socks..." );
9498 if (hasVpnService ()) {
9599 if (prepareVpnService ()) {
96100 startTunnelService (getBaseContext ());
97101 }
98- callbackContext .success ("Started tun2socks" );
99102 } else {
100103 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" );
102105 }
103106 }
104107
@@ -127,13 +130,16 @@ protected boolean prepareVpnService() throws ActivityNotFoundException {
127130 public void onActivityResult (int request , int result , Intent data ) {
128131 if (request == REQUEST_CODE_PREPARE_VPN && result == RESULT_OK ) {
129132 startTunnelService (getBaseContext ());
133+ } else {
134+ Log .e (LOG_TAG , "failed to prepare VPN" );
135+ m_onStartCallback .error ("Failed to start tun2socks" );
130136 }
131137 }
132138
133139 protected void startTunnelService (Context context ) {
134140 Log .i (LOG_TAG , "starting tunnel service" );
135141 if (isServiceRunning ()) {
136- Log .w (LOG_TAG , "already running service" );
142+ Log .d (LOG_TAG , "already running service" );
137143 TunnelManager tunnelManager = TunnelState .getTunnelState ().getTunnelManager ();
138144 if (tunnelManager != null ) {
139145 tunnelManager .restartTunnel (m_socksServerAddress );
@@ -172,6 +178,19 @@ private Context getBaseContext() {
172178 return this .cordova .getActivity ().getApplicationContext ();
173179 }
174180
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+
175194 public void onDisconnect () {
176195 if (m_onDisconnectCallback != null ) {
177196 PluginResult result = new PluginResult (PluginResult .Status .OK );
@@ -180,20 +199,27 @@ public void onDisconnect() {
180199 }
181200 }
182201
183- private DisconnectBroadcastReceiver m_disconnectBroadcastReceiver =
184- new DisconnectBroadcastReceiver (Tun2Socks .this );
202+ private VpnTunnelBroadcastReceiver m_vpnTunnelBroadcastReceiver =
203+ new VpnTunnelBroadcastReceiver (Tun2Socks .this );
185204
186- private class DisconnectBroadcastReceiver extends BroadcastReceiver {
205+ private class VpnTunnelBroadcastReceiver extends BroadcastReceiver {
187206 private Tun2Socks m_handler ;
188207
189- public DisconnectBroadcastReceiver (Tun2Socks handler ) {
208+ public VpnTunnelBroadcastReceiver (Tun2Socks handler ) {
190209 m_handler = handler ;
191210 }
192211
193212 @ Override
194213 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+ }
197223 }
198224 };
199225}
0 commit comments