fea: ceci support tcp proxy.

This commit is contained in:
Daniel Ding
2024-12-30 14:03:38 +08:00
parent 106c04b138
commit f2657dd28a
2 changed files with 45 additions and 24 deletions

View File

@@ -1,18 +1,38 @@
package main package main
import ( import (
"flag"
"github.com/luscis/openlan/pkg/config" "github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/proxy" "github.com/luscis/openlan/pkg/proxy"
) )
func main() { func main() {
c := config.NewHttpProxy() mode := "http"
if c != nil { conf := ""
flag.StringVar(&mode, "mode", "http", "Proxy mode for tcp or http")
flag.StringVar(&conf, "conf", "ceci.json", "The configuration file")
flag.Parse()
libol.PreNotify() libol.PreNotify()
h := proxy.NewHttpProxy(c, nil) if mode == "http" {
c := &config.HttpProxy{Conf: conf}
if err := c.Initialize(); err != nil {
return
}
p := proxy.NewHttpProxy(c, nil)
libol.Go(p.Start)
} else {
c := &config.TcpProxy{Conf: conf}
if err := c.Initialize(); err != nil {
return
}
p := proxy.NewTcpProxy(c)
libol.Go(p.Start)
}
libol.SdNotify() libol.SdNotify()
libol.Go(h.Start)
libol.Wait() libol.Wait()
} }
}

View File

@@ -43,24 +43,9 @@ type HttpProxy struct {
Backends []*HttpForward `json:"backends,omitempty" yaml:"backend,omitempty"` Backends []*HttpForward `json:"backends,omitempty" yaml:"backend,omitempty"`
} }
func NewHttpProxy() *HttpProxy {
h := &HttpProxy{}
h.Parse()
err := h.Initialize()
if err != nil {
return nil
}
return h
}
func (h *HttpProxy) Parse() {
flag.StringVar(&h.Conf, "conf", "", "The configure file")
flag.Parse()
}
func (h *HttpProxy) Initialize() error { func (h *HttpProxy) Initialize() error {
if err := h.Load(); err != nil { if err := h.Load(); err != nil {
libol.Error("Proxy.Initialize %s", err) libol.Error("HttpProxy.Initialize %s", err)
return err return err
} }
h.Correct() h.Correct()
@@ -68,7 +53,7 @@ func (h *HttpProxy) Initialize() error {
} }
func (h *HttpProxy) Load() error { func (h *HttpProxy) Load() error {
if h.Conf == "" { if h.Conf == "" || libol.FileExist(h.Conf) != nil {
return libol.NewErr("invalid configure file") return libol.NewErr("invalid configure file")
} }
return libol.UnmarshalLoad(h, h.Conf) return libol.UnmarshalLoad(h, h.Conf)
@@ -138,10 +123,26 @@ func (h *HttpProxy) Save() {
} }
type TcpProxy struct { type TcpProxy struct {
Conf string `json:"-" yaml:"-"`
Listen string `json:"listen,omitempty"` Listen string `json:"listen,omitempty"`
Target []string `json:"target,omitempty"` Target []string `json:"target,omitempty"`
} }
func (t *TcpProxy) Initialize() error {
if err := t.Load(); err != nil {
libol.Error("TcpProxy.Initialize %s", err)
return err
}
return nil
}
func (t *TcpProxy) Load() error {
if t.Conf == "" || libol.FileExist(t.Conf) != nil {
return libol.NewErr("invalid configure file")
}
return libol.UnmarshalLoad(t, t.Conf)
}
type Proxy struct { type Proxy struct {
Conf string `json:"-" yaml:"-"` Conf string `json:"-" yaml:"-"`
ConfDir string `json:"-" yaml:"-"` ConfDir string `json:"-" yaml:"-"`