Skip to content

Commit 66fac19

Browse files
2dustAnGgIt886
authored andcommitted
Run hysteria using a plugin-like method
1 parent 83c7331 commit 66fac19

File tree

15 files changed

+621
-13
lines changed

15 files changed

+621
-13
lines changed

app/src/main/kotlin/com/neko/v2ray/AngApplication.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.neko.v2ray
22

33
import android.content.Context
4+
import android.content.pm.PackageManager
5+
import android.os.Build
46
import androidx.multidex.MultiDexApplication
57
import androidx.work.Configuration
68
import androidx.work.WorkManager
@@ -27,4 +29,10 @@ class AngApplication : MultiDexApplication() {
2729
WorkManager.initialize(this, workManagerConfiguration)
2830
SettingsManager.initRoutingRulesets(this)
2931
}
32+
33+
fun getPackageInfo(packageName: String) = packageManager.getPackageInfo(
34+
packageName, if (Build.VERSION.SDK_INT >= 28) PackageManager.GET_SIGNING_CERTIFICATES
35+
else @Suppress("DEPRECATION") PackageManager.GET_SIGNATURES
36+
)!!
37+
3038
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.neko.v2ray.dto
2+
3+
data class Hysteria2Bean(
4+
val server: String?,
5+
val auth: String?,
6+
val lazy: Boolean? = true,
7+
val socks5: Socks5Bean?,
8+
val tls: TlsBean?
9+
) {
10+
data class Socks5Bean(
11+
val listen: String?,
12+
)
13+
14+
data class TlsBean(
15+
val sni: String?
16+
)
17+
}

app/src/main/kotlin/com/neko/v2ray/extension/_Ext.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.neko.v2ray.extension
22

3+
import android.content.BroadcastReceiver
34
import android.content.Context
5+
import android.content.Intent
6+
import android.content.IntentFilter
47
import android.os.Build
58
import android.widget.Toast
69
import com.neko.v2ray.AngApplication
@@ -56,4 +59,26 @@ val URI.idnHost: String
5659

5760
fun String.removeWhiteSpace(): String = replace("\\s+".toRegex(), "")
5861

