All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.github.tim06.xrayConfiguration.serializer.OutboundsSerializer.kt Maven / Gradle / Ivy

package io.github.tim06.xrayConfiguration.serializer

import io.github.tim06.xrayConfiguration.Protocol
import io.github.tim06.xrayConfiguration.outbounds.Mux
import io.github.tim06.xrayConfiguration.outbounds.Outbound
import io.github.tim06.xrayConfiguration.outbounds.Proxy
import io.github.tim06.xrayConfiguration.outbounds.settings.BlackholeOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.DnsOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.FreedomOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.HttpOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.ShadowsocksOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.SocksOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.TrojanOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.VlessOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.outbounds.settings.VmessOutboundConfigurationObject
import io.github.tim06.xrayConfiguration.settings.StreamSettings
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonEncoder
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.put

object OutboundObjectSerializer : KSerializer {
    override val descriptor: SerialDescriptor = buildClassSerialDescriptor("InboundObject") {
        element(elementName = "sendThrough", isOptional = true)
        element(elementName = "protocol", isOptional = false)
        element(elementName = "settings", isOptional = false)
        element(elementName = "streamSettings", isOptional = true)
        element(elementName = "tag", isOptional = false)
        element(elementName = "proxySettings", isOptional = true)
        element(elementName = "mux", isOptional = true)
    }

    override fun serialize(encoder: Encoder, value: Outbound) {
        val jsonEncoder = encoder as? JsonEncoder ?: throw SerializationException("This class can be saved only by JSON")
        val jsonObject = buildJsonObject {
            put("sendThrough", value.sendThrough)
            put("protocol", jsonEncoder.json.encodeToJsonElement(value.protocol))
            put("settings", jsonEncoder.json.encodeToJsonElement(value.settings))
            put("streamSettings", jsonEncoder.json.encodeToJsonElement(value.streamSettings))
            put("tag", value.tag)
            put("proxySettings", jsonEncoder.json.encodeToJsonElement(value.proxySettings))
            put("mux", jsonEncoder.json.encodeToJsonElement(value.mux))
        }
        jsonEncoder.encodeJsonElement(jsonObject)
    }

    override fun deserialize(decoder: Decoder): Outbound {
        val jsonDecoder = decoder as? JsonDecoder ?: throw SerializationException("This class can be loaded only by JSON")
        val jsonObject = jsonDecoder.decodeJsonElement().jsonObject
        val intermediate = jsonDecoder.json.decodeFromJsonElement(jsonObject)

        val settings = intermediate.settings?.let { settings ->
            when (intermediate.protocol) {
                Protocol.SOCKS -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.HTTP -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.TROJAN -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.FREEDOM -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.BLACKHOLE -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.VLESS -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.SHADOWSOCKS -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.DNS -> jsonDecoder.json.decodeFromJsonElement(settings)
                Protocol.VMESS -> jsonDecoder.json.decodeFromJsonElement(settings)
                else -> throw SerializationException("Unknown protocol type: ${intermediate.protocol}")
            }
        }

        return Outbound(
            sendThrough = intermediate.sendThrough,
            protocol = intermediate.protocol,
            settings = settings,
            streamSettings = intermediate.streamSettings,
            tag = intermediate.tag,
            proxySettings = intermediate.proxySettings,
            mux = intermediate.mux,
        )
    }
}

@Serializable
private data class OutboundObjectIntermediate(
    @SerialName("sendThrough")
    val sendThrough: String? = null,
    @SerialName("protocol")
    val protocol: Protocol? = null,
    @SerialName("settings")
    val settings: JsonObject? = null,
    @SerialName("streamSettings")
    val streamSettings: StreamSettings? = null,
    @SerialName("tag")
    val tag: String,
    @SerialName("proxySettings")
    val proxySettings: Proxy? = null,
    @SerialName("mux")
    val mux: Mux? = null,
)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy