Commit 0b0b6137 authored by Max Lv's avatar Max Lv

disable NAT mode on lollipop by default

parent 156b0c27
...@@ -69,6 +69,9 @@ ...@@ -69,6 +69,9 @@
<string name="flushing">刷新中...</string> <string name="flushing">刷新中...</string>
<string name="add_profile">添加配置文件</string> <string name="add_profile">添加配置文件</string>
<string name="enable_nat">启用 NAT 模式.</string>
<string name="disable_nat">禁用 NAT 模式.</string>
<string name="udp_dns">UDP 转发</string> <string name="udp_dns">UDP 转发</string>
<string name="udp_dns_summary">由远程服务器转发 UDP 协议的数据包</string> <string name="udp_dns_summary">由远程服务器转发 UDP 协议的数据包</string>
......
...@@ -59,6 +59,8 @@ ...@@ -59,6 +59,8 @@
<string name="stop">Stop the service</string> <string name="stop">Stop the service</string>
<string name="stopping">Shutting down...</string> <string name="stopping">Shutting down...</string>
<string name="vpn_error">Error: \"%s\".</string> <string name="vpn_error">Error: \"%s\".</string>
<string name="enable_nat">Turn on NAT mode.</string>
<string name="disable_nat">Turn off NAT mode.</string>
<!-- alert category --> <!-- alert category -->
<string name="ok_iknow">OK</string> <string name="ok_iknow">OK</string>
......
...@@ -51,6 +51,7 @@ import android.net.{Uri, VpnService} ...@@ -51,6 +51,7 @@ import android.net.{Uri, VpnService}
import android.os._ import android.os._
import android.preference._ import android.preference._
import android.util.{DisplayMetrics, Log} import android.util.{DisplayMetrics, Log}
import android.view.View.OnLongClickListener
import android.view._ import android.view._
import android.webkit.{WebView, WebViewClient} import android.webkit.{WebView, WebViewClient}
import android.widget._ import android.widget._
...@@ -499,13 +500,30 @@ class Shadowsocks ...@@ -499,13 +500,30 @@ class Shadowsocks
getActionBar.setDisplayShowTitleEnabled(false) getActionBar.setDisplayShowTitleEnabled(false)
getActionBar.setDisplayShowCustomEnabled(true) getActionBar.setDisplayShowCustomEnabled(true)
getActionBar.setIcon(R.drawable.ic_stat_shadowsocks) getActionBar.setIcon(R.drawable.ic_stat_shadowsocks)
title.setOnLongClickListener(new OnLongClickListener {
override def onLongClick(v: View): Boolean = {
if (Utils.isLollipopOrAbove && bgService != null
&& (bgService.getState == State.INIT || bgService.getState == State.STOPPED)) {
val natEnabled = status.getBoolean(Key.isNAT, false)
status.edit().putBoolean(Key.isNAT, !natEnabled).commit()
if (!natEnabled) {
Toast.makeText(getBaseContext, R.string.enable_nat, Toast.LENGTH_LONG).show()
} else {
Toast.makeText(getBaseContext, R.string.disable_nat, Toast.LENGTH_LONG).show()
}
true
} else {
false
}
}
})
// Register broadcast receiver // Register broadcast receiver
registerReceiver(preferenceReceiver, new IntentFilter(Action.UPDATE_PREFS)) registerReceiver(preferenceReceiver, new IntentFilter(Action.UPDATE_PREFS))
// Bind to the service // Bind to the service
spawn { spawn {
val isRoot = Console.isRoot val isRoot = (!Utils.isLollipopOrAbove || status.getBoolean(Key.isNAT, false)) && Console.isRoot
handler.post(new Runnable { handler.post(new Runnable {
override def run() { override def run() {
status.edit.putBoolean(Key.isRoot, isRoot).commit() status.edit.putBoolean(Key.isRoot, isRoot).commit()
......
...@@ -117,7 +117,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -117,7 +117,7 @@ class ShadowsocksNatService extends Service with BaseService {
val cmdBuf = new ArrayBuffer[String]() val cmdBuf = new ArrayBuffer[String]()
networks.foreach(network => { networks.foreach(network => {
val networkInfo = manager.getNetworkInfo(network) val networkInfo = manager.getNetworkInfo(network)
if (networkInfo.isAvailable) { if (networkInfo.isConnected) {
val netId = getNetId(network) val netId = getNetId(network)
val curDnsList = manager.getLinkProperties(network).getDnsServers val curDnsList = manager.getLinkProperties(network).getDnsServers
if (curDnsList != null) { if (curDnsList != null) {
...@@ -507,7 +507,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -507,7 +507,7 @@ class ShadowsocksNatService extends Service with BaseService {
filter.addAction(Action.CLOSE) filter.addAction(Action.CLOSE)
closeReceiver = new BroadcastReceiver() { closeReceiver = new BroadcastReceiver() {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
Toast.makeText(p1, R.string.stopping, Toast.LENGTH_SHORT) Toast.makeText(p1, R.string.stopping, Toast.LENGTH_SHORT).show()
stopRunner() stopRunner()
} }
} }
......
...@@ -50,7 +50,8 @@ import com.github.shadowsocks.aidl.IShadowsocksService ...@@ -50,7 +50,8 @@ import com.github.shadowsocks.aidl.IShadowsocksService
class ShadowsocksRunnerActivity extends Activity { class ShadowsocksRunnerActivity extends Activity {
lazy val settings = PreferenceManager.getDefaultSharedPreferences(this) private lazy val settings = PreferenceManager.getDefaultSharedPreferences(this)
private lazy val status = getSharedPreferences(Key.status, Context.MODE_PRIVATE)
val handler = new Handler() val handler = new Handler()
val connection = new ServiceConnection { val connection = new ServiceConnection {
...@@ -72,10 +73,10 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -72,10 +73,10 @@ class ShadowsocksRunnerActivity extends Activity {
def isVpnEnabled: Boolean = { def isVpnEnabled: Boolean = {
if (vpnEnabled < 0) { if (vpnEnabled < 0) {
vpnEnabled = if (!Console.isRoot) { vpnEnabled = if ((!Utils.isLollipopOrAbove || status.getBoolean(Key.isNAT, false)) && Console.isRoot) {
1
} else {
0 0
} else {
1
} }
} }
if (vpnEnabled == 1) true else false if (vpnEnabled == 1) true else false
......
...@@ -65,6 +65,7 @@ object Key { ...@@ -65,6 +65,7 @@ object Key {
val proxied = "Proxyed" val proxied = "Proxyed"
val isNAT = "isNAT"
val isRoot = "isRoot" val isRoot = "isRoot"
val status = "status" val status = "status"
val proxyedApps = "proxyedApps" val proxyedApps = "proxyedApps"
......
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