0
点赞
收藏
分享

微信扫一扫

自建使用隧道代理全教程,太难?阿布云快代理小象代理的隧道代理用谁的?

圣杰 2024-08-29 阅读 23

在信息技术飞速发展的今天,通过部署隧道代理,可以实现对数据流的加密隧道化,从而增强网络通讯的整体安全性,也可以0代码接入大量使用隧道代理IP,极大简化数据采集过程中的使用代理IP的过程。

不过隧道代理技术相对而言还是会更难一些,成本也更高,所以目前市面上的代理IP服务提供商有这项服务的不多,所以今天,我们就来讨论一下,要如何部署隧道代理,以便我们日常使用。

前期准备

准备一波可以用的代理IP,免费的不建议啦,2024了,不会还有人去用免费的吧。

随便挑一家量大实惠的代理IP服务提供商都行啊:

自建使用隧道代理全教程,太难?阿布云快代理小象代理的隧道代理用谁的?_IP


随便挑,OK?你自己看上的就行。

老规矩了,安装requests库:

pip install requests


接着就进入我们的重点:搭建隧道

如前面,我用的是青果网络的代理IP,具体你们可以替换成你们自己买的API地址。

import requests
import time

proxy_api_url = 'https://api.qg.net.com/getip?appid=YOUR_ID&appkey=YOUR_KEY&num=1&proxy_type=http'

proxy_server = 'PROXY_IP'
proxy_port = PROXY_PORT

target_url = '目标网站'

def get_and_use_proxy():
    while True:
        try:
            response = requests.get(proxy_api_url)
            proxy_json = response.json()
            proxy = proxy_json[0]['proxy']

            response = requests.get(target_url, proxies={'http': f'http://{proxy_server}:{proxy_port}'})
    
            if response.status_code == 200:
                print(response.text)
            else:
                print(f'Request failed with status code: {response.status_code}')
        except Exception as e:
            print(f'Error: {str(e)}')

        time.sleep(10)

proxy_thread = threading.Thread(target=get_and_use_proxy)
proxy_thread.start()

同时,记得用 Redis 的 Hash 这个数据结构周期性访问url,拉取当前最新可用的HTTP代理。

部分代码如下:

import yaml
import time
import json
import redis
import datetime
import requests
 
 
class ProxyManager:
    def __init__(self):
        self.config = self.read_config()
        self.redis_config = self.config['redis']
        self.client = redis.Redis(host=self.redis_config['host'],
                                  password=self.redis_config['password'],
                                  port=self.redis_config['port'])
        self.instance_dict = {}

 
    def run(self):
        while True:
            live_ips = self.read_ip()
            pool_ips = [x.decode() for x in self.client.hgetall(self.redis_config['key'])]
            self.delete_ip(live_ips, pool_ips)
            self.add_new_ips(live_ips, pool_ips)
            time.sleep(40)
 
 
if __name__ == '__main__':
    manager = ProxyManager()
    manager.run()


我们最好确保这波从url拉下来的代理IP是有效的,可以自己设置程序检查筛出,不具体说了,过。

接下来就是设置代理IP自动转发这一过程,我们可以使用 OpenResty 帮我们实现。

部分代码展示:

worker_processes  16; 
error_log /usr/local/openresty/nginx/logs/error.log;  
events {
    worker_connections 1024;
}
 
 
stream {
    log_format tcp_proxy '$remote_addr [$time_local] '
                         '$protocol $status $bytes_sent $bytes_received '
                         '$session_time "$upstream_addr" '
                         '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    access_log /usr/local/openresty/nginx/logs/access.log tcp_proxy;
    open_log_file_cache off;
 
    upstream backend{
        server 127.0.0.2:1101;
        balancer_by_lua_block {
            local balancer = require "ngx.balancer"
            local host = ""
            local port = 0
            host = ngx.ctx.proxy_host
            port = ngx.ctx.proxy_port
            local ok, err = balancer.set_current_peer(host, port)
            if not ok then
                ngx.log(ngx.ERR, "failed to set the peer: ", err)
            end
        }
    }
 
 
    server {
        preread_by_lua_block{
 
            local redis = require("resty.redis")
            local redis_instance = redis:new()
            redis_instance:set_timeout(3000)
            local rhost = "123.45.67.89"
            local rport = 6739
            local rpass = "abcdefg"
            local rkey = "proxy:pool"
            local ok, err = redis_instance:connect(rhost, rport)
            ngx.log(ngx.ERR, "1111111 ", ok, " ", err)
 
            
        }
       listen 0.0.0.0:9976; 
       proxy_connect_timeout 3s;
       proxy_timeout 10s;
       proxy_pass backend; 
    }
}


接着我们用Docker 来启动它,再执行命令和运行就完事了。


隧道代理总体搭建过程就是如此了。当然实际应用中,我们大部分还是会更倾向于购买代理IP服务提供商的隧道代理。限于大规模隧道代理技术问题,目前市面上有这项服务的提供商还不多。我现在使用的是青果网络家的隧道代理,使用下来总体而言还是可以的:

自建使用隧道代理全教程,太难?阿布云快代理小象代理的隧道代理用谁的?_ipv6_02


还会提供数据看板:

自建使用隧道代理全教程,太难?阿布云快代理小象代理的隧道代理用谁的?_IP_03


看了很多家隧道代理的价格以后,不得不说,不是阿布云快代理这种隧道代理IP服务提供商买不起,而是青果网络这种更具有性价比。

说一嘴,他们家还有一个隧道代理的隐藏入口,就这么水灵灵的放在了短效代理这边:

自建使用隧道代理全教程,太难?阿布云快代理小象代理的隧道代理用谁的?_ipv6_04

看着还挺实在的,有需要的朋友们可以自己去了解去测试啦。

举报

相关推荐

xshell 隧道代理

0 条评论