54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"local/args"
|
|
"log"
|
|
"time"
|
|
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
type Config struct {
|
|
Listen string
|
|
Timeout time.Duration
|
|
TLSInsecure bool
|
|
Limiter *rate.Limiter
|
|
DNS string
|
|
TCPProxy string
|
|
TCPProxyTLS bool
|
|
}
|
|
|
|
func NewConfig() *Config {
|
|
as := args.NewArgSet()
|
|
|
|
as.Append(args.INT, "p", "port to listen on", 61113)
|
|
as.Append(args.INT, "kbps", "kilobytes per sec limit", -1)
|
|
as.Append(args.BOOL, "tls-insecure", "permit tls insecure", false)
|
|
as.Append(args.BOOL, "tcp-proxy-tls", "tcp proxy uses tls", true)
|
|
as.Append(args.DURATION, "t", "timeout", time.Minute)
|
|
|
|
as.Append(args.STRING, "dns", "dns ip:port", "1.1.1.1:53")
|
|
as.Append(args.STRING, "tcp-proxy", "host:port to tcp proxy to instead", "")
|
|
|
|
if err := as.Parse(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var limiter *rate.Limiter
|
|
if kbps := as.GetInt("kbps"); kbps > 0 {
|
|
limiter = rate.NewLimiter(rate.Limit(kbps*1024), 100*1024)
|
|
log.Printf("created limiter with (%v, %v)", limiter.Limit(), limiter.Burst())
|
|
}
|
|
|
|
return &Config{
|
|
Listen: fmt.Sprintf(":%v", as.GetInt("p")),
|
|
Timeout: as.GetDuration("t"),
|
|
TLSInsecure: as.GetBool("tls-insecure"),
|
|
Limiter: limiter,
|
|
DNS: as.GetString("dns"),
|
|
TCPProxy: as.GetString("tcp-proxy"),
|
|
TCPProxyTLS: as.GetBool("tcp-proxy-tls"),
|
|
}
|
|
}
|