diff --git a/Makefile b/Makefile index 0085ecc7..36a01536 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,11 @@ GOBUILD=CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/Dreamacro/clas -X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \ -w -s -buildid=' +GOBUILDOP=CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \ + -X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \ + -X "github.com/Dreamacro/clash/constant.OpenWrt:=true" \ + -w -s -buildid=' + PLATFORM_LIST = \ darwin-amd64 \ darwin-arm64 \ @@ -30,7 +35,7 @@ WINDOWS_ARCH_LIST = \ windows-amd64 \ windows-arm32v7 -all: linux-amd64 darwin-amd64 windows-amd64 # Most used +all: linux-arm64-openwrt linux-amd64-openwrt linux-arm64 linux-amd64 darwin-amd64 darwin-arm64 windows-amd64 windows-386 # Most used docker: $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ @@ -47,21 +52,39 @@ linux-386: linux-amd64: GOARCH=amd64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ +linux-amd64-openwrt: + GOARCH=amd64 GOOS=linux $(GOBUILDOP) -o $(BINDIR)/$(NAME)-$@ + linux-arm64: GOARCH=arm64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ +linux-arm64-openwrt: + GOARCH=arm64 GOOS=linux $(GOBUILDOP) -o $(BINDIR)/$(NAME)-$@ + linux-armv5: GOARCH=arm GOOS=linux GOARM=5 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ +linux-armv5-openwrt: + GOARCH=arm GOOS=linux GOARM=5 $(GOBUILDOP) -o $(BINDIR)/$(NAME)-$@ + linux-armv6: GOARCH=arm GOOS=linux GOARM=6 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ +linux-armv6-openwrt: + GOARCH=arm GOOS=linux GOARM=6 $(GOBUILDOP) -o $(BINDIR)/$(NAME)-$@ + linux-armv7: GOARCH=arm GOOS=linux GOARM=7 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ +linux-armv7-openwrt: + GOARCH=arm GOOS=linux GOARM=7 $(GOBUILDOP) -o $(BINDIR)/$(NAME)-$@ + linux-armv8: GOARCH=arm64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ +linux-armv8-openwrt: + GOARCH=arm64 GOOS=linux $(GOBUILDOP) -o $(BINDIR)/$(NAME)-$@ + linux-mips-softfloat: GOARCH=mips GOMIPS=softfloat GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ @@ -94,7 +117,7 @@ windows-386: windows-amd64: GOARCH=amd64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe - + windows-arm32v7: GOARCH=arm GOOS=windows GOARM=7 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe diff --git a/constant/version.go b/constant/version.go index bd19059f..9a604deb 100644 --- a/constant/version.go +++ b/constant/version.go @@ -4,4 +4,5 @@ var ( Meta = true Version = "1.8.0" BuildTime = "unknown time" + OpenWrt bool ) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 8f0d98e0..25d655c0 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -2,7 +2,11 @@ package executor import ( "fmt" + "github.com/Dreamacro/clash/listener/tproxy" + "net" "os" + "runtime" + "strconv" "strings" "sync" @@ -74,7 +78,7 @@ func ApplyConfig(cfg *config.Config, force bool) { updateRules(cfg.Rules, cfg.RuleProviders) updateHosts(cfg.Hosts) updateProfile(cfg) - //updateIPTables(cfg.DNS, cfg.General) + updateIPTables(cfg.DNS, cfg.General) updateDNS(cfg.DNS, cfg.General) updateGeneral(cfg.General, force) updateExperimental(cfg) @@ -284,35 +288,35 @@ func patchSelectGroup(proxies map[string]C.Proxy) { } } -//func updateIPTables(dns *config.DNS, general *config.General) { -// if runtime.GOOS != "linux" || dns.Listen == "" || general.TProxyPort == 0 || general.Tun.Enable { -// return -// } -// -// _, dnsPortStr, err := net.SplitHostPort(dns.Listen) -// if dnsPortStr == "0" || dnsPortStr == "" || err != nil { -// return -// } -// -// dnsPort, err := strconv.Atoi(dnsPortStr) -// if err != nil { -// return -// } -// -// tproxy.CleanUpTProxyLinuxIPTables() -// -// err = tproxy.SetTProxyLinuxIPTables(general.Interface, general.TProxyPort, dnsPort) -// -// if err != nil { -// log.Errorln("Can not setting iptables for TProxy on linux, %s", err.Error()) -// os.Exit(2) -// } -//} +func updateIPTables(dns *config.DNS, general *config.General) { + if runtime.GOOS != "linux" || dns.Listen == "" || general.TProxyPort == 0 || general.Tun.Enable || C.OpenWrt { + return + } + + _, dnsPortStr, err := net.SplitHostPort(dns.Listen) + if dnsPortStr == "0" || dnsPortStr == "" || err != nil { + return + } + + dnsPort, err := strconv.Atoi(dnsPortStr) + if err != nil { + return + } + + tproxy.CleanUpTProxyLinuxIPTables() + + err = tproxy.SetTProxyLinuxIPTables(general.Interface, general.TProxyPort, dnsPort) + + if err != nil { + log.Errorln("Can not setting iptables for TProxy on linux, %s", err.Error()) + os.Exit(2) + } +} func CleanUp() { P.CleanUp() - //if runtime.GOOS == "linux" { - // tproxy.CleanUpTProxyLinuxIPTables() - //} + if runtime.GOOS == "linux" && C.OpenWrt { + tproxy.CleanUpTProxyLinuxIPTables() + } } diff --git a/listener/tproxy/.tproxy_linux_iptables.go b/listener/tproxy/tproxy_linux_iptables.go similarity index 99% rename from listener/tproxy/.tproxy_linux_iptables.go rename to listener/tproxy/tproxy_linux_iptables.go index 09650837..d517c6f7 100644 --- a/listener/tproxy/.tproxy_linux_iptables.go +++ b/listener/tproxy/tproxy_linux_iptables.go @@ -20,7 +20,7 @@ var ( const ( PROXY_FWMARK = "0x2d0" PROXY_ROUTE_TABLE = "0x2d0" - USERNAME = "clash" + USERNAME = "Clash.Meta" ) func SetTProxyLinuxIPTables(ifname string, tport int, dport int) error { diff --git a/rule/process.go b/rule/process.go index b60c46bb..12a041ba 100644 --- a/rule/process.go +++ b/rule/process.go @@ -27,9 +27,8 @@ func (ps *Process) Match(metadata *C.Metadata) bool { if metadata.Process != "" { return strings.EqualFold(metadata.Process, ps.process) } - // ignore match in proxy type "tproxy" - if metadata.Type == C.TPROXY { + if metadata.Type == C.TPROXY || !C.OpenWrt { return false }