# Redsocks

在windows上，我们可以安装proxifier来做sock5代理，在centos上，我们可以安装Redsocks来做代理。

## 1、安装依赖

```
$ yum -y install libevent-devel git gcc
```

## 2、下载源码与编译

```
$ git clone https://github.com/darkk/redsocks
$ cd redsocks
$ make
```

此时，会在redsocks目录里生成可执行文件redsocks

## 3、拷贝到`/usr/bin`目录下

```
$ cp redsocks/redsocks /usr/bin/
```

## 4、创建配置文件

创建目录`/etc/redsocks`，创建文件`/var/log/redsocks/redsocks.log`

```
$ mkdir -p /etc/redsocks /var/log/redsocks
$ touch /var/log/redsocks/redsocks.log
```

然后创建配置文件`/etc/redsocks/redsocks.conf`，内容如下，

```
base {
        log_debug = off;
        log_info = on;
        log = "file:/var/log/redsocks/redsocks.log";
        daemon = off;
        redirector = iptables;
}
redsocks {
        local_ip = 127.0.0.1;
        local_port = 8881;
        ip = x.x.x.x;
        port = xx;
        type = socks5;
}
```

其中`local_port`表示redsocks在本机监听的端口，`ip`表示远程代理服务器的IP，`port`表示远程代理服务器的端口。

## 5、启动

执行以下命令启动

```
$ /usr/bin/redsocks -c /etc/redsocks/redsocks.conf &
```

## 6、添加代理规则

假设我们只想让目的地址为`10.142.0.0/16`的包走代理，那以，添加如下的iptables规则

```
$ iptables -t nat -A OUTPUT -d 10.142.0.0/16 -p tcp -j REDIRECT --to-ports 8881
```

## Reference

* <https://github.com/darkk/redsocks>
* <https://crosp.net/blog/administration/install-configure-redsocks-proxy-centos-linux/>
* <https://www.imzcy.cn/1560.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pshizhsysu.gitbook.io/linux/chang-yong-ruan-jian-an-zhuang-pian/redsocks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
