Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Jul 30, 2024
1 parent f97a945 commit a359526
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
11 changes: 5 additions & 6 deletions clash.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/metacubex/meta-rules-converter/output/sing"

"github.com/sagernet/sing-box/option"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -113,7 +112,7 @@ func convertClash(cmd *cobra.Command, args []string) error {
for name, domain := range domainFull {
os.MkdirAll(outDir+"/"+name, 0755)
if len(domain) != 0 || len(domainSuffix[name]) != 0 || len(domainKeyword[name]) != 0 || len(domainRegex[name]) != 0 {
domainRule := []option.DefaultHeadlessRule{
domainRule := []sing.DefaultHeadlessRule{
{
Domain: domain,
DomainKeyword: domainKeyword[name],
Expand All @@ -124,15 +123,15 @@ func convertClash(cmd *cobra.Command, args []string) error {
sing.SaveSingRuleSet(domainRule, outDir+"/"+name+"/domain")
}
if len(ipCIDR[name]) != 0 {
ipRule := []option.DefaultHeadlessRule{
ipRule := []sing.DefaultHeadlessRule{
{
IPCIDR: ipCIDR[name],
},
}
sing.SaveSingRuleSet(ipRule, outDir+"/"+name+"/ip")
}
if len(processName[name]) != 0 || len(packageName[name]) != 0 || len(processPath[name]) != 0 {
processRule := []option.DefaultHeadlessRule{
processRule := []sing.DefaultHeadlessRule{
{
ProcessName: processName[name],
PackageName: packageName[name],
Expand All @@ -142,14 +141,14 @@ func convertClash(cmd *cobra.Command, args []string) error {
sing.SaveSingRuleSet(processRule, outDir+"/"+name+"/process")
}
if len(dstPort[name]) != 0 {
otherRule := []option.DefaultHeadlessRule{
otherRule := []sing.DefaultHeadlessRule{
{
Port: dstPort[name],
},
}
sing.SaveSingRuleSet(otherRule, outDir+"/"+name+"/other")
}
classicalRule := []option.DefaultHeadlessRule{
classicalRule := []sing.DefaultHeadlessRule{
{
Domain: domainFull[name],
DomainKeyword: domainKeyword[name],
Expand Down
13 changes: 7 additions & 6 deletions geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"gopkg.in/yaml.v3"

"github.com/metacubex/mihomo/component/geodata/router"
"github.com/sagernet/sing-box/option"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -109,11 +108,12 @@ func convertIP(cmd *cobra.Command, args []string) error {
if err != nil {
fmt.Println(code, " output err: ", err)
}
err = os.WriteFile(outDir+"/"+code+".list", []byte(strings.Join(cidrs, "\n")), 0755)
ipcidrOut = []byte(strings.Join(cidrs, "\n"))
err = os.WriteFile(outDir+"/"+code+".list", ipcidrOut, 0755)
if err != nil {
fmt.Println(code, " output err: ", err)
}
err = meta.SaveMetaRuleSet(ipcidrOut, "ipcidr", "yaml", outDir+"/"+code+".mrs")
err = meta.SaveMetaRuleSet(ipcidrOut, "ipcidr", "text", outDir+"/"+code+".mrs")
if err != nil {
fmt.Println(code, " output err: ", err)
}
Expand All @@ -130,15 +130,16 @@ func convertIP(cmd *cobra.Command, args []string) error {
if err != nil {
fmt.Println(code, " output err: ", err)
}
err = os.WriteFile(outDir+"/classical/"+code+".list", []byte(strings.Join(cidrs, "\n")), 0755)
classicalOut = []byte(strings.Join(cidrs, "\n"))
err = os.WriteFile(outDir+"/classical/"+code+".list", classicalOut, 0755)
if err != nil {
fmt.Println(code, " output err: ", err)
}
// meta.SaveMetaRuleSet(classicalOut, "classical", "yaml", outDir+"/classical/"+code+".mrs")
// meta.SaveMetaRuleSet(classicalOut, "classical", "text", outDir+"/classical/"+code+".mrs")
}
case "sing-box":
for code, cidrs := range countryCIDRs {
ipcidrRule := []option.DefaultHeadlessRule{
ipcidrRule := []sing.DefaultHeadlessRule{
{
IPCIDR: cidrs,
},
Expand Down
13 changes: 7 additions & 6 deletions geosite.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"gopkg.in/yaml.v3"

"github.com/metacubex/mihomo/component/geodata/router"
"github.com/sagernet/sing-box/option"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -171,11 +170,12 @@ func convertSite(cmd *cobra.Command, args []string) error {
if err != nil {
fmt.Println(code, " output err: ", err)
}
err = os.WriteFile(outDir+"/"+code+".list", []byte(strings.Join(domain, "\n")), 0755)
domainOut = []byte(strings.Join(domain, "\n"))
err = os.WriteFile(outDir+"/"+code+".list", domainOut, 0755)
if err != nil {
fmt.Println(code, " output err: ", err)
}
err = meta.SaveMetaRuleSet(domainOut, "domain", "yaml", outDir+"/"+code+".mrs")
err = meta.SaveMetaRuleSet(domainOut, "domain", "text", outDir+"/"+code+".mrs")
if err != nil {
fmt.Println(code, " output err: ", err)
}
Expand All @@ -190,15 +190,16 @@ func convertSite(cmd *cobra.Command, args []string) error {
if err != nil {
fmt.Println(code, " output err: ", err)
}
err = os.WriteFile(outDir+"/classical/"+code+".list", []byte(strings.Join(classical[code], "\n")), 0755)
classicalOut = []byte(strings.Join(classical[code], "\n"))
err = os.WriteFile(outDir+"/classical/"+code+".list", classicalOut, 0755)
if err != nil {
fmt.Println(code, " output err: ", err)
}
// meta.SaveMetaRuleSet(classicalOut, "classical", "yaml", outDir+"/classical/"+code+".mrs")
// meta.SaveMetaRuleSet(classicalOut, "classical", "text", outDir+"/classical/"+code+".mrs")
}
case "sing-box":
for code, domain := range domainFull {
domainRule := []option.DefaultHeadlessRule{
domainRule := []sing.DefaultHeadlessRule{
{
Domain: domain,
DomainKeyword: domainKeyword[code],
Expand Down
6 changes: 3 additions & 3 deletions output/meta/meta_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func SaveMetaRuleSet(buf []byte, b string, f string, outputPath string) error {
if err != nil {
return err
}
RP.ConvertToMrs(buf, behavior, format, targetFile)
err = targetFile.Close()
err = RP.ConvertToMrs(buf, behavior, format, targetFile)
closeErr := targetFile.Close()
if err != nil {
return err
}
return nil
return closeErr
}
4 changes: 3 additions & 1 deletion output/sing/sing_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/sagernet/sing/common/json"
)

func SaveSingRuleSet(rules []option.DefaultHeadlessRule, outputPath string) error {
type DefaultHeadlessRule = option.DefaultHeadlessRule

func SaveSingRuleSet(rules []DefaultHeadlessRule, outputPath string) error {
plainRuleSet := option.PlainRuleSetCompat{
Version: 1,
Options: option.PlainRuleSet{
Expand Down

0 comments on commit a359526

Please sign in to comment.