add component module & clean up code

This commit is contained in:
Kr328 committed 2020-04-01 14:45:06 +08:00
1 parent 7bfaacf936
commit 88b28faa48
16 files changed
+187 -29

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
plugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-android-extensions")
}
val rootExtra = rootProject.extra
val gCompileSdkVersion: Int by rootExtra
val gBuildToolsVersion: String by rootExtra
val gMinSdkVersion: Int by rootExtra
val gTargetSdkVersion: Int by rootExtra
val gVersionCode: Int by rootExtra
val gVersionName: String by rootExtra
val gKotlinVersion: String by rootExtra
val gKotlinCoroutineVersion: String by rootExtra
val gAndroidKtxVersion: String by rootExtra
android {
compileSdkVersion(gCompileSdkVersion)
buildToolsVersion(gBuildToolsVersion)
defaultConfig {
minSdkVersion(gMinSdkVersion)
targetSdkVersion(gTargetSdkVersion)
versionCode = gVersionCode
versionName = gVersionName
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
maybeCreate("release").apply {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.core:core-ktx:$gAndroidKtxVersion")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$gKotlinVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$gKotlinCoroutineVersion")
}
repositories {
mavenCentral()
}
View File
Whitespace-only changes.
+21
View File
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
+4
View File
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.kr328.clash.component">
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
@@ -0,0 +1,55 @@
package com.github.kr328.clash.component.time
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.sync.Mutex
class Ticker(private val timeMillis: Long) {
val c: Channel<Int> = Channel()
private var tickerJob: Job? = null
private val locker: Mutex = Mutex()
suspend fun resume() {
locker.lock()
if ( tickerJob == null ) {
start()
}
locker.unlock()
}
suspend fun pause() {
locker.lock()
if ( tickerJob != null ) {
tickerJob!!.cancel()
}
locker.unlock()
}
private suspend fun start() {
withContext(Dispatchers.Unconfined) {
tickerJob = launch {
try {
var count = 0
while (isActive) {
c.send(count++)
delay(timeMillis)
}
}
finally {
locker.lock()
tickerJob = null
locker.unlock()
}
}
}
}
}
+5 -1
View File
@@ -41,7 +41,10 @@ android {
buildTypes {
maybeCreate("release").apply {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
@@ -59,6 +62,7 @@ dependencies {
kapt("androidx.room:room-compiler:$gRoomVersion")
implementation(project(":core"))
implementation(project(":component"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$gKotlinSerializationVersion")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$gKotlinVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$gKotlinCoroutineVersion")
+7 -7
View File
@@ -12,8 +12,8 @@
<service
android:name="TunService"
android:exported="false"
android:process=":background"
android:permission="android.permission.BIND_VPN_SERVICE">
android:permission="android.permission.BIND_VPN_SERVICE"
android:process=":background">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
@@ -37,8 +37,8 @@
android:process=":background" />
<provider
android:authorities="${applicationId}.profiles"
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.profiles"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
@@ -46,14 +46,14 @@
android:resource="@xml/profile_provider" />
</provider>
<provider
android:authorities="${applicationId}.status"
android:name=".ServiceStatusProvider"
android:authorities="${applicationId}.status"
android:exported="false"
android:process=":background"/>
android:process=":background" />
<provider
android:authorities="${applicationId}.settings"
android:name=".ServiceSettingsProvider"
android:authorities="${applicationId}.settings"
android:exported="false"
android:process=":background"/>
android:process=":background" />
</application>
</manifest>
@@ -100,7 +100,7 @@ class ClashCore(private val service: Service) : CoroutineScope by MainScope() {
refreshCount++
if ( refreshCount > CALL_GC_PERIOD ) {
if (refreshCount > CALL_GC_PERIOD) {
refreshCount = 0
System.gc()
}
@@ -39,7 +39,6 @@ class ClashNotification(
PendingIntent.FLAG_UPDATE_CURRENT
)
)
private var currentProfile = "None"
init {
@@ -59,13 +59,19 @@ class ProfileProcessor(private val context: Context) {
}
}
private suspend fun downloadProfile(source: Uri, target: File, baseDir: File, newRecord: Boolean) {
private suspend fun downloadProfile(
source: Uri,
target: File,
baseDir: File,
newRecord: Boolean
) {
try {
target.parentFile?.mkdirs()
baseDir.mkdirs()
if (source.scheme.equals("content", ignoreCase = true)
|| source.scheme.equals("file", ignoreCase = true)) {
|| source.scheme.equals("file", ignoreCase = true)
) {
val parcelFileDescriptor = context.contentResolver.openFileDescriptor(source, "r")
?: throw FileNotFoundException("Unable to open file $source")
@@ -76,7 +82,7 @@ class ProfileProcessor(private val context: Context) {
Clash.downloadProfile(source.toString(), target, baseDir).await()
}
} catch (e: Exception) {
if ( newRecord ) {
if (newRecord) {
target.delete()
baseDir.deleteRecursively()
}
@@ -15,11 +15,17 @@ class ServiceSettingsProvider : PreferenceProvider() {
companion object {
fun createSharedPreferencesFromContext(context: Context): SharedPreferences {
return when ( context ) {
return when (context) {
is BaseService, is TunService ->
context.getSharedPreferences(Constants.SERVICE_SETTING_FILE_NAME, Context.MODE_PRIVATE)
context.getSharedPreferences(
Constants.SERVICE_SETTING_FILE_NAME,
Context.MODE_PRIVATE
)
else ->
MultiProcessPreference(context, context.packageName + Constants.SETTING_PROVIDER_SUFFIX)
MultiProcessPreference(
context,
context.packageName + Constants.SETTING_PROVIDER_SUFFIX
)
}
}
}
@@ -9,7 +9,6 @@ import com.github.kr328.clash.service.settings.ServiceSettings
import com.github.kr328.clash.service.util.asSocketAddressText
import com.github.kr328.clash.service.util.broadcastNetworkChanged
import kotlinx.coroutines.*
import kotlin.time.Duration
class TunService : VpnService(), CoroutineScope by MainScope() {
companion object {
@@ -23,7 +22,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
private const val PRIVATE_VLAN6_MIRROR = "fdfe:dcba:9876::2"
private const val PRIVATE_VLAN_DNS = "198.18.0.1"
private const val VLAN4_ANY = "0.0.0.0"
}
}
private var clashCore: ClashCore? = null
@@ -42,16 +41,19 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
.establish()
?: throw NullPointerException("Unable to create VPN Service")
val dnsAddress = if (settings.get(ServiceSettings.DNS_HIJACKING)) VLAN4_ANY else PRIVATE_VLAN_DNS
val dnsAddress =
if (settings.get(ServiceSettings.DNS_HIJACKING)) VLAN4_ANY else PRIVATE_VLAN_DNS
Log.i("TunService.startTun ${fd.fd}")
Clash.setDnsOverrideEnabled(settings.get(ServiceSettings.OVERRIDE_DNS))
Clash.startTunDevice(fd.detachFd(), VPN_MTU,
Clash.startTunDevice(
fd.detachFd(), VPN_MTU,
"$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET", PRIVATE_VLAN4_MIRROR,
dnsAddress,
this::protect, this::stopSelf)
this::protect, this::stopSelf
)
fd.close()
}
@@ -156,7 +158,8 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
private fun Builder.addBypassApplications(): Builder {
when (settings.get(ServiceSettings.ACCESS_CONTROL_MODE)) {
ServiceSettings.ACCESS_CONTROL_MODE_ALL -> {}
ServiceSettings.ACCESS_CONTROL_MODE_ALL -> {
}
ServiceSettings.ACCESS_CONTROL_MODE_WHITELIST -> {
for (app in settings.get(ServiceSettings.ACCESS_CONTROL_PACKAGES).toSet()) {
runCatching {
@@ -2,9 +2,7 @@ package com.github.kr328.clash.service.settings
import android.content.Context
import android.content.SharedPreferences
import com.github.kr328.clash.service.Constants
import com.github.kr328.clash.service.ServiceSettingsProvider
import rikka.preference.MultiProcessPreference
class ServiceSettings(preference: SharedPreferences) :
BaseSettings(preference) {
@@ -4,8 +4,8 @@
android:height="200dp"
android:viewportWidth="200"
android:viewportHeight="200">
<path
android:fillColor="#000000"
android:pathData="M47.211,168.128C70.531,-34.962 67.471,13.788 94.071,43.818c13.45,-1.52 27.24,-3.47 40.82,-0.67c2.64,0.13 5.42,1.86 7.71,0.18c4.12,-6.27 7.35,-13.54 11.35,-20c12.19,-24.44 12.85,19.54 15.48,26.52c5.23,32.99 10.89,64.46 14.67,97.59c0.31,10.72 5.74,32.92 1.08,33.56c-49.36,5.23 -147.71,3.91 -160.84,-6.3c-15.85,-10.5 -15.18,-35.33 2.03,-43.72c3.63,-2.03 10.68,-3.72 11.94,0.7c-2.41,4.99 -8.79,5.77 -12.12,11.17C16.621,158.948 33.111,168.888 47.211,168.128zM87.841,74.008c-10.42,0.52 -9.59,14.89 -0.07,15.18C98.191,88.668 97.361,74.298 87.841,74.008zM149.121,89.188c10.46,-0.34 9.85,-14.71 0.38,-15.18C139.031,74.348 139.651,88.718 149.121,89.188zM107.871,99.228c2.16,3.48 5.28,3.29 9.79,0.16c3.81,3.17 8.06,3.28 9.18,-0.19c-3.78,1.17 -7.04,0.79 -9.4,-3.49C115.371,100.108 112.071,100.428 107.871,99.228z"
tools:ignore="VectorPath" />
<path
android:fillColor="#000000"
android:pathData="M47.211,168.128C70.531,-34.962 67.471,13.788 94.071,43.818c13.45,-1.52 27.24,-3.47 40.82,-0.67c2.64,0.13 5.42,1.86 7.71,0.18c4.12,-6.27 7.35,-13.54 11.35,-20c12.19,-24.44 12.85,19.54 15.48,26.52c5.23,32.99 10.89,64.46 14.67,97.59c0.31,10.72 5.74,32.92 1.08,33.56c-49.36,5.23 -147.71,3.91 -160.84,-6.3c-15.85,-10.5 -15.18,-35.33 2.03,-43.72c3.63,-2.03 10.68,-3.72 11.94,0.7c-2.41,4.99 -8.79,5.77 -12.12,11.17C16.621,158.948 33.111,168.888 47.211,168.128zM87.841,74.008c-10.42,0.52 -9.59,14.89 -0.07,15.18C98.191,88.668 97.361,74.298 87.841,74.008zM149.121,89.188c10.46,-0.34 9.85,-14.71 0.38,-15.18C139.031,74.348 139.651,88.718 149.121,89.188zM107.871,99.228c2.16,3.48 5.28,3.29 9.79,0.16c3.81,3.17 8.06,3.28 9.18,-0.19c-3.78,1.17 -7.04,0.79 -9.4,-3.49C115.371,100.108 112.071,100.428 107.871,99.228z"
tools:ignore="VectorPath" />
</vector>
@@ -5,5 +5,5 @@
path="profiles/" />
<cache-path
name="export_profiles"
path="profiles/"/>
path="profiles/" />
</paths>
+1
View File
@@ -2,5 +2,6 @@ include(":app")
include(":core")
include(":service")
include(":design")
include(":component")
rootProject.name = "ClashForAndroid"