defaults dont work for conf
parent
73de851e81
commit
2099ae50c6
|
|
@ -6,3 +6,5 @@ crt: ./testdata/rproxy3server.crt
|
||||||
key: ./testdata/rproxy3server.key
|
key: ./testdata/rproxy3server.key
|
||||||
user: bel
|
user: bel
|
||||||
pass: bel
|
pass: bel
|
||||||
|
rate: 1
|
||||||
|
burst: 2
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"local/rproxy3/storage/packable"
|
"local/rproxy3/storage/packable"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -52,3 +54,25 @@ func notEmpty(s ...string) bool {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetRate() (int, int) {
|
||||||
|
r := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagRate, r)
|
||||||
|
b := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagBurst, b)
|
||||||
|
|
||||||
|
rate, _ := strconv.Atoi(r.String())
|
||||||
|
burst, _ := strconv.Atoi(b.String())
|
||||||
|
|
||||||
|
return rate, burst
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTimeout() int {
|
||||||
|
t := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagTimeout, t)
|
||||||
|
|
||||||
|
timeout, _ := strconv.Atoi(t.String())
|
||||||
|
log.Printf("TIMEOUT t:%q, i:%v", t.String(), timeout)
|
||||||
|
|
||||||
|
return timeout
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,9 @@ const flagCert = "crt"
|
||||||
const flagKey = "key"
|
const flagKey = "key"
|
||||||
const flagUser = "user"
|
const flagUser = "user"
|
||||||
const flagPass = "pass"
|
const flagPass = "pass"
|
||||||
|
const flagRate = "rate"
|
||||||
|
const flagBurst = "burst"
|
||||||
|
const flagTimeout = "timeout"
|
||||||
|
|
||||||
var conf = storage.NewMap()
|
var conf = storage.NewMap()
|
||||||
|
|
||||||
|
|
@ -35,6 +38,9 @@ type fileConf struct {
|
||||||
KeyPath string `yaml:"key"`
|
KeyPath string `yaml:"key"`
|
||||||
Username string `yaml:"user"`
|
Username string `yaml:"user"`
|
||||||
Password string `yaml:"pass"`
|
Password string `yaml:"pass"`
|
||||||
|
Rate string `yaml:"rate"`
|
||||||
|
Burst string `yaml:"burst"`
|
||||||
|
Timeout string `yaml:"timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() error {
|
func Init() error {
|
||||||
|
|
@ -85,6 +91,15 @@ func fromFile() error {
|
||||||
if err := conf.Set(nsConf, flagPass, packable.NewString(c.Password)); err != nil {
|
if err := conf.Set(nsConf, flagPass, packable.NewString(c.Password)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := conf.Set(nsConf, flagRate, packable.NewString(c.Rate)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagBurst, packable.NewString(c.Burst)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagTimeout, packable.NewString(c.Timeout)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,10 +112,14 @@ func fromFlags() error {
|
||||||
binds = append(binds, addFlag(flagKey, "", "path to .key"))
|
binds = append(binds, addFlag(flagKey, "", "path to .key"))
|
||||||
binds = append(binds, addFlag(flagUser, "", "basic auth username"))
|
binds = append(binds, addFlag(flagUser, "", "basic auth username"))
|
||||||
binds = append(binds, addFlag(flagPass, "", "basic auth password"))
|
binds = append(binds, addFlag(flagPass, "", "basic auth password"))
|
||||||
|
binds = append(binds, addFlag(flagRate, "100", "rate limit per second"))
|
||||||
|
binds = append(binds, addFlag(flagBurst, "100", "rate limit burst"))
|
||||||
|
binds = append(binds, addFlag(flagTimeout, "30", "seconds to wait for limiter"))
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
for _, bind := range binds {
|
for _, bind := range binds {
|
||||||
confFlag := flag.Lookup(bind.flag)
|
confFlag := flag.Lookup(bind.flag)
|
||||||
|
log.Printf("flag:%v", confFlag)
|
||||||
if confFlag == nil || confFlag.Value.String() == "" {
|
if confFlag == nil || confFlag.Value.String() == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,16 @@ package server
|
||||||
import (
|
import (
|
||||||
"local/rproxy3/config"
|
"local/rproxy3/config"
|
||||||
"local/rproxy3/storage"
|
"local/rproxy3/storage"
|
||||||
|
|
||||||
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New() *Server {
|
func New() *Server {
|
||||||
port := config.GetPort()
|
port := config.GetPort()
|
||||||
|
r, b := config.GetRate()
|
||||||
return &Server{
|
return &Server{
|
||||||
db: storage.NewMap(),
|
db: storage.NewMap(),
|
||||||
addr: port,
|
addr: port,
|
||||||
|
limiter: rate.NewLimiter(rate.Limit(r), b),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"local/rproxy3/config"
|
"local/rproxy3/config"
|
||||||
|
|
@ -10,6 +11,9 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
const nsRouting = "routing"
|
const nsRouting = "routing"
|
||||||
|
|
@ -36,6 +40,7 @@ type Server struct {
|
||||||
addr string
|
addr string
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
|
limiter *rate.Limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Route(src, dst string) error {
|
func (s *Server) Route(src, dst string) error {
|
||||||
|
|
@ -80,7 +85,16 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
|
func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
return s.doAuth(foo)
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx, can := context.WithTimeout(r.Context(), time.Second*time.Duration(config.GetTimeout()))
|
||||||
|
log.Printf("context iwth tiemout: %v", time.Second*time.Duration(config.GetTimeout()))
|
||||||
|
defer can()
|
||||||
|
if err := s.limiter.Wait(ctx); err != nil {
|
||||||
|
w.WriteHeader(http.StatusTooManyRequests)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.doAuth(foo)(w, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue