Compare commits

...

29 Commits

Author SHA1 Message Date
Kr328
cdd0e3dd30 fix break connection 2020-05-03 00:51:51 +08:00
Kr328
5823955c71 update clash core 2020-05-03 00:50:34 +08:00
Kr328
714c9a554f break connection if set proxy for selector 2020-05-03 00:50:14 +08:00
Kr328
e621324b48 update clash core 2020-05-03 00:29:45 +08:00
Kr328
dfe1e7ecd9 exclude 127.0.0.0/8 from route table 2020-05-03 00:17:55 +08:00
Kr328
4c3380d822 update clash core & tun2socket 2020-04-29 13:22:36 +08:00
Kr328
1d68516cdb fix receivers & service connection leak 2020-04-24 00:49:03 +08:00
Kr328
821bb49914 update issue template 2020-04-23 19:33:28 +08:00
Kr328
c95993a629 fix crash on uploaded 2020-04-23 19:15:46 +08:00
Kr328
e6495f5e1f add uploaded feedback 2020-04-23 19:15:16 +08:00
Kr328
c104942b5f update version 2020-04-23 19:13:34 +08:00
Kr328
59835667aa add report logcat button 2020-04-23 19:04:51 +08:00
Kr328
8a98eea8fa improve core log 2020-04-23 18:52:23 +08:00
Kr328
3dc20b9e70 update issue template 2020-04-23 16:29:17 +08:00
Kr328
d8ab6fd755 update tun2socket version 2020-04-23 13:10:22 +08:00
Kr328
cc499a7897 improve package name modify 2020-04-23 12:14:05 +08:00
Kr328
76f5261b20 Revert "Fix tun start at second user"
This reverts commit 94a0282c
2020-04-23 11:08:36 +08:00
Kr328
6a506b761f update tun2socket 2020-04-23 11:06:50 +08:00
Kr328
fe966f9c5d Revert "Fix tun start at second user"
This reverts commit 94a0282c
2020-04-23 10:52:59 +08:00
Kr328
bcf7f793ec add stop tun on destroy & update version 2020-04-23 10:51:20 +08:00
Kr328
75de6ce041 add full crash logcat report 2020-04-23 00:12:07 +08:00
Kr328
4417a85ef7 update version 2020-04-23 00:09:24 +08:00
Kr328
9e0b67c04a add dump AndroidRuntime tag 2020-04-23 00:06:53 +08:00
Kr328
8cd203e7b0 detect vpn establish failure 2020-04-23 00:03:05 +08:00
Kr328
211160f7f8 Rename fields to let lint happy :) 2020-04-22 13:25:50 +08:00
Kr328
2afc7262eb Update version 2020-04-22 11:25:01 +08:00
Kr328
94a0282c70 Fix tun start at second user 2020-04-22 11:05:10 +08:00
Kr328
96b78829ab Fix cancel profile auto update 2020-04-22 00:25:22 +08:00
Kr328
35d5a90b2a Improve database migrations 2020-04-21 23:50:01 +08:00
32 changed files with 620 additions and 426 deletions

View File

@@ -7,6 +7,10 @@ assignees: ''
---
<!-- Be sure to put a clear title after [BUG] in the text box above -->
<!-- Be sure to put a clear title after [BUG] in the text box above -->
<!-- Be sure to put a clear title after [BUG] in the text box above -->
**Describe the bug**
A clear and concise description of what the bug is.
@@ -23,9 +27,6 @@ A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Logs**
if applicable, add logs to help detect problem
**Device Info (please complete the following information):**
- Device: [e.g. Pixel 4]
@@ -41,3 +42,10 @@ if applicable, add logs to help detect problem
**Additional context**
Add any other context about the problem here.
<!--
*Logs*
if applicable, upload logs to help detect problem
`Open App` -> `Support` -> `Feedback` -> `Upload Logcat`
-->

View File

@@ -7,6 +7,10 @@ assignees: ''
---
<!-- Be sure to put a clear title after [Feature Request] in the text box above -->
<!-- Be sure to put a clear title after [Feature Request] in the text box above -->
<!-- Be sure to put a clear title after [Feature Request] in the text box above -->
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

View File

@@ -7,6 +7,10 @@ assignees: ''
---
<!-- 请务必在上方文本框处 [BUG] 后填入清晰明了的标题 -->
<!-- 请务必在上方文本框处 [BUG] 后填入清晰明了的标题 -->
<!-- 请务必在上方文本框处 [BUG] 后填入清晰明了的标题 -->
**描述出现的错误**
请简洁的描述你遇到的错误
@@ -23,9 +27,6 @@ assignees: ''
**屏幕截图**
如果适用, 上传屏幕截图以帮助描述错误
**日志**
如果适用, 上传日志以帮助侦测错误
**设备信息 (请完成以下信息):**
- 机型: [例如: Pixel 4]
- 系统/ROM: [例如: MIUI 11]
@@ -39,3 +40,10 @@ assignees: ''
**附加信息**
其他的可能与改错误相关的信息
<!--
*日志*
如果适用, 上传日志以帮助侦测错误
`打开应用` -> `支持` -> `反馈` -> `上传日志`
-->

View File

@@ -7,6 +7,10 @@ assignees: ''
---
<!-- 请务必在上方文本框处 [Feature Request] 后填入清晰明了的标题 -->
<!-- 请务必在上方文本框处 [Feature Request] 后填入清晰明了的标题 -->
<!-- 请务必在上方文本框处 [Feature Request] 后填入清晰明了的标题 -->
**功能描述**
请清晰的描述你想要的功能

