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

com.tencent.devops.common.web.proxy.CustomProxySelector.kt Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.tencent.devops.common.web.proxy

import org.slf4j.LoggerFactory
import sun.net.spi.DefaultProxySelector
import java.io.IOException
import java.net.Proxy
import java.net.SocketAddress
import java.net.URI

class CustomProxySelector(
    private val proxy: Proxy,
    private val proxyHostsRegex: List
) : DefaultProxySelector() {

    override fun select(uri: URI?): MutableList {
        if (uri == null) return mutableListOf(Proxy.NO_PROXY)
        LOG.debug("[CustomProxySelector|try to match $uri]")
        val inHosts = proxyHostsRegex.any {
            val matches = it.containsMatchIn(uri.host)
            if (matches) {
                LOG.debug("[CustomProxySelector|$uri matched $it]")
            }
            matches
        }
        if (inHosts) {
            return mutableListOf(proxy)
        }
        // 匹配不上的保留 Java 原有逻辑
        return super.select(uri)
    }

    override fun connectFailed(uri: URI?, sa: SocketAddress?, ioe: IOException?) {
        LOG.warn("[CustomProxySelector|connect to proxy failed: uri: $uri|sa: $sa|", ioe)
    }

    companion object {
        private val LOG = LoggerFactory.getLogger(CustomProxySelector::class.java.name)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy