Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

Commit

Permalink
Fix limit counter
Browse files Browse the repository at this point in the history
  • Loading branch information
damnever committed Nov 2, 2017
1 parent 9742164 commit fe28cda
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
5 changes: 3 additions & 2 deletions sun/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions sun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"os"
"path/filepath"
"strings"

"github.com/damnever/cc"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions sun/web/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion sun/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type userCtx struct {

type Config struct {
Addr string
DataDir string
MuxDomain string
AllowOrigins []string
HostIP string
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions sun/web/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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())
}
}
Expand Down
19 changes: 15 additions & 4 deletions sun/web/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fe28cda

Please sign in to comment.