更新一下背景:
源端是 k8s 的 pod,socks 服务器是 k8s 同命名空间的一个 pod,源端通过 svc name 访问 socks 服务器:proxy = "socks-server:1080"
不使用代理时访问 k8s apiclient 如下:这一类请求是可以成功的。
from urllib3.contrib.socks import SOCKSProxyManager
from kubernetes.client import api_client
from kubernetes.client.api import core_v1_api
from kubernetes import client
configuration = client.Configuration()
configuration.verify_ssl = False
configuration.host = "xxx"
configuration.api_key = {"authorization": "Bearer " + self.token}
c = api_client.ApiClient(configuration=configuration)
api = core_v1_api.CoreV1Api(c)
# 查询命名空间,该步骤成功
result = api.list_namespace()
由于k8s使用的是rest client,所以没办法传递socks5代理,手动对实例化后的 rest client 赋值,结果是访问超时(无法连通):
# 省略导入信息
configuration = client.Configuration()
configuration.verify_ssl = False
configuration.api_key = {"authorization": "Bearer " + self.token}
c = api_client.ApiClient(configuration=configuration)
configuration.host = "xxx"
configuration.proxy = "socks5://xxx:1080"
# 该步骤设置socks代理
c.rest_client.pool_manager = SOCKSProxyManager(proxy_url=configuration.proxy)
api = core_v1_api.CoreV1Api(c)
# 查询命名空间,该步骤超时,无法连接
result = api.list_namespace()
通过以上方式发送请求依旧超时,查看socks代理服务器上的请求,发现tcp包里面没有host的信息。
请教下怎么通过 socks5 代理访问 k8s 的 api client,以上的方式是否有误?
你的代码已经正确地设置了代理,但是可能需要进行一些调整以确保使用代理时能正确传递host信息。可以试一下下面的代码,通过在 ApiClient 类中重写 request 方法来添加代理支持:
这段代码创建了一个自定义的 ApiClientWithSocksProxy 类,继承自 ApiClient 并重写了 init 方法,以使用 SOCKSProxyManager 替换默认的连接池管理器。这样做可以确保在使用代理时能正确传递host信息。