-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
301 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package trie | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
"io" | ||
) | ||
|
||
func (ss *DomainSet) WriteBin(w io.Writer, count int64) (err error) { | ||
// version | ||
_, err = w.Write([]byte{1}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// count | ||
err = binary.Write(w, binary.BigEndian, count) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// leaves | ||
err = binary.Write(w, binary.BigEndian, int64(len(ss.leaves))) | ||
if err != nil { | ||
return err | ||
} | ||
for _, d := range ss.leaves { | ||
err = binary.Write(w, binary.BigEndian, d) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// labelBitmap | ||
err = binary.Write(w, binary.BigEndian, int64(len(ss.labelBitmap))) | ||
if err != nil { | ||
return err | ||
} | ||
for _, d := range ss.labelBitmap { | ||
err = binary.Write(w, binary.BigEndian, d) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// labels | ||
err = binary.Write(w, binary.BigEndian, int64(len(ss.labels))) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = w.Write(ss.labels) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ReadDomainSetBin(r io.Reader) (ds *DomainSet, count int64, err error) { | ||
// version | ||
version := make([]byte, 1) | ||
_, err = io.ReadFull(r, version) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if version[0] != 1 { | ||
return nil, 0, errors.New("version is invalid") | ||
} | ||
|
||
// count | ||
err = binary.Read(r, binary.BigEndian, &count) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
ds = &DomainSet{} | ||
var length int64 | ||
|
||
// leaves | ||
err = binary.Read(r, binary.BigEndian, &length) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if length < 1 { | ||
return nil, 0, errors.New("length is invalid") | ||
} | ||
ds.leaves = make([]uint64, length) | ||
for i := int64(0); i < length; i++ { | ||
err = binary.Read(r, binary.BigEndian, &ds.leaves[i]) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
} | ||
|
||
// labelBitmap | ||
err = binary.Read(r, binary.BigEndian, &length) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if length < 1 { | ||
return nil, 0, errors.New("length is invalid") | ||
} | ||
ds.labelBitmap = make([]uint64, length) | ||
for i := int64(0); i < length; i++ { | ||
err = binary.Read(r, binary.BigEndian, &ds.labelBitmap[i]) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
} | ||
|
||
// labels | ||
err = binary.Read(r, binary.BigEndian, &length) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if length < 1 { | ||
return nil, 0, errors.New("length is invalid") | ||
} | ||
ds.labels = make([]byte, length) | ||
_, err = io.ReadFull(r, ds.labels) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
ds.init() | ||
return ds, count, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package provider | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
P "github.com/metacubex/mihomo/constant/provider" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
) | ||
|
||
func ConvertToMrs(buf []byte, behavior P.RuleBehavior, format P.RuleFormat, w io.Writer) (err error) { | ||
strategy := newStrategy(behavior, nil) | ||
strategy, err = rulesParse(buf, strategy, format) | ||
if err != nil { | ||
return err | ||
} | ||
if _strategy, ok := strategy.(mrsRuleStrategy); ok { | ||
var encoder *zstd.Encoder | ||
encoder, err = zstd.NewWriter(w) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
zstdErr := encoder.Close() | ||
if err == nil { | ||
err = zstdErr | ||
} | ||
}() | ||
return _strategy.WriteMrs(encoder) | ||
} else { | ||
return ErrInvalidFormat | ||
} | ||
} | ||
|
||
func ConvertMain(args []string) { | ||
if len(args) > 3 { | ||
behavior, err := P.ParseBehavior(args[0]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
format, err := P.ParseRuleFormat(args[1]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
source := args[2] | ||
target := args[3] | ||
|
||
sourceFile, err := os.ReadFile(source) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
targetFile, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = ConvertToMrs(sourceFile, behavior, format, targetFile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = targetFile.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} else { | ||
panic("Usage: convert-ruleset <behavior> <format> <source file> <target file>") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.