如何快速在服务器上搭建隧道ip
首先申明我也是看了別人的文章,然后第一次搞這個,本文章寫的可能會比較細節,適合沒搭建過的鐵子。
我看的是這篇帖子,我會在他這個帖子上進行一些基礎上的教程。建議可以先去看一下。
5分鐘,自己做一個隧道代理-云社區-華為云隧道代理不需要自己更換 IP,使用起來非常方便。但是隧道 IP 的價格遠遠高于普通代理。本文介紹一種基于普通代理自己搭建隧道代理的方法,能大大節約開發費用。https://bbs.huaweicloud.com/blogs/286309首先無論是在服務器上還是windows上搭建隧道ip,都需要配置好 redis、python3、OpenResty。
redis配置:
???????最前面的可以看這篇文章
????????Linux搭建redis單機 - 僅此而已-遠方 - 博客園
? ? ? ? 然后配置一下redis.conf ,允許外網訪問redis。
? ? ? ? 1、注釋綁定的主機地址
????????????????# bind 127.0.0.1
? ? ? ? 2、修改redis的守護進程為yes,不啟用
????????????????daemonize yes
? ? ? ? 3、修改redis的保護模式為no,不啟用
????????????????protected-mode no
? ? ? ? 修改完之后就重啟一下。
OpenResty配置
? ? ? ? ? ? ? ? ? ? ? ? 具體可以看一下這篇文章,寫的比較詳細。
? ? ? ? ? ? ? ??Linux下安裝配置OpenResty服務器 - 小得盈滿 - 博客園
? ? ? ? ? ? ? ? ? ? ? ? 安裝好之后修改nginx.conf的內容
? ? ? ? ? ? ? ? ? ? ? ? 原先里面的內容都不要,全部刪掉,直接替換成下面這個,修改里面需要改的內容,如logs地址、redis地址密碼等等,其余的不用改。
worker_processes 16; #nginx worker 數量 error_log /usr/local/openresty/nginx/logs/error.log; #指定錯誤日志文件路徑 這里要根據你自己的路徑來設置 events {worker_connections 1024; } stream {## TCP 代理日志格式定義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"';## TCP 代理日志配置access_log /usr/local/openresty/nginx/logs/access.log tcp_proxy; #這里要根據你自己的路徑來設置open_log_file_cache off;## TCP 代理配置upstream backend{server 127.0.0.2:1101;# 這里不用改balancer_by_lua_block {-- 初始化balancerlocal balancer = require "ngx.balancer"local host = ""local port = 0host = ngx.ctx.proxy_hostport = ngx.ctx.proxy_port-- 設置 balancerlocal ok, err = balancer.set_current_peer(host, port)if not ok thenngx.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)--建立連接,請在這里配置Redis 的 IP 地址、端口號、密碼和用到的 Key#redis需要改一下,沒有密碼就把下面說的那行刪掉就行了local rhost = "123.45.67.89"local rport = 6739local rpass = "abcdefg"local rkey = "proxy:pool"local ok, err = redis_instance:connect(rhost, rport)ngx.log(ngx.ERR, "1111111 ", ok, " ", err)-- 如果沒有密碼,移除下面這一行local res, err = redis_instance:auth(rpass)local res, err = redis_instance:hkeys(rkey)if not res thenngx.log(ngx.ERR,"res num error : ", err)return redis_instance:close()endmath.randomseed(tostring(ngx.now()):reverse():sub(1, 6))local proxy = res[math.random(#res)]local colon_index = string.find(proxy, ":")local proxy_ip = string.sub(proxy, 1, colon_index - 1)local proxy_port = string.sub(proxy, colon_index + 1)ngx.log(ngx.ERR,"redis data = ", proxy_ip, ":", proxy_port);ngx.ctx.proxy_host = proxy_ipngx.ctx.proxy_port = proxy_portredis_instance:close()}# 下面是本機的端口,也就是爬蟲固定寫死的端口#端口可以改一下,我是改成了5位數listen 0.0.0.0:9976; #監聽本機地址和端口,當使用keeplived的情況下使用keeplived VIPproxy_connect_timeout 3s;proxy_timeout 10s;proxy_pass backend; #這里填寫對端的地址} }改完后啟動一下nginx,這一塊就ok了
那邊配置的文章中提到用Docker來啟動他,你可以用,但是需要配置一下,我這邊是沒有用的,效果是一樣的,不會影響。
往redis里添加Ip
? ? ? ? 這個代碼里面基本上不用改,如果hset報錯說參數異常,就把hset改成hmset就行了。
""" ProxyManager.py ~~~~~~~~~~~~~~~~~~~~~ 簡易代理池管理工具,直接從URL中讀取所有 最新的代理,并寫入Redis。 """ 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 read_config(self):with open('config.yaml') as f:config = yaml.safe_load(f.read())return configdef read_ip(self):resp = requests.get(self.config['proxy']).textif '{' in resp:return []proxy_list = resp.split()return proxy_listdef delete_ip(self, live_ips, pool_ips):ip_to_removed = set(pool_ips) - set(live_ips)if ip_to_removed:print('ip to be removed:', ip_to_removed)self.client.hdel(self.redis_config['key'], *list(ip_to_removed))def add_new_ips(self, live_ips, pool_ips):ip_to_add = set(live_ips) - set(pool_ips)if ip_to_add:print('ip to add:', ip_to_add)ips = {}for ip in ip_to_add:ips[ip] = json.dumps({'private_ip': ip,'ts': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')})self.client.hset(self.redis_config['key'], mapping=ips)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()存IP配置文檔
? ? ? ? 有一點,鍵: 值 中間一點要有個空格,不要就會被識別為一整個字符串。?
redis:host: redis IP地址port: 6379password: 密碼,沒有隨便寫點啥或者刪掉就行了key: 'proxy:pool' proxy:'這里寫去ip代理的url'?到這里配置完之后基本上是沒有什么問題了。
歡迎隨時技術交流與探討,wx:Fzk666_
總結
以上是生活随笔為你收集整理的如何快速在服务器上搭建隧道ip的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rssi室内定位算法原理_基于RSSI的
- 下一篇: caj转pdf功能实现