Skip to content

Commit 2db2ef2

Browse files
committed
classical convert
1 parent a281fe1 commit 2db2ef2

File tree

5 files changed

+145
-39
lines changed

5 files changed

+145
-39
lines changed

convert/classical.go

+68
Original file line numberDiff line numberDiff line change
@@ -1 +1,69 @@
11
package convert
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"strings"
7+
8+
"github.com/dn-11/provider2domainset/conf"
9+
"github.com/dn-11/provider2domainset/log"
10+
"github.com/pkg/errors"
11+
)
12+
13+
func convertClassical(s *os.File, t *os.File) error {
14+
scanner := bufio.NewScanner(s)
15+
16+
for scanner.Scan() {
17+
line := scanner.Text()
18+
19+
line = strings.Split(line, "#")[0]
20+
line = strings.TrimSpace(line)
21+
22+
if line == "payload:" {
23+
continue
24+
}
25+
line = strings.TrimPrefix(line, "- ")
26+
27+
if len(line) == 0 {
28+
continue
29+
}
30+
31+
args := strings.Split(line, ",")
32+
if len(args) < 2 {
33+
log.L().Sugar().Warnf("invalid line: %s", line)
34+
}
35+
36+
res := ""
37+
38+
switch args[0] {
39+
case "DOMAIN":
40+
res = "full:" + args[1]
41+
case "DOMAIN-SUFFIX":
42+
res = "domain:" + args[1]
43+
case "DOMAIN-KEYWORD":
44+
res = "keyword:" + args[1]
45+
case "DOMAIN-REGEX":
46+
if conf.Convert.EnableRegex {
47+
res = "regex:" + args[1]
48+
} else {
49+
log.L().Sugar().Warnf("regex is disabled: %s", line)
50+
continue
51+
}
52+
default:
53+
log.L().Sugar().Warnf("unknown type: %s", line)
54+
continue
55+
}
56+
57+
log.L().Sugar().Debugf("convert line: %s -> %s", line, res)
58+
59+
if _, err := t.WriteString(res + "\n"); err != nil {
60+
return errors.Wrap(err, "write target file failed")
61+
}
62+
}
63+
64+
if err := scanner.Err(); err != nil {
65+
return errors.Wrap(err, "scan source file failed")
66+
}
67+
68+
return nil
69+
}

convert/classical_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -1 +1,58 @@
11
package convert_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/dn-11/provider2domainset/conf"
9+
"github.com/dn-11/provider2domainset/convert"
10+
"github.com/dn-11/provider2domainset/log"
11+
"go.uber.org/zap/zapcore"
12+
)
13+
14+
func TestConvertClassicalTxt(t *testing.T) {
15+
log.L().SetLogLevel(zapcore.DebugLevel)
16+
17+
pwd, err := os.Getwd()
18+
if err != nil {
19+
t.Error(err)
20+
}
21+
22+
task := convert.Task{
23+
Source: filepath.Join(pwd, "test_case"),
24+
Target: filepath.Join(pwd, "test_output"),
25+
File: conf.File{
26+
Name: "classical.txt",
27+
Type: "classical",
28+
},
29+
}
30+
31+
err = convert.Convert(task)
32+
if err != nil {
33+
t.Error(err)
34+
}
35+
}
36+
37+
func TestConvertClassicalYaml(t *testing.T) {
38+
log.L().SetLogLevel(zapcore.DebugLevel)
39+
40+
pwd, err := os.Getwd()
41+
if err != nil {
42+
t.Error(err)
43+
}
44+
45+
task := convert.Task{
46+
Source: filepath.Join(pwd, "test_case"),
47+
Target: filepath.Join(pwd, "test_output"),
48+
File: conf.File{
49+
Name: "classical.yaml",
50+
Type: "classical",
51+
},
52+
}
53+
54+
err = convert.Convert(task)
55+
if err != nil {
56+
t.Error(err)
57+
}
58+
}

convert/convert.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import (
44
"os"
55
"path/filepath"
66

7+
"github.com/dn-11/provider2domainset/conf"
8+
"github.com/dn-11/provider2domainset/log"
79
"github.com/pkg/errors"
810
)
911

1012
func Convert(t Task) error {
11-
source, err := os.ReadFile(filepath.Join(t.Source, t.Name))
13+
log.L().Sugar().Infof("convert %s of %s", t.Name, t.Type)
14+
15+
source, err := os.Open(filepath.Join(t.Source, t.Name))
1216
if err != nil {
1317
return errors.Wrap(err, "read source file failed")
1418
}
19+
defer source.Close()
1520

1621
err = os.MkdirAll(t.Target, os.ModePerm)
1722
if err != nil {
@@ -22,10 +27,18 @@ func Convert(t Task) error {
2227
if err != nil {
2328
return errors.Wrap(err, "create target file failed")
2429
}
30+
defer target.Close()
2531

26-
_, err = target.Write(source)
27-
if err != nil {
28-
return errors.Wrap(err, "write target file failed")
32+
switch t.Type {
33+
case conf.TypeClassical:
34+
err = convertClassical(source, target)
35+
if err != nil {
36+
return errors.Wrap(err, "convert classical failed")
37+
}
38+
// case conf.TypeDomain:
39+
// err = convertDomain(source, target)
40+
default:
41+
return errors.New("unknown convert type")
2942
}
3043

3144
return nil

convert/convert_test.go

-33
This file was deleted.

log/log.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ func Init() (*Logger, error) {
3535
EncodeTime: zapcore.ISO8601TimeEncoder,
3636
EncodeCaller: zapcore.ShortCallerEncoder,
3737
},
38-
OutputPaths: []string{"stdout"},
39-
ErrorOutputPaths: []string{"stderr"},
38+
OutputPaths: []string{"stdout"},
39+
ErrorOutputPaths: []string{"stderr"},
40+
DisableStacktrace: true,
4041
}
4142

4243
var err error

0 commit comments

Comments
 (0)