Skip to content

Commit

Permalink
fix duplicated subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
xxf098 committed Jan 20, 2020
1 parent 755cf90 commit c122e3c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="com.github.shadowsocks"
android:versionCode="326"
android:versionName="3.6.1">
android:versionName="3.6.1.1">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Expand Down
80 changes: 44 additions & 36 deletions src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,38 +117,18 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic

var profile = item
if (profile.isVmess) {
val future = Future{
if (!Utils.isNumeric(profile.v_add)) Utils.resolve(profile.v_add, enableIPv6 = true, hostname="1.1.1.1") match {
case Some(addr) => profile.v_add = addr
case None => throw new IOException("Name Not Resolved")
}
Tun2socks.testVmessLatency(profile, app.getV2rayAssetsPath())
}
future onSuccess {
case elapsed => {
runOnUiThread(() => {
profile.elapsed = elapsed
app.profileManager.updateProfile(profile)
this.updateText(0, 0, elapsed)
val result = getString(R.string.connection_test_available, elapsed: java.lang.Long)
singleTestProgressDialog.dismiss()
Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show
})
}
case _ =>
}
future onFailure {
profile.testLatency().map(elapsed => {
this.updateText(0, 0, elapsed)
getString(R.string.connection_test_available, elapsed: java.lang.Long)
}).recover {
case e: Exception => {
runOnUiThread(() => {
singleTestProgressDialog.dismiss()
Log.e(TAG, e.getMessage)
e.printStackTrace()
val result = getString(R.string.connection_test_error, e.getMessage)
singleTestProgressDialog.dismiss()
Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show
})
e.printStackTrace()
app.getString(R.string.connection_test_error, e.getMessage)
}
}
}.foreach(result => {
singleTestProgressDialog.dismiss()
Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show
})
}

if (!profile.isV2Ray) {
Expand Down Expand Up @@ -315,7 +295,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
case (`allGroup`, true) => app.profileManager.getAllProfilesByElapsed
case (`allGroup`, false) => app.profileManager.getAllProfiles
case (_, true) => app.profileManager.getAllProfilesByGroupOrderByElapse(groupName)
case (_, false) => app.profileManager.getAllProfilesByGroup(groupName)
case (_, false) => app.profileManager.getAllProfilesByGroup(groupName, -1)
}}.getOrElse(List.empty[Profile])
}

