chore: refactor

This commit is contained in:
xvzc 2023-05-07 14:57:52 +09:00
parent 2489cc450d
commit 7a6d318192
3 changed files with 14 additions and 89 deletions

View File

@ -3,38 +3,22 @@ package main
import (
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/doh"
"github.com/xvzc/SpoofDPI/packet"
"github.com/xvzc/SpoofDPI/proxy"
"github.com/xvzc/SpoofDPI/util"
)
func main() {
addr, port, dns, debug, banner, allowedHosts, allowedPattern := util.ParseArgs()
util.ParseArgs()
config := util.GetConfig()
if(len(*allowedHosts) > 0) {
var escapedUrls []string
for _, host := range *allowedHosts {
escapedUrls = append(escapedUrls, regexp.QuoteMeta(host))
}
allowedHostsRegex := strings.Join(escapedUrls, "|")
packet.UrlsMatcher = regexp.MustCompile(allowedHostsRegex)
}
if(allowedPattern != "") {
packet.PatternMatcher = regexp.MustCompile(allowedPattern)
}
p := proxy.New(addr, port)
doh.Init(dns)
if debug {
p := proxy.New(*config.Addr, *config.Port)
doh.Init(*config.Dns)
if *config.Debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
@ -44,13 +28,13 @@ func main() {
FullTimestamp: true,
})
if banner {
util.PrintColoredBanner(addr, port, dns, debug)
if *config.Banner {
util.PrintColoredBanner()
} else {
util.PrintSimpleInfo(addr, port, dns, debug)
util.PrintSimpleInfo()
}
if err := util.SetOsProxy(port); err != nil {
if err := util.SetOsProxy(*config.Port); err != nil {
log.Fatal(err)
}

View File

@ -1,7 +1,7 @@
package packet
import (
"regexp"
"github.com/xvzc/SpoofDPI/util"
)
type HttpsPacket struct {
@ -18,20 +18,18 @@ func (p *HttpsPacket) Raw() []byte {
return p.raw
}
var PatternMatcher *regexp.Regexp
var UrlsMatcher *regexp.Regexp
func (p *HttpsPacket) SplitInChunks() [][]byte {
if len(p.Raw()) < 1 {
return [][]byte{p.Raw()}
}
config := util.GetConfig()
// If the packet matches the pattern or the URLs, we don't split it
if PatternMatcher != nil || UrlsMatcher != nil {
if (PatternMatcher != nil && PatternMatcher.Match(p.Raw())) || (UrlsMatcher != nil && UrlsMatcher.Match(p.Raw())) {
if config.PatternExists() {
if (config.PatternMatches(p.Raw())) {
return [][]byte{(p.Raw())[:1], (p.Raw())[1:]}
}
return [][]byte{p.Raw()}
}

View File

@ -1,57 +0,0 @@
package util
import (
"flag"
"fmt"
"github.com/pterm/pterm"
)
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
}
func ParseArgs() (string, int, string, bool, bool, *ArrayFlags, string) {
addr := flag.String("addr", "127.0.0.1", "Listen addr")
port := flag.Int("port", 8080, "port")
dns := flag.String("dns", "8.8.8.8", "DNS server")
debug := flag.Bool("debug", false, "true | false")
banner := flag.Bool("banner", true, "true | false")
var allowedUrls ArrayFlags
flag.Var(&allowedUrls, "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()
return *addr, *port, *dns, *debug, *banner, &allowedUrls, *allowedPattern
}
func PrintColoredBanner(addr string, port int, dns string, debug bool) {
cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan))
purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta))
pterm.DefaultBigText.WithLetters(cyan, purple).Render()
pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{
{Level: 0, Text: "ADDR : " + addr},
{Level: 0, Text: "PORT : " + fmt.Sprint(port)},
{Level: 0, Text: "DNS : " + dns},
{Level: 0, Text: "DEBUG : " + fmt.Sprint(debug)},
}).Render()
}
func PrintSimpleInfo(addr string, port int, dns string, debug bool) {
fmt.Println("")
fmt.Println("- ADDR : ", addr)
fmt.Println("- PORT : ", port)
fmt.Println("- DNS : ", dns)
fmt.Println("- DEBUG : ", debug)
fmt.Println("")
}