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,
timeout: *config.Timeout,
windowSize: *config.WindowSize,
allowedPattern: config.AllowedPattern,
allowedPattern: config.AllowedPatterns,
resolver: dns.NewResolver(config),
}
}

View File

@ -3,7 +3,6 @@ package util
import (
"flag"
"fmt"
"regexp"
)
type Args struct {
@ -16,7 +15,7 @@ type Args struct {
NoBanner *bool
SystemProxy *bool
Timeout *int
AllowedPattern []*regexp.Regexp
AllowedPattern *StringArray
WindowSize *int
Version *bool
}
@ -33,7 +32,7 @@ func (arr *StringArray) Set(value string) error {
}
func ParseArgs() *Args {
args := new(Args)
args := new(Args)
args.Addr = flag.String("addr", "127.0.0.1", "listen address")
args.Port = flag.Int("port", 8080, "port")
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")
var allowedPattern StringArray
flag.Var(
&allowedPattern,
args.AllowedPattern,
"pattern",
"bypass DPI only on packets matching this regex pattern; can be given multiple times",
)
flag.Parse()
for _, pattern := range allowedPattern {
args.AllowedPattern = append(args.AllowedPattern, regexp.MustCompile(pattern))
}
return args
}

View File

@ -9,40 +9,50 @@ import (
)
type Config struct {
Addr *string
Port *int
DnsAddr *string
DnsPort *int
EnableDoh *bool
Debug *bool
NoBanner *bool
SystemProxy *bool
Timeout *int
WindowSize *int
AllowedPattern []*regexp.Regexp
Addr *string
Port *int
DnsAddr *string
DnsPort *int
EnableDoh *bool
Debug *bool
NoBanner *bool
SystemProxy *bool
Timeout *int
WindowSize *int
AllowedPatterns []*regexp.Regexp
}
var config *Config
func GetConfig() *Config {
if config == nil {
config = new(Config)
}
if config == nil {
config = new(Config)
}
return config
}
func (c *Config) Load(args *Args) {
c.Addr = args.Addr
c.Port = args.Port
c.DnsAddr = args.DnsAddr
c.DnsPort = args.DnsPort
c.Debug = args.Debug
c.EnableDoh = args.EnableDoh
c.NoBanner = args.NoBanner
c.SystemProxy = args.SystemProxy
c.Timeout = args.Timeout
c.AllowedPattern = args.AllowedPattern
c.WindowSize = args.WindowSize
c.Addr = args.Addr
c.Port = args.Port
c.DnsAddr = args.DnsAddr
c.DnsPort = args.DnsPort
c.Debug = args.Debug
c.EnableDoh = args.EnableDoh
c.NoBanner = args.NoBanner
c.SystemProxy = args.SystemProxy
c.Timeout = args.Timeout
c.AllowedPatterns = parseAllowedPattern(args.AllowedPattern)
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() {