Chore: code style
This commit is contained in:
parent
71e002c2ef
commit
ba7bcce895
6 changed files with 29 additions and 36 deletions
20
README.md
20
README.md
|
@ -75,7 +75,7 @@ On Windows, you should download the [Wintun](https://www.wintun.net) driver and
|
||||||
# Enable the TUN listener
|
# Enable the TUN listener
|
||||||
tun:
|
tun:
|
||||||
enable: true
|
enable: true
|
||||||
stack: system # system or gvisor
|
stack: gvisor # system or gvisor
|
||||||
dns-listen: 0.0.0.0:53 # additional dns server listen on TUN
|
dns-listen: 0.0.0.0:53 # additional dns server listen on TUN
|
||||||
auto-route: true # auto set global route
|
auto-route: true # auto set global route
|
||||||
```
|
```
|
||||||
|
@ -93,7 +93,7 @@ rules:
|
||||||
- DOMAIN-SUFFIX,example.com,DIRECT,tcp
|
- DOMAIN-SUFFIX,example.com,DIRECT,tcp
|
||||||
- DOMAIN-SUFFIX,example.com,REJECT,udp
|
- DOMAIN-SUFFIX,example.com,REJECT,udp
|
||||||
|
|
||||||
# process(add 'P:' prefix) condition for all rules
|
# process condition for all rules (add 'P:' prefix)
|
||||||
- DOMAIN-SUFFIX,example.com,REJECT,P:Google Chrome Helper
|
- DOMAIN-SUFFIX,example.com,REJECT,P:Google Chrome Helper
|
||||||
|
|
||||||
# multiport condition for rules SRC-PORT and DST-PORT
|
# multiport condition for rules SRC-PORT and DST-PORT
|
||||||
|
@ -108,20 +108,15 @@ rules:
|
||||||
- GEOSITE,facebook,PROXY
|
- GEOSITE,facebook,PROXY
|
||||||
- GEOSITE,youtube,PROXY
|
- GEOSITE,youtube,PROXY
|
||||||
- GEOSITE,geolocation-cn,DIRECT
|
- GEOSITE,geolocation-cn,DIRECT
|
||||||
- GEOSITE,gfw,PROXY
|
- GEOSITE,geolocation-!cn,PROXY
|
||||||
- GEOSITE,greatfire,PROXY
|
|
||||||
#- GEOSITE,geolocation-!cn,PROXY
|
# source IPCIDR condition for all rules in gateway proxy
|
||||||
|
#- GEOSITE,geolocation-!cn,REJECT,192.168.1.88/32,192.168.1.99/32
|
||||||
|
|
||||||
- GEOIP,telegram,PROXY,no-resolve
|
- GEOIP,telegram,PROXY,no-resolve
|
||||||
- GEOIP,private,DIRECT,no-resolve
|
- GEOIP,private,DIRECT,no-resolve
|
||||||
- GEOIP,cn,DIRECT
|
- GEOIP,cn,DIRECT
|
||||||
|
|
||||||
# Not match condition for rule GEOIP
|
|
||||||
#- GEOIP,!cn,PROXY
|
|
||||||
|
|
||||||
# source IPCIDR condition for all rules in gateway proxy
|
|
||||||
#- GEOSITE,geolocation-!cn,REJECT,192.168.1.88/32,192.168.1.99/32
|
|
||||||
|
|
||||||
- MATCH,PROXY
|
- MATCH,PROXY
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -184,9 +179,6 @@ Add field `Process` to `Metadata` and prepare to get process name for Restful AP
|
||||||
|
|
||||||
To display process name in GUI please use https://yaling888.github.io/yacd/.
|
To display process name in GUI please use https://yaling888.github.io/yacd/.
|
||||||
|
|
||||||
## Premium Release
|
|
||||||
[Release](https://github.com/Dreamacro/clash/releases/tag/premium)
|
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
If you want to build an application that uses clash as a library, check out the the [GitHub Wiki](https://github.com/Dreamacro/clash/wiki/use-clash-as-a-library)
|
If you want to build an application that uses clash as a library, check out the the [GitHub Wiki](https://github.com/Dreamacro/clash/wiki/use-clash-as-a-library)
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter"
|
"github.com/Dreamacro/clash/adapter"
|
||||||
|
@ -73,6 +72,8 @@ func ApplyConfig(cfg *config.Config, force bool) {
|
||||||
mux.Lock()
|
mux.Lock()
|
||||||
defer mux.Unlock()
|
defer mux.Unlock()
|
||||||
|
|
||||||
|
log.SetLevel(log.DEBUG)
|
||||||
|
|
||||||
updateUsers(cfg.Users)
|
updateUsers(cfg.Users)
|
||||||
updateProxies(cfg.Proxies, cfg.Providers)
|
updateProxies(cfg.Proxies, cfg.Providers)
|
||||||
updateRules(cfg.Rules)
|
updateRules(cfg.Rules)
|
||||||
|
@ -82,6 +83,8 @@ func ApplyConfig(cfg *config.Config, force bool) {
|
||||||
updateDNS(cfg.DNS, cfg.General)
|
updateDNS(cfg.DNS, cfg.General)
|
||||||
updateGeneral(cfg.General, force)
|
updateGeneral(cfg.General, force)
|
||||||
updateExperimental(cfg)
|
updateExperimental(cfg)
|
||||||
|
|
||||||
|
log.SetLevel(cfg.General.LogLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGeneral() *config.General {
|
func GetGeneral() *config.General {
|
||||||
|
@ -152,9 +155,7 @@ func updateDNS(c *config.DNS, general *config.General) {
|
||||||
resolver.DefaultResolver = r
|
resolver.DefaultResolver = r
|
||||||
resolver.MainResolver = mr
|
resolver.MainResolver = mr
|
||||||
resolver.DefaultHostMapper = m
|
resolver.DefaultHostMapper = m
|
||||||
if general.Tun.Enable && strings.EqualFold(general.Tun.Stack, "system") {
|
resolver.DefaultLocalServer = dns.NewLocalServer(r, m)
|
||||||
resolver.DefaultLocalServer = dns.NewLocalServer(r, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
dns.ReCreateServer(c.Listen, r, m)
|
dns.ReCreateServer(c.Listen, r, m)
|
||||||
}
|
}
|
||||||
|
@ -172,7 +173,6 @@ func updateRules(rules []C.Rule) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateGeneral(general *config.General, force bool) {
|
func updateGeneral(general *config.General, force bool) {
|
||||||
log.SetLevel(log.DEBUG)
|
|
||||||
tunnel.SetMode(general.Mode)
|
tunnel.SetMode(general.Mode)
|
||||||
resolver.DisableIPv6 = !general.IPv6
|
resolver.DisableIPv6 = !general.IPv6
|
||||||
|
|
||||||
|
@ -200,7 +200,6 @@ func updateGeneral(general *config.General, force bool) {
|
||||||
iface.FlushCache()
|
iface.FlushCache()
|
||||||
|
|
||||||
if !force {
|
if !force {
|
||||||
log.SetLevel(general.LogLevel)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,8 +218,6 @@ func updateGeneral(general *config.General, force bool) {
|
||||||
P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
|
P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
|
||||||
P.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
|
P.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
|
||||||
P.ReCreateTun(general.Tun, tcpIn, udpIn)
|
P.ReCreateTun(general.Tun, tcpIn, udpIn)
|
||||||
|
|
||||||
log.SetLevel(general.LogLevel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateUsers(users []auth.AuthUser) {
|
func updateUsers(users []auth.AuthUser) {
|
||||||
|
@ -283,7 +280,10 @@ func updateIPTables(dns *config.DNS, general *config.General) {
|
||||||
|
|
||||||
tproxy.CleanUpTProxyLinuxIPTables()
|
tproxy.CleanUpTProxyLinuxIPTables()
|
||||||
|
|
||||||
dialer.DefaultRoutingMark.Store(2158)
|
if dialer.DefaultRoutingMark.Load() == 0 {
|
||||||
|
dialer.DefaultRoutingMark.Store(2158)
|
||||||
|
}
|
||||||
|
|
||||||
err = tproxy.SetTProxyLinuxIPTables(general.Interface, general.TProxyPort, dnsPort)
|
err = tproxy.SetTProxyLinuxIPTables(general.Interface, general.TProxyPort, dnsPort)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -292,8 +292,8 @@ func updateIPTables(dns *config.DNS, general *config.General) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CleanUp() {
|
func Cleanup() {
|
||||||
P.CleanUp()
|
P.Cleanup()
|
||||||
|
|
||||||
if runtime.GOOS == "linux" {
|
if runtime.GOOS == "linux" {
|
||||||
tproxy.CleanUpTProxyLinuxIPTables()
|
tproxy.CleanUpTProxyLinuxIPTables()
|
||||||
|
|
|
@ -49,6 +49,6 @@ func Parse(options ...Option) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CleanUp() {
|
func Cleanup() {
|
||||||
executor.CleanUp()
|
executor.Cleanup()
|
||||||
}
|
}
|
||||||
|
|
|
@ -403,8 +403,8 @@ func genAddr(host string, port int, allowLan bool) string {
|
||||||
return fmt.Sprintf("127.0.0.1:%d", port)
|
return fmt.Sprintf("127.0.0.1:%d", port)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanUp clean up something
|
// Cleanup clean up something
|
||||||
func CleanUp() {
|
func Cleanup() {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
if tunAdapter != nil {
|
if tunAdapter != nil {
|
||||||
tunAdapter.Close()
|
tunAdapter.Close()
|
||||||
|
|
6
main.go
6
main.go
|
@ -104,9 +104,9 @@ func main() {
|
||||||
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-sigCh
|
<-sigCh
|
||||||
|
|
||||||
// clean up
|
// cleanup
|
||||||
log.Warnln("Clash clean up")
|
log.Warnln("Clash cleanup")
|
||||||
hub.CleanUp()
|
hub.Cleanup()
|
||||||
|
|
||||||
log.Warnln("Clash shutting down")
|
log.Warnln("Clash shutting down")
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,8 @@ var (
|
||||||
// default timeout for UDP session
|
// default timeout for UDP session
|
||||||
udpTimeout = 60 * time.Second
|
udpTimeout = 60 * time.Second
|
||||||
|
|
||||||
preProcessCacheFinder, _ = R.NewProcess("", "", nil)
|
// a default rule type of process, use to pre resolve process name
|
||||||
|
defaultProcessRule, _ = R.NewProcess("", "", nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -149,6 +150,9 @@ func preHandleMetadata(metadata *C.Metadata) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pre resolve process name
|
||||||
|
defaultProcessRule.Match(metadata)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,9 +322,6 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
||||||
resolved = true
|
resolved = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// preset process name and cache it
|
|
||||||
preProcessCacheFinder.Match(metadata)
|
|
||||||
|
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
if !resolved && shouldResolveIP(rule, metadata) {
|
if !resolved && shouldResolveIP(rule, metadata) {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(metadata.Host)
|
||||||
|
|
Loading…
Reference in a new issue