网络配置和合理上网:若干种方式
你也可以通过另外一篇文章 ShadowSS安装 来合理上网。
一.前言
在伟大的某国,某些网络会被受限,有一堵特别长的墙阻碍着你.作为一名开发工程师或者谷歌系语言工具开发者, 上个技术论坛或者某语言官网,只要访问谷歌API全部KO!好吧,我们来搭一个VPN或者一个代理服务器吧!
你只要有一台境外或者能不受某国域名污染的机器, 如亚马逊云。
你可以通过VPN合理上网,也可以在在浏览器等客户端中设置合理上网(http或sock5)!
以下操作系统均是CentOS7!假设我们的机器IP是 45.45.45.45
。
然后我假设你知道如何在本地的 Window 操作系统使用 VPN 等!
二.方式一: SSH高级功能
sock5代理
ssh -ND 1082 ubuntu@45.45.45.45
在本地开一个1082端口,做动态转发,暴露为socks5协议。你可以使用 proxychains4
或 google SwitchyOmega
设置此代理。
这时可以使用sock5 127.0.0.1 1082
代理, 所有经过此代理的 TCP 请求会经远程主机 45.45.45.45
转发…
你还可以双重转发, 比如你可以经主机1去合理上网,但是主机1合理上网速度慢,你可以让主机1通过主机2接力合理上网:
viaproxy 'socks5 127.0.0.1 1082' ssh -ND 1081 ubuntu@2.2.2.2
我们本地 1082
是代理, 经 45.45.45.45
转发, 现在我们 45.45.45.45
继续转发给 2.2.2.2
viaproxy
脚本参考(需要安装 proxychains4):
#!/bin/bash
# Author: wonderbeyond@gmail.com
# Usage: viaproxy "socks5 127.0.0.1 1080" telnet some-host
command -v proxychains4 && proxychains=proxychains4 || proxychains=proxychains
proxy="$1"
shift
$proxychains -f <(/bin/echo -e "strict_chain\nproxy_dns\nlocalnet 127.0.0.0/255.0.0.0\n[ProxyList]\n$proxy") "$@"
隧道(跳板机或堡垒机)
本地是host1:
情形一: host1和host3互通,host3和host2互通, host1和host2不通, 需要host3做接力让host1访问host2
# 本地端口转发
# 在host1执行
# 本地端口转发, 开一个 5556 端口,以目标服务器身份访问指定IP的指定端口。所有经过本地 5556 的数据都会由 host3 转到 host2 的 555 端口
ssh -NL 5556:host2:5555 ubuntu@host3
# 在host1执行
# 本地端口转发, 开一个 9901 端口,以目标服务器身份访问指定IP的指定端口。所有经过本地 9901 的数据都会由 host3 转到 host2 的 22 端口
ssh -NL 9901:host2:22 ubuntu@host3
# host1 只要登录本地 9901 就可以通过 host3 ssh 登录host2
ssh -p 9901 localhost
情形二: host3能访问host1, 但host1不能访问host3, host3和host2互通, host1和host2不通, 需要host3做接力让host1访问host2, 此时情形一的本地端口转发失效
# 远程端口转发
# 在host3执行
# 在host3起命令, 登录host1, 让host1监听2121端口
ssh -R 2121:host2:21 host1
# 在host1执行
# 在host1经2121传数据, 会被host3收到并送到host2的21端口
ftp localhost:2121
三.方式二: 远程机器,安装VPN服务器
虚拟专用网络的功能是:在公用网络上建立专用网络,进行加密通讯。在企业网络中有广泛应用。VPN网关通过对数据包的加密和数据包目标地址的转换实现远程访问。VPN有多种分类方式,主要是按协议进行分类。VPN可通过服务器、硬件、软件等多种方式实现。
1.安装PPTP,以用来提供VPN服务.
yum install pptpd.x86_64 # 按tab补全
2.配置VPN
vim /etc/pptpd.conf
localip 192.168.0.1
remoteip 192.168.0.12-238,192.168.0.245
localip是主机VPN内网地址,remoteip是连接VPN分配的内网地址,也就是说VPN这个网络里实际上形成一个局域网, 每个连接到这个网络的远程机器都有一个局域网IP!这个上面12-238有226台机器,所以同时可以有两百多个客户连接到这个VPN.
3.分配账号给自己使用.
vim /etc/ppp/chap-secrets
在里面添加账户按如下格式
大头 pptpd 123456 *
大头1 pptpd 123456 *
大头为你的用户名,123456为你的密码,最后的*号表示允许在任意IP连接到服务
然后我们重启这个东东!
service pptpd restart
重启服务后如果发现还访问不了外网。我们需要让他能访问外网。首先,
vim /etc/ppp/pptpd-options
文件名可能为
vim /etc/ppp/options.pptpd
找到ms-dns,取消掉注释,改成你喜欢的DNS比如8.8.8.8,8.8.4.4
再开启内核IP转发
sudo vi /etc/sysctl.conf
# 取消掉 net.ipv4.ip_forward=1 这一行的注释. 可能找不到这一句
然后执行
sudo sysctl -p
使修改后的文件配置立即生效。
开启NAT转发.
NAT(Network Address Translation,网络地址转换)是1994年提出的。当在专用网内部的一些主机本来已经分配到了本地IP地址(即仅在本专用网内使用的专用地址),但现在又想和因特网上的主机通信(并不需要加密)时,可使用NAT方法。
这种方法需要在专用网连接到因特网的路由器上安装NAT软件。装有NAT软件的路由器叫做NAT路由器,它至少有一个有效的外部全球IP地址。这样,所有使用本地地址的主机在和外界通信时,都要在NAT路由器上将其本地地址转换成全球IP地址,才能和因特网连接。
另外,这种通过使用少量的公有IP 地址代表较多的私有IP 地址的方式,将有助于减缓可用的IP地址空间的枯竭。在RFC 1632中有对NAT的说明
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eno16777728 -j MASQUERADE
service iptables save
service iptables restart
192.168.0.0/24
是你在上面设置的IP段,让这个段转发,注意 eno16777728
是你连接外网的那块网卡,这样就以NAT的方式请求外网的东西了。
不知道你的机器哪块网卡连的外网的话ifconfig
一下!!
再来一次
service pptpd restart
如果还是无法使用VPN,防火墙上,全部接受,完美!!
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P INPUT ACCEPT
service iptables save
service iptables restart
没有iptables
?那先装一个!
#先检查是否安装了iptables
service iptables status
#安装iptables
yum install -y iptables
#升级iptables
yum update iptables
#安装iptables-services
yum install iptables-services
#停止firewalld服务
systemctl stop firewalld
#禁用firewalld服务
systemctl mask firewalld
#查看iptables现有规则
iptables -L -n
#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT
#清空所有默认规则
iptables -F
#清空所有自定义规则
iptables -X
#所有计数器归0
iptables -Z
#允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
#开放22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#开放21端口(FTP)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#开放80端口(HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#开放5902端口(VCN)
iptables -A INPUT -p tcp --dport 5902 -j ACCEPT
#开放5901端口(VNC root)
iptables -A INPUT -p tcp --dport 5901 -j ACCEPT
#开放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#开放3306端口(MYSQL)
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
#允许ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
#允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#其他入站一律丢弃
iptables -P INPUT DROP
#所有出站一律绿灯
iptables -P OUTPUT ACCEPT
#所有转发一律丢弃
iptables -P FORWARD DROP
#保存上述规则
service iptables save
#注册iptables服务
#相当于以前的chkconfig iptables on
systemctl enable iptables.service
#开启服务
systemctl start iptables.service
#查看状态
systemctl status iptables.service
#重启
systemctl restart iptables.service
查看/etc/sysconfig/iptables
,结果是这样的
[root@spider2 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Fri Nov 4 10:01:51 2016
*filter
:INPUT ACCEPT [4:192]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [49:7158]
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5902 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5901 -j ACCEPT
COMMIT
# Completed on Fri Nov 4 10:01:51 2016
# Generated by iptables-save v1.4.21 on Fri Nov 4 10:01:51 2016
*nat
:PREROUTING ACCEPT [50:2445]
:INPUT ACCEPT [16:835]
:OUTPUT ACCEPT [24:1821]
:POSTROUTING ACCEPT [24:1821]
-A POSTROUTING -s 192.168.0.0/24 -o eno16777728 -j MASQUERADE
COMMIT
# Completed on Fri Nov 4 10:01:51 2016
到这里一般都可以了
参考:http://www.cnblogs.com/apexchu/p/4274416.html
四.方式三:安装HTTP代理服务器
1.安装squid
yum -y install squid
# 版本
squid -v
Squid Cache: Version 3.3.8
#开机使用
chkconfig --level 35 squid on
安装密码,大头
是你的用户名,根据提示输入密码,我输入了:12345
yum install httpd-tools
htpasswd -c /etc/squid/passwd 大头
# 一定要设置!!
chmod 777 /etc/squid/passwd
编辑,看后面完整
vim /etc/squid/squid.conf
# squid.conf后加上,请看最下面完整配置
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
完整配置:
#
# Recommended minimum configuration:
#
# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports
# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports
# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager
# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
#http_access allow localnet
#http_access allow localhost
# Uncomment and adjust the following to add a disk cache directory.
# squid是一个缓存服务器,我们不要它的缓存功能
cache_dir ufs /var/spool/squid 100 16 256 no-store
cache deny all
# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid
#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320
# 这里是授权区,需要用户名和密码
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
# Squid normally listens to port 3128
# 我们开808端口给代理服务器
http_port 808
# And finally deny all other access to this proxy
# 拒绝透明代理!
http_access deny all
#http_access allow all
# 转发的头部隐藏,高匿代理
via off
forwarded_for off
follow_x_forwarded_for deny all
request_header_access X-Forwarded-For deny all
#header_access X_Forwarded_For deny all
# 某些情况出错,设置这个!
visible_hostname 大头
保存 然后,检查一下,有没有语法错误:
#squid -k parse
#squid -k reconfigure
如果想透明代理,不需要帐号和密码访问,请去掉http_access deny all
,加上http_access allow all
,
改端口请修改http_port 808
防火墙将这个808端口打开
iptables -A INPUT -p tcp --dport 808 -j ACCEPT
初始化缓存
squid -z #初始化缓存
启动
systemctl start squid.service
查看状态
squid -Nd1
停止
systemctl stop squid.service
重启
systemctl restart squid.service
http://blog.csdn.net/mingzznet/article/details/52921218
2.多IP squid设置!
如果你的机器是多个IP的话,那么从不同IP进来的话也应该由不同的IP来代理,这种适合大型爬虫自己架设代理服务器, 我曾经架设过有五个网关,两百多个IP的一台机器,注意,是一台机器有两百多个IP,2333!
由于IP实在太多,我编了一个程序来实现….hahha
所以
# !/usr/bin/python3.4
# -*-coding:utf-8-*-
# on 2016/11/5.
# 功能:
# 生成IP给Squid配置文件用
# acl ip1 localip 192.168.1.2
# acl ip2 localip 192.168.1.3
# acl ip3 localip 192.168.1.4
# tcp_outgoing_address 192.168.1.2 ip1
# tcp_outgoing_address 192.168.1.3 ip2
# tcp_outgoing_address 192.168.1.4 ip3
if __name__ == "__main__":
ii = 0
dudu = []
try:
while True:
ips = input("如:146.148.149.202-254:")
temp = ips.split("-")
ipend = int(temp[1])
temptemp = temp[0].split(".")
ipprefix = ".".join(temptemp[0:3])
ipbegin = int(temptemp[3])
for i in range(ipbegin, ipend + 1):
dudu.append(ipprefix + "." + str(i))
ii = ii + 1
except Exception as e:
print(e)
pass
for i in dudu:
# acl ip3 localip 192.168.1.4
# tcp_outgoing_address 192.168.1.2 ip1
print("acl ip" + i + " localip " + i)
print("tcp_outgoing_address " + i + " " + "ip" + i)
将以下追加到配置中,然后重启即可:systemctl restart squid.service
acl ip1 localip 192.168.1.2
acl ip2 localip 192.168.1.3
acl ip3 localip 192.168.1.4
tcp_outgoing_address 192.168.1.2 ip1
tcp_outgoing_address 192.168.1.3 ip2
tcp_outgoing_address 192.168.1.4 ip3
五.赠送:多网关和多IP机器配置
有很多方法,我只写一种! 参考: 这篇文章 。
目前一台机器有两个IP网段(IP群),分别对应两个网关,且只有一块网卡,网卡名为eno16777984
比如
IP群:146.148.149.202-254 网关:146.148.149.193
IP群:146.148.150.194-254 网关:146.148.150.193
1.多网关绑定
我们要将一个网关和一个网段绑定,这样网段中的IP出去的时候才会从指定的网关出去
在/etc/sysconfig/static-routes
:写入路由信息,如果没有这个文件自己创建 (尝试过十分好用)!(推荐!!!!!!)
any net 146.148.149.0/24 gw 146.148.149.193
any net 146.148.150.0/24 gw 146.148.150.193
any net 0.0.0.0/0 gw 146.148.149.193 # 默认路由写法,这里的子网掩码为0
创建完请这样,ifcfg-Wired_connection_1
这个是你自己的网络连接,要自己把握哦
cd /etc/sysconfig/network-scripts
ifdown ifcfg-Wired_connection_2 && ifup ifcfg-Wired_connection_2 && service network restart
[root@centos7 network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 146.148.149.193 0.0.0.0 UG 0 0 0 eno16777984
0.0.0.0 146.148.149.193 0.0.0.0 UG 100 0 0 eno16777984
146.148.149.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777984
146.148.150.0 146.148.150.193 255.255.255.0 UG 0 0 0 eno16777984
146.148.150.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777984
修改好后保存退出,然后重启网络:
service network restart
2.多IP设置
如在/etc/sysconfig/network-scripts/ifcfg-eno16777984
手填,网关不要设置
HWADDR=00:50:56:83:7B:B0
TYPE=Ethernet
BOOTPROTO=none
DNS1=8.8.8.8
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME="Wired connection 2"
UUID=94ce4100-6639-4950-8d94-6cd594a2759c
ONBOOT=yes
IPADDR1=146.148.149.203
PREFIX1=24
IPADDR2=146.148.149.204
PREFIX2=24
IPADDR3=146.148.149.205
PREFIX3=24
IPADDR4=146.148.150.194
PREFIX4=24
IPADDR=146.148.149.202
PREFIX=24
#GATEWAY=
写了个程序批量生成:
# !/usr/bin/python3.4
# -*-coding:utf-8-*-
# on 2016/11/5.
# 功能:
# 生成IP给Linux配置文件用
# IPADDR=146.148.149.202
# PREFIX=24
# IPADDR1=146.148.149.203
# PREFIX1=24
# IPADDR2=146.148.149.204
# PREFIX2=24
# IPADDR3=146.148.149.205
# PREFIX3=24
# IPADDR4=146.148.150.194
# PREFIX4=24
if __name__ == "__main__":
ii = 0
dudu = []
try:
while True:
ips = input("如:146.148.149.202-254:")
temp = ips.split("-")
ipend = int(temp[1])
temptemp = temp[0].split(".")
ipprefix = ".".join(temptemp[0:3])
ipbegin = int(temptemp[3])
for i in range(ipbegin, ipend + 1):
if ii == 0:
a = "IPADDR="
b = "PREFIX="
else:
a = "IPADDR" + str(ii) + "="
b = "PREFIX" + str(ii) + "="
dudu.append(a + ipprefix + "." + str(i))
dudu.append(b + "24")
ii = ii + 1
except Exception as e:
print(e)
pass
for i in dudu:
print(i)
然后
ifdown ifcfg-Wired_connection_1
ifup ifcfg-Wired_connection_1
六.客户端使用
1.proxychains
命令行代理工具用法,任何命令只需在前面加上
proxychains go get -u -v github.com/hunterhug/huhu
安装:
yum install proxychains # Centos7系统
sudo apt-get install proxychains # Ubuntu系统
配置,我们之前装了代理squid服务器,端口808
,帐号 大头
, 密码 12345
, 所以:
在 /etc/proxychains.conf
最下面加一行,http 45.45.45.45 808 "大头" "12345"
格式是这样的
# ProxyList format
# type host port [user pass]
# (values separated by 'tab' or 'blank')
#
#
# Examples:
#
# socks5 192.168.67.78 1080 lamer secret
# http 192.168.89.3 8080 justu hidden
# socks4 192.168.1.49 1080
# http 192.168.39.93 8080
#
#
# proxy types: http, socks4, socks5
# ( auth types supported: "basic"-http "user/pass"-socks )
#
2. 浏览器代理插件
请下载chrome浏览器, 自己想办法 this 。
然后下载代理上网插件 Proxy-Switch 参见说明配置。