Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bannedbook committed Jun 6, 2020
1 parent 9104b56 commit 814b94b
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 207 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ buildscript {
junitVersion = '4.13'
androidTestVersion = '1.2.0'
androidEspressoVersion = '3.2.0'
versionCode = 5000848
versionName = '5.1.8-nightly'
versionCode = 5000868
versionName = '5.1.10-nightly'
resConfigs = ['ar', 'es', 'fa', 'fr', 'ja', 'ko', 'ru', 'tr', 'zh-rCN', 'zh-rTW']
}

Expand Down
45 changes: 1 addition & 44 deletions core/src/main/java/com/github/shadowsocks/bg/V2RayTestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class V2RayTestService : Service() , BaseService.Interface {
data.connectingJob = GlobalScope.launch(Dispatchers.Main) {
try {
activeProfile = ProfileManager.getProfile(DataStore.profileId)!!
genStoreV2rayConfig()
ProfileManager.genStoreV2rayConfig(activeProfile,true)
startV2ray()
} catch (_: CancellationException) {
// if the job was cancelled, it is canceller's responsibility to call stopRunner
Expand All @@ -80,51 +80,8 @@ class V2RayTestService : Service() , BaseService.Interface {
stopRunner()
return Service.START_NOT_STICKY
}
fun profileToVmessBean(profile: Profile): VmessBean {
var vmess = VmessBean()
vmess.guid=profile.id.toString()
vmess.remoteDns=profile.remoteDns
vmess.address=profile.host
vmess.alterId=profile.alterId
vmess.headerType=profile.headerType
vmess.id=profile.password
vmess.network=profile.network
vmess.path=profile.path
vmess.port=profile.remotePort
vmess.remarks= profile.name.toString()
vmess.requestHost=profile.requestHost
vmess.security=profile.method
vmess.streamSecurity=profile.streamSecurity
vmess.subid=profile.url_group
vmess.testResult=profile.elapsed.toString()

if(profile.route=="all")vmess.route="0"
else if(profile.route=="bypass-lan")vmess.route="1"
else if(profile.route=="bypass-china")vmess.route="2"
else if(profile.route=="bypass-lan-china")vmess.route="3"
else vmess.route="0"

return vmess
}
/**
* gen and store v2ray config file
*/
fun genStoreV2rayConfig(): Boolean {
try {
val result = V2rayConfigUtil.getV2rayConfig(Core.app, profileToVmessBean(activeProfile))
if (result.status) {
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG, result.content)
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG_GUID, activeProfile.id.toString())
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG_NAME, activeProfile.name)
return true
} else {
return false
}
} catch (e: Exception) {
e.printStackTrace()
return false
}
}

override fun onLowMemory() {
stopV2Ray()
Expand Down
50 changes: 4 additions & 46 deletions core/src/main/java/com/github/shadowsocks/bg/V2RayVpnService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class V2RayVpnService : VpnService() , BaseService.Interface{
activeProfile = ProfileManager.getProfile(DataStore.profileId)!!
val proxy = V2ProxyInstance(v2rayPoint,activeProfile,activeProfile.route)
data.proxy = proxy
genStoreV2rayConfig()
ProfileManager.genStoreV2rayConfig(activeProfile)
startV2ray()
} catch (_: CancellationException) {
// if the job was cancelled, it is canceller's responsibility to call stopRunner
Expand All @@ -151,53 +151,10 @@ class V2RayVpnService : VpnService() , BaseService.Interface{
}
}
stopRunner()
//stopRunner(false,null)
return Service.START_NOT_STICKY
}
fun profileToVmessBean(profile: Profile): VmessBean {
var vmess = VmessBean()
vmess.guid=profile.id.toString()
vmess.remoteDns=profile.remoteDns
vmess.address=profile.host
vmess.alterId=profile.alterId
vmess.headerType=profile.headerType
vmess.id=profile.password
vmess.network=profile.network
vmess.path=profile.path
vmess.port=profile.remotePort
vmess.remarks= profile.name.toString()
vmess.requestHost=profile.requestHost
vmess.security=profile.method
vmess.streamSecurity=profile.streamSecurity
vmess.subid=profile.url_group
vmess.testResult=profile.elapsed.toString()

if(profile.route=="all")vmess.route="0"
else if(profile.route=="bypass-lan")vmess.route="1"
else if(profile.route=="bypass-china")vmess.route="2"
else if(profile.route=="bypass-lan-china")vmess.route="3"
else vmess.route="0"

return vmess
}
/**
* gen and store v2ray config file
*/
fun genStoreV2rayConfig(): Boolean {
try {
val result = V2rayConfigUtil.getV2rayConfig(Core.app, profileToVmessBean(activeProfile))
if (result.status) {
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG, result.content)
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG_GUID, activeProfile.id.toString())
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG_NAME, activeProfile.name)
return true
} else {
return false
}
} catch (e: Exception) {
e.printStackTrace()
return false
}
}


