Refactor Args (#185)

The pointer fields have been converted to the usual ones in the ARGS structure for convenience.
This commit is contained in:
vps 2024-08-23 15:17:35 +03:00 committed by GitHub
parent af6ce7d297
commit 74963fef64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 63 additions and 62 deletions

View File

@ -2,11 +2,12 @@ package main
import ( import (
"context" "context"
"github.com/xvzc/SpoofDPI/util/log"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/xvzc/SpoofDPI/util/log"
"github.com/xvzc/SpoofDPI/proxy" "github.com/xvzc/SpoofDPI/proxy"
"github.com/xvzc/SpoofDPI/util" "github.com/xvzc/SpoofDPI/util"
"github.com/xvzc/SpoofDPI/version" "github.com/xvzc/SpoofDPI/version"
@ -14,7 +15,7 @@ import (
func main() { func main() {
args := util.ParseArgs() args := util.ParseArgs()
if *args.Version { if args.Version {
version.PrintVersion() version.PrintVersion()
os.Exit(0) os.Exit(0)
} }
@ -28,14 +29,14 @@ func main() {
pxy := proxy.New(config) pxy := proxy.New(config)
if *config.NoBanner { if config.NoBanner {
util.PrintSimpleInfo() util.PrintSimpleInfo()
} else { } else {
util.PrintColoredBanner() util.PrintColoredBanner()
} }
if *config.SystemProxy { if config.SystemProxy {
if err := util.SetOsProxy(*config.Port); err != nil { if err := util.SetOsProxy(config.Port); err != nil {
logger.Fatal().Msgf("error while changing proxy settings: %s", err) logger.Fatal().Msgf("error while changing proxy settings: %s", err)
} }
defer func() { defer func() {

View File

@ -29,11 +29,11 @@ type Dns struct {
} }
func NewDns(config *util.Config) *Dns { func NewDns(config *util.Config) *Dns {
addr := *config.DnsAddr addr := config.DnsAddr
port := strconv.Itoa(*config.DnsPort) port := strconv.Itoa(config.DnsPort)
return &Dns{ return &Dns{
host: *config.DnsAddr, host: config.DnsAddr,
port: port, port: port,
systemClient: resolver.NewSystemResolver(), systemClient: resolver.NewSystemResolver(),
generalClient: resolver.NewGeneralResolver(net.JoinHostPort(addr, port)), generalClient: resolver.NewGeneralResolver(net.JoinHostPort(addr, port)),

View File

@ -27,11 +27,11 @@ type Proxy struct {
func New(config *util.Config) *Proxy { func New(config *util.Config) *Proxy {
return &Proxy{ return &Proxy{
addr: *config.Addr, addr: config.Addr,
port: *config.Port, port: config.Port,
timeout: *config.Timeout, timeout: config.Timeout,
windowSize: *config.WindowSize, windowSize: config.WindowSize,
enableDoh: *config.EnableDoh, enableDoh: config.EnableDoh,
allowedPattern: config.AllowedPatterns, allowedPattern: config.AllowedPatterns,
resolver: dns.NewDns(config), resolver: dns.NewDns(config),
} }

View File

@ -6,18 +6,18 @@ import (
) )
type Args struct { type Args 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
AllowedPattern *StringArray AllowedPattern StringArray
WindowSize *int WindowSize int
Version *bool Version bool
} }
type StringArray []string type StringArray []string
@ -33,25 +33,24 @@ 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.Port = flag.Int("port", 8080, "port") flag.StringVar(&args.Addr, "addr", "127.0.0.1", "listen address")
args.DnsAddr = flag.String("dns-addr", "8.8.8.8", "dns address") flag.IntVar(&args.Port, "port", 8080, "port")
args.DnsPort = flag.Int("dns-port", 53, "port number for dns") flag.StringVar(&args.DnsAddr, "dns-addr", "8.8.8.8", "dns address")
args.EnableDoh = flag.Bool("enable-doh", false, "enable 'dns-over-https'") flag.IntVar(&args.DnsPort, "dns-port", 53, "port number for dns")
args.Debug = flag.Bool("debug", false, "enable debug output") flag.BoolVar(&args.EnableDoh, "enable-doh", false, "enable 'dns-over-https'")
args.NoBanner = flag.Bool("no-banner", false, "disable banner") flag.BoolVar(&args.Debug, "debug", false, "enable debug output")
args.SystemProxy = flag.Bool("system-proxy", true, "enable system-wide proxy") flag.BoolVar(&args.NoBanner, "no-banner", false, "disable banner")
args.Timeout = flag.Int("timeout", 0, "timeout in milliseconds; no timeout when not given") flag.BoolVar(&args.SystemProxy, "system-proxy", true, "enable system-wide proxy")
args.WindowSize = flag.Int("window-size", 0, `chunk size, in number of bytes, for fragmented client hello, flag.IntVar(&args.Timeout, "timeout", 0, "timeout in milliseconds; no timeout when not given")
flag.IntVar(&args.WindowSize, "window-size", 0, `chunk size, in number of bytes, for fragmented client hello,
try lower values if the default value doesn't bypass the DPI; try lower values if the default value doesn't bypass the DPI;
when not given, the client hello packet will be sent in two parts: when not given, the client hello packet will be sent in two parts:
fragmentation for the first data packet and the rest fragmentation for the first data packet and the rest
`) `)
args.AllowedPattern = new(StringArray) flag.BoolVar(&args.Version, "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")
flag.Var( flag.Var(
args.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",
) )

View File

@ -9,16 +9,16 @@ 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
AllowedPatterns []*regexp.Regexp AllowedPatterns []*regexp.Regexp
} }
@ -45,10 +45,10 @@ func (c *Config) Load(args *Args) {
c.WindowSize = args.WindowSize c.WindowSize = args.WindowSize
} }
func parseAllowedPattern(patterns *StringArray) []*regexp.Regexp { func parseAllowedPattern(patterns StringArray) []*regexp.Regexp {
var allowedPatterns []*regexp.Regexp var allowedPatterns []*regexp.Regexp
for _, pattern := range *patterns { for _, pattern := range patterns {
allowedPatterns = append(allowedPatterns, regexp.MustCompile(pattern)) allowedPatterns = append(allowedPatterns, regexp.MustCompile(pattern))
} }
@ -61,18 +61,18 @@ func PrintColoredBanner() {
pterm.DefaultBigText.WithLetters(cyan, purple).Render() pterm.DefaultBigText.WithLetters(cyan, purple).Render()
pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{ pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{
{Level: 0, Text: "ADDR : " + fmt.Sprint(*config.Addr)}, {Level: 0, Text: "ADDR : " + fmt.Sprint(config.Addr)},
{Level: 0, Text: "PORT : " + fmt.Sprint(*config.Port)}, {Level: 0, Text: "PORT : " + fmt.Sprint(config.Port)},
{Level: 0, Text: "DNS : " + fmt.Sprint(*config.DnsAddr)}, {Level: 0, Text: "DNS : " + fmt.Sprint(config.DnsAddr)},
{Level: 0, Text: "DEBUG : " + fmt.Sprint(*config.Debug)}, {Level: 0, Text: "DEBUG : " + fmt.Sprint(config.Debug)},
}).Render() }).Render()
} }
func PrintSimpleInfo() { func PrintSimpleInfo() {
fmt.Println("") fmt.Println("")
fmt.Println("- ADDR : ", *config.Addr) fmt.Println("- ADDR : ", config.Addr)
fmt.Println("- PORT : ", *config.Port) fmt.Println("- PORT : ", config.Port)
fmt.Println("- DNS : ", *config.DnsAddr) fmt.Println("- DNS : ", config.DnsAddr)
fmt.Println("- DEBUG : ", *config.Debug) fmt.Println("- DEBUG : ", config.Debug)
fmt.Println("") fmt.Println("")
} }

View File

@ -3,10 +3,11 @@ package log
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/rs/zerolog"
"github.com/xvzc/SpoofDPI/util"
"os" "os"
"time" "time"
"github.com/rs/zerolog"
"github.com/xvzc/SpoofDPI/util"
) )
const ( const (
@ -42,7 +43,7 @@ func InitLogger(cfg *util.Config) {
} }
logger = zerolog.New(consoleWriter).Hook(ctxHook{}) logger = zerolog.New(consoleWriter).Hook(ctxHook{})
if *cfg.Debug { if cfg.Debug {
logger = logger.Level(zerolog.DebugLevel) logger = logger.Level(zerolog.DebugLevel)
} else { } else {
logger = logger.Level(zerolog.InfoLevel) logger = logger.Level(zerolog.InfoLevel)