Skip to content

Commit

Permalink
Chore: sort output lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier committed Aug 6, 2024
1 parent 18e14e6 commit 1af42df
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
6 changes: 5 additions & 1 deletion plugin/maxmind/mmdb_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
"slices"
"strings"

"github.com/Loyalsoldier/geoip/lib"
Expand Down Expand Up @@ -159,7 +160,7 @@ func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string {
}
}

list := make([]string, 0, 200)
list := make([]string, 0, 300)
for entry := range container.Loop() {
name := entry.GetName()
_, found := overwriteMap[name]
Expand All @@ -169,6 +170,9 @@ func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string {
list = append(list, name)
}

// Sort the lists
slices.Sort(list)

// Make sure the names in overwriteList are written at last
list = append(list, overwriteList...)

Expand Down
23 changes: 20 additions & 3 deletions plugin/plaintext/text_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plaintext
import (
"encoding/json"
"log"
"slices"
"strings"

"github.com/Loyalsoldier/geoip/lib"
Expand Down Expand Up @@ -36,16 +37,29 @@ func (t *textOut) GetDescription() string {

func (t *textOut) Output(container lib.Container) error {
// Filter want list
wantList := make(map[string]bool)
wantList := make([]string, 0, 50)
for _, want := range t.Want {
if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
wantList[want] = true
wantList = append(wantList, want)
}
}

switch len(wantList) {
case 0:
list := make([]string, 0, 300)
for entry := range container.Loop() {
list = append(list, entry.GetName())
}

// Sort the list
slices.Sort(list)

for _, name := range list {
entry, found := container.GetEntry(name)
if !found {
log.Printf("❌ entry %s not found", name)
continue
}
data, err := t.marshalBytes(entry)
if err != nil {
return err
Expand All @@ -57,7 +71,10 @@ func (t *textOut) Output(container lib.Container) error {
}

default:
for name := range wantList {
// Sort the list
slices.Sort(wantList)

for _, name := range wantList {
entry, found := container.GetEntry(name)
if !found {
log.Printf("❌ entry %s not found", name)
Expand Down
23 changes: 20 additions & 3 deletions plugin/singbox/srs_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"slices"
"strings"

"github.com/Loyalsoldier/geoip/lib"
Expand Down Expand Up @@ -82,23 +83,39 @@ func (s *srsOut) GetDescription() string {

func (s *srsOut) Output(container lib.Container) error {
// Filter want list
wantList := make(map[string]bool)
wantList := make([]string, 0, 50)
for _, want := range s.Want {
if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
wantList[want] = true
wantList = append(wantList, want)
}
}

switch len(wantList) {
case 0:
list := make([]string, 0, 300)
for entry := range container.Loop() {
list = append(list, entry.GetName())
}

// Sort the list
slices.Sort(list)

for _, name := range list {
entry, found := container.GetEntry(name)
if !found {
log.Printf("❌ entry %s not found", name)
continue
}
if err := s.run(entry); err != nil {
return err
}
}

default:
for name := range wantList {
// Sort the list
slices.Sort(wantList)

for _, name := range wantList {
entry, found := container.GetEntry(name)
if !found {
log.Printf("❌ entry %s not found", name)
Expand Down
23 changes: 20 additions & 3 deletions plugin/v2ray/dat_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,33 @@ func (g *geoIPDatOut) GetDescription() string {

func (g *geoIPDatOut) Output(container lib.Container) error {
// Filter want list
wantList := make(map[string]bool)
wantList := make([]string, 0, 50)
for _, want := range g.Want {
if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
wantList[want] = true
wantList = append(wantList, want)
}
}

geoIPList := new(router.GeoIPList)
geoIPList.Entry = make([]*router.GeoIP, 0, 300)
updated := false

switch len(wantList) {
case 0:
list := make([]string, 0, 300)
for entry := range container.Loop() {
list = append(list, entry.GetName())
}

// Sort the list
sort.Strings(list)

for _, name := range list {
entry, found := container.GetEntry(name)
if !found {
log.Printf("❌ entry %s not found", name)
continue
}
geoIP, err := g.generateGeoIP(entry)
if err != nil {
return err
Expand All @@ -128,7 +142,10 @@ func (g *geoIPDatOut) Output(container lib.Container) error {
}

default:
for name := range wantList {
// Sort the list
sort.Strings(wantList)

for _, name := range wantList {
entry, found := container.GetEntry(name)
if !found {
log.Printf("❌ entry %s not found", name)
Expand Down

0 comments on commit 1af42df

Please sign in to comment.