override fun onRevoke() {
stopV2Ray()
Expand All @@ -211,6 +168,7 @@ class V2RayVpnService : VpnService() , BaseService.Interface{
override fun onDestroy() {
super.onDestroy()
data.binder.close()
//stopRunner(false,null)
//cancelNotification()
}

Expand Down
57 changes: 52 additions & 5 deletions core/src/main/java/com/github/shadowsocks/database/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ data class Profile(

var host: String = sponsored,
var remotePort: Int = 8388,
var password: String = "u1rRWTssNv0p",
var password: String = "",
var method: String = "aes-256-cfb",

var route: String = "bypass-lan-china",
var route: String = "all", //""bypass-lan-china"
var remoteDns: String = "1.1.1.1",
var proxyApps: Boolean = false,
var bypass: Boolean = false,
Expand Down Expand Up @@ -483,7 +483,7 @@ data class Profile(
profile.udpdns = udpdns
}

fun toUri(): Uri {
fun toSsUri(): Uri {
val auth = Base64.encodeToString("$method:$password".toByteArray(),
Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE)
val wrappedHost = if (host.contains(':')) "[$host]" else host
Expand All @@ -497,12 +497,41 @@ data class Profile(
if (!name.isNullOrEmpty()) builder.fragment(name)
return builder.build()
}

fun toVmessUri(): String {
if (isBuiltin()) return ""
try {
val vmess = ProfileManager.profileToVmessBean(this)
val vmessQRCode = VmessQRCode()
vmessQRCode.v = vmess.configVersion.toString()
vmessQRCode.ps = vmess.remarks
vmessQRCode.add = vmess.address
vmessQRCode.port = vmess.port.toString()
vmessQRCode.id = vmess.id
vmessQRCode.aid = vmess.alterId.toString()
vmessQRCode.net = vmess.network
vmessQRCode.type = vmess.headerType
vmessQRCode.host = vmess.requestHost
vmessQRCode.path = vmess.path
vmessQRCode.tls = vmess.streamSecurity
val json = Gson().toJson(vmessQRCode)
val conf = VMESS_PROTOCOL + encodeForVmess(json)
return conf
} catch (e: Exception) {
e.printStackTrace()
return ""
}
}
fun isSameAs(other: Profile): Boolean = other.host == host

override fun toString() = toUri().toString()
override fun toString() : String {
if (profileType=="ss")
return toSsUri().toString()
else
return toVmessUri()
}

fun toJson(profiles: LongSparseArray<Profile>? = null): JSONObject = JSONObject().apply {
if (profileType=="vmess")return@apply
put("server", host)
put("server_port", remotePort)
put("password", password)
Expand Down Expand Up @@ -553,6 +582,15 @@ data class Profile(
DataStore.plugin = plugin ?: ""
DataStore.udpFallback = udpFallback
DataStore.privateStore.remove(Key.dirty)
//add for vmess begin
DataStore.privateStore.putString(Key.profileType,profileType)
DataStore.privateStore.putString(Key.alterId,alterId.toString())
DataStore.privateStore.putString(Key.network,network)
DataStore.privateStore.putString(Key.headerType,headerType)
DataStore.privateStore.putString(Key.requestHost,requestHost)
DataStore.privateStore.putString(Key.path,path)
DataStore.privateStore.putString(Key.streamSecurity,streamSecurity)
//add for vmess end
}

fun deserialize() {
Expand All @@ -576,6 +614,15 @@ data class Profile(
individual = DataStore.individual
plugin = DataStore.plugin
udpFallback = DataStore.udpFallback
//add for vmess begin
profileType=DataStore.privateStore.getString(Key.profileType) ?: "ss"
alterId=(DataStore.privateStore.getString(Key.alterId)?: "64").toInt()
network=DataStore.privateStore.getString(Key.network) ?: "tcp"
headerType=DataStore.privateStore.getString(Key.headerType) ?: ""
requestHost=DataStore.privateStore.getString(Key.requestHost) ?: ""
path=DataStore.privateStore.getString(Key.path) ?: ""
streamSecurity=DataStore.privateStore.getString(Key.streamSecurity) ?: ""
//add for vmess end
}
fun isBuiltin(): Boolean {
return VpnEncrypt.vpnGroupName == url_group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import android.util.Base64
import android.util.Log
import android.util.LongSparseArray
import com.github.shadowsocks.Core
import com.github.shadowsocks.Core.defaultDPreference
import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.DirectBoot
import com.github.shadowsocks.utils.V2rayConfigUtil
import com.github.shadowsocks.utils.forEachTry
import com.github.shadowsocks.utils.printLog
import com.google.gson.JsonStreamParser
Expand Down Expand Up @@ -314,4 +316,50 @@ object ProfileManager {
return null
}
}

fun profileToVmessBean(profile: Profile): VmessBean {
var vmess = VmessBean()
vmess.guid=profile.id.toString()
vmess.remoteDns=profile.remoteDns
vmess.address=profile.host
vmess.alterId=profile.alterId
vmess.headerType=profile.headerType
vmess.id=profile.password
vmess.network=profile.network
vmess.path=profile.path
vmess.port=profile.remotePort
vmess.remarks= profile.name.toString()
vmess.requestHost=profile.requestHost
vmess.security=profile.method
vmess.streamSecurity=profile.streamSecurity
vmess.subid=profile.url_group
vmess.testResult=profile.elapsed.toString()

if(profile.route=="all")vmess.route="0"
else if(profile.route=="bypass-lan")vmess.route="1"
else if(profile.route=="bypass-china")vmess.route="2"
else if(profile.route=="bypass-lan-china")vmess.route="3"
else vmess.route="0"

return vmess
}
/**
* gen and store v2ray config file
*/
fun genStoreV2rayConfig(activeProfile:Profile,isTest:Boolean=false): Boolean {
try {
val result = V2rayConfigUtil.getV2rayConfig(Core.app, profileToVmessBean(activeProfile),isTest)
if (result.status) {
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG, result.content)
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG_GUID, activeProfile.id.toString())
defaultDPreference.setPrefString(AppConfig.PREF_CURR_CONFIG_NAME, activeProfile.name)
return true
} else {
return false
}
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
}
11 changes: 10 additions & 1 deletion core/src/main/java/com/github/shadowsocks/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ object Key {
const val DB_PUBLIC = "config.db"
const val DB_PROFILE = "profile.db"

const val profileType = "profileType"
const val v2rayVpn = "v2rayVpn"
const val alterId = "alterId"
const val network = "network"
const val headerType = "headerType"
const val requestHost = "requestHost"
const val path = "path"
const val streamSecurity = "streamSecurity"

const val id = "profileId"
const val oldId = "oldProfileId"
const val name = "profileName"
Expand All @@ -38,7 +47,7 @@ object Key {
const val serviceMode = "serviceMode"
const val modeProxy = "proxy"
const val modeVpn = "vpn"
const val v2rayVpn = "v2rayVpn"

const val modeTransproxy = "transproxy"
const val shareOverLan = "shareOverLan"
const val portProxy = "portProxy"
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/com/github/shadowsocks/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import android.os.Build
import android.system.ErrnoException
import android.system.Os
import android.system.OsConstants
import android.util.Base64
import android.util.Log
import android.util.TypedValue
import androidx.annotation.AttrRes
Expand Down Expand Up @@ -275,4 +276,16 @@ private fun Float.toShortString(): String {
if (s.length <= 4)
return s
return s.substring(0, 4).removeSuffix(".")
}

/**
* base64 encode
*/
fun encodeForVmess(text: String): String {
try {
return Base64.encodeToString(text.toByteArray(charset("UTF-8")), Base64.NO_WRAP)
} catch (e: Exception) {
e.printStackTrace()
return ""
}
}
Loading

0 comments on commit 814b94b

Please sign in to comment.