Add cloud based ACL updating

This commit is contained in:
Max Lv
2016-10-09 10:51:08 +08:00
parent bbf614484d
commit 0f24d7c0bc
9 changed files with 47 additions and 33 deletions
-3
View File
@@ -121,9 +121,6 @@
android:summary="@string/nat_summary"/>
<Preference android:key="recovery" android:title="@string/recovery"
android:summary="@string/recovery_summary"/>
<Preference android:key="flush_dnscache"
android:title="@string/flush_dnscache"
android:summary="@string/flush_dnscache_summary"/>
<Preference android:key="about" android:title="@string/about"/>
</be.mygod.preference.PreferenceCategory>
@@ -39,6 +39,7 @@
package com.github.shadowsocks
import java.io.File
import java.io.IOException
import java.util.{Timer, TimerTask}
@@ -55,6 +56,10 @@ import com.github.shadowsocks.utils._
import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.database.Profile
import com.google.firebase.FirebaseApp
import com.google.firebase.storage.FileDownloadTask
import com.google.firebase.storage.FirebaseStorage
trait BaseService extends Service {
@volatile private var state = State.STOPPED
@@ -276,6 +281,7 @@ trait BaseService extends Service {
override def onCreate() {
super.onCreate()
FirebaseApp.initializeApp(this)
app.refreshContainerHolder
}
@@ -311,4 +317,28 @@ trait BaseService extends Service {
case ex: Exception => "exclude = " + default + ";"
}
}
def getAcl(route: String) {
val localAcl = route match {
case Route.BYPASS_LAN => "bypass_lan.acl"
case Route.BYPASS_CHN => "bypass_chn.acl"
case Route.BYPASS_LAN_CHN => "bypass_lan_chn.acl"
case Route.GFWLIST => "gfwlist.acl"
case Route.CHINALIST => "chinalist.acl"
}
val cloudAclFile = new File(getApplicationInfo.dataDir + "/" + localAcl)
if (!cloudAclFile.exists) cloudAclFile.createNewFile
val tempFile = File.createTempFile(localAcl, "acl")
val storage = FirebaseStorage.getInstance
val storageRef = storage.getReferenceFromUrl("gs://admob-app-id-3330146721.appspot.com/acl/")
val aclRef = storageRef.child(localAcl)
val task = aclRef.getFile(tempFile)
task.addOnSuccessListener((_) => {
tempFile.renameTo(cloudAclFile)
return
})
task.addOnFailureListener((_) => Log.e("shadowsocks", "Unable to download " + localAcl))
tempFile.deleteOnExit()
}
}
@@ -545,15 +545,6 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
}
}
def flushDnsCache() {
val h = showProgress(R.string.flushing)
Utils.ThrowableFuture {
if (!Utils.toggleAirplaneMode(getBaseContext)) h.post(() => Snackbar.make(findViewById(android.R.id.content),
R.string.flush_dnscache_no_root, Snackbar.LENGTH_LONG).show)
h.sendEmptyMessage(0)
}
}
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) = resultCode match {
case Activity.RESULT_OK =>
serviceLoad()
@@ -96,6 +96,7 @@ class ShadowsocksNatService extends BaseService {
if (profile.route != Route.ALL) {
cmd += "--acl"
profile.route match {
case Route.BYPASS_LAN => cmd += (getApplicationInfo.dataDir + "/bypass_lan.acl")
case Route.BYPASS_CHN => cmd += (getApplicationInfo.dataDir + "/bypass_chn.acl")
@@ -336,8 +337,10 @@ class ShadowsocksNatService extends BaseService {
}
handleConnection()
// Set DNS
su.addCommand(Utils.FLUSH_DNS)
// lazily get ACL from cloud
getAcl(profile.route)
changeState(State.CONNECTED)
notification = new ShadowsocksNotification(this, profile.name, true)
}
@@ -170,12 +170,6 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
true
})
findPreference("flush_dnscache").setOnPreferenceClickListener(_ => {
app.track(TAG, "flush_dnscache")
activity.flushDnsCache()
true
})
findPreference("about").setOnPreferenceClickListener((preference: Preference) => {
app.track(TAG, "about")
val web = new WebView(activity)
@@ -162,6 +162,10 @@ class ShadowsocksVpnService extends VpnService with BaseService {
handleConnection()
changeState(State.CONNECTED)
// lazily get ACL from cloud
getAcl(profile.route)
notification = new ShadowsocksNotification(this, profile.name)
}
@@ -257,6 +261,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (profile.route != Route.ALL) {
cmd += "--acl"
profile.route match {
case Route.BYPASS_LAN => cmd += (getApplicationInfo.dataDir + "/bypass_lan.acl")
case Route.BYPASS_CHN => cmd += (getApplicationInfo.dataDir + "/bypass_chn.acl")
@@ -169,19 +169,6 @@ object Utils {
result
}
// Because /sys/class/net/* isn't accessible since API level 24
final val FLUSH_DNS = "for if in /sys/class/net/*; do " +
"if [ \"down\" != $(cat $if/operstate) ]; then " + // up or unknown
"ndc resolver flushif ${if##*/}; " +
"fi " +
"done; echo done"
// Blocked > 3 seconds
def toggleAirplaneMode(context: Context) = {
val result = Shell.SU.run(FLUSH_DNS)
result != null && !result.isEmpty
}
def resolve(host: String, addrType: Int): Option[String] = {
try {
val lookup = new Lookup(host, addrType)