doc: add a new document

This commit is contained in:
ICKelin
2024-04-27 22:06:10 +08:00
parent 93ce440730
commit 254a570040
7 changed files with 174 additions and 11 deletions

View File

@@ -34,10 +34,10 @@ gtun支持多线路配置可以同时对美国日本欧洲目的网络
- [配置加速ip](#配置加速ip)
- [加速效果测试](#加速效果)
- [最佳实践]()
- [最佳实践: 基于gtun+ipset实现ip代理加速和分流](doc/最佳实践: 基于gtun+ipset实现ip代理加速和分流.md)
- [最佳实践: 基于gtun+ipset实现ip代理加速和分流](doc/最佳实践:基于gtun+ipset实现ip代理加速和分流.md)
- [最佳实践: 基于gtun+dnsmasq实现域名代理加速和分流]()
- [最佳实践: openwrt搭载gtun打造加速软路由连接Wi-Fi即可畅游网络]()
- [玩法分享基于gtun+n1盒子实现软路由加速]()
- [玩法分享基于gtun+n1盒子实现软路由加速](doc/玩法分享:基于gtun+n1盒子实现软路由加速.md)
- [玩法分享基于gtun实现的tiktok加速路由]()
- [玩法分享基于gtun实现公有云访问外部加速]()
- [玩法分享基于gtun实现办公网访问公有云内网]()

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View File

@@ -1,4 +0,0 @@
# 最佳实践: 基于gtun+ipset实现ip代理加速和分流
gtun的基础功能是ip加速本文通过如何配置gtun来实现指定ip地址段的加速最终达到以下效果
-

View File

@@ -0,0 +1,169 @@
# 最佳实践: 基于gtun+ipset实现ip代理加速和分流
gtun的基础功能是ip加速本文通过具体的配置来讲解如何配置gtun的ip加速功能最终实现的效果是除了局域网IP之外访问其他所有IP都通过gtun转发到美国出口。
最终拓扑如下:
![img.png](assets/ip_acc_topology.png)
如图所示,本文会包含两个部分:
- 本地gtun美国gtund的部署
- 加速流量和非加速流量的区分这部分通过ipset和iptables来进行
我们可以通过iptables非常灵活的控制加速和非加速流量。
**本文的是基于gtun的2.0.7版本。**
# 安装
安装包括两个组件:
- gtundip加速的服务端程序部署在美国
- gtunip加速的客户端程序部署在本地linux
## 安装gtund
在release里面找到2.0.7版本的产物并进行下载gtund部署在美国的AWS上。
```
cd gtund
./install.sh
```
install_gtund.sh 会创建gtund的运行目录并通过systemd把gtund程序拉起。
执行install_gtund.sh完成之后gtund会
- 监听tcp的3002作为mux协议的服务端口
- 监听udp的3002作为kcp协议的服务端口
- 日志记录在/opt/apps/gtund/logs/gtund.log
gtund的默认配置为默认情况下不需要作任何的修改即可
```yaml
enable_auth: true
auths:
- access_token: "ICKelin:free"
expired_ath: 0
trace: ":3003"
server:
- listen: ":3002"
scheme: "kcp"
- listen: ":3002"
scheme: "mux"
log:
days: 5
level: "debug"
path: "/opt/apps/gtund/logs/gtund.log"
```
您也可以使用docker-compose来进行安装
```shell
cd gtund
docker-compose up --build -d
```
执行完之后docker ps 看是否启动成功
## 安装gtun
gtun的安装也类似在release里面找到2.0.7版本的产物并进行下载然后在本地linux上进行部署
```shell
cd gtun
export ACCESS_KEY="ICKelin:free"
export SERVER_IP="gtund所在的服务器的ip"
./install_gtun.sh
```
安装完成之后查看是否有错误日志
```shell
tail -f /opt/apps/gtun/logs/gtun.log
```
同样你也可以使用docker-compose来安装
```shell
cd gtun
docker-compose up --build -d
```
执行完成之后docker ps 看是否启动成功。
# 配置转发规则
本文的转发规则比较简单,需要加速的地址为`0.0.0.0/0`不需要加速的地址列表在gtun/scripts/noproxy.txt文件里面主要包含一些局域网地址
需要记住的是,**一定要把gtund的公网IP加入到noproxy.txt里面**,防止自己把服务器地址拦截。
第一步把不需要的加速的地址配置好:
```shell
noproxy_set=NOPROXY
clear_noproxy() {
iptables -t mangle -D PREROUTING -m set --match-set $noproxy_set dst -j ACCEPT
iptables -t mangle -D OUTPUT -m set --match-set $noproxy_set dst -j ACCEPT
ipset destroy $noproxy_set >/dev/null
}
add_noproxy() {
ipset create $noproxy_set hash:net
cat noproxy.txt | while read line
do
echo "no proxy for" $line
ipset add $noproxy_set $line
done
iptables -t mangle -A PREROUTING -m set --match-set $noproxy_set dst -j ACCEPT
iptables -t mangle -A OUTPUT -m set --match-set $noproxy_set dst -j ACCEPT
}
```
通过创建NOPROXY的ipset并且把noproxy.txt文件的cidr列表加入进去然后通过iptables匹配到NOPROXY这个ipset的地址全部ACCEPT掉。
第二步把需要加速的地址配置好:
```shell
setname=GTUN_ALL
redirect_port=8524
add_proxy() {
ipset create $setname hash:net
echo "proxy for 0.0.0.0/1"
echo "proxy for 128.0.0.0/1"
ipset add $setname 0.0.0.0/1
ipset add $setname 128.0.0.0/1
iptables -t mangle -A PREROUTING -p tcp -m set --match-set $setname dst -j TPROXY --tproxy-mark 1/1 --on-port $redirect_port
iptables -t mangle -A PREROUTING -p udp -m set --match-set $setname dst -j TPROXY --tproxy-mark 1/1 --on-port $redirect_port
iptables -t mangle -A OUTPUT -m set --match-set $setname dst -j MARK --set-mark 1
# redirect dns query
# iptables -t mangle -A PREROUTING -p udp --dport 53 -j TPROXY --tproxy-mark 1/1 --on-port $redirect_port
# iptables -t mangle -A OUTPUT -p udp --dport 53 -j MARK --set-mark 1
ip rule add fwmark 1 lookup 100
ip ro add local default dev lo table 100
}
```
通过创建GTUN_ALL的ipset并且把`0.0.0.0/1``128.0.0.0/1`加入到其中。然后通过iptables打mark和策略路由来实现访问GTUN_ALL这个ipset的cidr地址的流量的劫持。
通过上面两个步骤由于iptables使用的是-A的方式添加到表尾部所以会先匹配noproxy_setnoproxy_set匹配不到再匹配到GTUN_ALL。
以上两个步骤的脚本已经打包进`gtun/scripts/redirect_all.sh`文件里面,有需要可以根据具体情况进行修改。
上面是实现的所有流量加速的但是有时候我们需要部分不加速比如大陆地区的ip访问不加速那么只需要把大陆地区的ip加入到noproxy这一ipset即可。
```shell
wget https://raw.githubusercontent.com/herrbischoff/country-ip-blocks/master/ipv4/cn.cidr
cat cn.cidr | while read line
do
echo "no proxy for" $line
ipset add $noproxy_set $line
done
```
用法非常多,后续文章会不断分享一些用法。
# 结束语
以上是gtun的最基本的功能实现所有流量劫持并进行加速同时也提了一嘴如何访问大陆地区的ip不加速通过这个实例基本上能了解gtun是如何跑起来的也能定制一些更加适合自己的用法。
后续会继续介绍如何通过gtun跟dnsmasq结合实现访问域名的加速。

View File

@@ -2,7 +2,7 @@ version: '3'
services:
accelerator:
build: .
container_name: gtun
container_name: gtun_2.0.7
restart: always
network_mode: host
privileged: true

View File

@@ -6,11 +6,9 @@ auths:
trace: ":3003"
server:
- listen: ":3002"
authKey: "rewrite with your auth key"
scheme: "kcp"
- listen: ":3001"
authKey: "rewrite with your auth key"
- listen: ":3002"
scheme: "mux"
log:

View File

@@ -1,4 +1,4 @@
systemctl stop gtun
systemctl stop gtund
GTUND_DIR="/opt/apps/gtund"
mkdir -p $GTUND_DIR/logs
cp -r . $GTUND_DIR