Skip to content

Commit

Permalink
fix no network after exit in vpn mode #126
Browse files Browse the repository at this point in the history
  • Loading branch information
XndroidDev committed Oct 1, 2018
1 parent 6bc9bc1 commit b93e9ad
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "net.xndroid"
minSdkVersion 14
targetSdkVersion 23
versionCode 24
versionName "1.2.3"
versionCode 25
versionName "1.2.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/net/xndroid/AppModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public static void appInit(Context context){
sEnableXXNet = sPreferences.getBoolean(PRE_ENABLE_XXNET, true);
sEnableFqDNS = sPreferences.getBoolean(PRE_ENABLE_FQDNS, true);
sEnableTeredo = sPreferences.getBoolean(PRE_ENABLE_TEREDO, true);
sAutoTeredo = sPreferences.getBoolean(PRE_AUTO_TEREDO, true);
sAutoTeredo = sPreferences.getBoolean(PRE_AUTO_TEREDO, false);
sEnableNotification = sPreferences.getBoolean(PRE_ENABLE_NOTIFICATION, true);
sAutoStart = sPreferences.getBoolean(PRE_AUTO_START, false);

Expand Down
18 changes: 5 additions & 13 deletions app/src/main/java/net/xndroid/AutoStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import static net.xndroid.AppModel.sContext;

public class AutoStart extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
SharedPreferences preferences = context.getSharedPreferences("AutoStart", ContextWrapper.MODE_PRIVATE);

