SpoofDPI/util/config.go

119 lines
3.1 KiB
Go
Raw Normal View History

2023-05-07 05:57:37 +00:00
package util
import (
"flag"
"fmt"
"regexp"
"strings"
"github.com/pterm/pterm"
2024-07-21 08:12:16 +00:00
"github.com/pterm/pterm/putils"
2023-05-07 05:57:37 +00:00
log "github.com/sirupsen/logrus"
)
type Config struct {
Addr *string
Port *int
2024-07-21 07:57:47 +00:00
DnsAddr *string
2024-07-21 08:12:16 +00:00
DnsPort *int
2024-07-21 07:57:47 +00:00
EnableDoh *bool
2023-05-07 05:57:37 +00:00
Debug *bool
2024-07-21 07:57:47 +00:00
NoBanner *bool
2023-09-08 08:35:41 +00:00
Timeout *int
2023-05-07 05:57:37 +00:00
AllowedPattern *regexp.Regexp
AllowedUrls *regexp.Regexp
}
type ArrayFlags []string
func (i *ArrayFlags) String() string {
return "my string representation"
}
func (i *ArrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var config *Config
var allowedHosts ArrayFlags
var allowedPattern *string
func GetConfig() *Config {
return config
}
func (c *Config) PatternExists() bool {
return c.AllowedPattern != nil || c.AllowedUrls != nil
}
func (c *Config) PatternMatches(bytes []byte) bool {
return (c.AllowedPattern != nil && c.AllowedPattern.Match(bytes)) ||
(c.AllowedUrls != nil && c.AllowedUrls.Match(bytes))
}
func ParseArgs() {
config = &Config{}
config.Addr = flag.String("addr", "127.0.0.1", "Listen addr")
config.Port = flag.Int("port", 8080, "port")
2024-07-21 07:57:47 +00:00
config.DnsAddr = flag.String("dns-addr", "8.8.8.8", "DNS addr")
2024-07-21 08:12:16 +00:00
config.DnsPort = flag.Int("dns-port", 53, "DNS port")
2024-07-21 07:57:47 +00:00
config.EnableDoh = flag.Bool("enable-doh", false, "Enable DOH")
2024-07-21 08:17:45 +00:00
config.Debug = flag.Bool("debug", false, "Enable debug output")
config.NoBanner = flag.Bool("no-banner", false, "Disable banner")
2024-07-21 05:34:38 +00:00
config.Timeout = flag.Int("timeout", 2000, "timeout in milliseconds")
2023-05-07 05:57:37 +00:00
flag.Var(&allowedHosts, "url", "Bypass DPI only on this url, can be passed multiple times")
allowedPattern = flag.String(
"pattern",
"",
"Bypass DPI only on packets matching this regex pattern",
)
flag.Parse()
if len(allowedHosts) > 0 {
var escapedUrls []string
for _, host := range allowedHosts {
escapedUrls = append(escapedUrls, regexp.QuoteMeta(host))
}
allowedHostsRegex := strings.Join(escapedUrls, "|")
config.AllowedUrls = regexp.MustCompile(allowedHostsRegex)
}
if *allowedPattern != "" {
config.AllowedPattern = regexp.MustCompile(*allowedPattern)
}
}
func PrintColoredBanner() {
2024-07-21 08:12:16 +00:00
cyan := putils.LettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan))
purple := putils.LettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta))
2023-05-07 05:57:37 +00:00
pterm.DefaultBigText.WithLetters(cyan, purple).Render()
pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{
2023-09-08 08:35:41 +00:00
{Level: 0, Text: "ADDR : " + fmt.Sprint(*config.Addr)},
2023-05-07 05:57:37 +00:00
{Level: 0, Text: "PORT : " + fmt.Sprint(*config.Port)},
2024-07-21 07:57:47 +00:00
{Level: 0, Text: "DNS : " + fmt.Sprint(*config.DnsAddr)},
2023-05-07 05:57:37 +00:00
{Level: 0, Text: "DEBUG : " + fmt.Sprint(*config.Debug)},
}).Render()
2023-09-08 08:35:41 +00:00
if allowedHosts != nil && len(allowedHosts) > 0 {
log.Info("White listed urls: ", allowedHosts)
}
2023-05-07 05:57:37 +00:00
2023-09-08 08:35:41 +00:00
if *allowedPattern != "" {
log.Info("Regex Pattern: ", *allowedPattern)
}
2023-05-07 05:57:37 +00:00
}
func PrintSimpleInfo() {
fmt.Println("")
2023-09-08 08:35:41 +00:00
fmt.Println("- ADDR : ", *config.Addr)
fmt.Println("- PORT : ", *config.Port)
2024-07-21 07:57:47 +00:00
fmt.Println("- DNS : ", *config.DnsAddr)
2023-09-08 08:35:41 +00:00
fmt.Println("- DEBUG : ", *config.Debug)
2023-05-07 05:57:37 +00:00
fmt.Println("")
}