Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add anti-pattern CLI param; like pattern, but opposite #190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ import (
const scopeProxy = "PROXY"

type Proxy struct {
addr string
port int
timeout int
resolver *dns.Dns
windowSize int
enableDoh bool
allowedPattern []*regexp.Regexp
addr string
port int
timeout int
resolver *dns.Dns
windowSize int
enableDoh bool
allowedPattern []*regexp.Regexp
unallowedPattern []*regexp.Regexp
}

func New(config *util.Config) *Proxy {
return &Proxy{
addr: config.Addr,
port: config.Port,
timeout: config.Timeout,
windowSize: config.WindowSize,
enableDoh: config.EnableDoh,
allowedPattern: config.AllowedPatterns,
resolver: dns.NewDns(config),
addr: config.Addr,
port: config.Port,
timeout: config.Timeout,
windowSize: config.WindowSize,
enableDoh: config.EnableDoh,
allowedPattern: config.AllowedPatterns,
unallowedPattern: config.UnallowedPatterns,
resolver: dns.NewDns(config),
}
}

Expand Down Expand Up @@ -109,17 +111,27 @@ func (pxy *Proxy) Start(ctx context.Context) {
}

func (pxy *Proxy) patternMatches(bytes []byte) bool {
if pxy.allowedPattern == nil {
if pxy.allowedPattern == nil && pxy.unallowedPattern == nil {
return true
}

for _, pattern := range pxy.allowedPattern {
if pxy.unallowedPattern == nil {
for _, pattern := range pxy.allowedPattern {
if pattern.Match(bytes) {
return true
}
}
return false

}

for _, pattern := range pxy.unallowedPattern {
if pattern.Match(bytes) {
return true
return false
}
}

return false
return true
}

func isLoopedRequest(ctx context.Context, ip net.IP) bool {
Expand Down
30 changes: 18 additions & 12 deletions util/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
)

type Args struct {
Addr string
Port int
DnsAddr string
DnsPort int
EnableDoh bool
Debug bool
NoBanner bool
SystemProxy bool
Timeout int
AllowedPattern StringArray
WindowSize int
Version bool
Addr string
Port int
DnsAddr string
DnsPort int
EnableDoh bool
Debug bool
NoBanner bool
SystemProxy bool
Timeout int
AllowedPattern StringArray
UnallowedPattern StringArray
WindowSize int
Version bool
}

type StringArray []string
Expand Down Expand Up @@ -54,6 +55,11 @@ fragmentation for the first data packet and the rest
"pattern",
"bypass DPI only on packets matching this regex pattern; can be given multiple times",
)
flag.Var(
&args.UnallowedPattern,
"anti-pattern",
"bypass DPI on all packets except matching this regex pattern; can be given multiple times",
)

flag.Parse()

Expand Down
34 changes: 18 additions & 16 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (
)

type Config struct {
Addr string
Port int
DnsAddr string
DnsPort int
EnableDoh bool
Debug bool
NoBanner bool
SystemProxy bool
Timeout int
WindowSize int
AllowedPatterns []*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
UnallowedPatterns []*regexp.Regexp
}

var config *Config
Expand All @@ -41,18 +42,19 @@ func (c *Config) Load(args *Args) {
c.NoBanner = args.NoBanner
c.SystemProxy = args.SystemProxy
c.Timeout = args.Timeout
c.AllowedPatterns = parseAllowedPattern(args.AllowedPattern)
c.AllowedPatterns = parsePattern(args.AllowedPattern)
c.UnallowedPatterns = parsePattern(args.UnallowedPattern)
c.WindowSize = args.WindowSize
}

func parseAllowedPattern(patterns StringArray) []*regexp.Regexp {
var allowedPatterns []*regexp.Regexp
func parsePattern(patterns StringArray) []*regexp.Regexp {
var result []*regexp.Regexp

for _, pattern := range patterns {
allowedPatterns = append(allowedPatterns, regexp.MustCompile(pattern))
result = append(result, regexp.MustCompile(pattern))
}

return allowedPatterns
return result
}

func PrintColoredBanner() {
Expand Down