Skip to content

Commit

Permalink
add adguardhome support
Browse files Browse the repository at this point in the history
  • Loading branch information
whalechoi committed Jun 16, 2024
1 parent 479ceb8 commit c219993
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 44 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ XrayHelper use yml format configuration file, default is `/data/adb/xray/xrayhel
## Update Components
- update core
`xrayhelper update core`, should configure **xrayHelper.coreType** first
- update adghome
`xrayhelper update adghome`, update adghome from [AdguardTeam/AdGuardHome](https://github.com/AdguardTeam/AdGuardHome)
- update tun2socks
`xrayhelper update tun2socks`, update tun2socks from [heiher/hev-socks5-tunnel](https://github.com/heiher/hev-socks5-tunnel)
- update geodata
Expand Down Expand Up @@ -52,6 +54,7 @@ XrayHelper use yml format configuration file, default is `/data/adb/xray/xrayhel
## Credits
- [@Loyalsoldier/v2ray-rules-dat](https://github.com/Loyalsoldier/v2ray-rules-dat)
- [@2dust/v2rayNG](https://github.com/2dust/v2rayNG)
- [@AdguardTeam/AdGuardHome](https://github.com/AdguardTeam/AdGuardHome)
- [@heiher/hev-socks5-tunnel](https://github.com/heiher/hev-socks5-tunnel)
- ~~[@haishanh/yacd](https://github.com/haishanh/yacd)~~
- [@MetaCubeX/Yacd-meta](https://github.com/MetaCubeX/Yacd-meta)
15 changes: 11 additions & 4 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ XrayHelper 使用 yml 格式的配置文件,默认使用`/data/adb/xray/xrayhe
- `proxyTag`默认值`proxy`,使用 XrayHelper 进行节点切换时,将进行替换的出站代理 Tag
- `subList`可选,数组,节点订阅链接(SIP002/v2rayNg/Hysteria/Hysteria2),也支持 clash 订阅链接(需要在订阅链接前添加`clash+`前缀)
- `userAgent`可选,自定义 XrayHelper http 请求的 User-Agent
- clash
- `dnsPort`使用`mihomo`时必填,默认值`65533`,mihomo 监听的 dns 端口, XrayHelper 会将本机 DNS 请求劫持到该端口
- `template`可选,mihomo 配置模板,指定配置模板后,该模板会**覆盖(或注入)** mihomo 配置文件对应内容
- adgHome
- `enable`默认值`false`,是否随核心服务一同启动 AdGuardHome ,需要提前下载 adgHome 二进制文件(可使用命令`xrayhelper update adghome`),由于`mihomo``hysteria2`的 DNS 模块并不完善,仅建议与他们配合使用,其他核心直接使用核心 DNS 即可,使用不当可能导致 DNS 泄露
- `address`启用时必填,默认值`127.0.0.1:65530`,AdGuardHome WebUI 监听地址
- `workDir`启用时必填,AdGuardHome 的工作目录(该目录需包含配置文件`config.yaml`
- `dnsPort`启用时必填,AdGuardHome 监听的 DNS 端口;需要注意,由于`hysteria2`没有 DNS 模块,使用该核心时 XrayHelper 会将本机 DNS 请求劫持到该端口
- proxy
- `method`默认值`tproxy`,代理模式,可选`tproxy``tun``tun2socks`,使用 tun 模式时,请确保你的核心支持 tun 并正确配置它;使用 tun2socks 模式时,需要提前下载 tun2socks 二进制文件(可使用命令`xrayhelper update tun2socks`
- `tproxyPort`默认值`65535`,透明代理端口,该值需要与核心的 tproxy 入站代理端口相对应,`tproxy`模式需要
Expand All @@ -27,9 +35,6 @@ XrayHelper 使用 yml 格式的配置文件,默认使用`/data/adb/xray/xrayhe
- `apList`,可选,数组,需代理的 ap 接口名,例如`wlan+`可代理 wlan 热点,`rndis+`可代理 usb 网络共享
- `ignoreList`,可选,数组,需要忽略的接口名,例如`wlan+`可以实现连上 wifi 不走代理
- `intraList`,可选,数组,CIDR,默认情况下,内网地址不会被标记,若需要将部分内网地址标记,可配置此项
- clash
- `dnsPort`默认值`65533`,mihomo 监听的 dns 端口
- `template`可选,mihomo 配置模板,指定配置模板后,该模板会**覆盖(或注入)** mihomo 配置文件对应内容

## 命令
- service
Expand All @@ -43,9 +48,10 @@ XrayHelper 使用 yml 格式的配置文件,默认使用`/data/adb/xray/xrayhe
- `refresh`刷新系统代理规则
- update
- `core`更新核心,需要指定 **xrayHelper.coreType**
- `adghome`[AdguardTeam/AdGuardHome](https://github.com/AdguardTeam/AdGuardHome) 更新 adghome
- `tun2socks`[hev-socks5-tunnel](https://github.com/heiher/hev-socks5-tunnel) 更新 tun2socks
- `geodata`[Loyalsoldier/v2ray-rules-dat](https://github.com/Loyalsoldier/v2ray-rules-dat) 更新 GEO 数据文件
- `subscribe`更新订阅节点(或 clash 订阅)到`${xrayHelper.dataDir}/sub.txt`(或`${xrayHelper.dataDir}/clashSub#{index}.yaml`),需要指定 **xrayHelper.subList**
- `tun2socks`[hev-socks5-tunnel](https://github.com/heiher/hev-socks5-tunnel) 更新 tun2socks
- `yacd-meta`更新 [Yacd-meta](https://github.com/MetaCubeX/Yacd-meta)`${xrayHelper.dataDir}/Yacd-meta-gh-pages`
### xray、sing-box、hysteria2
- switch
Expand All @@ -64,6 +70,7 @@ XrayHelper 使用 yml 格式的配置文件,默认使用`/data/adb/xray/xrayhe
## 鸣谢
- [@Loyalsoldier/v2ray-rules-dat](https://github.com/Loyalsoldier/v2ray-rules-dat)
- [@2dust/v2rayNG](https://github.com/2dust/v2rayNG)
- [@AdguardTeam/AdGuardHome](https://github.com/AdguardTeam/AdGuardHome)
- [@heiher/hev-socks5-tunnel](https://github.com/heiher/hev-socks5-tunnel)
- ~~[@haishanh/yacd](https://github.com/haishanh/yacd)~~
- [@MetaCubeX/Yacd-meta](https://github.com/MetaCubeX/Yacd-meta)
23 changes: 18 additions & 5 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ xrayHelper:
- clash+https://testclashsuburl.com
# Optional, custom User-Agent for http requests send by xrayhelper
userAgent: 'ClashMeta'
clash:
# Required for mihomo, Default value: 65533, all dns request will be redirected to the port which listen by mihomo
dnsPort: 65533
# Optional, if not empty, the template config will replace (or inject to) the actual mihomo config
template: /data/adb/xray/mihomoconfs/template.yaml
adgHome:
# Default value: false, start AdGuardHome with core service or not
# please run command "xrayhelper update adghome" to install it first
# recommended for use with mihomo and hysteria2 only, because their DNS module is incomprehension
# improper usage will lead to dns leak
enable: false
# Required for adgHome, Default value: 127.0.0.1:65530, AdGuardHome's webui
address: 127.0.0.1:65530
# Required for adgHome, AdGuardHome's workDir(need include config with ${workDir}/config.yaml)
workDir: /data/adb/xray/adghomeconfs/
# Required for adgHome, Default value: 65531, AdGuardHome's DNS port
# Special, when your core is hysteria2, all dns request will be redirected to this port, because hysteria2 don't have DNS module
dnsPort: 65531
proxy:
# Required, Default value: tproxy, proxy method you want to use, support tproxy, tun, tun2socks
# If you use tun mode, please make sure your core support tun, and configure it correctly
Expand Down Expand Up @@ -56,8 +74,3 @@ proxy:
intraList:
- 192.168.123.0/24
- fd12:3456:789a:bcde::/64
clash:
# Required for mihomo, Default value: 65533, all dns request will be redirected to the port which listen by mihomo
dnsPort: 65533
# Optional, if not empty, the template config will replace (or inject to) the actual mihomo config
template: /data/adb/xray/mihomoconfs/template.yaml
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/coreos/go-iptables v0.7.0
github.com/creasty/defaults v1.7.0
github.com/fatih/color v1.17.0
github.com/jessevdk/go-flags v1.5.0
github.com/jessevdk/go-flags v1.6.1
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ github.com/creasty/defaults v1.7.0 h1:eNdqZvc5B509z18lD8yc212CAqJNvfT1Jq6L8WowdB
github.com/creasty/defaults v1.7.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
Expand Down
17 changes: 12 additions & 5 deletions main/builds/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ var Config struct {
SubList []string `yaml:"subList"`
UserAgent string `yaml:"userAgent"`
} `yaml:"xrayHelper"`
Clash struct {
DNSPort string `default:"65533" yaml:"dnsPort"`
Template string `yaml:"template"`
} `yaml:"clash"`
AdgHome struct {
Enable bool `default:"false" yaml:"enable"`
Address string `default:"127.0.0.1:65530" yaml:"address"`
WorkDir string `yaml:"workDir"`
DNSPort string `default:"65531" yaml:"dnsPort"`
} `yaml:"adgHome"`
Proxy struct {
Method string `default:"tproxy" yaml:"method"`
TproxyPort string `default:"65535" yaml:"tproxyPort"`
Expand All @@ -43,10 +53,6 @@ var Config struct {
IgnoreList []string `yaml:"ignoreList"`
IntraList []string `yaml:"intraList"`
} `yaml:"proxy"`
Clash struct {
DNSPort string `default:"65533" yaml:"dnsPort"`
Template string `yaml:"template"`
} `yaml:"clash"`
}

// LoadConfig load program configuration file, should be called before any command Execute
Expand All @@ -62,8 +68,9 @@ func LoadConfig() error {
return e.New("unmarshal config failed, ", err).WithPrefix(tagConfig)
}
log.HandleDebug(Config.XrayHelper)
log.HandleDebug(Config.Proxy)
log.HandleDebug(Config.Clash)
log.HandleDebug(Config.AdgHome)
log.HandleDebug(Config.Proxy)
return nil
}

Expand Down
84 changes: 77 additions & 7 deletions main/commands/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,19 @@ OUT:
if listenFlag {
if err := os.WriteFile(path.Join(builds.Config.XrayHelper.RunDir, "core.pid"), []byte(strconv.Itoa(service.Pid())), 0644); err != nil {
_ = service.Kill()
stopService()
return e.New("write core pid failed, ", err).WithPrefix(tagService)
}
if builds.Config.AdgHome.Enable {
if err := startAdgHome(); err != nil {
_ = service.Kill()
stopService()
return err
}
}
} else {
_ = service.Kill()
stopService()
return e.New("core service not listen, please check error.log").WithPrefix(tagService)
}
return nil
Expand Down Expand Up @@ -214,6 +223,7 @@ func stopService() {
} else {
log.HandleDebug(err)
}
stopAdgHome()
}

// getServicePid get core pid from pid file
Expand Down Expand Up @@ -310,8 +320,6 @@ func replaceRayDNSStrategy(conf []byte, ipv6 bool) (replacedConf []byte, err err
default:
return nil, e.New("unsupported core type " + builds.Config.XrayHelper.CoreType).WithPrefix(tagService)
}
// replace
jsonMap.Set("dns", dnsMap)
// marshal
marshal, err := json.MarshalIndent(jsonMap, "", " ")
if err != nil {
Expand Down Expand Up @@ -363,14 +371,11 @@ func overrideClashConfig(template string, target string) error {
} // if enable AutoDNSStrategy
if builds.Config.Proxy.AutoDNSStrategy {
templateYamlMap.Set("ipv6", builds.Config.Proxy.EnableIPv6)
dns, ok := templateYamlMap.Get("dns")
if ok {
if dns, ok := templateYamlMap.Get("dns"); ok {
// assert dns
dnsMap, ok := dns.Value.(serial.OrderedMap)
if ok {
if dnsMap, ok := dns.Value.(serial.OrderedMap); ok {
dnsMap.Set("listen", "127.0.0.1:"+builds.Config.Clash.DNSPort)
}
templateYamlMap.Set("dns", dnsMap)
}
}
// save template
Expand All @@ -397,3 +402,68 @@ func overrideClashConfig(template string, target string) error {
}
return nil
}

func startAdgHome() error {
adgHomePath := path.Join(path.Dir(builds.Config.XrayHelper.CorePath), "adguardhome")
adgHomeConfigPath := path.Join(builds.Config.AdgHome.WorkDir, "config.yaml")
adgHomeConfigFile, err := os.ReadFile(adgHomeConfigPath)
if err != nil {
return e.New("load adgHome config failed, ", err).WithPrefix(tagService)
}
var adgHomeConfig serial.OrderedMap
if err := yaml.Unmarshal(adgHomeConfigFile, &adgHomeConfig); err != nil {
return e.New("unmarshal adgHome config failed, ", err).WithPrefix(tagService)
}
if http, ok := adgHomeConfig.Get("http"); ok {
httpMap := http.Value.(serial.OrderedMap)
// set address
httpMap.Set("address", builds.Config.AdgHome.Address)
}
if dns, ok := adgHomeConfig.Get("dns"); ok {
dnsMap := dns.Value.(serial.OrderedMap)
// set dnsPort
dnsMap.Set("port", builds.Config.AdgHome.DNSPort)
// set dnsStrategy
if builds.Config.Proxy.AutoDNSStrategy {
dnsMap.Set("aaaa_disabled", !builds.Config.Proxy.EnableIPv6)
}
}
// save config
marshal, err := yaml.Marshal(adgHomeConfig)
if err != nil {
return e.New("marshal adgHome config failed, ", err).WithPrefix(tagService)
}
// write new config
if err := os.WriteFile(adgHomeConfigPath, marshal, 0644); err != nil {
return e.New("write adgHome config failed, ", err).WithPrefix(tagService)
}
// create adghome service
service := common.NewExternal(0, nil, nil, adgHomePath,
"--no-check-update",
"-w", builds.Config.AdgHome.WorkDir,
"-c", adgHomeConfigPath,
"--pidfile", path.Join(builds.Config.XrayHelper.RunDir, "adghome.pid"),
"-l", path.Join(builds.Config.XrayHelper.RunDir, "adghome.log"))
service.AppendEnv("SSL_CERT_DIR=/system/etc/security/cacerts/")
service.SetUidGid("0", common.CoreGid)
service.Start()
return nil
}

func stopAdgHome() {
if _, err := os.Stat(path.Join(builds.Config.XrayHelper.RunDir, "adghome.pid")); err == nil {
pidFile, err := os.ReadFile(path.Join(builds.Config.XrayHelper.RunDir, "adghome.pid"))
if err != nil {
log.HandleDebug(err)
}
pid, _ := strconv.Atoi(string(pidFile))
if serviceProcess, err := os.FindProcess(pid); err == nil {
_ = serviceProcess.Kill()
_ = os.Remove(path.Join(builds.Config.XrayHelper.RunDir, "adghome.pid"))
} else {
log.HandleDebug(err)
}
} else {
log.HandleDebug(err)
}
}
Loading

0 comments on commit c219993

Please sign in to comment.