如何快速在服务器上搭建隧道ip
首先申明我也是看了別人的文章,然后第一次搞這個(gè),本文章寫的可能會(huì)比較細(xì)節(jié),適合沒搭建過的鐵子。
我看的是這篇帖子,我會(huì)在他這個(gè)帖子上進(jìn)行一些基礎(chǔ)上的教程。建議可以先去看一下。
5分鐘,自己做一個(gè)隧道代理-云社區(qū)-華為云隧道代理不需要自己更換 IP,使用起來非常方便。但是隧道 IP 的價(jià)格遠(yuǎn)遠(yuǎn)高于普通代理。本文介紹一種基于普通代理自己搭建隧道代理的方法,能大大節(jié)約開發(fā)費(fèi)用。https://bbs.huaweicloud.com/blogs/286309首先無論是在服務(wù)器上還是windows上搭建隧道ip,都需要配置好 redis、python3、OpenResty。
redis配置:
???????最前面的可以看這篇文章
????????Linux搭建redis單機(jī) - 僅此而已-遠(yuǎn)方 - 博客園
? ? ? ? 然后配置一下redis.conf ,允許外網(wǎng)訪問redis。
? ? ? ? 1、注釋綁定的主機(jī)地址
????????????????# bind 127.0.0.1
? ? ? ? 2、修改redis的守護(hù)進(jìn)程為yes,不啟用
????????????????daemonize yes
? ? ? ? 3、修改redis的保護(hù)模式為no,不啟用
????????????????protected-mode no
? ? ? ? 修改完之后就重啟一下。
OpenResty配置
? ? ? ? ? ? ? ? ? ? ? ? 具體可以看一下這篇文章,寫的比較詳細(xì)。
? ? ? ? ? ? ? ??Linux下安裝配置OpenResty服務(wù)器 - 小得盈滿 - 博客園
? ? ? ? ? ? ? ? ? ? ? ? 安裝好之后修改nginx.conf的內(nèi)容
? ? ? ? ? ? ? ? ? ? ? ? 原先里面的內(nèi)容都不要,全部刪掉,直接替換成下面這個(gè),修改里面需要改的內(nèi)容,如logs地址、redis地址密碼等等,其余的不用改。
worker_processes 16; #nginx worker 數(shù)量 error_log /usr/local/openresty/nginx/logs/error.log; #指定錯(cuò)誤日志文件路徑 這里要根據(jù)你自己的路徑來設(shè)置 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; #這里要根據(jù)你自己的路徑來設(shè)置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-- 設(shè)置 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")--創(chuàng)建實(shí)例local redis_instance = redis:new()--設(shè)置超時(shí)(毫秒)redis_instance:set_timeout(3000)--建立連接,請(qǐng)?jiān)谶@里配置Redis 的 IP 地址、端口號(hào)、密碼和用到的 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)-- 如果沒有密碼,移除下面這一行l(wèi)ocal 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()}# 下面是本機(jī)的端口,也就是爬蟲固定寫死的端口#端口可以改一下,我是改成了5位數(shù)listen 0.0.0.0:9976; #監(jiān)聽本機(jī)地址和端口,當(dāng)使用keeplived的情況下使用keeplived VIPproxy_connect_timeout 3s;proxy_timeout 10s;proxy_pass backend; #這里填寫對(duì)端的地址} }改完后啟動(dòng)一下nginx,這一塊就ok了
那邊配置的文章中提到用Docker來啟動(dòng)他,你可以用,但是需要配置一下,我這邊是沒有用的,效果是一樣的,不會(huì)影響。
往redis里添加Ip
? ? ? ? 這個(gè)代碼里面基本上不用改,如果hset報(bào)錯(cuò)說參數(shù)異常,就把hset改成hmset就行了。
""" ProxyManager.py ~~~~~~~~~~~~~~~~~~~~~ 簡(jiǎn)易代理池管理工具,直接從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配置文檔
? ? ? ? 有一點(diǎn),鍵: 值 中間一點(diǎn)要有個(gè)空格,不要就會(huì)被識(shí)別為一整個(gè)字符串。?
redis:host: redis IP地址port: 6379password: 密碼,沒有隨便寫點(diǎn)啥或者刪掉就行了key: 'proxy:pool' proxy:'這里寫去ip代理的url'?到這里配置完之后基本上是沒有什么問題了。
歡迎隨時(shí)技術(shù)交流與探討,wx:Fzk666_
總結(jié)
以上是生活随笔為你收集整理的如何快速在服务器上搭建隧道ip的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rssi室内定位算法原理_基于RSSI的
- 下一篇: caj转pdf功能实现