View File

@@ -1,4 +1,5 @@
import java.util.*
import java.security.*
plugins {
id("com.android.application")
@@ -121,6 +122,18 @@ task("injectAppCenterKey") {
}
}
task("injectPackageNameBase64") {
doFirst {
val packageName = android.defaultConfig.applicationId ?: return@doFirst
val base64 = Base64.getEncoder().encodeToString(packageName.toByteArray(Charsets.UTF_8))
android.buildTypes.forEach {
it.buildConfigField("String", "PACKAGE_NAME_BASE64", "\"$base64\"")
}
}
}
afterEvaluate {
tasks["preBuild"].dependsOn(tasks["injectAppCenterKey"])
tasks["preBuild"].dependsOn(tasks["injectAppCenterKey"], tasks["injectPackageNameBase64"])
}

View File

@@ -19,7 +19,6 @@ import java.io.File
class LogViewerActivity : BaseActivity() {
private val pauseMutex = Mutex()
private var pollingThread: Thread? = null
private val connection = object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
finish()
@@ -48,12 +47,6 @@ class LogViewerActivity : BaseActivity() {
startFileMode(file.toFile())
}
override fun onDestroy() {
super.onDestroy()
pollingThread?.interrupt()
}
override fun onStop() {
super.onStop()
@@ -78,6 +71,7 @@ class LogViewerActivity : BaseActivity() {
mainList.itemAnimator?.removeDuration = 100
stop.setOnClickListener {
unbindService(connection)
stopService(LogcatService::class.intent)
finish()
}

View File

@@ -42,7 +42,7 @@ class MainApplication : Application() {
if (!report.stackTrace.contains("DeadObjectException"))
return mutableListOf()
val logcat = LogcatDumper.dump().joinToString(separator = "\n")
val logcat = LogcatDumper.dumpCrash()
return mutableListOf(
ErrorAttachmentLog.attachmentWithText(logcat, "logcat.txt")

View File

@@ -4,9 +4,19 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.Html
import androidx.appcompat.app.AlertDialog
import com.github.kr328.clash.dump.LogcatDumper
import com.google.android.material.snackbar.Snackbar
import com.microsoft.appcenter.crashes.Crashes
import com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog
import kotlinx.android.synthetic.main.activity_support.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class SupportActivity : BaseActivity() {
class UserRequestTrackException: Exception()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -46,6 +56,20 @@ class SupportActivity : BaseActivity() {
category(text = getString(R.string.feedback))
option(
title = getString(R.string.upload_logcat),
summary = getString(R.string.upload_logcat_summary)
) {
onClick {
AlertDialog.Builder(this@SupportActivity)
.setTitle(R.string.upload_logcat)
.setMessage(R.string.upload_logcat_warn)
.setNegativeButton(R.string.cancel) {_, _ -> }
.setPositiveButton(R.string.ok) {_, _ -> upload() }
.show()
}
}
option(
title = getString(R.string.github_issues),
summary = getString(R.string.github_issues_url)
@@ -77,4 +101,19 @@ class SupportActivity : BaseActivity() {
}
}
}
private fun upload() {
launch {
withContext(Dispatchers.IO) {
val attachment = ErrorAttachmentLog
.attachmentWithText(LogcatDumper.dumpAll(), "logcat.txt")
Crashes.trackError(UserRequestTrackException(), null, listOf(attachment))
}
withContext(Dispatchers.Main) {
Snackbar.make(rootView, R.string.uploaded, Snackbar.LENGTH_LONG).show()
}
}
}
}

View File

@@ -1,34 +1,37 @@
package com.github.kr328.clash.dump
object LogcatDumper {
fun dump(): List<String> {
fun dumpCrash(): String {
return try {
val process =
Runtime.getRuntime().exec(arrayOf("logcat", "-d", "-s", "-v", "raw", "Go"))
Runtime.getRuntime().exec(arrayOf("logcat", "-d", "-s", "Go", "AndroidRuntime", "DEBUG"))
val result = process.inputStream.bufferedReader().useLines {
var list = mutableListOf<String>()
var capture = false
it.forEach { line ->
if (line.startsWith("panic")) {
capture = true
list = mutableListOf()
}
if (capture)
list.add(line)
}
list
val result = process.inputStream.use {
it.reader().readText()
}
process.waitFor()
result
} catch (e: Exception) {
emptyList()
""
}
}
fun dumpAll(): String {
return try {
val process =
Runtime.getRuntime().exec(arrayOf("logcat", "-d"))
val result = process.inputStream.use {
it.reader().readText()
}
process.waitFor()
result
} catch (e: Exception) {
""
}
}
}

View File

@@ -8,6 +8,7 @@ import android.content.ServiceConnection
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.util.Base64
import androidx.core.content.edit
import com.github.kr328.clash.ApkBrokenActivity
import com.github.kr328.clash.BuildConfig
@@ -89,7 +90,7 @@ object Remote {
val application = Global.application
if ( it ) {
if (it) {
handler.removeMessages(0)
GlobalScope.launch {
@@ -153,7 +154,36 @@ object Remote {
if (sp.getLong(Constants.PREFERENCE_KEY_LAST_INSTALL, 0) == pkg.lastUpdateTime)
return true
if ( application.packageName != BuildConfig.APPLICATION_ID )
val pkgName: String = try {
application::class.java.getMethod(
String(
charArrayOf(
'g',
'e',
't',
'P',
'a',
'c',
'k',
'a',
'g',
'e',
'N',
'a',
'm',
'e'
)
)
).invoke(application)?.toString()
} catch (e: Exception) {
Log.w("getPackageName failure", e)
null
} ?: application.packageName
val packageNameBase64 = Base64
.encodeToString(pkgName.toByteArray(Charsets.UTF_8), Base64.NO_WRAP)
if (packageNameBase64 != BuildConfig.PACKAGE_NAME_BASE64)
return false
val info = application.applicationInfo

View File

@@ -7,6 +7,7 @@ import com.github.kr328.clash.ApkBrokenActivity
import com.github.kr328.clash.common.utils.intent
import com.github.kr328.clash.service.Constants
import com.github.kr328.clash.service.ServiceStatusProvider
import java.lang.Exception
object RemoteUtils {
fun detectClashRunning(context: Context): Boolean {
@@ -24,7 +25,7 @@ object RemoteUtils {
)
return pong != null
} catch (e: IllegalArgumentException) {
} catch (e: Exception) {
context.startActivity(ApkBrokenActivity::class.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
return false
@@ -46,7 +47,7 @@ object RemoteUtils {
)
return pong?.getString("name")
} catch (e: IllegalArgumentException) {
} catch (e: Exception) {
context.startActivity(ApkBrokenActivity::class.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
return null

View File

@@ -131,4 +131,8 @@
<string name="tips_support"><![CDATA[Clash for Android 是一个<strong>免费开源</strong>的项目<br /> 我们<strong>不提供</strong>任何代理服务<br />请务必<strong>不要</strong>反馈非应用自身引起的问题]]></string>
<string name="donate">捐赠</string>
<string name="clone_profile">复制配置</string>
<string name="upload_logcat">上传日志</string>
<string name="upload_logcat_summary">上传日志以帮助我们侦测问题</string>
<string name="upload_logcat_warn">请注意, 上传的日志可能包含个人敏感信息, 仍要继续吗?</string>
<string name="uploaded">已上传</string>
</resources>

View File

@@ -173,4 +173,9 @@
<string name="missing_vpn_component">Missing VPN Components</string>
<string name="profile_not_found">Profile not found</string>
<string name="upload_logcat">Upload Logcat</string>
<string name="upload_logcat_summary">Upload logcat to help us detect issues</string>
<string name="upload_logcat_warn">Please note that the uploaded logs may contain personally sensitive information, do you still want to continue?</string>
<string name="uploaded">Uploaded</string>
</resources>

View File

@@ -10,8 +10,8 @@ buildscript {
this["gMinSdkVersion"] = 24
this["gTargetSdkVersion"] = 29
this["gVersionCode"] = 10207
this["gVersionName"] = "1.2.7"
this["gVersionCode"] = 10213
this["gVersionName"] = "1.2.13"
this["gKotlinVersion"] = kotlinVersion
this["gKotlinCoroutineVersion"] = "1.3.5"
@@ -30,7 +30,7 @@ buildscript {
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.0-beta04")
classpath("com.android.tools.build:gradle:4.0.0-beta05")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion")
}

View File

@@ -106,23 +106,42 @@ func QueryAllProxyGroups(collection ProxyGroupCollection) {
func SetSelectedProxy(name, proxy string) bool {
p := tunnel.Proxies()[name]
if p == nil {
log.Infoln("Set %s: Not such proxy group", name)
return false
}
pb, ok := p.(*outbound.Proxy)
if !ok {
log.Infoln("Set %s: Not a proxy object", name)
return false
}
selector, ok := pb.ProxyAdapter.(*outboundgroup.Selector)
if !ok {
log.Infoln("Set %s: Not a selector group", name)
return false
}
selected := selector.Now()
if selected == proxy {
log.Infoln("Set " + name + " -> " + proxy)
return true
}
if err := selector.Set(proxy); err != nil {
log.Infoln("Set %s: %s", name, err.Error())
return false
}
for _, conn := range tunnel.DefaultManager.Snapshot().Connections {
for _, p := range conn.Chain() {
if p == name {
_ = conn.Close()
break
}
}
}
log.Infoln("Set " + name + " -> " + proxy)
return true

View File

@@ -30,7 +30,7 @@ var client = &http.Client{
client, server := net.Pipe()
tunnel.Add(inbound.NewSocket(socks5.ParseAddr(address), server, constant.HTTP, constant.TCP))
tunnel.Add(inbound.NewSocket(socks5.ParseAddr(address), server, constant.HTTP))
return client, nil
},
@@ -97,14 +97,10 @@ func PullLocal(fd int, output, baseDir string) error {
}
func save(data []byte, output, baseDir string) error {
cfg, err := parseConfig(data, baseDir)
_, err := parseConfig(data, baseDir)
if err != nil {
return err
}
for _, v := range cfg.Providers {
_ = v.Destroy()
}
return ioutil.WriteFile(output, data, defaultFileMode)
}

View File

@@ -4,7 +4,7 @@ go 1.13
require (
github.com/Dreamacro/clash v0.0.0 // local
github.com/kr328/tun2socket v0.0.0-20200421133501-c8848c95a7ca
github.com/kr328/tun2socket v0.0.0-20200429021948-00f70a9cb042
github.com/miekg/dns v1.1.29
)

View File

@@ -3,17 +3,22 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/go-chi/chi v4.0.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/chi v4.1.1+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/cors v1.0.1/go.mod h1:K2Yje0VW/SJzxiyMYu6iPQYa7hMjQX2i/F491VChg1I=
github.com/go-chi/cors v1.1.1/go.mod h1:K2Yje0VW/SJzxiyMYu6iPQYa7hMjQX2i/F491VChg1I=
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr328/tun2socket v0.0.0-20200421133501-c8848c95a7ca/go.mod h1:FWfSixjrLgtK+dHkDoN6lHMNhvER24gnjUZd/wt8Z9o=
github.com/kr328/tun2socket v0.0.0-20200423032118-6f5116368120/go.mod h1:FWfSixjrLgtK+dHkDoN6lHMNhvER24gnjUZd/wt8Z9o=
github.com/kr328/tun2socket v0.0.0-20200429021948-00f70a9cb042 h1:Orn4L0/9fcf2ppxI3mmPl5q1xKB1P5roRmXUkc6adBw=
github.com/kr328/tun2socket v0.0.0-20200429021948-00f70a9cb042/go.mod h1:FWfSixjrLgtK+dHkDoN6lHMNhvER24gnjUZd/wt8Z9o=
github.com/miekg/dns v1.1.29/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/oschwald/geoip2-golang v1.4.0/go.mod h1:8QwxJvRImBH+Zl6Aa6MaIcs5YdlZSTKtzmPGzQqi9ng=
github.com/oschwald/maxminddb-golang v1.6.0/go.mod h1:DUJFucBg2cvqx42YmDa/+xHvb0elJtOm3o4aFQ/nb/w=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -23,11 +28,13 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200320220750-118fecf932d8/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -35,6 +42,7 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@@ -5,17 +5,17 @@ import "github.com/Dreamacro/clash/log"
type ClashLogger struct{}
func (c *ClashLogger) D(format string, args ...interface{}) {
log.Debugln(format, args)
log.Debugln(format, args...)
}
func (c *ClashLogger) I(format string, args ...interface{}) {
log.Infoln(format, args)
log.Infoln(format, args...)
}
func (c *ClashLogger) W(format string, args ...interface{}) {
log.Warnln(format, args)
log.Warnln(format, args...)
}
func (c *ClashLogger) E(format string, args ...interface{}) {
log.Errorln(format, args)
log.Errorln(format, args...)
}

View File

@@ -81,7 +81,7 @@ func StartTunDevice(fd, mtu int, gateway, mirror, dnsAddress string) error {
Zone: "",
})
tunnel.Add(adapters.NewSocket(addr, conn, C.SOCKS, C.TCP))
tunnel.Add(adapters.NewSocket(addr, conn, C.SOCKS))
})
adapter.SetUDPHandler(func(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSender) {
if gatewayNet.Contains(endpoint.Target.IP) {

View File

@@ -45,11 +45,11 @@ class ProfileReceiver : BroadcastReceiver() {
val metadata = ProfileDao.queryById(id)?.asProfile(context) ?: return
val service = context.getSystemService<AlarmManager>() ?: return
val pendingIntent = cancelNextUpdate(context, id)
if (metadata.interval <= 0)
return
val pendingIntent = cancelNextUpdate(context, id)
service.set(
AlarmManager.RTC,
metadata.lastModified + metadata.interval,

View File

@@ -96,6 +96,8 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
}
override fun onDestroy() {
TunModule.requestStop()
ServiceStatusProvider.serviceRunning = false
service.broadcastClashStopped(reason)
@@ -119,7 +121,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
return if (settings.get(ServiceSettings.BYPASS_PRIVATE_NETWORK))
resources.getStringArray(R.array.bypass_private_route).toList()
else
listOf(VLAN_ANY)
resources.getStringArray(R.array.bypass_local_route).toList()
}
override val dnsAddress: String
get() = PRIVATE_VLAN_DNS
@@ -139,7 +141,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
}
override fun onCreateTunFailure() {
stopSelfForReason("Start VPN rejected by the system")
stopSelfForReason("Establish VPN rejected by system")
}
}

View File

@@ -87,8 +87,14 @@ class ClashRuntime(private val context: Context) {
}
}
} finally {
modules.reversed().forEach {
it.onStop()
runCatching {
modules.reversed().forEach {
it.onStop()
}
}
runCatching {
context.unregisterReceiver(receiver)
}
Clash.stop()

View File

@@ -9,6 +9,7 @@ import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.service.util.parseCIDR
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.lang.NullPointerException
class TunModule(private val service: VpnService) : Module() {
interface Configure {
@@ -63,8 +64,12 @@ class TunModule(private val service: VpnService) : Module() {
builder.setMetered(false)
}
val fd = builder.establish()
?: return@withContext c.onCreateTunFailure()
val fd = try {
builder.establish() ?: throw NullPointerException()
}
catch (e: Exception) {
return@withContext c.onCreateTunFailure()
}
if (c.dnsHijacking) {
Clash.startTunDevice(

View File

@@ -4,9 +4,7 @@ import android.content.Context
import androidx.room.Room
import androidx.room.RoomDatabase
import com.github.kr328.clash.common.Global
import com.github.kr328.clash.service.data.DatabaseMigrations.VERSION_1_2
import com.github.kr328.clash.service.data.DatabaseMigrations.VERSION_2_3
import com.github.kr328.clash.service.data.DatabaseMigrations.VERSION_3_4
import com.github.kr328.clash.service.data.migrations.MIGRATIONS
import androidx.room.Database as DatabaseMetadata
@DatabaseMetadata(
@@ -26,7 +24,7 @@ abstract class Database : RoomDatabase() {
context.applicationContext,
Database::class.java,
"clash-config"
).addMigrations(VERSION_1_2, VERSION_2_3, VERSION_3_4).build()
).addMigrations(*MIGRATIONS).build()
}
}
}

View File

@@ -1,327 +0,0 @@
package com.github.kr328.clash.service.data
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.CONFLICT_ABORT
import android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.edit
import androidx.core.database.getStringOrNull
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.github.kr328.clash.common.Global
import com.github.kr328.clash.common.utils.Log
import com.github.kr328.clash.service.Constants
import com.github.kr328.clash.service.settings.ServiceSettings
import com.github.kr328.clash.service.util.resolveBaseDir
import com.github.kr328.clash.service.util.resolveProfileFile
import java.io.File
object DatabaseMigrations {
val VERSION_1_2 = object : Migration(1, 2) {
private fun process(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _profile_select_proxies")
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `last_update` INTEGER NOT NULL, `update_interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
database.execSQL("CREATE TABLE IF NOT EXISTS `profile_select_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
database.query("SELECT name, token, file, active, last_update, id FROM _profiles")
.use { cursor ->
Global.application.filesDir.resolve(Constants.CLASH_DIR).listFiles()?.forEach {
it.deleteRecursively()
}
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// name, token, file, active, last_update, id
val name = cursor.getString(0)
val token = cursor.getString(1)
val file = cursor.getString(2)
val active = cursor.getInt(3)
val lastUpdate = cursor.getLong(4)
val id = cursor.getLong(5)
// new
// name, type, uri, source, active, last_update, update_interval, id
val type = when {
token.startsWith("url") -> ProfileEntity.TYPE_URL
token.startsWith("file") -> ProfileEntity.TYPE_FILE
else -> ProfileEntity.TYPE_UNKNOWN
}
File(file).renameTo(Global.application.resolveProfileFile(id))
Global.application.resolveBaseDir(id).mkdirs()
database.insert("profiles",
CONFLICT_ABORT,
ContentValues().apply {
put("name", name)
put("type", type)
put("uri", token.removePrefix("url|").removePrefix("file|"))
putNull("source")
put("active", active)
put("last_update", lastUpdate)
put("update_interval", 0)
put("id", id)
})
cursor.moveToNext()
}
}
database.query("SELECT profile_id, proxy, selected FROM _profile_select_proxies ORDER BY id")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// profile_id, proxy, selected, id
val profileId = cursor.getLong(0)
val proxy: String = cursor.getString(1)
val selected = cursor.getString(2)
// new
// profile_id, proxy, selected
database.insert("profile_select_proxies",
CONFLICT_REPLACE,
ContentValues().apply {
put("profile_id", profileId)
put("proxy", proxy)
put("selected", selected)
})
cursor.moveToNext()
}
}
database.execSQL("DROP TABLE IF EXISTS _profiles")
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
// Migration settings
val oldSettings = Global.application
.getSharedPreferences("clash_service", Context.MODE_PRIVATE)
val newSettings = ServiceSettings(
Global.application
.getSharedPreferences(Constants.SERVICE_SETTING_FILE_NAME, Context.MODE_PRIVATE)
)
val accessMode = oldSettings
.getInt("key_access_control_mode", 0)
val accessPackages = oldSettings
.getStringSet("ley_access_control_apps", emptySet())!! // just typo :)
val dnsHijack = oldSettings
.getBoolean("key_dns_hijacking_enabled", true)
val bypassPrivate = oldSettings
.getBoolean("key_bypass_private_network", true)
oldSettings.edit {
clear()
}
newSettings.commit {
val newAccessMode = when (accessMode) {
0 -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
1 -> ServiceSettings.ACCESS_CONTROL_MODE_WHITELIST
2 -> ServiceSettings.ACCESS_CONTROL_MODE_BLACKLIST
else -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
}
put(ServiceSettings.ACCESS_CONTROL_MODE, newAccessMode)
put(ServiceSettings.ACCESS_CONTROL_PACKAGES, accessPackages)
put(ServiceSettings.DNS_HIJACKING, dnsHijack)
put(ServiceSettings.BYPASS_PRIVATE_NETWORK, bypassPrivate)
}
}
override fun migrate(database: SupportSQLiteDatabase) {
try {
process(database)
Log.i("Database Migrated 1 -> 2")
} catch (e: Exception) {
Log.e("Migration failure", e)
}
}
}
val VERSION_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
try {
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _selected_proxies")
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
database.query("SELECT name, type, uri, source, active, update_interval, id FROM _profiles")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// name, type, uri, source, active, last_update, update_interval(seconds), id
// new
// name, type, uri, source, active, interval(millis seconds), id
val name = cursor.getString(0)
val type = cursor.getInt(1)
val uri = cursor.getString(2)
val source = cursor.getStringOrNull(3)
val active = cursor.getInt(4)
val interval = cursor.getLong(5)
val id = cursor.getLong(6)
database.insert("profiles",
CONFLICT_ABORT,
ContentValues().apply {
put("name", name)
put("type", type)
put("uri", uri)
put("source", source)
put("active", active)
put("interval", interval * 1000)
put("id", id)
})
cursor.moveToNext()
}
}
database.query("SELECT profile_id, proxy, selected FROM _selected_proxies")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// just copy
// profile_id, proxy, selected
val profileId = cursor.getLong(0)
val proxy = cursor.getString(1)
val selected = cursor.getString(2)
database.insert("selected_proxies",
CONFLICT_REPLACE,
ContentValues().apply {
put("profile_id", profileId)
put("proxy", proxy)
put("selected", selected)
})
cursor.moveToNext()
}
}
database.execSQL("DROP TABLE IF EXISTS _profiles")
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
val uiSp = Global.application
.getSharedPreferences("ui", Context.MODE_PRIVATE)
val srvSp = Global.application
.getSharedPreferences("service", Context.MODE_PRIVATE)
srvSp.edit {
putBoolean("enable_vpn", uiSp.getBoolean("enable_vpn", true))
}
NotificationManagerCompat.from(Global.application).apply {
deleteNotificationChannel("profile_service_status")
deleteNotificationChannel("profile_service_result")
}
Log.i("Database Migrated 2 -> 3")
} catch (e: Exception) {
Log.e("Migration failure", e)
}
}
}
val VERSION_3_4 = object : Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) {
try {
val profiles = mutableListOf<ProfileEntity>()
try {
database.query("SELECT name, type, uri, source, active, interval, id FROM profiles")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// name, type, uri, source, active, last_update, update_interval(seconds), id
// new
// name, type, uri, source, active, interval(millis seconds), id
val name = cursor.getString(0)
val type = cursor.getInt(1)
val uri = cursor.getString(2)
val source = cursor.getStringOrNull(3)
val active = cursor.getInt(4)
val interval = cursor.getLong(5)
val id = cursor.getLong(6)
profiles.add(ProfileEntity(name, type, uri, source, active != 0, interval, id))
cursor.moveToNext()
}
}
}
catch (e: Exception) {
Log.w("Query old data failure", e)
}
val selectedProxies = mutableListOf<SelectedProxyEntity>()
try {
database.query("SELECT profile_id, proxy, selected FROM selected_proxies")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// just copy
// profile_id, proxy, selected
val profileId = cursor.getLong(0)
val proxy = cursor.getString(1)
val selected = cursor.getString(2)
selectedProxies.add(SelectedProxyEntity(profileId, proxy, selected))
cursor.moveToNext()
}
}
}
catch (e: Exception) {
Log.w("Query old data failure", e)
}
database.execSQL("DROP TABLE IF EXISTS profile_select_proxies")
database.execSQL("DROP TABLE IF EXISTS selected_proxies")
database.execSQL("DROP TABLE IF EXISTS profiles")
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
database.execSQL("DROP TABLE IF EXISTS _profiles")
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
profiles.forEach {
database.insert("profiles", CONFLICT_ABORT, ContentValues().apply {
put("name", it.name)
put("type", it.type)
put("uri", it.uri)
put("source", it.source)
put("active", it.active)
put("interval", it.interval)
put("id", it.id)
})
}
selectedProxies.forEach {
database.insert("selected_proxies", CONFLICT_REPLACE, ContentValues().apply {
put("profile_id", it.profileId)
put("proxy", it.proxy)
put("selected", it.selected)
})
}
Log.i("Database Migrated 3 -> 4")
} catch (e: Exception) {
Log.e("Migration failure", e)
}
}
}
}

View File

@@ -0,0 +1,144 @@
package com.github.kr328.clash.service.data.migrations
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import androidx.core.content.edit
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.github.kr328.clash.common.Global
import com.github.kr328.clash.common.utils.Log
import com.github.kr328.clash.service.Constants
import com.github.kr328.clash.service.data.ProfileEntity
import com.github.kr328.clash.service.settings.ServiceSettings
import com.github.kr328.clash.service.util.resolveBaseDir
import com.github.kr328.clash.service.util.resolveProfileFile
import java.io.File
object Migration12: Migration(1, 2) {
private fun process(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _profile_select_proxies")
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `last_update` INTEGER NOT NULL, `update_interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
database.execSQL("CREATE TABLE IF NOT EXISTS `profile_select_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
database.query("SELECT name, token, file, active, last_update, id FROM _profiles")
.use { cursor ->
Global.application.filesDir.resolve(Constants.CLASH_DIR).listFiles()?.forEach {
it.deleteRecursively()
}
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// name, token, file, active, last_update, id
val name = cursor.getString(0)
val token = cursor.getString(1)
val file = cursor.getString(2)
val active = cursor.getInt(3)
val lastUpdate = cursor.getLong(4)
val id = cursor.getLong(5)
// new
// name, type, uri, source, active, last_update, update_interval, id
val type = when {
token.startsWith("url") -> ProfileEntity.TYPE_URL
token.startsWith("file") -> ProfileEntity.TYPE_FILE
else -> ProfileEntity.TYPE_UNKNOWN
}
File(file).renameTo(Global.application.resolveProfileFile(id))
Global.application.resolveBaseDir(id).mkdirs()
database.insert("profiles",
SQLiteDatabase.CONFLICT_ABORT,
ContentValues().apply {
put("name", name)
put("type", type)
put("uri", token.removePrefix("url|").removePrefix("file|"))
putNull("source")
put("active", active)
put("last_update", lastUpdate)
put("update_interval", 0)
put("id", id)
})
cursor.moveToNext()
}
}
database.query("SELECT profile_id, proxy, selected FROM _profile_select_proxies ORDER BY id")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// profile_id, proxy, selected, id
val profileId = cursor.getLong(0)
val proxy: String = cursor.getString(1)
val selected = cursor.getString(2)
// new
// profile_id, proxy, selected
database.insert("profile_select_proxies",
SQLiteDatabase.CONFLICT_REPLACE,
ContentValues().apply {
put("profile_id", profileId)
put("proxy", proxy)
put("selected", selected)
})
cursor.moveToNext()
}
}
database.execSQL("DROP TABLE IF EXISTS _profiles")
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
// Migration settings
val oldSettings = Global.application
.getSharedPreferences("clash_service", Context.MODE_PRIVATE)
val newSettings = ServiceSettings(
Global.application
.getSharedPreferences(Constants.SERVICE_SETTING_FILE_NAME, Context.MODE_PRIVATE)
)
val accessMode = oldSettings
.getInt("key_access_control_mode", 0)
val accessPackages = oldSettings
.getStringSet("ley_access_control_apps", emptySet())!! // just typo :)
val dnsHijack = oldSettings
.getBoolean("key_dns_hijacking_enabled", true)
val bypassPrivate = oldSettings
.getBoolean("key_bypass_private_network", true)
oldSettings.edit {
clear()
}
newSettings.commit {
val newAccessMode = when (accessMode) {
0 -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
1 -> ServiceSettings.ACCESS_CONTROL_MODE_WHITELIST
2 -> ServiceSettings.ACCESS_CONTROL_MODE_BLACKLIST
else -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
}
put(ServiceSettings.ACCESS_CONTROL_MODE, newAccessMode)
put(ServiceSettings.ACCESS_CONTROL_PACKAGES, accessPackages)
put(ServiceSettings.DNS_HIJACKING, dnsHijack)
put(ServiceSettings.BYPASS_PRIVATE_NETWORK, bypassPrivate)
}
}
override fun migrate(database: SupportSQLiteDatabase) {
try {
process(database)
Log.i("Database Migrated 1 -> 2")
} catch (e: Exception) {
Log.e("Migration failure", e)
}
}
}

View File

@@ -0,0 +1,99 @@
package com.github.kr328.clash.service.data.migrations
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.edit
import androidx.core.database.getStringOrNull
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.github.kr328.clash.common.Global
import com.github.kr328.clash.common.utils.Log
object Migration23: Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
try {
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _selected_proxies")
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
database.query("SELECT name, type, uri, source, active, update_interval, id FROM _profiles")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// name, type, uri, source, active, last_update, update_interval(seconds), id
// new
// name, type, uri, source, active, interval(millis seconds), id
val name = cursor.getString(0)
val type = cursor.getInt(1)
val uri = cursor.getString(2)
val source = cursor.getStringOrNull(3)
val active = cursor.getInt(4)
val interval = cursor.getLong(5)
val id = cursor.getLong(6)
database.insert("profiles",
SQLiteDatabase.CONFLICT_ABORT,
ContentValues().apply {
put("name", name)
put("type", type)
put("uri", uri)
put("source", source)
put("active", active)
put("interval", interval * 1000)
put("id", id)
})
cursor.moveToNext()
}
}
database.query("SELECT profile_id, proxy, selected FROM _selected_proxies")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// just copy
// profile_id, proxy, selected
val profileId = cursor.getLong(0)
val proxy = cursor.getString(1)
val selected = cursor.getString(2)
database.insert("selected_proxies",
SQLiteDatabase.CONFLICT_REPLACE,
ContentValues().apply {
put("profile_id", profileId)
put("proxy", proxy)
put("selected", selected)
})
cursor.moveToNext()
}
}
database.execSQL("DROP TABLE IF EXISTS _profiles")
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
val uiSp = Global.application
.getSharedPreferences("ui", Context.MODE_PRIVATE)
val srvSp = Global.application
.getSharedPreferences("service", Context.MODE_PRIVATE)
srvSp.edit {
putBoolean("enable_vpn", uiSp.getBoolean("enable_vpn", true))
}
NotificationManagerCompat.from(Global.application).apply {
deleteNotificationChannel("profile_service_status")
deleteNotificationChannel("profile_service_result")
}
Log.i("Database Migrated 2 -> 3")
} catch (e: Exception) {
Log.e("Migration failure", e)
}
}
}

