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" mode := "http"
conf := "" conf := ""
flag.StringVar(&mode, "mode", "http", "Proxy mode for tcp or http") 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() flag.Parse()
libol.PreNotify() libol.PreNotify()

View File

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

View File

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

View File

@@ -1,12 +1,11 @@
package libol package libol
import ( import (
"bufio"
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "gopkg.in/yaml.v2"
"math/rand" "math/rand"
"net" "net"
"os" "os"
@@ -27,6 +26,14 @@ const MacBase = 0x00
var Letters = []byte("0123456789abcdefghijklmnopqrstuvwxyz") 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 { func GenString(n int) string {
buffer := make([]byte, n) buffer := make([]byte, n)
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
@@ -93,12 +100,22 @@ func MarshalSave(v interface{}, file string, pretty bool) error {
return err return err
} }
defer f.Close() defer f.Close()
str, err := Marshal(v, true)
if err != nil { var data []byte
Error("MarshalSave error: %s", err) if IsYaml(file) {
return err data, err = yaml.Marshal(v)
if err != nil {
Error("MarshalSave error: %s", err)
return err
}
} else {
data, err = Marshal(v, true)
if err != nil {
Error("MarshalSave error: %s", err)
return err
}
} }
if _, err := f.Write(str); err != nil { if _, err := f.Write(data); err != nil {
Error("MarshalSave: %s", err) Error("MarshalSave: %s", err)
return err return err
} }
@@ -112,38 +129,8 @@ func FileExist(file string) error {
return nil return nil
} }
func ScanAnn(r io.Reader) ([]byte, error) { func LoadFile(file string) ([]byte, error) {
data := make([]byte, 0, 1024) return os.ReadFile(file)
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 Unmarshal(v interface{}, contents []byte) error { func Unmarshal(v interface{}, contents []byte) error {
@@ -157,11 +144,16 @@ func UnmarshalLoad(v interface{}, file string) error {
if err := FileExist(file); err != nil { if err := FileExist(file); err != nil {
return nil return nil
} }
contents, err := LoadWithoutAnn(file) contents, err := LoadFile(file)
if err != nil { if err != nil {
return NewErr("%s %s", file, err) return NewErr("%s %s", file, err)
} }
return Unmarshal(v, contents)
if IsYaml(file) {
return yaml.Unmarshal(contents, v)
} else {
return Unmarshal(v, contents)
}
} }
func FunName(i interface{}) string { func FunName(i interface{}) string {

View File

@@ -71,25 +71,3 @@ func TestPrettyBytes(t *testing.T) {
s = PrettyBytes(1024*1024*1024 + 1024*1024*512 + 59) s = PrettyBytes(1024*1024*1024 + 1024*1024*512 + 59)
assert.Equal(t, "1.50G", s, "be the same.") 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.")
}