From 0f32c1d19fb15d2ac237d0e463f30f130a078f6c Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Fri, 5 Nov 2021 11:50:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/router-http/manager.go | 53 ++++++++++++++++++++++++++++------- router/router-http/router.go | 40 +++++++++++++++----------- router/router-http/routers.go | 12 -------- 3 files changed, 67 insertions(+), 38 deletions(-) diff --git a/router/router-http/manager.go b/router/router-http/manager.go index ad39f913..867ef423 100644 --- a/router/router-http/manager.go +++ b/router/router-http/manager.go @@ -1,9 +1,12 @@ package router_http import ( + "crypto/tls" "errors" "sync" + "github.com/eolinker/eosc/config" + "github.com/eolinker/eosc/log" traffic_http_fast "github.com/eolinker/eosc/traffic/traffic-http-fast" @@ -28,12 +31,14 @@ var manager iManager func init() { var tf traffic.ITraffic + var cfg *config.ListensMsg bean.Autowired(&tf) + bean.Autowired(&cfg) bean.AddInitializingBeanFunc(func() { log.Debug("init router manager") - manager = NewManager(tf) + manager = NewManager(tf, cfg) }) } @@ -56,13 +61,32 @@ func (m *Manager) Cancel() { } //NewManager 创建路由管理器 -func NewManager(tf traffic.ITraffic) *Manager { - +func NewManager(tf traffic.ITraffic, listenCfg *config.ListensMsg) *Manager { + log.Debug("new router manager") m := &Manager{ routers: NewRouters(), tf: traffic_http_fast.NewHttpTraffic(tf), locker: sync.Mutex{}, } + + for _, cfg := range listenCfg.Listens { + port := int(cfg.Port) + l, err := tf.ListenTcp("", port) + if err != nil { + log.Warn("worker listen tcp error:", err) + continue + } + if cfg.Scheme == "https" { + cert, err := config.NewCert(cfg.Certificate, listenCfg.Dir) + if err != nil { + log.Warn("worker create certificate error:", err) + continue + } + m.tf.Set(port, traffic_http_fast.NewHttpService(tls.NewListener(l, &tls.Config{GetCertificate: cert.GetCertificate}))) + continue + } + m.tf.Set(port, traffic_http_fast.NewHttpService(l)) + } return m } @@ -70,18 +94,27 @@ func NewManager(tf traffic.ITraffic) *Manager { func (m *Manager) Add(port int, id string, config *Config) error { m.locker.Lock() defer m.locker.Unlock() - + if port == 0 { + srv := m.tf.All() + for p, s := range srv { + router, _, err := m.routers.Set(p, id, config) + if err != nil { + return err + } + s.Set(router.Handler) + } + return nil + } router, _, err := m.routers.Set(port, id, config) if err != nil { return err } - if config.Protocol == "https" { - certs := newCerts(config.Cert) - m.tf.Get(port).SetHttps(router.Handler(), certs.certs) - - } else { - m.tf.Get(port).SetHttp(router.Handler()) + service, has := m.tf.Get(port) + if !has { + log.Debug("not has port") + return nil } + service.Set(router.Handler) return nil } diff --git a/router/router-http/router.go b/router/router-http/router.go index 2e38bb38..4a751ad4 100644 --- a/router/router-http/router.go +++ b/router/router-http/router.go @@ -19,7 +19,11 @@ type IRouter interface { SetRouter(id string, config *Config) error Count() int Del(id string) int - Handler() fasthttp.RequestHandler + Handler(ctx *fasthttp.RequestCtx) +} + +type IRouterFilter interface { + DoFilter(ctx *http_context.Context) (isContinue bool, err error) } //Router 实现了路由树接口 @@ -28,6 +32,7 @@ type Router struct { data eosc.IUntyped match IMatcher handler fasthttp.RequestHandler + chain []IRouterFilter } //NewRouter 新建路由树 @@ -45,22 +50,25 @@ func (r *Router) Count() int { } //Handler 路由树的handler方法 -func (r *Router) Handler() fasthttp.RequestHandler { - return func(requestCtx *fasthttp.RequestCtx) { - match := r.match - if match == nil { - requestCtx.NotFound() - return - } - log.Debug("router handler", requestCtx.Request.String()) - ctx := http_context.NewContext(requestCtx) - h, e, has := match.Match(ctx.Request()) - if !has { - requestCtx.NotFound() - return - } - h.Handle(ctx, NewEndPoint(e)) +func (r *Router) Handler(requestCtx *fasthttp.RequestCtx) { + match := r.match + if r.match == nil { + requestCtx.NotFound() + return } + log.Debug("router handler", requestCtx.Request.String()) + ctx := http_context.NewContext(requestCtx) + // TODO: 执行全局的Filter + h, e, has := match.Match(ctx.Request()) + if !has { + requestCtx.NotFound() + return + } + h.Handle(ctx, NewEndPoint(e)) + for _, c := range r.chain { + + } + } //SetRouter 将路由配置加入到路由树中 diff --git a/router/router-http/routers.go b/router/router-http/routers.go index 07ae17bb..8e17c1c0 100644 --- a/router/router-http/routers.go +++ b/router/router-http/routers.go @@ -34,7 +34,6 @@ func (rs *Routers) Set(port int, id string, conf *Config) (IRouter, bool, error) rs.data.Set(name, router) return router, true, nil } - // todo 这里需要校验端口已使用的的http协议是否与之前配置冲突,并返回新的合并后的证书列表 router := r.(IRouter) err := router.SetRouter(id, conf) @@ -51,17 +50,6 @@ func NewRouters() *Routers { } } -//func (rs *Routers) GetEmployee(port int) (IRouter, bool) { -// name := strconv.Itoa(port) -// r, has := rs.data.GetEmployee(name) -// if !has { -// var router IRouter = NewRouter() -// rs.data.SetStatus(name, router) -// return router, true -// } -// return r.(IRouter), false -//} - //Del 将路由配置从对应端口的路由树中删去 func (rs *Routers) Del(port int, id string) (IRouter, bool) { name := strconv.Itoa(port)