Commit 655ef1ee authored by Mygod's avatar Mygod

Refine TFO

parent 04be6057
...@@ -112,12 +112,7 @@ class ShadowsocksApplication extends Application { ...@@ -112,12 +112,7 @@ class ShadowsocksApplication extends Application {
} }
pending.setResultCallback(callback, 2, TimeUnit.SECONDS) pending.setResultCallback(callback, 2, TimeUnit.SECONDS)
// TFO TcpFastOpen.enabled(settings.getBoolean(Key.tfo, false))
val tfo = settings.getBoolean(Key.tfo, false)
if (tfo && !TcpFastOpen.isEnabled)
{
TcpFastOpen.enabled(true)
}
} }
def refreshContainerHolder { def refreshContainerHolder {
......
...@@ -105,7 +105,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -105,7 +105,7 @@ class ShadowsocksNatService extends BaseService {
if (profile.auth) cmd += "-A" if (profile.auth) cmd += "-A"
if (TcpFastOpen.isEnabled) cmd += "--fast-open" if (TcpFastOpen.sendEnabled) cmd += "--fast-open"
if (profile.route != Route.ALL) { if (profile.route != Route.ALL) {
cmd += "--acl" cmd += "--acl"
......
...@@ -155,17 +155,14 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -155,17 +155,14 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
} }
switch.setChecked(BootReceiver.getEnabled(activity)) switch.setChecked(BootReceiver.getEnabled(activity))
if (getPreferenceManager.getSharedPreferences.getBoolean(Key.tfo, false) && !TcpFastOpen.isEnabled) {
TcpFastOpen.enabled(true)
}
val tfo = findPreference(Key.tfo).asInstanceOf[SwitchPreference] val tfo = findPreference(Key.tfo).asInstanceOf[SwitchPreference]
tfo.setChecked(TcpFastOpen.isEnabled) tfo.setChecked(TcpFastOpen.sendEnabled)
tfo.setOnPreferenceChangeListener((_, v) => { tfo.setOnPreferenceChangeListener((_, v) => {
val value = v.asInstanceOf[Boolean] val value = v.asInstanceOf[Boolean]
val result = TcpFastOpen.enabled(value) val result = TcpFastOpen.enabled(value)
if (result != null && result != "Success.") if (result != null && result != "Success.")
Snackbar.make(activity.findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show() Snackbar.make(activity.findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show()
value == TcpFastOpen.isEnabled value == TcpFastOpen.sendEnabled
}) })
if (!TcpFastOpen.supported) { if (!TcpFastOpen.supported) {
tfo.setEnabled(false) tfo.setEnabled(false)
......
...@@ -266,7 +266,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -266,7 +266,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
cmd += (getApplicationInfo.dataDir + "/acl.list") cmd += (getApplicationInfo.dataDir + "/acl.list")
} }
if (TcpFastOpen.isEnabled) cmd += "--fast-open" if (TcpFastOpen.sendEnabled) cmd += "--fast-open"
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
......
...@@ -23,12 +23,12 @@ object TcpFastOpen { ...@@ -23,12 +23,12 @@ object TcpFastOpen {
case _ => false case _ => false
} }
def isEnabled = { def sendEnabled = {
val file = new File("/proc/sys/net/ipv4/tcp_fastopen") val file = new File("/proc/sys/net/ipv4/tcp_fastopen")
file.canRead && (Utils.readAllLines(file).toInt & 1) > 0 file.canRead && (Utils.readAllLines(file).toInt & 1) > 0
} }
def enabled(value: Boolean): String = { def enabled(value: Boolean): String = if (sendEnabled != value) {
val res = Shell.run("su", Array( val res = Shell.run("su", Array(
"if echo " + (if (value) 3 else 0) + " > /proc/sys/net/ipv4/tcp_fastopen; then", "if echo " + (if (value) 3 else 0) + " > /proc/sys/net/ipv4/tcp_fastopen; then",
" echo Success.", " echo Success.",
...@@ -36,5 +36,5 @@ object TcpFastOpen { ...@@ -36,5 +36,5 @@ object TcpFastOpen {
" echo Failed.", " echo Failed.",
"fi"), null, true) "fi"), null, true)
if (res != null) res.asScala.mkString("\n") else null if (res != null) res.asScala.mkString("\n") else null
} } else null
} }
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