Commit 45fc2b45 authored by Max Lv's avatar Max Lv

add auto start option

parent 17ae4653
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.shadowsocks" package="com.github.shadowsocks"
android:installLocation="auto" android:installLocation="auto"
android:versionCode="3" android:versionCode="4"
android:versionName="1.0.2"> android:versionName="1.1">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-feature <uses-feature
android:name="android.hardware.touchscreen" android:name="android.hardware.touchscreen"
...@@ -41,6 +42,12 @@ ...@@ -41,6 +42,12 @@
android:name=".ShadowsocksService" android:name=".ShadowsocksService"
android:enabled="true"/> android:enabled="true"/>
<receiver android:name=".ShadowsocksReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application> </application>
</manifest> </manifest>
...@@ -164,6 +164,11 @@ ...@@ -164,6 +164,11 @@
console.log = function() {}; console.log = function() {};
var pid = ""+process.pid; // need to turn into a string
fs.writeFile('/data/data/com.github.shadowsocks/shadowsocks.pid', pid, function (err) {
// Ignore
});
console.log("calculating ciphers"); console.log("calculating ciphers");
tables = getTable(KEY); tables = getTable(KEY);
...@@ -332,11 +337,6 @@ ...@@ -332,11 +337,6 @@
}); });
}); });
var pid = ""+process.pid; // need to turn into a string
fs.writeFile('/data/data/com.github.shadowsocks/shadowsocks.pid', pid, function (err) {
// Ignore
});
server.listen(PORT, function() { server.listen(PORT, function() {
return console.log("server listening at port " + PORT); return console.log("server listening at port " + PORT);
}); });
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.github.shadowsocks</groupId> <groupId>com.github.shadowsocks</groupId>
<artifactId>shadowsocks</artifactId> <artifactId>shadowsocks</artifactId>
<version>1.0.2</version> <version>1.1</version>
<packaging>apk</packaging> <packaging>apk</packaging>
<name>Shadowsocks</name> <name>Shadowsocks</name>
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
<PreferenceCategory android:title="@string/function_cat"> <PreferenceCategory android:title="@string/function_cat">
<CheckBoxPreference <CheckBoxPreference
android:key="isRunning" android:key="isRunning"
android:disableDependentsState="true"
android:summary="@string/service_summary" android:summary="@string/service_summary"
android:title="@string/service_controller"> android:title="@string/service_controller">
</CheckBoxPreference> </CheckBoxPreference>
...@@ -11,24 +12,28 @@ ...@@ -11,24 +12,28 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/proxy_cat"> <PreferenceCategory android:title="@string/proxy_cat">
<EditTextPreference <EditTextPreference
android:dependency="isRunning"
android:defaultValue="127.0.0.1" android:defaultValue="127.0.0.1"
android:key="proxy" android:key="proxy"
android:summary="@string/proxy_summary" android:summary="@string/proxy_summary"
android:title="@string/proxy"> android:title="@string/proxy">
</EditTextPreference> </EditTextPreference>
<EditTextPreference <EditTextPreference
android:dependency="isRunning"
android:defaultValue="1984" android:defaultValue="1984"
android:key="remotePort" android:key="remotePort"
android:summary="@string/remote_port_summary" android:summary="@string/remote_port_summary"
android:title="@string/remote_port"> android:title="@string/remote_port">
</EditTextPreference> </EditTextPreference>
<EditTextPreference <EditTextPreference
android:dependency="isRunning"
android:defaultValue="1080" android:defaultValue="1080"
android:key="port" android:key="port"
android:summary="@string/port_summary" android:summary="@string/port_summary"
android:title="@string/port"> android:title="@string/port">
</EditTextPreference> </EditTextPreference>
<EditTextPreference <EditTextPreference
android:dependency="isRunning"
android:defaultValue="" android:defaultValue=""
android:key="sitekey" android:key="sitekey"
android:summary="@string/sitekey_summary" android:summary="@string/sitekey_summary"
...@@ -37,29 +42,38 @@ ...@@ -37,29 +42,38 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/fearute_cat"> <PreferenceCategory android:title="@string/fearute_cat">
<CheckBoxPreference <CheckBoxPreference
android:dependency="isRunning"
android:defaultValue="true" android:defaultValue="true"
android:key="isGFWList" android:key="isGFWList"
android:summary="@string/auto_set_gfwlist_summary" android:summary="@string/auto_set_gfwlist_summary"
android:title="@string/auto_set_gfwlist"> android:title="@string/auto_set_gfwlist">
</CheckBoxPreference> </CheckBoxPreference>
<CheckBoxPreference <CheckBoxPreference
android:dependency="isRunning"
android:defaultValue="true" android:defaultValue="true"
android:key="isGlobalProxy" android:key="isGlobalProxy"
android:summary="@string/auto_set_proxy_summary" android:summary="@string/auto_set_proxy_summary"
android:title="@string/auto_set_proxy"> android:title="@string/auto_set_proxy">
</CheckBoxPreference> </CheckBoxPreference>
<CheckBoxPreference <CheckBoxPreference
android:dependency="isRunning"
android:defaultValue="false" android:defaultValue="false"
android:key="isBypassApps" android:key="isBypassApps"
android:summary="@string/bypass_apps_summary" android:summary="@string/bypass_apps_summary"
android:title="@string/bypass_apps"> android:title="@string/bypass_apps">
</CheckBoxPreference> </CheckBoxPreference>
<Preference <Preference
android:dependency="isRunning"
android:key="proxyedApps" android:key="proxyedApps"
android:summary="@string/proxyed_apps_summary" android:summary="@string/proxyed_apps_summary"
android:title="@string/proxyed_apps"> android:title="@string/proxyed_apps">
</Preference> </Preference>
<CheckBoxPreference
android:dependency="isRunning"
android:key="isAutoConnect"
android:summary="@string/auto_connect_summary"
android:title="@string/auto_connect">
</CheckBoxPreference>
</PreferenceCategory> </PreferenceCategory>
......
...@@ -103,13 +103,10 @@ public class Shadowsocks extends PreferenceActivity implements ...@@ -103,13 +103,10 @@ public class Shadowsocks extends PreferenceActivity implements
private static ProgressDialog mProgressDialog = null; private static ProgressDialog mProgressDialog = null;
private CheckBoxPreference isGlobalProxyCheck;
private EditTextPreference proxyText; private EditTextPreference proxyText;
private EditTextPreference portText; private EditTextPreference portText;
private EditTextPreference remotePortText; private EditTextPreference remotePortText;
private EditTextPreference sitekeyText; private EditTextPreference sitekeyText;
private CheckBoxPreference isGFWListCheck;
private CheckBoxPreference isRunningCheck;
private Preference proxyedApps; private Preference proxyedApps;
private CheckBoxPreference isBypassAppsCheck; private CheckBoxPreference isBypassAppsCheck;
...@@ -159,32 +156,6 @@ public class Shadowsocks extends PreferenceActivity implements ...@@ -159,32 +156,6 @@ public class Shadowsocks extends PreferenceActivity implements
} }
private void disableAll() {
proxyText.setEnabled(false);
portText.setEnabled(false);
remotePortText.setEnabled(false);
sitekeyText.setEnabled(false);
proxyedApps.setEnabled(false);
isGFWListCheck.setEnabled(false);
isBypassAppsCheck.setEnabled(false);
isGlobalProxyCheck.setEnabled(false);
}
private void enableAll() {
proxyText.setEnabled(true);
portText.setEnabled(true);
remotePortText.setEnabled(true);
sitekeyText.setEnabled(true);
isGlobalProxyCheck.setEnabled(true);
isGFWListCheck.setEnabled(true);
if (!isGlobalProxyCheck.isChecked()) {
proxyedApps.setEnabled(true);
isBypassAppsCheck.setEnabled(true);
}
}
private boolean isTextEmpty(String s, String msg) { private boolean isTextEmpty(String s, String msg) {
if (s == null || s.length() <= 0) { if (s == null || s.length() <= 0) {
showAToast(msg); showAToast(msg);
...@@ -208,9 +179,6 @@ public class Shadowsocks extends PreferenceActivity implements ...@@ -208,9 +179,6 @@ public class Shadowsocks extends PreferenceActivity implements
sitekeyText = (EditTextPreference) findPreference("sitekey"); sitekeyText = (EditTextPreference) findPreference("sitekey");
proxyedApps = findPreference("proxyedApps"); proxyedApps = findPreference("proxyedApps");
isRunningCheck = (CheckBoxPreference) findPreference("isRunning");
isGlobalProxyCheck = (CheckBoxPreference) findPreference("isGlobalProxy");
isGFWListCheck = (CheckBoxPreference) findPreference("isGFWList");
isBypassAppsCheck = (CheckBoxPreference) findPreference("isBypassApps"); isBypassAppsCheck = (CheckBoxPreference) findPreference("isBypassApps");
if (mProgressDialog == null) if (mProgressDialog == null)
...@@ -350,23 +318,14 @@ public class Shadowsocks extends PreferenceActivity implements ...@@ -350,23 +318,14 @@ public class Shadowsocks extends PreferenceActivity implements
SharedPreferences settings = PreferenceManager SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this); .getDefaultSharedPreferences(this);
if (settings.getBoolean("isSystemProxy", false)) { if (settings.getBoolean("isGlobalProxy", false)) {
isGlobalProxyCheck.setEnabled(false);
isGFWListCheck.setEnabled(false);
proxyedApps.setEnabled(false); proxyedApps.setEnabled(false);
isBypassAppsCheck.setEnabled(false); isBypassAppsCheck.setEnabled(false);
} else { } else {
if (settings.getBoolean("isGlobalProxy", false)) { proxyedApps.setEnabled(true);
proxyedApps.setEnabled(false); isBypassAppsCheck.setEnabled(true);
isBypassAppsCheck.setEnabled(false);
} else {
proxyedApps.setEnabled(true);
isBypassAppsCheck.setEnabled(true);
}
} }
sitekeyText.setEnabled(true);
Editor edit = settings.edit(); Editor edit = settings.edit();
if (ShadowsocksService.isServiceStarted()) { if (ShadowsocksService.isServiceStarted()) {
...@@ -386,14 +345,6 @@ public class Shadowsocks extends PreferenceActivity implements ...@@ -386,14 +345,6 @@ public class Shadowsocks extends PreferenceActivity implements
edit.commit(); edit.commit();
if (settings.getBoolean("isRunning", false)) {
isRunningCheck.setChecked(true);
disableAll();
} else {
isRunningCheck.setChecked(false);
enableAll();
}
// Setup the initial values // Setup the initial values
if (!settings.getString("sitekey", "").equals("")) if (!settings.getString("sitekey", "").equals(""))
...@@ -448,15 +399,6 @@ public class Shadowsocks extends PreferenceActivity implements ...@@ -448,15 +399,6 @@ public class Shadowsocks extends PreferenceActivity implements
} }
} }
if (key.equals("isRunning")) {
if (settings.getBoolean("isRunning", false)) {
disableAll();
isRunningCheck.setChecked(true);
} else {
isRunningCheck.setChecked(false);
enableAll();
}
}
if (key.equals("remotePort")) if (key.equals("remotePort"))
if (settings.getString("remotePort", "").equals("")) if (settings.getString("remotePort", "").equals(""))
......
package com.github.shadowsocks;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
public class ShadowsocksReceiver extends BroadcastReceiver {
private static final String TAG = "Shadowsocks";
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
String versionName;
try {
versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
versionName = "NONE";
}
final boolean isAutoConnect = settings.getBoolean("isAutoConnect", false);
final boolean isInstalled = settings.getBoolean(versionName, false);
if (isAutoConnect && isInstalled) {
final String portText = settings.getString("port", "");
if (portText == null || portText.length() <= 0) {
return;
}
try {
int port = Integer.valueOf(portText);
if (port <= 1024) {
return;
}
} catch (Exception e) {
return;
}
Intent it = new Intent(context, ShadowsocksService.class);
context.startService(it);
}
}
}
...@@ -56,8 +56,7 @@ import android.preference.PreferenceManager; ...@@ -56,8 +56,7 @@ import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import com.google.analytics.tracking.android.EasyTracker; import com.google.analytics.tracking.android.EasyTracker;
import java.io.DataOutputStream; import java.io.*;
import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -183,7 +182,22 @@ public class ShadowsocksService extends Service { ...@@ -183,7 +182,22 @@ public class ShadowsocksService extends Service {
return isServiceStarted; return isServiceStarted;
} }
public void startShadowsocksDaemon() { private int getPid(String name) {
try {
BufferedReader reader = new BufferedReader(new FileReader(BASE + name + ".pid"));
final String line = reader.readLine();
return Integer.valueOf(line);
} catch (FileNotFoundException e) {
Log.e(TAG, "Cannot open pid file: " + name);
} catch (IOException e) {
Log.e(TAG, "Cannot read pid file: " + name);
} catch (NumberFormatException e) {
Log.e(TAG, "Invalid pid", e);
}
return -1;
}
private void startShadowsocksDaemon() {
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
...@@ -195,9 +209,9 @@ public class ShadowsocksService extends Service { ...@@ -195,9 +209,9 @@ public class ShadowsocksService extends Service {
}.start(); }.start();
} }
public void startDnsDaemon() { private void startDnsDaemon() {
final String cmd = BASE + "pdnsd -c " + BASE + "pdnsd.conf"; final String cmd = BASE + "pdnsd -c " + BASE + "pdnsd.conf";
Utils.runRootCommand(cmd); Utils.runCommand(cmd);
} }
private String getVersionName() { private String getVersionName() {
...@@ -277,7 +291,26 @@ public class ShadowsocksService extends Service { ...@@ -277,7 +291,26 @@ public class ShadowsocksService extends Service {
BASE, BASE, BASE); BASE, BASE, BASE);
Utils.runRootCommand("echo \"" + conf + "\" > " + BASE + "redsocks.conf\n" Utils.runRootCommand("echo \"" + conf + "\" > " + BASE + "redsocks.conf\n"
+ cmd); + cmd);
}
private boolean waitForProcess(final String name) {
final int pid = getPid(name);
if (pid == -1) return false;
Exec.hangupProcessGroup(-pid);
Thread t = new Thread() {
@Override
public void run() {
Exec.waitFor(-pid);
Log.d(TAG, "Successfully exit pid: " + pid);
}
};
t.start();
try {
t.join(300);
} catch (InterruptedException ignored) {
}
return !t.isAlive();
} }
/** /**
...@@ -436,9 +469,13 @@ public class ShadowsocksService extends Service { ...@@ -436,9 +469,13 @@ public class ShadowsocksService extends Service {
private void onDisconnect() { private void onDisconnect() {
Utils.runRootCommand(Utils.getIptables() + " -t nat -F OUTPUT"); Utils.runRootCommand(Utils.getIptables() + " -t nat -F OUTPUT");
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/pdnsd.pid`").append("\n");
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/redsocks.pid`").append("\n"); sb.append("kill -9 `cat /data/data/com.github.shadowsocks/redsocks.pid`").append("\n");
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/shadowsocks.pid`").append("\n"); if (!waitForProcess("pdnsd")) {
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/pdnsd.pid`").append("\n");
}
if (!waitForProcess("shadowsocks")) {
sb.append("kill -9 `cat /data/data/com.github.shadowsocks/shadowsocks.pid`").append("\n");
}
Utils.runRootCommand(sb.toString()); Utils.runRootCommand(sb.toString());
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment