summaryrefslogtreecommitdiff
path: root/plugin/plaintext/text_out.go
blob: ff35c6b51bb88570fdad316d112019f6f98e5f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package plaintext

import (
	"encoding/json"
	"log"
	"path/filepath"
	"slices"
	"strings"

	"github.com/Loyalsoldier/geoip/lib"
)

const (
	TypeTextOut = "text"
	DescTextOut = "Convert data to plaintext CIDR format"
)

func init() {
	lib.RegisterOutputConfigCreator(TypeTextOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
		return NewTextOutFromBytes(TypeTextOut, DescTextOut, action, data)
	})
	lib.RegisterOutputConverter(TypeTextOut, &text_out{
		Description: DescTextOut,
	})
}

func NewTextOut(oType string, oDesc string, action lib.Action, opts ...lib.OutputOption) lib.OutputConverter {
	t := &text_out{
		Type:        oType,
		Action:      action,
		Description: oDesc,
	}

	for _, opt := range opts {
		if opt != nil {
			opt(t)
		}
	}

	return t
}

func WithTextOutputDir(dir string) lib.OutputOption {
	return func(t lib.OutputConverter) {
		dir = strings.TrimSpace(dir)
		if dir == "" {
			// Use default based on type (set in NewTextOutFromBytes)
			return
		}
		t.(*text_out).OutputDir = dir
	}
}

func WithTextOutputExt(ext string) lib.OutputOption {
	return func(t lib.OutputConverter) {
		ext = strings.TrimSpace(ext)
		if ext == "" {
			ext = ".txt"
		}
		t.(*text_out).OutputExt = ext
	}
}

func WithTextOutputWantedList(lists []string) lib.OutputOption {
	return func(t lib.OutputConverter) {
		t.(*text_out).Want = lists
	}
}

func WithTextOutputExcludedList(lists []string) lib.OutputOption {
	return func(t lib.OutputConverter) {
		t.(*text_out).Exclude = lists
	}
}

func WithTextOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
	return func(t lib.OutputConverter) {
		t.(*text_out).OnlyIPType = onlyIPType
	}
}

func WithTextAddPrefixInLine(prefix string) lib.OutputOption {
	return func(t lib.OutputConverter) {
		t.(*text_out).AddPrefixInLine = prefix
	}
}

func WithTextAddSuffixInLine(suffix string) lib.OutputOption {
	return func(t lib.OutputConverter) {
		t.(*text_out).AddSuffixInLine = suffix
	}
}

func NewTextOutFromBytes(oType string, oDesc string, action lib.Action, data []byte) (lib.OutputConverter, error) {
	var tmp struct {
		OutputDir  string     `json:"outputDir"`
		OutputExt  string     `json:"outputExtension"`
		Want       []string   `json:"wantedList"`
		Exclude    []string   `json:"excludedList"`
		OnlyIPType lib.IPType `json:"onlyIPType"`

		AddPrefixInLine string `json:"addPrefixInLine"`
		AddSuffixInLine string `json:"addSuffixInLine"`
	}

	if len(data) > 0 {
		if err := json.Unmarshal(data, &tmp); err != nil {
			return nil, err
		}
	}

	// Set default output directory based on type
	if tmp.OutputDir == "" {
		switch oType {
		case TypeTextOut:
			tmp.OutputDir = filepath.Join("./", "output", "text")
		case TypeClashRuleSetClassicalOut:
			tmp.OutputDir = filepath.Join("./", "output", "clash", "classical")
		case TypeClashRuleSetIPCIDROut:
			tmp.OutputDir = filepath.Join("./", "output", "clash", "ipcidr")
		case TypeSurgeRuleSetOut:
			tmp.OutputDir = filepath.Join("./", "output", "surge")
		}
	}

	if tmp.OutputExt == "" {
		tmp.OutputExt = ".txt"
	}

	return NewTextOut(
		oType,
		oDesc,
		action,
		WithTextOutputDir(tmp.OutputDir),
		WithTextOutputExt(tmp.OutputExt),
		WithTextOutputWantedList(tmp.Want),
		WithTextOutputExcludedList(tmp.Exclude),
		WithTextOutputOnlyIPType(tmp.OnlyIPType),
		WithTextAddPrefixInLine(tmp.AddPrefixInLine),
		WithTextAddSuffixInLine(tmp.AddSuffixInLine),
	), nil
}

func (t *text_out) GetType() string {
	return t.Type
}

func (t *text_out) GetAction() lib.Action {
	return t.Action
}

func (t *text_out) GetDescription() string {
	return t.Description
}

func (t *text_out) Output(container lib.Container) error {
	for _, name := range t.filterAndSortList(container) {
		entry, found := container.GetEntry(name)
		if !found {
			log.Printf("❌ entry %s not found\n", name)
			continue
		}

		data, err := t.marshalBytes(entry)
		if err != nil {
			return err
		}

		filename := strings.ToLower(entry.GetName()) + t.OutputExt
		if err := t.writeFile(filename, data); err != nil {
			return err
		}
	}

	return nil
}

func (t *text_out) filterAndSortList(container lib.Container) []string {
	excludeMap := make(map[string]bool)
	for _, exclude := range t.Exclude {
		if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" {
			excludeMap[exclude] = true
		}
	}

	wantList := make([]string, 0, len(t.Want))
	for _, want := range t.Want {
		if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] {
			wantList = append(wantList, want)
		}
	}

	if len(wantList) > 0 {
		// Sort the list
		slices.Sort(wantList)
		return wantList
	}

	list := make([]string, 0, 300)
	for entry := range container.Loop() {
		name := entry.GetName()
		if excludeMap[name] {
			continue
		}
		list = append(list, name)
	}

	// Sort the list
	slices.Sort(list)

	return list
}