Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Restia-Ashbell committed Sep 21, 2024
1 parent 0a75b25 commit d9b7df3
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 64 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/io/nekohasekai/sagernet/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ object Key {
const val MTU = "mtu"
const val ALWAYS_SHOW_ADDRESS = "alwaysShowAddress"

const val CUSTOM_GLOBAL_CONFIG = "customGlobalConfig"

// Protocol Settings
const val MUX_TYPE = "muxType"
const val MUX_PROTOCOLS = "mux"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ object DataStore : OnPreferenceDataStoreChangeListener {
var logBufSize by configurationStore.int(Key.LOG_BUF_SIZE) { 0 }
var acquireWakeLock by configurationStore.boolean(Key.ACQUIRE_WAKE_LOCK)

// profileCacheStore???
var customGlobalConfig by profileCacheStore.string(Key.CUSTOM_GLOBAL_CONFIG)

// hopefully hashCode = mHandle doesn't change, currently this is true from KitKat to Nougat
private val userIndex by lazy { Binder.getCallingUserHandle().hashCode() }
var mixedPort: Int
Expand Down
72 changes: 50 additions & 22 deletions app/src/main/java/io/nekohasekai/sagernet/database/ProxyEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,21 @@ import com.esotericsoftware.kryo.io.ByteBufferInput
import com.esotericsoftware.kryo.io.ByteBufferOutput
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.fmt.*
import io.nekohasekai.sagernet.fmt.http.HttpBean
import io.nekohasekai.sagernet.fmt.http.toUri
import io.nekohasekai.sagernet.fmt.hysteria.*
import io.nekohasekai.sagernet.fmt.internal.ChainBean
import io.nekohasekai.sagernet.fmt.mieru.MieruBean
import io.nekohasekai.sagernet.fmt.mieru.buildMieruConfig
import io.nekohasekai.sagernet.fmt.naive.NaiveBean
import io.nekohasekai.sagernet.fmt.naive.buildNaiveConfig
import io.nekohasekai.sagernet.fmt.naive.toUri
import io.nekohasekai.sagernet.fmt.http.*
import io.nekohasekai.sagernet.fmt.socks.*
import io.nekohasekai.sagernet.fmt.shadowsocks.*
import io.nekohasekai.sagernet.fmt.shadowsocksr.ShadowsocksRBean
import io.nekohasekai.sagernet.fmt.shadowsocksr.toUri
import moe.matsuri.nb4a.proxy.shadowtls.ShadowTLSBean
import io.nekohasekai.sagernet.fmt.socks.SOCKSBean
import io.nekohasekai.sagernet.fmt.socks.toUri
import io.nekohasekai.sagernet.fmt.ssh.SSHBean
import io.nekohasekai.sagernet.fmt.trojan.TrojanBean
import io.nekohasekai.sagernet.fmt.trojan_go.TrojanGoBean
import io.nekohasekai.sagernet.fmt.trojan_go.buildTrojanGoConfig
import io.nekohasekai.sagernet.fmt.trojan_go.toUri
import io.nekohasekai.sagernet.fmt.tuic.TuicBean
import io.nekohasekai.sagernet.fmt.tuic.toUri
import io.nekohasekai.sagernet.fmt.shadowsocksr.*
import io.nekohasekai.sagernet.fmt.v2ray.*
import io.nekohasekai.sagernet.fmt.wireguard.WireGuardBean
import io.nekohasekai.sagernet.fmt.tuic.*
import io.nekohasekai.sagernet.fmt.hysteria.*
import io.nekohasekai.sagernet.fmt.wireguard.*
import io.nekohasekai.sagernet.fmt.ssh.*
import moe.matsuri.nb4a.proxy.shadowtls.*
import io.nekohasekai.sagernet.fmt.mieru.*
import io.nekohasekai.sagernet.fmt.naive.*
import io.nekohasekai.sagernet.fmt.trojan.*
import io.nekohasekai.sagernet.fmt.trojan_go.*
import io.nekohasekai.sagernet.fmt.internal.ChainBean
import io.nekohasekai.sagernet.ktx.app
import io.nekohasekai.sagernet.ktx.applyDefaultValues
import io.nekohasekai.sagernet.ui.profile.*
Expand All @@ -39,6 +30,7 @@ import moe.matsuri.nb4a.proxy.config.ConfigBean
import moe.matsuri.nb4a.proxy.config.ConfigSettingActivity
import moe.matsuri.nb4a.proxy.neko.*
import moe.matsuri.nb4a.proxy.shadowtls.ShadowTLSSettingsActivity
import moe.matsuri.nb4a.utils.JavaUtil.gson

@Entity(
tableName = "proxy_entities", indices = [Index("groupId", name = "groupId")]
Expand Down Expand Up @@ -230,6 +222,42 @@ data class ProxyEntity(
} ?: error("Null ${displayType()} profile")
}

fun buildSingBoxOutbound(bean: AbstractBean): MutableMap<String, Any> {
return when (bean) {
is ConfigBean ->
gson.fromJson<MutableMap<String, Any>>(bean.config, MutableMap::class.java)

is ShadowTLSBean -> // before StandardV2RayBean
buildSingBoxOutboundShadowTLSBean(bean).asMap()

is StandardV2RayBean -> // http/trojan/vmess/vless
buildSingBoxOutboundStandardV2RayBean(bean).asMap()

is HysteriaBean ->
buildSingBoxOutboundHysteriaBean(bean)

is TuicBean ->
buildSingBoxOutboundTuicBean(bean).asMap()

is SOCKSBean ->
buildSingBoxOutboundSocksBean(bean).asMap()

is ShadowsocksBean ->
buildSingBoxOutboundShadowsocksBean(bean).asMap()

is ShadowsocksRBean ->
buildSingBoxOutboundShadowsocksRBean(bean).asMap()

is WireGuardBean ->
buildSingBoxOutboundWireguardBean(bean).asMap()

is SSHBean ->
buildSingBoxOutboundSSHBean(bean).asMap()

else -> throw IllegalStateException("can't reach")
}
}

fun haveLink(): Boolean {
return when (type) {
TYPE_CHAIN -> false
Expand Down
64 changes: 22 additions & 42 deletions app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ import io.nekohasekai.sagernet.database.ProxyEntity.Companion.TYPE_CONFIG
import io.nekohasekai.sagernet.database.SagerDatabase
import io.nekohasekai.sagernet.fmt.ConfigBuildResult.IndexEntity
import io.nekohasekai.sagernet.fmt.hysteria.HysteriaBean
import io.nekohasekai.sagernet.fmt.hysteria.buildSingBoxOutboundHysteriaBean
import io.nekohasekai.sagernet.fmt.internal.ChainBean
import io.nekohasekai.sagernet.fmt.shadowsocks.ShadowsocksBean
import io.nekohasekai.sagernet.fmt.shadowsocks.buildSingBoxOutboundShadowsocksBean
import io.nekohasekai.sagernet.fmt.shadowsocksr.ShadowsocksRBean
import io.nekohasekai.sagernet.fmt.shadowsocksr.buildSingBoxOutboundShadowsocksRBean
import io.nekohasekai.sagernet.fmt.socks.SOCKSBean
import io.nekohasekai.sagernet.fmt.socks.buildSingBoxOutboundSocksBean
import io.nekohasekai.sagernet.fmt.ssh.SSHBean
import io.nekohasekai.sagernet.fmt.ssh.buildSingBoxOutboundSSHBean
import io.nekohasekai.sagernet.fmt.tuic.TuicBean
import io.nekohasekai.sagernet.fmt.tuic.buildSingBoxOutboundTuicBean
import io.nekohasekai.sagernet.fmt.v2ray.StandardV2RayBean
import io.nekohasekai.sagernet.fmt.v2ray.buildSingBoxOutboundStandardV2RayBean
import io.nekohasekai.sagernet.fmt.wireguard.WireGuardBean
import io.nekohasekai.sagernet.fmt.wireguard.buildSingBoxOutboundWireguardBean
import io.nekohasekai.sagernet.ktx.isIpAddress
import io.nekohasekai.sagernet.ktx.mkPort
import io.nekohasekai.sagernet.utils.PackageCache
Expand All @@ -38,6 +30,8 @@ import moe.matsuri.nb4a.utils.JavaUtil.gson
import moe.matsuri.nb4a.utils.Util
import moe.matsuri.nb4a.utils.listByLineOrComma
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.json.JSONArray
import org.json.JSONObject

const val TAG_MIXED = "mixed-in"

Expand Down Expand Up @@ -81,6 +75,25 @@ fun buildConfig(
}
}

if (!DataStore.customGlobalConfig.isNullOrBlank()) {
val jsonObject = JSONObject(DataStore.customGlobalConfig)
val outboundsArray: JSONArray = if (jsonObject.has("outbounds")) {
jsonObject.getJSONArray("outbounds")
} else {
JSONArray().also { jsonObject.put("outbounds", it) }
}
outboundsArray.put(0, JSONObject(proxy.buildSingBoxOutbound(proxy.requireBean()) as Map<Any, Any>))

return ConfigBuildResult(
jsonObject.toString(),
listOf(),
proxy.id, //
mapOf(TAG_PROXY to listOf(proxy)), //
mapOf(proxy.id to TAG_PROXY), //
-1L
)
}

val trafficMap = HashMap<String, List<ProxyEntity>>()
val tagMap = HashMap<Long, String>()
val globalOutbounds = HashMap<Long, String>()
Expand Down Expand Up @@ -350,40 +363,7 @@ fun buildConfig(
server_port = localPort
}.asMap()
} else { // internal outbound
currentOutbound = when (bean) {
is ConfigBean ->
gson.fromJson(bean.config, currentOutbound.javaClass)

is ShadowTLSBean -> // before StandardV2RayBean
buildSingBoxOutboundShadowTLSBean(bean).asMap()

is StandardV2RayBean -> // http/trojan/vmess/vless
buildSingBoxOutboundStandardV2RayBean(bean).asMap()

is HysteriaBean ->
buildSingBoxOutboundHysteriaBean(bean)

is TuicBean ->
buildSingBoxOutboundTuicBean(bean).asMap()

is SOCKSBean ->
buildSingBoxOutboundSocksBean(bean).asMap()

is ShadowsocksBean ->
buildSingBoxOutboundShadowsocksBean(bean).asMap()

is ShadowsocksRBean ->
buildSingBoxOutboundShadowsocksRBean(bean).asMap()

is WireGuardBean ->
buildSingBoxOutboundWireguardBean(bean).asMap()

is SSHBean ->
buildSingBoxOutboundSSHBean(bean).asMap()

else -> throw IllegalStateException("can't reach")
}

currentOutbound = proxyEntity.buildSingBoxOutbound(bean)
currentOutbound.apply {
// TODO nb4a keepAliveInterval?
// val keepAliveInterval = DataStore.tcpKeepAliveInterval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.nekohasekai.sagernet.SagerNet
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.database.preference.EditTextPreferenceModifiers
import io.nekohasekai.sagernet.ktx.*
import io.nekohasekai.sagernet.ui.profile.ConfigEditActivity
import io.nekohasekai.sagernet.utils.Theme
import io.nekohasekai.sagernet.widget.AppListPreference
import moe.matsuri.nb4a.Protocols
Expand Down Expand Up @@ -123,6 +124,18 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
true
}

val customGlobalConfig = findPreference<Preference>(Key.CUSTOM_GLOBAL_CONFIG)!!
customGlobalConfig.setOnPreferenceClickListener {
DataStore.customGlobalConfig = ""
startActivity(
Intent(requireContext(), ConfigEditActivity::class.java).apply {
putExtra("key", Key.CUSTOM_GLOBAL_CONFIG)
}
)
needRestart()
true
}

val muxProtocols = findPreference<MultiSelectListPreference>(Key.MUX_PROTOCOLS)!!

muxProtocols.apply {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
<string name="deprecated">已废弃</string>
<string name="show_direct_speed">显示直连速度</string>
<string name="show_direct_speed_sum">在通知中也显示不经过代理的流量速度</string>
<string name="merge_custom_config">当前出站使用完整自定义配置</string>
<string name="select_profile">选择配置</string>
<string name="proxy_chain">链式代理</string>
<string name="chain_settings">链设置</string>
Expand Down Expand Up @@ -459,6 +460,7 @@
<string name="enable_clash_api_summary">在 127.0.0.1:9090 提供 clash api 和 yacd 仪表板</string>
<string name="xtls_flow">Flow(VLESS 子协议)</string>
<string name="log_level">日志级别</string>
<string name="custom_global_config">自定义全局配置</string>
<string name="ads">推广</string>
<string name="need_restart">重新启动应用程序以应用更改</string>
<string name="use_selector">启用 selector (免重载切换节点)</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="show_direct_speed">Show Direct Speed</string>
<string name="show_direct_speed_sum">Show the traffic speed without proxy in the notification as
well</string>
<string name="merge_custom_config">Merge with currently selected outbound using custom global configuration</string>
<string name="security_settings">TLS Security Settings</string>
<string name="allow_insecure">Allow Insecure</string>
<string name="allow_insecure_sum">Disable certificate checking. When enabled, this configuration
Expand Down Expand Up @@ -309,6 +310,7 @@
<string name="auto">Auto</string>
<string name="enable_log">Enable Log</string>
<string name="enable_log_sum">For debugging purposes</string>
<string name="custom_global_config">Custom global config</string>
<string name="list">List</string>
<string name="random">Random</string>
<string name="leastPing" translatable="false">Ping</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/global_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
app:key="logLevel"
app:title="@string/log_level"
app:useSimpleSummaryProvider="true" />
<Preference
app:key="customGlobalConfig"
app:title="@string/custom_global_config"
app:summary="@string/merge_custom_config"
app:icon="@android:drawable/ic_menu_manage" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/cag_route">
Expand Down

0 comments on commit d9b7df3

Please sign in to comment.