Expand Down Expand Up @@ -714,7 +694,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
ssrsubAdapter.notifyDataSetChanged()
}): DialogInterface.OnClickListener)
.setNeutralButton(R.string.ssrsub_remove_tip_delete, ((_, _) => {
var delete_profiles = app.profileManager.getAllProfilesByGroup(viewHolder.asInstanceOf[SSRSubViewHolder].item.url_group) match {
val ssrsubItem = viewHolder.asInstanceOf[SSRSubViewHolder].item
val delete_profiles = app.profileManager.getAllProfilesByGroup(ssrsubItem.url_group, ssrsubItem.id) match {
case Some(profiles) =>
profiles
case _ => null
Expand Down Expand Up @@ -767,8 +748,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
app.ssrsubManager.getAllSSRSubs match {
case Some(ssrsubs) =>
ssrsubs.foreach((ssrsub: SSRSub) => {

var delete_profiles = app.profileManager.getAllProfilesByGroup(ssrsub.url_group) match {
var delete_profiles = app.profileManager.getAllProfilesByGroup(ssrsub.url_group, ssrsub.id) match {
case Some(profiles) =>
profiles
case _ => null
Expand Down Expand Up @@ -804,6 +784,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
val profiles_vmess = Parser.findAllVmess(response_string)
.map(profile => {
profile.url_group = ssrsub.url_group
profile.ssrsub_id = ssrsub.id
profile
})
val profiles = profiles_ssr ++ profiles_vmess
Expand Down Expand Up @@ -840,7 +821,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
}): DialogInterface.OnClickListener)
.setNegativeButton(android.R.string.no, null)
.setNeutralButton(R.string.ssrsub_add, ((_, _) => {
// add url
// add sub url
val UrlAddEdit = new EditText(this);
new AlertDialog.Builder(this)
.setTitle(getString(R.string.ssrsub_add))
Expand Down Expand Up @@ -1126,6 +1107,8 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
startActivity(new Intent(this, classOf[V2RayConfigActivity]))
true
case R.id.action_full_test =>

// Option(ProfileManagerActivity.getProfilesByGroup(currentGroupName, is_sort)) match {
app.profileManager.getAllProfiles match {
case Some(profiles) =>

Expand All @@ -1147,16 +1130,41 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
}
})

// TODO: refactor
testAsyncJob = new Thread {
override def run() {
// Do some background work
Looper.prepare()
profiles.filter(!_.isV2Ray).foreach((profile: Profile) => {
profiles.foreach((profile: Profile) => {
if (isTesting) {

if (testAsyncJob.isInterrupted()) {
isTesting = false
}

// v2rayjson
if (profile.isV2RayJSON) {
return
}

// vmess
if (profile.isVmess) {
// profile.testLatency().map(elapsed => {
// getString(R.string.connection_test_available, elapsed: java.lang.Long)
// }).recover {
// case e: Exception => {
// e.printStackTrace()
// app.getString(R.string.connection_test_error, e.getMessage)
// }
// }.foreach(result => {
// val msg = Message.obtain()
// msg.obj = profile.name + " " + result
// msg.setTarget(showProgresshandler)
// msg.sendToTarget()
// })
return
}

// Resolve the server address
var host = profile.host
if (!Utils.isNumeric(host)) Utils.resolve(host, enableIPv6 = true) match {
Expand Down
11 changes: 10 additions & 1 deletion src/main/scala/com/github/shadowsocks/V2RayConfigActivity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,22 @@ class V2RayConfigActivity extends AppCompatActivity with
case _ => runOnUiThread(() => Toast.makeText(this, "Config is not valid!", Toast.LENGTH_SHORT))
}
future onFailure {
case e: Exception => runOnUiThread(() => Toast.makeText(this, "config is not valid!", Toast.LENGTH_SHORT).show())
case e: Exception => {
e.printStackTrace()
runOnUiThread(() => Toast.makeText(this, "config is not valid!", Toast.LENGTH_SHORT).show())
}
}
}

def checkConfig(config: String): Future[String] = {
Future {
val jsonObject = new JsonParser().parse(config).getAsJsonObject
// val outbounds = jsonObject.getAsJsonArray("outbounds")
// val vmess = outbounds.get(0).getAsJsonObject
// val settings = vmess.getAsJsonObject("settings")
// val vnext = vmess.getAsJsonArray("vnext")
// Log.e(TAG, vmess.toString)
// Log.e(TAG, "protocol==")
val prettyConfig = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject)
// val assetPath = getApplicationInfo.dataDir + "/files/"
// Tun2socks.testConfig(prettyConfig, assetPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ object DBHelper {
}

class DBHelper(val context: Context)
extends OrmLiteSqliteOpenHelper(context, DBHelper.PROFILE, null, 27) {
extends OrmLiteSqliteOpenHelper(context, DBHelper.PROFILE, null, 28) {
import DBHelper._

lazy val profileDao: Dao[Profile, Int] = getDao(classOf[Profile])
Expand Down Expand Up @@ -173,6 +173,9 @@ class DBHelper(val context: Context)
if (oldVersion < 27) {
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN v_json_config VARCHAR DEFAULT '';")
}
if (oldVersion < 28) {
profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN ssrsub_id INTEGER DEFAULT '';")
}
} catch {
case ex: Exception =>
app.track(ex)
Expand Down
27 changes: 27 additions & 0 deletions src/main/scala/com/github/shadowsocks/database/Profile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

package com.github.shadowsocks.database

import java.io.IOException
import java.net.URLEncoder
import java.nio.charset.Charset
import java.util.Locale
Expand All @@ -50,8 +51,14 @@ import tun2socks.{Tun2socks, Vmess}

import scala.language.implicitConversions
import Profile._
import com.github.shadowsocks.R
import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.utils.Utils

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global


object Profile {
implicit def profileToVmess(profile: Profile): Vmess = {
if (!profile.isVmess) {
Expand Down Expand Up @@ -124,6 +131,9 @@ class Profile {
@DatabaseField
var url_group: String = ""

@DatabaseField
var ssrsub_id: Int = 0

@DatabaseField
var dns: String = "1.1.1.1:53,8.8.8.8:53,9.9.9.9:53"

Expand Down Expand Up @@ -212,4 +222,21 @@ class Profile {
def isV2RayJSON = this.proxy_protocol == "v2ray_json"

def isV2Ray = isVmess || isV2RayJSON

def testLatency (): Future[Long] = {
if (isVmess) {
return Future{
if (!Utils.isNumeric(v_add)) Utils.resolve(v_add, enableIPv6 = true, hostname="1.1.1.1") match {
case Some(addr) => v_add = addr
case None => throw new IOException("Name Not Resolved")
}
Tun2socks.testVmessLatency(this, app.getV2rayAssetsPath())
}.map(elapsed => {
this.elapsed = elapsed
app.profileManager.updateProfile(this)
elapsed
})
}
Future(throw new Exception("Not Supported!"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,29 @@ class ProfileManager(dbHelper: DBHelper) {
}
}

def getAllProfilesByGroup(group:String): Option[List[Profile]] = {
// def getAllProfilesByGroup(group:String): Option[List[Profile]] = {
// try {
// import scala.collection.JavaConversions._
// Option(dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.where().eq("url_group", group).prepare).toList)
// } catch {
// case ex: Exception =>
// Log.e(TAG, "getAllProfiles", ex)
// app.track(ex)
// None
// }
// }

def getAllProfilesByGroup(group:String, ssrsub_id: Int): Option[List[Profile]] = {
try {
import scala.collection.JavaConversions._
Option(dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.where().eq("url_group", group).prepare).toList)
ssrsub_id match {
case id if id > 0 => {
val profiles = Option(dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.where().eq("ssrsub_id", ssrsub_id).prepare).toList)
.getOrElse(dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.where().eq("url_group", group).prepare).toList)
Option(profiles)
}
case _ => Option(dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.where().eq("url_group", group).prepare).toList)
}
} catch {
case ex: Exception =>
Log.e(TAG, "getAllProfiles", ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SSRSubUpdateJob() extends Job {
var result = 1
ssrsubs.foreach((ssrsub: SSRSub) => {

var delete_profiles = app.profileManager.getAllProfilesByGroup(ssrsub.url_group) match {
var delete_profiles = app.profileManager.getAllProfilesByGroup(ssrsub.url_group, ssrsub.id) match {
case Some(profiles) =>
profiles
case _ => null
Expand Down

0 comments on commit c122e3c

Please sign in to comment.