View File

@@ -0,0 +1,113 @@
package com.github.kr328.clash.service.data.migrations
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import androidx.core.database.getStringOrNull
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.github.kr328.clash.common.utils.Log
import com.github.kr328.clash.service.data.ProfileEntity
import com.github.kr328.clash.service.data.SelectedProxyEntity
object Migration34: Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) {
try {
val profiles = mutableListOf<ProfileEntity>()
try {
database.query("SELECT name, type, uri, source, active, interval, id FROM profiles")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// old
// name, type, uri, source, active, last_update, update_interval(seconds), id
// new
// name, type, uri, source, active, interval(millis seconds), id
val name = cursor.getString(0)
val type = cursor.getInt(1)
val uri = cursor.getString(2)
val source = cursor.getStringOrNull(3)
val active = cursor.getInt(4)
val interval = cursor.getLong(5)
val id = cursor.getLong(6)
profiles.add(ProfileEntity(name, type, uri, source, active != 0, interval, id))
cursor.moveToNext()
}
}
}
catch (e: Exception) {
Log.w("Query old data failure", e)
}
val selectedProxies = mutableListOf<SelectedProxyEntity>()
try {
database.query("SELECT profile_id, proxy, selected FROM selected_proxies")
.use { cursor ->
cursor.moveToFirst()
while (!cursor.isAfterLast) {
// just copy
// profile_id, proxy, selected
val profileId = cursor.getLong(0)
val proxy = cursor.getString(1)
val selected = cursor.getString(2)
selectedProxies.add(SelectedProxyEntity(profileId, proxy, selected))
cursor.moveToNext()
}
}
}
catch (e: Exception) {
Log.w("Query old data failure", e)
}
// Clean up database
runCatching {
database.execSQL("DROP TABLE IF EXISTS profile_select_proxies")
database.execSQL("DROP TABLE IF EXISTS selected_proxies")
database.execSQL("DROP TABLE IF EXISTS profiles")
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
database.execSQL("DROP TABLE IF EXISTS _profiles")
}
runCatching {
database.execSQL("DROP TABLE IF EXISTS profile_select_proxies")
database.execSQL("DROP TABLE IF EXISTS selected_proxies")
database.execSQL("DROP TABLE IF EXISTS profiles")
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
database.execSQL("DROP TABLE IF EXISTS _profiles")
}
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
profiles.forEach {
database.insert("profiles", SQLiteDatabase.CONFLICT_ABORT, ContentValues().apply {
put("name", it.name)
put("type", it.type)
put("uri", it.uri)
put("source", it.source)
put("active", it.active)
put("interval", it.interval)
put("id", it.id)
})
}
selectedProxies.forEach {
database.insert("selected_proxies",
SQLiteDatabase.CONFLICT_REPLACE, ContentValues().apply {
put("profile_id", it.profileId)
put("proxy", it.proxy)
put("selected", it.selected)
})
}
Log.i("Database Migrated 3 -> 4")
} catch (e: Exception) {
Log.e("Migration failure", e)
}
}
}

