Skip to content

Commit

Permalink
Merge branch 'private'
Browse files Browse the repository at this point in the history
  • Loading branch information
eycorsican committed Mar 1, 2019
2 parents 92f2a04 + dc2fe01 commit 2369d19
Show file tree
Hide file tree
Showing 18 changed files with 499 additions and 257 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
applicationVariants.all { variant ->
variant.resValue "string", "coreName", "4.17.0"
variant.resValue "string", "versionName", variant.versionName
}
productFlavors {
}
}
Expand All @@ -36,6 +40,7 @@ dependencies {
implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation "androidx.paging:paging-runtime:2.1.0-rc01"
implementation 'com.beust:klaxon:3.0.1'

implementation(name: 'tun2socks', ext: 'aar')

implementation 'androidx.room:room-runtime:2.1.0-alpha03'
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<activity
android:name=".ui.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down Expand Up @@ -59,6 +60,12 @@
android:parentActivityName=".ui.perapp.PerAppActivity">
</activity>

<activity
android:name=".ui.SubscribeConfigActivity"
android:label="Subscribe Config"
android:parentActivityName=".ui.MainActivity">
</activity>

<service
android:name=".service.SimpleVpnService"
android:permission="android.permission.BIND_VPN_SERVICE" >
Expand Down
200 changes: 200 additions & 0 deletions app/src/main/java/fun/kitsunebi/kitsunebi4android/common/contants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package `fun`.kitsunebi.kitsunebi4android.common

class Constants {
companion object {
val PREFERENCE_CONFIG_KEY = "preference_config_key"
val SUBSCRIBE_CONFIG_URL_KEY = "subscribe_config_url_key"
val DEFAULT_CONFIG = """
{
"outbounds": [
{
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "1.2.3.4",
"port": 10086,
"users": [
{
"id": "0e8575fb-a71f-455b-877f-b74e19d3f495"
}
]
}
]
},
"streamSettings": {
"network": "tcp"
},
"tag": "proxy"
},
{
"protocol": "freedom",
"settings": {
"domainStrategy": "UseIP"
},
"streamSettings": {},
"tag": "direct"
},
{
"protocol": "blackhole",
"settings": {},
"tag": "block"
},
{
"protocol": "dns",
"tag": "dns-out"
}
],
"dns": {
"clientIp": "115.239.211.92",
"hosts": {
"localhost": "127.0.0.1"
},
"servers": [
"114.114.114.114",
{
"address": "8.8.8.8",
"domains": [
"google",
"android",
"fbcdn",
"facebook",
"domain:fb.com",
"instagram",
"whatsapp",
"akamai",
"domain:line-scdn.net",
"domain:line.me",
"domain:naver.jp"
],
"port": 53
}
]
},
"log": {
"loglevel": "warning"
},
"policy": {
"levels": {
"0": {
"bufferSize": 4096,
"connIdle": 30,
"downlinkOnly": 0,
"handshake": 4,
"uplinkOnly": 0
}
}
},
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"inboundTag": ["tun2socks"],
"network": "udp",
"port": 53,
"outboundTag": "dns-out",
"type": "field"
},
{
"domain": [
"domain:setup.icloud.com"
],
"outboundTag": "proxy",
"type": "field"
},
{
"ip": [
"8.8.8.8/32",
"8.8.4.4/32",
"1.1.1.1/32",
"1.0.0.1/32",
"9.9.9.9/32",
"149.112.112.112/32",
"208.67.222.222/32",
"208.67.220.220/32"
],
"outboundTag": "proxy",
"type": "field"
},
{
"ip": [
"geoip:cn",
"geoip:private"
],
"outboundTag": "direct",
"type": "field"
},
{
"outboundTag": "direct",
"port": "123",
"type": "field"
},
{
"domain": [
"domain:pstatp.com",
"domain:snssdk.com",
"domain:toutiao.com",
"domain:ixigua.com",
"domain:apple.com",
"domain:crashlytics.com",
"domain:icloud.com",
"cctv",
"umeng",
"domain:weico.cc",
"domain:jd.com",
"domain:360buy.com",
"domain:360buyimg.com",
"domain:douyu.tv",
"domain:douyu.com",
"domain:douyucdn.cn",
"geosite:cn"
],
"outboundTag": "direct",
"type": "field"
},
{
"ip": [
"149.154.167.0/24",
"149.154.175.0/24",
"91.108.56.0/24",
"125.209.222.0/24"
],
"outboundTag": "proxy",
"type": "field"
},
{
"domain": [
"twitter",
"domain:twimg.com",
"domain:t.co",
"google",
"domain:ggpht.com",
"domain:gstatic.com",
"domain:youtube.com",
"domain:ytimg.com",
"pixiv",
"domain:pximg.net",
"tumblr",
"instagram",
"domain:line-scdn.net",
"domain:line.me",
"domain:naver.jp",
"domain:facebook.com",
"domain:fbcdn.net",
"pinterest",
"github",
"dropbox",
"netflix",
"domain:medium.com",
"domain:fivecdm.com"
],
"outboundTag": "proxy",
"type": "field"
}
],
"strategy": "rules"
}
}
""".trimIndent()
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/fun/kitsunebi/kitsunebi4android/common/utils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package `fun`.kitsunebi.kitsunebi4android.common

import android.app.AlertDialog
import android.content.Context

open class SingletonHolder<out T, in A>(creator: (A) -> T) {
private var creator: ((A) -> T)? = creator
@Volatile private var instance: T? = null
Expand Down Expand Up @@ -30,4 +33,11 @@ public fun humanReadableByteCount(bytes: Long, si: Boolean): String {
val exp = (Math.log(bytes.toDouble()) / Math.log(unit.toDouble())).toInt()
val pre = (if (si) "kMGTPE" else "KMGTPE")[exp - 1] + if (si) "" else "i"
return String.format("%.1f %sB", bytes / Math.pow(unit.toDouble(), exp.toDouble()), pre)
}

public fun showAlert(context: Context, msg: String) {
val dialog = AlertDialog.Builder(context).setTitle("Message").setMessage(msg)
.setPositiveButton("Ok", { dialog, i ->
})
dialog.show()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package `fun`.kitsunebi.kitsunebi4android.service

import `fun`.kitsunebi.kitsunebi4android.R
import `fun`.kitsunebi.kitsunebi4android.common.Constants
import `fun`.kitsunebi.kitsunebi4android.storage.PROXY_LOG_DB_NAME
import `fun`.kitsunebi.kitsunebi4android.storage.Preferences
import `fun`.kitsunebi.kitsunebi4android.storage.ProxyLog
Expand Down Expand Up @@ -160,14 +161,19 @@ open class SimpleVpnService : VpnService() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
configString = Preferences.getString(applicationContext, getString(R.string.preference_config_key), getString(R.string.default_config))
configString = Preferences.getString(applicationContext, Constants.PREFERENCE_CONFIG_KEY, Constants.DEFAULT_CONFIG)

bgThread = thread(start = true) {
val config = Klaxon().parse<Config>(configString)
val config = try { Klaxon().parse<Config>(configString) } catch (e: Exception) {
sendBroadcast(android.content.Intent("vpn_start_err_config"))
stopVPN()
return@thread
}
if (config != null) {
if (config.dns == null || config.dns.servers == null || config.dns.servers.size == 0) {
println("must configure dns servers since v2ray will use localhost if there isn't any dns servers")
sendBroadcast(Intent("vpn_start_err_dns"))
stopVPN()
return@thread
}

Expand All @@ -176,12 +182,14 @@ open class SimpleVpnService : VpnService() {
if (dnsServer != null && dnsServer == "localhost") {
println("using local dns resolver is not allowed since it will cause infinite loop")
sendBroadcast(Intent("vpn_start_err_dns"))
stopVPN()
return@thread
}
}
} else {
println("parsing v2ray config failed")
sendBroadcast(Intent("vpn_start_err"))
stopVPN()
return@thread
}

Expand All @@ -191,7 +199,6 @@ open class SimpleVpnService : VpnService() {
.setMtu(1500)
.addAddress("10.233.233.233", 30)
.addDnsServer(localDns)
.addSearchDomain("local")
.addRoute("0.0.0.0", 0)

val isEnablePerAppVpn = Preferences.getBool(applicationContext, getString(R.string.is_enable_per_app_vpn), null)
Expand Down Expand Up @@ -225,6 +232,7 @@ open class SimpleVpnService : VpnService() {
if ((pfd == null) || !Tun2socks.setNonblock(pfd!!.fd.toLong(), false)) {
println("failed to put tunFd in blocking mode")
sendBroadcast(Intent("vpn_start_err"))
stopVPN()
return@thread
}

Expand All @@ -244,23 +252,49 @@ open class SimpleVpnService : VpnService() {
}

val files = filesDir.list()
if (!files.contains("geoip.dat") || !files.contains("geosite.dat")) {
val geoipBytes = resources.openRawResource(R.raw.geoip).readBytes()
val fos = openFileOutput("geoip.dat", Context.MODE_PRIVATE)
fos.write(geoipBytes)
fos.close()

val geositeBytes = resources.openRawResource(R.raw.geosite).readBytes()
val fos2 = openFileOutput("geosite.dat", Context.MODE_PRIVATE)
fos2.write(geositeBytes)
fos2.close()
}
// FIXME copy only when update
val geoipBytes = resources.openRawResource(R.raw.geoip).readBytes()
val fos = openFileOutput("geoip.dat", Context.MODE_PRIVATE)
fos.write(geoipBytes)
fos.close()

val geositeBytes = resources.openRawResource(R.raw.geosite).readBytes()
val fos2 = openFileOutput("geosite.dat", Context.MODE_PRIVATE)
fos2.write(geositeBytes)
fos2.close()

// if (!files.contains("geoip.dat") || !files.contains("geosite.dat")) {
// val geoipBytes = resources.openRawResource(R.raw.geoip).readBytes()
// val fos = openFileOutput("geoip.dat", Context.MODE_PRIVATE)
// fos.write(geoipBytes)
// fos.close()
//
// val geositeBytes = resources.openRawResource(R.raw.geosite).readBytes()
// val fos2 = openFileOutput("geosite.dat", Context.MODE_PRIVATE)
// fos2.write(geositeBytes)
// fos2.close()
// }

ProxyLogDatabase.getInstance(applicationContext).proxyLogDao().getAllCount()
val dbPath = getDatabasePath(PROXY_LOG_DB_NAME).absolutePath

var sniffing = Preferences.getString(applicationContext, getString(R.string.sniffing), "http,tls")
// Just ensure no whitespaces in the the string.
val sniffingList = sniffing.split(",")
var sniffings = ArrayList<String>()
for (s in sniffingList) {
sniffings.add(s.trim())
}
sniffing = sniffings.joinToString(",")

val inboundTag = Preferences.getString(applicationContext, getString(R.string.inbound_tag), "tun2socks")

Tun2socks.setLocalDNS("$localDns:53")
Tun2socks.startV2Ray(flow, service, dbService, configString.toByteArray(), filesDir.absolutePath, dbPath)
val ret = Tun2socks.startV2Ray(flow, service, dbService, configString.toByteArray(), inboundTag, sniffing, filesDir.absolutePath)
if (ret.toInt() != 0) {
sendBroadcast(Intent("vpn_start_err_config"))
stopVPN()
return@thread
}

sendBroadcast(Intent("vpn_started"))
Preferences.putBool(applicationContext, getString(R.string.vpn_is_running), true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface ProxyLogDao {
@Query("SELECT * FROM proxy_log ORDER BY end_time DESC")
fun getAllPaged(): DataSource.Factory<Int, ProxyLog>

@Query("SELECT * FROM proxy_log WHERE record_type != 1 ORDER BY end_time DESC")
@Query("SELECT * FROM proxy_log WHERE record_type != 1 AND target NOT LIKE 'udp:%:53' ORDER BY end_time DESC")
fun getAllNonDnsPaged(): DataSource.Factory<Int, ProxyLog>

@Insert
Expand Down
Loading

0 comments on commit 2369d19

Please sign in to comment.