Skip to content

Commit

Permalink
refactor: remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
igoogolx committed Jul 6, 2024
1 parent 35453ec commit d38414c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 71 deletions.
19 changes: 5 additions & 14 deletions internal/cfg/distribution/system_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
C "github.com/Dreamacro/clash/constant"
"github.com/igoogolx/itun2socks/internal/cfg/distribution/ruleEngine"
"github.com/igoogolx/itun2socks/internal/constants"
"github.com/igoogolx/itun2socks/internal/matcher"
"github.com/igoogolx/itun2socks/pkg/log"
)

type SystemProxyConfig struct {
RuleEngine *ruleEngine.Engine
}

func (c SystemProxyConfig) connMatcher(metadata *C.Metadata, _ ruleEngine.Rule) (ruleEngine.Rule, error) {

if metadata.Host != "" {
var rule, err = c.RuleEngine.Match(metadata.Host, constants.DomainRuleTypes)
var rule, err = matcher.GetRule().Match(metadata.Host, constants.DomainRuleTypes)
if err == nil {
return rule, nil
}
}

if metadata.DstIP.String() != "" {
rule, err := c.RuleEngine.Match(metadata.DstIP.String(), constants.IpRuleTypes)
rule, err := matcher.GetRule().Match(metadata.DstIP.String(), constants.IpRuleTypes)
if err == nil {
return rule, nil
}
Expand All @@ -44,16 +44,7 @@ func (c SystemProxyConfig) ConnMatcher(metadata *C.Metadata, prevRule ruleEngine
return result, nil
}

func NewSystemProxy(
ruleId string,
rules []string,
) (SystemProxyConfig, error) {
func NewSystemProxy() (SystemProxyConfig, error) {
ResetCache()
rEngine, err := ruleEngine.New(ruleId, rules)
if err != nil {
return SystemProxyConfig{}, err
}
return SystemProxyConfig{
RuleEngine: rEngine,
}, nil
return SystemProxyConfig{}, nil
}
42 changes: 2 additions & 40 deletions internal/cfg/distribution/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

type Config struct {
Dns DnsDistribution
RuleEngine *ruleEngine.Engine
Dns DnsDistribution
}

func (c Config) getIpRuleFromDns(ip string) (ruleEngine.Rule, bool) {
Expand Down Expand Up @@ -57,59 +56,22 @@ func (c Config) ConnMatcher(metadata *C.Metadata, prevRule ruleEngine.Rule) (rul
return result, nil
}

func convertRulePolicyToDnsType(rule ruleEngine.Rule) (constants.DnsType, error) {
if rule.GetPolicy() == constants.PolicyDirect {
return constants.LocalDns, nil
} else if rule.GetPolicy() == constants.PolicyProxy {
return constants.RemoteDns, nil
} else if rule.GetPolicy() == constants.PolicyReject {
return constants.LocalDns, fmt.Errorf("reject dns")
}

return constants.RemoteDns, nil
}

func (c Config) GetDnsType(domain string, metadata *C.Metadata) (constants.DnsType, error) {
processPath := metadata.ProcessPath
var rule ruleEngine.Rule
var err error
if len(processPath) != 0 {
rule, err = matcher.GetRule().Match(processPath, constants.ProcessRuleTypes)
if err == nil {
return convertRulePolicyToDnsType(rule)
}
}

rule, err = matcher.GetRule().Match(domain, constants.DomainRuleTypes)
if err == nil {
return convertRulePolicyToDnsType(rule)
}

return constants.RemoteDns, nil
}

func NewTun(
boostDns []string,
remoteDns []string,
localDns []string,
ruleId string,
rules []string,
defaultInterfaceName string,
disableCache bool,
) (Config, error) {
if len(boostDns) == 0 || len(remoteDns) == 0 || len(localDns) == 0 {
return Config{}, fmt.Errorf("dns can't be empty")
}
ResetCache()
rEngine, err := ruleEngine.New(ruleId, rules)
if err != nil {
return Config{}, err
}
dns, err := NewDnsDistribution(boostDns, remoteDns, localDns, defaultInterfaceName, disableCache)
if err != nil {
return Config{}, err
}
return Config{
Dns: dns, RuleEngine: rEngine,
dns,
}, nil
}
10 changes: 1 addition & 9 deletions internal/cfg/system_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,8 @@ func NewSystemProxy() (*SystemProxyConfig, error) {
if err != nil {
return nil, err
}
selectedRule, err := configuration.GetSelectedRule()
if err != nil {
return nil, err
}

rule, err := distribution.NewSystemProxy(
selectedRule,
rawConfig.Rules,
)

rule, err := distribution.NewSystemProxy()
if err != nil {
return nil, err
}
Expand Down
6 changes: 0 additions & 6 deletions internal/cfg/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ func NewTun(defaultInterfaceName string) (*Config, error) {
if err != nil {
return nil, err
}
selectedRule, err := configuration.GetSelectedRule()
if err != nil {
return nil, err
}
device, err := tun.New()
if err != nil {
return nil, err
Expand All @@ -37,8 +33,6 @@ func NewTun(defaultInterfaceName string) (*Config, error) {
rawConfig.Setting.Dns.Server.Boost,
rawConfig.Setting.Dns.Server.Remote,
rawConfig.Setting.Dns.Server.Local,
selectedRule,
rawConfig.Rules,
defaultInterfaceName,
rawConfig.Setting.Dns.DisableCache,
)
Expand Down
28 changes: 26 additions & 2 deletions internal/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
cResolver "github.com/Dreamacro/clash/component/resolver"
"github.com/igoogolx/itun2socks/internal/cfg"
"github.com/igoogolx/itun2socks/internal/cfg/distribution/ruleEngine"
"github.com/igoogolx/itun2socks/internal/configuration"
"github.com/igoogolx/itun2socks/internal/conn"
"github.com/igoogolx/itun2socks/internal/dns"
Expand All @@ -26,6 +27,23 @@ type Client interface {
RuntimeDetail() (interface{}, error)
}

func UpdateRule() error {
rawConfig, err := configuration.Read()
if err != nil {
return err
}
selectedRule, err := configuration.GetSelectedRule()
if err != nil {
return err
}
rEngine, err := ruleEngine.New(selectedRule, rawConfig.Rules)
if err != nil {
return err
}
matcher.UpdateRule(rEngine)
return nil
}

func newTun() (Client, error) {
err := network_iface.StartMonitor()
if err != nil {
Expand Down Expand Up @@ -79,9 +97,12 @@ func newTun() (Client, error) {
}
tunnel.UpdateShouldFindProcess(config.ShouldFindProcess)
conn.UpdateConnMatcher(matchers)
matcher.UpdateRule(config.Rule.RuleEngine)
conn.UpdateProxy(config.Proxy)
dns.UpdateDnsMap(config.Rule.Dns.Local.Client, config.Rule.Dns.Remote.Client)
err = UpdateRule()
if err != nil {
return nil, err
}

return &TunClient{
stack: stack,
Expand All @@ -102,8 +123,11 @@ func newSysProxy() (Client, error) {
conn.UpdateConnMatcher([]conn.Matcher{
config.Rule.ConnMatcher,
})
matcher.UpdateRule(config.Rule.RuleEngine)
conn.UpdateProxy(config.Proxy)
err = UpdateRule()
if err != nil {
return nil, err
}
return &SystemProxyClient{
localserver: newLocalServer,
config: config,
Expand Down

0 comments on commit d38414c

Please sign in to comment.