mirror of
https://github.com/simon987/Architeuthis.git
synced 2025-12-14 17:39:03 +00:00
More config options, bug fixes
This commit is contained in:
59
config.go
59
config.go
@@ -2,16 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"golang.org/x/time/rate"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type HostConfig struct {
|
||||
Every string `json:"every"`
|
||||
Burst int `json:"burst"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
EveryStr string `json:"every"`
|
||||
Burst int `json:"burst"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
Every time.Duration
|
||||
}
|
||||
|
||||
type ProxyConfig struct {
|
||||
@@ -20,9 +23,15 @@ type ProxyConfig struct {
|
||||
}
|
||||
|
||||
var config struct {
|
||||
Addr string `json:"addr"`
|
||||
Hosts map[string]HostConfig `json:"hosts"`
|
||||
Proxies []ProxyConfig `json:"proxies"`
|
||||
Addr string `json:"addr"`
|
||||
TimeoutStr string `json:"timeout"`
|
||||
WaitStr string `json:"wait"`
|
||||
Multiplier float64 `json:"multiplier"`
|
||||
Retries int `json:"retries"`
|
||||
Hosts map[string]*HostConfig `json:"hosts"`
|
||||
Proxies []ProxyConfig `json:"proxies"`
|
||||
Wait int64
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func loadConfig() {
|
||||
@@ -35,15 +44,47 @@ func loadConfig() {
|
||||
|
||||
err = json.Unmarshal(configBytes, &config)
|
||||
handleErr(err)
|
||||
|
||||
validateConfig()
|
||||
|
||||
config.Timeout, err = time.ParseDuration(config.TimeoutStr)
|
||||
wait, err := time.ParseDuration(config.WaitStr)
|
||||
config.Wait = int64(wait)
|
||||
|
||||
for _, conf := range config.Hosts {
|
||||
conf.Every, err = time.ParseDuration(conf.EveryStr)
|
||||
handleErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
func validateConfig() {
|
||||
|
||||
hasDefaultHost := false
|
||||
|
||||
for host, conf := range config.Hosts {
|
||||
|
||||
if host == "*" {
|
||||
hasDefaultHost = true
|
||||
}
|
||||
|
||||
for k := range conf.Headers {
|
||||
if strings.ToLower(k) == "accept-encoding" {
|
||||
panic(fmt.Sprintf("headers config for '%s':"+
|
||||
" Do not set the Accept-Encoding header, it breaks goproxy", host))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !hasDefaultHost {
|
||||
panic("config.json: You must specify a default host ('*')")
|
||||
}
|
||||
}
|
||||
|
||||
func applyConfig(proxy *Proxy) {
|
||||
|
||||
for host, conf := range config.Hosts {
|
||||
duration, err := time.ParseDuration(conf.Every)
|
||||
handleErr(err)
|
||||
proxy.Limiters[host] = &ExpiringLimiter{
|
||||
rate.NewLimiter(rate.Every(duration), conf.Burst),
|
||||
rate.NewLimiter(rate.Every(conf.Every), conf.Burst),
|
||||
time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user