59-
fun String.toLongEx(): Long = toLongOrNull() ?: 0
62+
fun String.toLongEx(): Long = toLongOrNull() ?: 0
63+
64+
fun Context.listenForPackageChanges(onetime: Boolean = true, callback: () -> Unit) =
65+
object : BroadcastReceiver() {
66+
override fun onReceive(context: Context, intent: Intent) {
67+
callback()
68+
if (onetime) context.unregisterReceiver(this)
69+
}
70+
}.apply {
71+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
72+
registerReceiver(this, IntentFilter().apply {
73+
addAction(Intent.ACTION_PACKAGE_ADDED)
74+
addAction(Intent.ACTION_PACKAGE_REMOVED)
75+
addDataScheme("package")
76+
}, Context.RECEIVER_EXPORTED)
77+
} else {
78+
registerReceiver(this, IntentFilter().apply {
79+
addAction(Intent.ACTION_PACKAGE_ADDED)
80+
addAction(Intent.ACTION_PACKAGE_REMOVED)
81+
addDataScheme("package")
82+
})
83+
}
84+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/******************************************************************************
2+
* *
3+
* Copyright (C) 2021 by nekohasekai <contact-sagernet@sekai.icu> *
4+
* Copyright (C) 2021 by Max Lv <max.c.lv@gmail.com> *
5+
* Copyright (C) 2021 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
6+
* *
7+
* This program is free software: you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation, either version 3 of the License, or *
10+
* (at your option) any later version. *
11+
* *
12+
* This program is distributed in the hope that it will be useful, *
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15+
* GNU General Public License for more details. *
16+
* *
17+
* You should have received a copy of the GNU General Public License *
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19+
* *
20+
******************************************************************************/
21+
22+
package com.neko.v2ray.plugin
23+
24+
import android.content.pm.ResolveInfo
25+
26+
class NativePlugin(resolveInfo: ResolveInfo) : ResolvedPlugin(resolveInfo) {
27+
init {
28+
check(resolveInfo.providerInfo != null)
29+
}
30+
31+
override val componentInfo get() = resolveInfo.providerInfo!!
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/******************************************************************************
2+
* *
3+
* Copyright (C) 2021 by nekohasekai <contact-sagernet@sekai.icu> *
4+
* Copyright (C) 2021 by Max Lv <max.c.lv@gmail.com> *
5+
* Copyright (C) 2021 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
6+
* *
7+
* This program is free software: you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation, either version 3 of the License, or *
10+
* (at your option) any later version. *
11+
* *
12+
* This program is distributed in the hope that it will be useful, *
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15+
* GNU General Public License for more details. *
16+
* *
17+
* You should have received a copy of the GNU General Public License *
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19+
* *
20+
******************************************************************************/
21+
22+
package com.neko.v2ray.plugin
23+
24+
import android.graphics.drawable.Drawable
25+
26+
abstract class Plugin {
27+
abstract val id: String
28+
abstract val label: CharSequence
29+
abstract val version: Int
30+
abstract val versionName: String
31+
open val icon: Drawable? get() = null
32+
open val defaultConfig: String? get() = null
33+
open val packageName: String get() = ""
34+
open val directBootAware: Boolean get() = true
35+
36+
override fun equals(other: Any?): Boolean {
37+
if (this === other) return true
38+
if (javaClass != other?.javaClass) return false
39+
return id == (other as Plugin).id
40+
}
41+
42+
override fun hashCode() = id.hashCode()
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/******************************************************************************
2+
* *
3+
* Copyright (C) 2021 by nekohasekai <contact-sagernet@sekai.icu> *
4+
* *
5+
* This program is free software: you can redistribute it and/or modify *
6+
* it under the terms of the GNU General Public License as published by *
7+
* the Free Software Foundation, either version 3 of the License, or *
8+
* (at your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* *
15+
* You should have received a copy of the GNU General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
* *
18+
******************************************************************************/
19+
20+
package com.neko.v2ray.plugin
21+
22+
object PluginContract {
23+
24+
const val ACTION_NATIVE_PLUGIN = "io.nekohasekai.sagernet.plugin.ACTION_NATIVE_PLUGIN"
25+
const val EXTRA_ENTRY = "io.nekohasekai.sagernet.plugin.EXTRA_ENTRY"
26+
const val METADATA_KEY_ID = "io.nekohasekai.sagernet.plugin.id"
27+
const val METADATA_KEY_EXECUTABLE_PATH = "io.nekohasekai.sagernet.plugin.executable_path"
28+
const val METHOD_GET_EXECUTABLE = "sagernet:getExecutable"
29+
30+
const val COLUMN_PATH = "path"
31+
const val COLUMN_MODE = "mode"
32+
const val SCHEME = "plugin"
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/******************************************************************************
2+
* *
3+
* Copyright (C) 2021 by nekohasekai <contact-sagernet@sekai.icu> *
4+
* Copyright (C) 2021 by Max Lv <max.c.lv@gmail.com> *
5+
* Copyright (C) 2021 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
6+
* *
7+
* This program is free software: you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation, either version 3 of the License, or *
10+
* (at your option) any later version. *
11+
* *
12+
* This program is distributed in the hope that it will be useful, *
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15+
* GNU General Public License for more details. *
16+
* *
17+
* You should have received a copy of the GNU General Public License *
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19+
* *
20+
******************************************************************************/
21+
22+
package com.neko.v2ray.plugin
23+
24+
import android.content.Intent
25+
import android.content.pm.PackageManager
26+
import com.neko.v2ray.AngApplication
27+
28+
class PluginList : ArrayList<Plugin>() {
29+
init {
30+
addAll(
31+
AngApplication.application.packageManager.queryIntentContentProviders(
32+
Intent(PluginContract.ACTION_NATIVE_PLUGIN), PackageManager.GET_META_DATA)
33+
.filter { it.providerInfo.exported }.map { NativePlugin(it) })
34+
}
35+
36+
val lookup = mutableMapOf<String, Plugin>().apply {
37+
for (plugin in this@PluginList.toList()) {
38+
fun check(old: Plugin?) {
39+
if (old != null && old != plugin) {
40+
this@PluginList.remove(old)
41+
}
42+
/* if (old != null && old !== plugin) {
43+
val packages = this@PluginList.filter { it.id == plugin.id }
44+
.joinToString { it.packageName }
45+
val message = "Conflicting plugins found from: $packages"
46+
Toast.makeText(SagerNet.application, message, Toast.LENGTH_LONG).show()
47+
throw IllegalStateException(message)
48+
}*/
49+
}
50+
check(put(plugin.id, plugin))
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)