mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
7a0af318df
* mergers code optimize * api log work with pipe * remove inbounds/outbounds by tags *fix rmo flag parse * cmds description and message optimize
96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
package mergers
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
"github.com/v2fly/v2ray-core/v4/common/cmdarg"
|
|
)
|
|
|
|
// MergeAs load input and merge as specified format into m
|
|
func MergeAs(formatName string, input interface{}, m map[string]interface{}) error {
|
|
f, found := mergersByName[formatName]
|
|
if !found {
|
|
return newError("format merger not found for: ", formatName)
|
|
}
|
|
return f.Merge(input, m)
|
|
}
|
|
|
|
// Merge loads inputs and merges them into m
|
|
// it detects extension for merger selecting, or try all mergers
|
|
// if no extension found
|
|
func Merge(input interface{}, m map[string]interface{}) error {
|
|
switch v := input.(type) {
|
|
case string:
|
|
err := mergeSingleFile(v, m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case []string:
|
|
for _, file := range v {
|
|
err := mergeSingleFile(file, m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case cmdarg.Arg:
|
|
for _, file := range v {
|
|
err := mergeSingleFile(file, m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case []byte:
|
|
err := mergeSingleFile(v, m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case io.Reader:
|
|
// read to []byte incase it tries different mergers
|
|
bs, err := ioutil.ReadAll(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = mergeSingleFile(bs, m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
return newError("unknow merge input type")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func mergeSingleFile(input interface{}, m map[string]interface{}) error {
|
|
if file, ok := input.(string); ok {
|
|
ext := getExtension(file)
|
|
if ext != "" {
|
|
lext := strings.ToLower(ext)
|
|
f, found := mergersByExt[lext]
|
|
if !found {
|
|
return newError("unmergeable format extension: ", ext)
|
|
}
|
|
return f.Merge(file, m)
|
|
}
|
|
}
|
|
// no extension, try all mergers
|
|
for _, f := range mergersByName {
|
|
if f.Name == core.FormatAuto {
|
|
continue
|
|
}
|
|
err := f.Merge(input, m)
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return newError("tried all mergers but failed for: ", input).AtWarning()
|
|
}
|
|
|
|
func getExtension(filename string) string {
|
|
ext := filepath.Ext(filename)
|
|
return strings.ToLower(ext)
|
|
}
|