fea: ceci default is yaml.

This commit is contained in:
Daniel Ding
2024-12-31 21:23:51 +08:00
parent 5e0ec7082f
commit 196ecc2979
5 changed files with 42 additions and 70 deletions

View File

@@ -12,7 +12,7 @@ func main() {
mode := "http"
conf := ""
flag.StringVar(&mode, "mode", "http", "Proxy mode for tcp or http")
flag.StringVar(&conf, "conf", "ceci.json", "The configuration file")
flag.StringVar(&conf, "conf", "ceci.yaml", "The configuration file")
flag.Parse()
libol.PreNotify()

View File

@@ -25,10 +25,10 @@ type SocksProxy struct {
type HttpForward struct {
Protocol string `json:"protocol,omitempty" yaml:"protocol,omitempty"`
Server string `json:"server,omitempty"`
Insecure bool `json:"insecure,omitempty"`
Match []string `json:"match,omitempty"`
Secret string `json:"secret,omitempty"`
Server string `json:"server,omitempty" yaml:"server,omitempty"`
Insecure bool `json:"insecure,omitempty" yaml:"insecure,omitempty"`
Match []string `json:"match,omitempty" yaml:"match,omitempty"`
Secret string `json:"secret,omitempty" yaml:"secret,omitempty"`
}
type HttpProxy struct {
@@ -40,13 +40,14 @@ type HttpProxy struct {
Password string `json:"password,omitempty" yaml:"password,omitempty"`
CaCert string `json:"cacert,omitempty" yaml:"cacert,omitempty"`
Forward *HttpForward `json:"forward,omitempty" yaml:"forward,omitempty"`
Backends []*HttpForward `json:"backends,omitempty" yaml:"backend,omitempty"`
Backends []*HttpForward `json:"backends,omitempty" yaml:"backends,omitempty"`
}
func (h *HttpProxy) Initialize() error {
if h.ConfDir == "" {
h.ConfDir = path.Dir(os.Args[0])
}
libol.Info("HttpProxy.Initialize %s", h.Conf)
if err := h.Load(); err != nil {
libol.Error("HttpProxy.Initialize %s", err)
return err
@@ -135,6 +136,7 @@ type TcpProxy struct {
}
func (t *TcpProxy) Initialize() error {
libol.Info("TcpProxy.Initialize %s", h.Conf)
if err := t.Load(); err != nil {
libol.Error("TcpProxy.Initialize %s", err)
return err

View File

@@ -247,7 +247,7 @@ func (s *Switch) LoadNetworks() {
libol.Error("Switch.LoadNetwork %s", err)
}
for _, k := range files {
if data, err := libol.LoadWithoutAnn(k); err != nil {
if data, err := libol.LoadFile(k); err != nil {
libol.Warn("Switch.LoadNetwork %s", err)
} else if _, err := s.LoadNetworkJson(data); err != nil {
libol.Warn("Switch.LoadNetwork %s", err)

View File

@@ -1,12 +1,11 @@
package libol
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"gopkg.in/yaml.v2"
"math/rand"
"net"
"os"
@@ -27,6 +26,14 @@ const MacBase = 0x00
var Letters = []byte("0123456789abcdefghijklmnopqrstuvwxyz")
func IsYaml(file string) bool {
return strings.HasSuffix(file, ".yaml")
}
func IsJson(file string) bool {
return strings.HasSuffix(file, ".json")
}
func GenString(n int) string {
buffer := make([]byte, n)
rand.Seed(time.Now().UnixNano())
@@ -93,12 +100,22 @@ func MarshalSave(v interface{}, file string, pretty bool) error {
return err
}
defer f.Close()
str, err := Marshal(v, true)
var data []byte
if IsYaml(file) {
data, err = yaml.Marshal(v)
if err != nil {
Error("MarshalSave error: %s", err)
return err
}
if _, err := f.Write(str); err != nil {
} else {
data, err = Marshal(v, true)
if err != nil {
Error("MarshalSave error: %s", err)
return err
}
}
if _, err := f.Write(data); err != nil {
Error("MarshalSave: %s", err)
return err
}
@@ -112,38 +129,8 @@ func FileExist(file string) error {
return nil
}
func ScanAnn(r io.Reader) ([]byte, error) {
data := make([]byte, 0, 1024)
scan := bufio.NewScanner(r)
for scan.Scan() {
bs := scan.Bytes()
dis := false
for i, b := range bs {
if b == ' ' || b == '\t' || b == '\r' || b == '\n' {
continue
}
if b == '/' && len(bs) > i+1 && bs[i+1] == '/' {
dis = true // if start with //, need discard it.
}
break
}
if !dis {
data = append(data, bs...)
}
}
if err := scan.Err(); err != nil {
return nil, err
}
return data, nil
}
func LoadWithoutAnn(file string) ([]byte, error) {
fp, err := OpenRead(file)
if err != nil {
return nil, err
}
defer fp.Close()
return ScanAnn(fp)
func LoadFile(file string) ([]byte, error) {
return os.ReadFile(file)
}
func Unmarshal(v interface{}, contents []byte) error {
@@ -157,12 +144,17 @@ func UnmarshalLoad(v interface{}, file string) error {
if err := FileExist(file); err != nil {
return nil
}
contents, err := LoadWithoutAnn(file)
contents, err := LoadFile(file)
if err != nil {
return NewErr("%s %s", file, err)
}
if IsYaml(file) {
return yaml.Unmarshal(contents, v)
} else {
return Unmarshal(v, contents)
}
}
func FunName(i interface{}) string {
ptr := reflect.ValueOf(i).Pointer()

View File

@@ -71,25 +71,3 @@ func TestPrettyBytes(t *testing.T) {
s = PrettyBytes(1024*1024*1024 + 1024*1024*512 + 59)
assert.Equal(t, "1.50G", s, "be the same.")
}
func TestScanPure(t *testing.T) {
buff := bytes.NewBuffer([]byte(`// hi`))
_, err := ScanAnn(buff)
assert.Equal(t, nil, err, "be the same.")
buff = bytes.NewBuffer([]byte(`// hi
you are`))
data, err := ScanAnn(buff)
assert.Equal(t, string(data), "\t\t\tyou are", "be the same.")
buff = bytes.NewBuffer([]byte(`// hi
you are
// you are
//`))
data, err = ScanAnn(buff)
assert.Equal(t, string(data), "\t\t\tyou are", "be the same.")
buff = bytes.NewBuffer([]byte(`// hi
you are
// you are
/`))
data, err = ScanAnn(buff)
assert.Equal(t, string(data), "\t\t\tyou are\t\t\t/", "be the same.")
}