if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
//if (preferences.getBoolean("AddToAuto", false)) {
if (true){
if (PreferenceManager.getDefaultSharedPreferences(sContext)
.getBoolean(AppModel.PRE_AUTO_START, false)){
AppModel.appInit(context);

// Intent newIntent = context.getPackageManager()
// .getLaunchIntentForPackage("net.xndroid");
// newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// newIntent.putExtra("auto_start", true);
// context.startActivity(newIntent);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/net/xndroid/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private void selectComponent(){
boolean enableXXnet = AppModel.sPreferences.getBoolean(AppModel.PRE_ENABLE_XXNET, true);
boolean enableFqDNS = AppModel.sPreferences.getBoolean(AppModel.PRE_ENABLE_FQDNS, true);
boolean enableTeredo = AppModel.sPreferences.getBoolean(AppModel.PRE_ENABLE_TEREDO, true);
boolean autoTeredo = AppModel.sPreferences.getBoolean(AppModel.PRE_AUTO_TEREDO, true);
boolean autoTeredo = AppModel.sPreferences.getBoolean(AppModel.PRE_AUTO_TEREDO, false);
boolean enableNotification = AppModel.sPreferences.getBoolean(AppModel.PRE_ENABLE_NOTIFICATION, true);
boolean autoStart = AppModel.sPreferences.getBoolean(AppModel.PRE_AUTO_START, false);

Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/net/xndroid/fqrouter/FqrouterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.VpnService;
import android.os.Build;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import net.xndroid.AppModel;
Expand Down Expand Up @@ -36,7 +43,7 @@ public class FqrouterManager {
public static final int ASK_VPN_PERMISSION = 101;
public static boolean sRequestApproved = false;
private static Process mProcess;

protected static Messenger sMessenger;
public static boolean sIsQualified = false;
public static String sNATType = "UNKNOW";
public static String sTeredoIP = "UNKNOW";
Expand Down Expand Up @@ -128,7 +135,19 @@ public static void onRequestResult(int resultCode, Activity activity){
sRequestApproved = true;
Intent service = new Intent(sContext, SocksVpnService.class);
service.putExtra("origin_ipv6", sOriginIPv6);
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
FqrouterManager.sMessenger = new Messenger(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
FqrouterManager.sMessenger = null;
}
};
sContext.startService(service);
sContext.bindService(service, serviceConnection, Context.BIND_AUTO_CREATE);
} else {
AppModel.fatalError(sContext.getString(R.string.vpn_reject));
}
Expand Down Expand Up @@ -323,6 +342,13 @@ public static boolean quit(){

public static void postStop(){
if(!AppModel.sIsRootMode) {
if(null != sMessenger){
try {
sMessenger.send(Message.obtain(null, SocksVpnService.MSG_STOP_VPN, 0, 0));
} catch (RemoteException e) {
e.printStackTrace();
}
}
sContext.stopService(new Intent(sContext, SocksVpnService.class));
}
if(mProcess != null) {
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/net/xndroid/fqrouter/SocksVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import android.net.LocalSocket;
import android.net.VpnService;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.ParcelFileDescriptor;
import android.widget.Toast;

Expand Down Expand Up @@ -67,6 +70,30 @@ public class SocksVpnService extends VpnService {
private Set<String> skippedFds = new HashSet<String>();
private Set<Integer> stagingFds = new HashSet<Integer>();

public static final int MSG_STOP_VPN = 1;

/**
* Handler of incoming messages from clients.
*/
class SockHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_STOP_VPN:
SocksVpnService.this.onRevoke();
break;
default:
super.handleMessage(msg);
}
}
}


@Override
public IBinder onBind(Intent intent) {
Messenger messenger = new Messenger(new SockHandler());
return messenger.getBinder();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/net/xndroid/xxnet/XXnetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,15 @@ public static boolean waitReady(){
@Override
public void run() {
try {
Thread.sleep(2000);
Thread.sleep(1000);
HttpJson.get("http://google.com");
Thread.sleep(5000);
HttpJson.get("http://youtube.com");
Thread.sleep(15000);
HttpJson.get("http://facebook.com");
} catch (InterruptedException e) {
e.printStackTrace();
}
HttpJson.get("http://google.com");
}
}).start();
return true;
Expand Down
Binary file modified app/src/main/res/raw/xxnet
Binary file not shown.
4 changes: 2 additions & 2 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="waitting_respond">等待XX-Net响应</string>
<string name="public_appid">(未设置AppID,访问受限!)</string>
<string name="ignore">忽略</string>
<string name="connect_no_establish">连接尚未建立,稍等片刻</string>
<string name="connect_no_establish">连接尚未建立</string>
<string name="import_u"><u>导入</u></string>
<string name="import_cert">导入证书</string>
<string name="import_cert_tip">如设置了图案解锁却要求输入凭证,则先清除锁屏密码再尝试.</string>
Expand Down Expand Up @@ -134,7 +134,7 @@
<string name="change_server_tip">留空以挑选内置的服务器</string>
<string name="change_teredo_server">输入teredo服务器地址或域名</string>
<string name="not_ip_domain">失败, 不是有效的IPV4地址或域名.</string>
<string name="available_origin_ipv6">原生IPv6可用, 将禁用teredo.</string>
<string name="available_origin_ipv6">原生IPv6可用,将禁用teredo. 若ipv6不可用,可在启动设置中取消 IPV6可用时禁用teredo.</string>
<string name="DISABLED">已禁用</string>
<string name="update_xxnet">离线更新XX-Net</string>
<string name="xxnet_update_title">离线更新XX-Net</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="waitting_respond">Waiting for XX-net to respond</string>
<string name="public_appid">(Set your own AppID!!!)</string>
<string name="ignore">ignore</string>
<string name="connect_no_establish">No connection, please wait</string>
<string name="connect_no_establish">No connection</string>
<string name="import_u"><u>Import</u></string>
<string name="import_cert">import certification</string>
<string name="import_cert_tip">You can turn off the screen lock and try again if a password is required.</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public int getUrlBoxContentChoice() {
}

public int getUseTheme() {
return mPrefs.getInt(Name.THEME, 2);
return mPrefs.getInt(Name.THEME, 0);
}

public boolean getUseProxy() {
Expand Down

0 comments on commit b93e9ad

Please sign in to comment.