Skip to content

Commit

Permalink
增加tag配置 优化单规则更新失败的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
joyanhui committed Feb 1, 2024
1 parent e253d6d commit 0c7cb12
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 80 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## iKuai Bypass

fork 自 https://github.com/ztc1997/ikuai-bypass/
## 功能
ikuai 可以通过分流规则 把openwrt作为爱快的上级虚拟运营商,再把openwrt的出口绑定到实际的运营商,实现无感分流。 [具体实现](https://dev.leiyanhui.com/route/ikuai-bypass-joyanhui/)
这种方式比传统用openwrt的作为旁路由的方案,或者only openwrt的方案更加稳定无感,速度更好。
但是因为大家喜闻乐见的分流规则数据可能几万条,维护比较麻烦,这个工具就是为了自动更新爱快的分流规则。
fork 自 [ztc1997](https://github.com/ztc1997/ikuai-bypass/
## 功能)
自动或手动从大家常用的订阅规则里更新爱快分流规则,让大家喜闻乐见的域名或者ip(通常是非大陆ip)走指定的wan口(通常分流到openwrt),实现无感不影响他人的分流。
## 主要修改点
- 并发处理 运营商/IP分流 和 域名分流
Expand All @@ -15,8 +17,10 @@ fork 自 https://github.com/ztc1997/ikuai-bypass/
- `-r` : 运行模式 `cron` or `nocron` or `clean`
- `-tag` : 清理模式下的附加参数 指定清理的分流规则的备注,可以不添写`IKUAI_BYPASS`前缀 `cleanAll` 清理所有备注名包含`IKUAI_BYPASS`的分流规则

## todo list
- 某一分组规则更新失败导致这个分组的规则被删除的bug [#3](https://github.com/joyanhui/ikuai-bypass/issues/3)
## 更新日志
- 2023-02-1 某一分组规则因为网络问题失败导致相关的旧规则被删除的bug [#3](https://github.com/joyanhui/ikuai-bypass/issues/3)
- 2023-02-1 清理模式可以清理全部备注名包含`IKUAI_BYPASS`的分流规则,或者指定备注名全程或者后缀名的分流规则
- 旧版记录参考 commit信息
## 简要使用说明
需要两个文件 1、可执行程序[下载](https://github.com/joyanhui/ikuai-bypass/releases) 2、配置文件 config.yml [参考](https://github.com/joyanhui/ikuai-bypass/blob/main/config_example.yml)
命令格式: ` ./ikuai-bypass -c /路径/config.yml -r 运行模式`
Expand Down Expand Up @@ -51,8 +55,8 @@ chmod +x /opt/ikuai-bypass/ikuai-bypass && /opt/ikuai-bypass/ikuai-bypass -r cr

```
## v0.1.15 升级 新版本 说明
v0.2.x 以后规则的备注不在是`IKUAI_BYPASS` 而是`IKUAI_BYPASS_`+`分流规则的名字` 例如`IKUAI_BYPASS_`+`ISP`+`_`+`IP`+`_`+`分流规则的名字`
所以需要先清理掉旧的规则再更新新的规则。
v0.2.x 以后规则的备注不在是`IKUAI_BYPASS` 会有后缀,所以需要先清理掉旧的分流规则再添加
另外配置文件中每条规则都多了一个 `tag: 备注后缀` 用于区分不同的规则
```sh
./ikuai-bypass -c /路径/config.yml -r clean -tag cleanAll # 清理所有备注名包含`IKUAI_BYPASS`的分流规则
./ikuai-bypass -c /路径/config.yml -r cron #先运行一次 而后等待计划任务触发
Expand Down
Binary file removed api/api_exe
Binary file not shown.
10 changes: 5 additions & 5 deletions api/custom_isp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ func (i *IKuai) ShowCustomIspByComment() (result []CustomIspData, err error) {
return
}

func (i *IKuai) AddCustomIsp(name, ipgroup string) error {
func (i *IKuai) AddCustomIsp(name, tag, ipgroup string) error {
param := struct {
Name string `json:"name"`
Ipgroup string `json:"ipgroup"`
Comment string `json:"comment"`
}{
Name: name,
Ipgroup: ipgroup,
Comment: COMMENT_IKUAI_BYPASS,
Comment: COMMENT_IKUAI_BYPASS + "_" + tag,
}
req := CallReq{
FuncName: FUNC_NAME_CUSTOM_ISP,
Expand Down Expand Up @@ -92,16 +92,16 @@ func (i *IKuai) DelCustomIsp(id string) error {
}

// 预备删除
func (i *IKuai) PrepareDelCustomIspAll() (preIds string, err error) {
log.Println("运营商/IP分流== 正在查询 备注为:", COMMENT_IKUAI_BYPASS, "的运营商配置规则")
func (i *IKuai) PrepareDelCustomIspAll(tag string) (preIds string, err error) {
log.Println("运营商/IP分流== 正在查询 备注为:", COMMENT_IKUAI_BYPASS+"_"+tag, "的运营商配置规则")
preIds = ""
err = nil
for loop := 0; loop < 3; loop++ {
var data []CustomIspData
data, err = i.ShowCustomIspByComment()
var ids []string
for _, d := range data {
if d.Comment == COMMENT_IKUAI_BYPASS {
if d.Comment == COMMENT_IKUAI_BYPASS+"_"+tag {
ids = append(ids, strconv.Itoa(d.ID))
}
}
Expand Down
12 changes: 6 additions & 6 deletions api/stream_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type StreamDomainData struct {
Enabled string `json:"enabled"`
}

func (i *IKuai) AddStreamDomain(iface, srcAddr, domains string) error {
func (i *IKuai) AddStreamDomain(iface, tag, srcAddr, domains string) error {
param := struct {
Interface string `json:"interface"`
SrcAddr string `json:"src_addr"`
Expand All @@ -33,7 +33,7 @@ func (i *IKuai) AddStreamDomain(iface, srcAddr, domains string) error {
Interface: iface,
SrcAddr: srcAddr,
Domain: domains,
Comment: COMMENT_IKUAI_BYPASS,
Comment: COMMENT_IKUAI_BYPASS + "_" + tag,
Week: "1234567",
Time: "00:00-23:59",
Enabled: "yes",
Expand Down Expand Up @@ -107,19 +107,19 @@ func (i *IKuai) DelStreamDomain(id string) error {
}

// PrepareDelStreamDomainAll 为了防止误删,先查询,然后再删除
func (i *IKuai) PrepareDelStreamDomainAll() (preIds string, err error) {
log.Println("域名分流== 正在查询 备注为:", COMMENT_IKUAI_BYPASS, "的域名分流规则")
func (i *IKuai) PrepareDelStreamDomainAll(tag string) (preIds string, err error) {
log.Println("域名分流== 正在查询 备注为:", COMMENT_IKUAI_BYPASS+"_"+tag, "的域名分流规则")
preIds = ""
err = nil
for loop := 0; loop < 5; loop++ {
var data []StreamDomainData
data, err = i.ShowStreamDomainByComment(COMMENT_IKUAI_BYPASS)
data, err = i.ShowStreamDomainByComment(COMMENT_IKUAI_BYPASS + "_" + tag)
if err != nil {
return
}
var ids []string
for _, d := range data {
if d.Comment == COMMENT_IKUAI_BYPASS {
if d.Comment == COMMENT_IKUAI_BYPASS+"_"+tag {
ids = append(ids, strconv.Itoa(d.ID))
}
}
Expand Down
17 changes: 14 additions & 3 deletions config_example.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
## 爱快管理页面的 URL,结尾不要加 "/",
## 如不填写,则使用第一个接口的网关地址作为爱快地址,
## 如果在爱快自带 docker 运行,网关就是爱快地址,可以不写
ikuai-url: http://10.1.1.1
ikuai-url: http://10.1.1.1 # 爱快网页控制台登陆地址
username: admin # 爱快用户名
password: admin888 # 爱快密码
cron: 0 4 * * * # 执行更新的周期 crontab
cron: 0 4 * * * # 执行更新的周期 格式为 crontab
custom-isp: # 自定义运营商 IP分流
- name: 国内IP列表 # 自定义运营商名称
## 自定义运营商 cidr 列表网址,每行一个,超过5000行会自动分为多个,ipv6 地址会被删除
## 下面演示规则使用了ghproxy.com的代理,如果失效请自行更换或另外想办法
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/cn.txt
tag: ipcn # 规则的备注标签后缀 如果留空默认为自定义运营商名称
- name: telegram
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/telegram.txt
tag: iptg
- name: google
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/google.txt
tag: ipgoogle
- name: facebook
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/facebook.txt
tag: ipfb
- name: twitter
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/twitter.txt
tag: iptw
- name: cloudflare
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/cloudflare.txt
tag: ipcf
- name: netflix
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/netflix.txt
tag: ipnf

stream-domain: # 域名分流
#- interface: wan2 # 分流线路
Expand All @@ -31,11 +39,14 @@ stream-domain: # 域名分流
- interface: wan2
src-addr: 10.1.1.5-10.1.1.254
url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/gfw.txt
tag: gfw

#下面是自动走wan1直连的
#- interface: wan1
# src-addr: 10.1.1.5-10.1.1.254
# url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/china-list.txt
# tag: cn
#- interface: wan1
# src-addr: 10.1.1.5-10.1.1.254
# url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/apple-cn.txt
# url: https://mirror.ghproxy.com/https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/apple-cn.txt
# tag: apple
39 changes: 34 additions & 5 deletions helperFunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
Expand All @@ -12,7 +13,7 @@ import (
"gopkg.in/yaml.v3"
)

// 读取哦配置文件
// 读取配置文件 到 conf
func readConf(filename string) error {
buf, err := os.ReadFile(filename)
if err != nil {
Expand All @@ -22,11 +23,39 @@ func readConf(filename string) error {
if err != nil {
return fmt.Errorf("in file %q: %v", filename, err)
}

// 检查每个 CustomIsp 的 Tag,如果不存在,则使用 Name
for i := range conf.CustomIsp {
if conf.CustomIsp[i].Tag == "" {
log.Println("运营商分流规则中配置中Tag为空,采用:", conf.CustomIsp[i].Name)
conf.CustomIsp[i].Tag = conf.CustomIsp[i].Name
}
}

// 检查每个 StreamDomain 的 Tag,如果不存在,则使用 Interface
for i := range conf.StreamDomain {
if conf.StreamDomain[i].Tag == "" {
log.Println("域名分流规则中中Tag为空,采用:", conf.StreamDomain[i].Interface)
conf.StreamDomain[i].Tag = conf.StreamDomain[i].Interface
}
}

return nil
/*
buf, err := os.ReadFile(filename)
if err != nil {
return err
}
err = yaml.Unmarshal(buf, &conf)
if err != nil {
return fmt.Errorf("in file %q: %v", filename, err)
}
return nil
*/
}

// updateCustomIsp 更新运营商分流规则
func updateCustomIsp(iKuai *api.IKuai, name, url string) (err error) {
func updateCustomIsp(iKuai *api.IKuai, name string, tag string, url string) (err error) {
resp, err := http.Get(url)
if err != nil {
return
Expand All @@ -44,13 +73,13 @@ func updateCustomIsp(iKuai *api.IKuai, name, url string) (err error) {
ipGroups := group(ips, 5000) //5000条
for _, ig := range ipGroups {
ipGroup := strings.Join(ig, ",")
iKuai.AddCustomIsp(name, ipGroup)
err = iKuai.AddCustomIsp(name, tag, ipGroup)
}
return
}

// updateStreamDomain 更新域名分流规则
func updateStreamDomain(iKuai *api.IKuai, iface, srcAddr, url string) (err error) {
func updateStreamDomain(iKuai *api.IKuai, iface, tag, srcAddr, url string) (err error) {
resp, err := http.Get(url)
if err != nil {
return
Expand All @@ -67,7 +96,7 @@ func updateStreamDomain(iKuai *api.IKuai, iface, srcAddr, url string) (err error
domainGroup := group(domains, 1000) //1000条
for _, d := range domainGroup {
domain := strings.Join(d, ",")
iKuai.AddStreamDomain(iface, srcAddr, domain)
err = iKuai.AddStreamDomain(iface, tag, srcAddr, domain)
}
return
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ var conf struct {
CustomIsp []struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Tag string `yaml:"tag"`
} `yaml:"custom-isp"`
StreamDomain []struct {
Interface string `yaml:"interface"`
SrcAddr string `yaml:"src-addr"`
URL string `yaml:"url"`
Tag string `yaml:"tag"`
} `yaml:"stream-domain"`
}

Expand Down

0 comments on commit 0c7cb12

Please sign in to comment.