Skip to content

Commit

Permalink
fix bash shell bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liuwenru committed Mar 10, 2018
1 parent 0de8639 commit 18ab9c7
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 43 deletions.
1 change: 1 addition & 0 deletions GFlist.txt
@@ -1 +1,2 @@
216.58.194.99
180.97.33.107
21 changes: 0 additions & 21 deletions Home.md

This file was deleted.

100 changes: 90 additions & 10 deletions README.md
@@ -1,21 +1,101 @@
# intelliq-redsocks
实现Linux下的全局翻墙,使用redsocks配合iptables实现请求流量的转发,详细参见[此处Github主页](https://github.com/darkk/redsocks)
# 一、简介

## 一、使用方法
实现Linux下的全局翻墙,使用redsocks配合iptables实现请求流量的转发

1. 安装依赖包

yum install libevent libevent-deve

2. 启动myredsocks

./myredsocks.sh star
有的时候在实验环境中下载一些被`GFW`给墙掉的资源会出现下载不到的情况,好在网上有大神使用`C`写了一个库可以将将本地的流量从一个端口转向`Socket5`的端口,我们在使用一个`SSH -D`参数本地启动一个`Socket5`端口,使用`redsocks`将流量转到对应的`Socket5`上。这样就是就可以实现任何流量都可以翻墙了,由于官方的`redsocks`并没有提供`iptables`的配置方法,所以我写了一个脚本,自动的读取配置文件啥的,帮助快速使用。

感谢大神的项目,详细参见[此处Github主页](https://github.com/darkk/redsocks)

# 一、使用方法

本人已经针对`redsocks`的一个稳定版本编译好了一个直接可以运行的二进制文件,在`Centos`上可以直接使用。如果想编译其他`Linux`版本上的请按照`redsocks`官方文档操作



1. 使用前请安装依赖包
如果是`Centos`操作系统
```bash
Shell> yum install libevent libevent-deve
```
如果是`Ubuntu`操作系统
```bash
Shell> sudo apt-get install libevent-2.0-5 libevent-dev

```

2. 启动myredsocks
```bash
Shell > ./myredsocks.sh start #启动服务进程
start the redsocks........................
please tell me you sock_server:127.0.0.1 #输入socket5代理服务器的地址
please tell me you sock_port:7070 #输入socket5代理服务器的端口
```

3. 选择代理模式

./myredsocks.sh proxyall|prox
**全局代理模式**


```bash
./myredsocks.sh proxyall #启动全局代理模式,此模式下将代理所有的访问
please tell me you network:192.168.188.0/24 #输入你当前主机的网络信息,因为该网段的机器是不需要翻墙访问的
your iptabls OUTPUT chain like this....
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 RETURN tcp -- * * 0.0.0.0/0 192.168.188.0/24
2 0 0 RETURN tcp -- * * 0.0.0.0/0 127.0.0.1
3 0 0 RETURN tcp -- * * 0.0.0.0/0 127.0.0.1
4 0 0 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 redir ports 12345

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
```

**代理指定主机**

该模式下只代理`GFlist.txt`中指定的主机

```bash
Shell> ./myredsocks.sh proxy
please tell me you network:192.168.188.0/24 #输入你的网络信息,通全局代理模式一样,同网段的机器不要翻墙
this ip[216.58.194.99] will use proxy connected ....
this ip[180.97.33.107] will use proxy connected ....
your iptabls OUTPUT chain like this....
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 RETURN tcp -- * * 0.0.0.0/0 192.168.188.0/24
2 0 0 RETURN tcp -- * * 0.0.0.0/0 127.0.0.1
3 0 0 RETURN tcp -- * * 0.0.0.0/0 127.0.0.1
4 0 0 REDIRECT tcp -- * * 0.0.0.0/0 216.58.194.99 redir ports 12345
5 0 0 REDIRECT tcp -- * * 0.0.0.0/0 180.97.33.107 redir ports 12345

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination

```

4. 清理代理与关闭代理


```bash

Shell> ./myredsocks.sh clean #清理所有的代理模式
Shell> ./myredsocks.sh stop #关闭代理

4. 清理代理

./myredsocks.sh clean
```
30 changes: 21 additions & 9 deletions myredsocks.sh
@@ -1,18 +1,28 @@
#!/bin/bash
LINE="==============================================="
OSTYPE=$(cat /etc/os-release | grep -E "^NAME=.*" | awk -F\" '{print $2}')
sock_server="127.0.0.1" #socket5代理服务器
sock_port="7070" #socket5代理端口
proxy_port="12345" #redsock的监听端口


proxy_server="192.168.149.150"
proxy_ip=""
proxy_port="12345"

redsocks_pid="/tmp/redsocks.pid"
function start_redsocks()
{

echo "start the redsocks........................"
if [[ -f ${redsocks_pid} ]];then
echo "the redsocks is stared......"
echo "the redsocks is stared..................."
return 0
fi
rm -rf redsocks.conf
cp redsocks.conf.example redsocks.conf
read -p "please tell me you sock_server:" sock_server
read -p "please tell me you sock_port:" sock_port
sed -i '18s/daemon.*/daemon = on;/g' redsocks.conf
sed -i '44s/local_port.*/local_port = '${proxy_port}';/g' redsocks.conf
sed -i '61s/ip.*/ip = '${sock_server}';/g' redsocks.conf
sed -i '62s/port.*/port = '${sock_port}';/g' redsocks.conf
./redsocks -c redsocks.conf -p ${redsocks_pid}
}
function stop_redsocks()
Expand All @@ -31,10 +41,8 @@ function restart_redsocks()
stop_redsocks
start_redsocks
}

until [ $# -eq 0 ]
do

case $1 in
start)
start_redsocks
Expand All @@ -55,6 +63,10 @@ do
proxy)
#proxy the fwlist.txt
iptables -t nat -F
read -p "please tell me you network:" mynetwork
iptables -t nat -A OUTPUT -p tcp -d ${mynetwork} -j RETURN
iptables -t nat -A OUTPUT -p tcp -d ${sock_server} -j RETURN
iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 -j RETURN
while read line
do
echo -e "\033[32m this ip[${line}] will use proxy connected .... \033[0m"
Expand All @@ -67,9 +79,9 @@ do
proxyall)
#proxy all connection
iptables -t nat -F
read -p "please tell me you network..." mynetwork
read -p "please tell me you network:" mynetwork
iptables -t nat -A OUTPUT -p tcp -d ${mynetwork} -j RETURN
iptables -t nat -A OUTPUT -p tcp -d ${proxy_server} -j RETURN
iptables -t nat -A OUTPUT -p tcp -d ${sock_server} -j RETURN
iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 -j RETURN
iptables -t nat -A OUTPUT -p tcp -j REDIRECT --to-ports ${proxy_port}
echo -e "\033[32m your iptabls OUTPUT chain like this.... \033[0m"
Expand Down
6 changes: 3 additions & 3 deletions redsocks.conf
Expand Up @@ -10,7 +10,7 @@ base {
* "file:/path/to/file"
* syslog:FACILITY facility is any of "daemon", "local0"..."local7"
*/
log = "file:/tmp/redsocks.log";
log = stderr;
// log = "file:/path/to/file";
// log = "syslog:local7";

Expand Down Expand Up @@ -40,7 +40,7 @@ redsocks {
* use 0.0.0.0 if you want to listen on every interface.
* `local_*' are used as port to redirect to.
*/
local_ip = 0.0.0.0;
local_ip = 127.0.0.1;
local_port = 12345;

// listen() queue length. Default value is SOMAXCONN and it should be
Expand All @@ -58,7 +58,7 @@ redsocks {
// `ip' and `port' are IP and tcp-port of proxy-server
// You can also use hostname instead of IP, only one (random)
// address of multihomed host will be used.
ip = 192.168.149.150;
ip = 127.0.0.1;
port = 7070;


Expand Down

0 comments on commit 18ab9c7

Please sign in to comment.