chore: refactor config

This commit is contained in:
xvzc 2024-08-15 19:12:29 +09:00
parent b46a67dd20
commit 7f27c1ad43
3 changed files with 39 additions and 35 deletions

View File

@ -28,7 +28,7 @@ func New(config *util.Config) *Proxy {
port: *config.Port, port: *config.Port,
timeout: *config.Timeout, timeout: *config.Timeout,
windowSize: *config.WindowSize, windowSize: *config.WindowSize,
allowedPattern: config.AllowedPattern, allowedPattern: config.AllowedPatterns,
resolver: dns.NewResolver(config), resolver: dns.NewResolver(config),
} }
} }

View File

@ -3,7 +3,6 @@ package util
import ( import (
"flag" "flag"
"fmt" "fmt"
"regexp"
) )
type Args struct { type Args struct {
@ -16,7 +15,7 @@ type Args struct {
NoBanner *bool NoBanner *bool
SystemProxy *bool SystemProxy *bool
Timeout *int Timeout *int
AllowedPattern []*regexp.Regexp AllowedPattern *StringArray
WindowSize *int WindowSize *int
Version *bool Version *bool
} }
@ -33,7 +32,7 @@ func (arr *StringArray) Set(value string) error {
} }
func ParseArgs() *Args { func ParseArgs() *Args {
args := new(Args) args := new(Args)
args.Addr = flag.String("addr", "127.0.0.1", "listen address") args.Addr = flag.String("addr", "127.0.0.1", "listen address")
args.Port = flag.Int("port", 8080, "port") args.Port = flag.Int("port", 8080, "port")
args.DnsAddr = flag.String("dns-addr", "8.8.8.8", "dns address") args.DnsAddr = flag.String("dns-addr", "8.8.8.8", "dns address")
@ -50,18 +49,13 @@ fragmentation for the first data packet and the rest
`) `)
args.Version = flag.Bool("v", false, "print spoof-dpi's version; this may contain some other relevant information") args.Version = flag.Bool("v", false, "print spoof-dpi's version; this may contain some other relevant information")
var allowedPattern StringArray
flag.Var( flag.Var(
&allowedPattern, args.AllowedPattern,
"pattern", "pattern",
"bypass DPI only on packets matching this regex pattern; can be given multiple times", "bypass DPI only on packets matching this regex pattern; can be given multiple times",
) )
flag.Parse() flag.Parse()
for _, pattern := range allowedPattern {
args.AllowedPattern = append(args.AllowedPattern, regexp.MustCompile(pattern))
}
return args return args
} }

View File

@ -9,40 +9,50 @@ import (
) )
type Config struct { type Config struct {
Addr *string Addr *string
Port *int Port *int
DnsAddr *string DnsAddr *string
DnsPort *int DnsPort *int
EnableDoh *bool EnableDoh *bool
Debug *bool Debug *bool
NoBanner *bool NoBanner *bool
SystemProxy *bool SystemProxy *bool
Timeout *int Timeout *int
WindowSize *int WindowSize *int
AllowedPattern []*regexp.Regexp AllowedPatterns []*regexp.Regexp
} }
var config *Config var config *Config
func GetConfig() *Config { func GetConfig() *Config {
if config == nil { if config == nil {
config = new(Config) config = new(Config)
} }
return config return config
} }
func (c *Config) Load(args *Args) { func (c *Config) Load(args *Args) {
c.Addr = args.Addr c.Addr = args.Addr
c.Port = args.Port c.Port = args.Port
c.DnsAddr = args.DnsAddr c.DnsAddr = args.DnsAddr
c.DnsPort = args.DnsPort c.DnsPort = args.DnsPort
c.Debug = args.Debug c.Debug = args.Debug
c.EnableDoh = args.EnableDoh c.EnableDoh = args.EnableDoh
c.NoBanner = args.NoBanner c.NoBanner = args.NoBanner
c.SystemProxy = args.SystemProxy c.SystemProxy = args.SystemProxy
c.Timeout = args.Timeout c.Timeout = args.Timeout
c.AllowedPattern = args.AllowedPattern c.AllowedPatterns = parseAllowedPattern(args.AllowedPattern)
c.WindowSize = args.WindowSize c.WindowSize = args.WindowSize
}
func parseAllowedPattern(patterns *StringArray) []*regexp.Regexp {
var allowedPatterns []*regexp.Regexp
for _, pattern := range *patterns {
allowedPatterns = append(allowedPatterns, regexp.MustCompile(pattern))
}
return allowedPatterns
} }
func PrintColoredBanner() { func PrintColoredBanner() {