View File

@@ -0,0 +1,5 @@
package com.github.kr328.clash.service.data.migrations
import androidx.room.migration.Migration
val MIGRATIONS: Array<Migration> = arrayOf(Migration12, Migration23, Migration34)

View File

@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- exclude 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.18.0.0/16 -->
<!-- exclude 127.0.0.0/8 169.254.0.0/16 10.0.0.0/8 192.168.0.0/16 172.18.0.0/16 -->
<string-array name="bypass_private_route" translatable="false">
<item>0.0.0.0/5</item>
<item>1.0.0.0/8</item>
<item>2.0.0.0/7</item>
<item>4.0.0.0/6</item>
<item>8.0.0.0/7</item>
<item>11.0.0.0/8</item>
<item>12.0.0.0/6</item>
@@ -16,7 +18,16 @@
<item>126.0.0.0/8</item>
<item>128.0.0.0/3</item>
<item>160.0.0.0/5</item>
<item>168.0.0.0/6</item>
<item>168.0.0.0/8</item>
<item>169.0.0.0/9</item>
<item>169.128.0.0/10</item>
<item>169.192.0.0/11</item>
<item>169.224.0.0/12</item>
<item>169.240.0.0/13</item>
<item>169.248.0.0/14</item>
<item>169.252.0.0/15</item>
<item>169.255.0.0/16</item>
<item>170.0.0.0/7</item>
<item>172.0.0.0/12</item>
<item>172.32.0.0/11</item>
<item>172.64.0.0/10</item>
@@ -37,35 +48,37 @@
<item>196.0.0.0/6</item>
<item>200.0.0.0/5</item>
<item>208.0.0.0/4</item>
<item>224.0.0.0/4</item>
<item>240.0.0.0/5</item>
<item>248.0.0.0/6</item>
<item>252.0.0.0/7</item>
<item>254.0.0.0/8</item>
<item>255.0.0.0/9</item>
<item>255.128.0.0/10</item>
<item>255.192.0.0/11</item>
<item>255.224.0.0/12</item>
<item>255.240.0.0/13</item>
<item>255.248.0.0/14</item>
<item>255.252.0.0/15</item>
<item>255.254.0.0/16</item>
<item>255.255.0.0/17</item>
<item>255.255.128.0/18</item>
<item>255.255.192.0/19</item>
<item>255.255.224.0/20</item>
<item>255.255.240.0/21</item>
<item>255.255.248.0/22</item>
<item>255.255.252.0/23</item>
<item>255.255.254.0/24</item>
<item>255.255.255.0/25</item>
<item>255.255.255.128/26</item>
<item>255.255.255.192/27</item>
<item>255.255.255.224/28</item>
<item>255.255.255.240/29</item>
<item>255.255.255.248/30</item>
<item>255.255.255.252/31</item>
<item>255.255.255.254/32</item>
<item>224.0.0.0/3</item>
<item>172.31.255.252/30</item> <!-- tun device address -->
</string-array>
<!-- exclude 127.0.0.0/8 169.254.0.0/16 -->
<string-array name="bypass_local_route" translatable="false">
<item>1.0.0.0/8</item>
<item>2.0.0.0/7</item>
<item>4.0.0.0/6</item>
<item>8.0.0.0/5</item>
<item>16.0.0.0/4</item>
<item>32.0.0.0/3</item>
<item>64.0.0.0/3</item>
<item>96.0.0.0/4</item>
<item>112.0.0.0/5</item>
<item>120.0.0.0/6</item>
<item>124.0.0.0/7</item>
<item>126.0.0.0/8</item>
<item>128.0.0.0/3</item>
<item>160.0.0.0/5</item>
<item>168.0.0.0/8</item>
<item>169.0.0.0/9</item>
<item>169.128.0.0/10</item>
<item>169.192.0.0/11</item>
<item>169.224.0.0/12</item>
<item>169.240.0.0/13</item>
<item>169.248.0.0/14</item>
<item>169.252.0.0/15</item>
<item>169.255.0.0/16</item>
<item>170.0.0.0/7</item>
<item>172.0.0.0/6</item>
<item>176.0.0.0/4</item>
<item>192.0.0.0/2</item>
</string-array>
</resources>