From fe28cda0c54dce5c0fbcdabea86abc2abc1d39dd Mon Sep 17 00:00:00 2001 From: damnever Date: Thu, 2 Nov 2017 23:44:45 +0800 Subject: [PATCH] Fix limit counter --- sun/config.go | 5 +++-- sun/main.go | 6 ++++++ sun/web/download.go | 3 +++ sun/web/server.go | 3 ++- sun/web/tunnel.go | 4 ++-- sun/web/validator.go | 19 +++++++++++++++---- 6 files changed, 31 insertions(+), 9 deletions(-) diff --git a/sun/config.go b/sun/config.go index a88a317..9cdc63d 100644 --- a/sun/config.go +++ b/sun/config.go @@ -48,6 +48,7 @@ func buildCoreConfig(rawConf cc.Configer) Config { func buildWebConfig(controlPort string, rawConf cc.Configer) *web.Config { conf := &web.Config{} conf.HostIP = rawConf.StringOr("proxy_ip", rawConf.String("host_ip")) + conf.DataDir = rawConf.String("datadir") conf.MuxDomain = rawConf.String("domain") webC := rawConf.Config("web") conf.Addr = webC.String("addr") @@ -59,8 +60,8 @@ func buildWebConfig(controlPort string, rawConf cc.Configer) *web.Config { conf.MaxAdminTunnels = webC.IntAndOr("max_admin_tunnels", "N>=5&&N<=23", 23) conf.MaxUserAgents = webC.IntAndOr("max_user_agents", "N>=3&&N<=10", 5) conf.MaxUserTunnels = webC.IntAndOr("max_user_tunnels", "N>=3&&N<=12", 10) - conf.MaxDownloadsPerHour = webC.IntAndOr("max_tunnel_updates_per_hour", "N>=5&&N<=24", 12) - conf.MaxTunnelUpdatePerHour = webC.IntAndOr("max_downloads_per_hour", "N>=5&&N<=24", 8) + conf.MaxDownloadsPerHour = webC.IntAndOr("max_downloads_per_hour", "N>=3&&N<=10", 6) + conf.MaxTunnelUpdatePerHour = webC.IntAndOr("max_tunnel_updates_per_hour", "N>=5&&N<=24", 12) agentConfig := fmt.Sprintf("control_server: %s:%s\n%s", conf.HostIP, controlPort, webC.String("agent_config")) conf.AgentConfig = agentConfig return conf diff --git a/sun/main.go b/sun/main.go index b4cb490..b1bfbc5 100644 --- a/sun/main.go +++ b/sun/main.go @@ -6,6 +6,7 @@ import ( "io" "net" "os" + "path/filepath" "strings" "github.com/damnever/cc" @@ -43,6 +44,11 @@ func Run() { cconf.Set("host_ip", ip) debugAddr := cconf.String("debug_addr") datadir := cconf.String("datadir") + datadir, err = filepath.Abs(datadir) + if err != nil { + logger.Fatal("Resolve absolute path(%s) failed: %v", datadir, err) + } + cconf.Set("datadir", datadir) coreconf := buildCoreConfig(cconf) _, port, err := net.SplitHostPort(coreconf.RPCConf.ListenAddr) if err != nil { diff --git a/sun/web/download.go b/sun/web/download.go index ae5fcc3..c508474 100644 --- a/sun/web/download.go +++ b/sun/web/download.go @@ -22,6 +22,9 @@ func (s *Server) download(c echo.Context) error { data, err := s.builder.TryGetPkg(user.targetName, ahash, os, arch, arm) if err != nil { + if err == errIsBuilding || err == errUnknown { + return newUserError(err.Error()) + } return err } filename := fmt.Sprintf("attachment; filename=flower-%s-%s_%s%s.zip", version.Info(), os, arch, arm) diff --git a/sun/web/server.go b/sun/web/server.go index 443f875..5298b99 100644 --- a/sun/web/server.go +++ b/sun/web/server.go @@ -31,6 +31,7 @@ type userCtx struct { type Config struct { Addr string + DataDir string MuxDomain string AllowOrigins []string HostIP string @@ -54,7 +55,7 @@ type Server struct { } func New(conf *Config, db *storage.DB, pub pubsub.Publisher) (*Server, error) { - builder, err := NewBuilder(conf.AgentConfig) + builder, err := NewBuilder(conf.DataDir, conf.AgentConfig) if err != nil { return nil, err } diff --git a/sun/web/tunnel.go b/sun/web/tunnel.go index 3f92f1a..f452326 100644 --- a/sun/web/tunnel.go +++ b/sun/web/tunnel.go @@ -63,7 +63,7 @@ func (s *Server) createTunnel(c echo.Context) error { return newUserError(err.Error()) } exportAddr := c.FormValue("export_addr") - if err := ValidateAddr(exportAddr); err != nil { + if err := ValidateLocalAddr(exportAddr); err != nil { return newUserError(err.Error()) } tag := c.FormValue("tag") @@ -76,7 +76,7 @@ func (s *Server) createTunnel(c echo.Context) error { serverAddr = fmt.Sprintf("%s.%s", serverAddr, user.targetName) } else { serverAddr = fmt.Sprintf("0.0.0.0:%s", serverAddr) - if err := ValidateAddr(serverAddr); err != nil { + if err := ValidateServerAddr(serverAddr); err != nil { return newUserError(err.Error()) } } diff --git a/sun/web/validator.go b/sun/web/validator.go index 077285d..3523152 100644 --- a/sun/web/validator.go +++ b/sun/web/validator.go @@ -68,7 +68,15 @@ func ValidteProtocol(proto string) error { return fmt.Errorf("unsupported protocol %s", proto) } -func ValidateAddr(addr string) error { +func ValidateLocalAddr(addr string) error { + return validateAddr(addr, 0) +} + +func ValidateServerAddr(addr string) error { + return validateAddr(addr, 1024) +} + +func validateAddr(addr string, minPort int) error { host, port, err := net.SplitHostPort(addr) if err != nil { return err @@ -77,7 +85,7 @@ func ValidateAddr(addr string) error { if err != nil { return err } - if n < 1024 || n > 65535 { + if n < minPort || n > 65535 { return fmt.Errorf("port(%d) must range in [1024, 65535]", n) } if host == "localhost" { @@ -96,7 +104,10 @@ type counter struct { } func newCounster(max int) *counter { - return &counter{max: max} + return &counter{ + max: max, + counters: map[string]*counterItem{}, + } } func (c *counter) Incr(names ...string) bool { @@ -134,7 +145,7 @@ func (c *counterItem) Incr() bool { if time.Now().Sub(c.countTime).Hours() < float64(1) { c.count += 1 - if c.count >= c.max { + if c.count > c.max { return false } } else {