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 ( import (
"os" "os"
"os/signal" "os/signal"
"regexp"
"strings"
"syscall" "syscall"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/doh" "github.com/xvzc/SpoofDPI/doh"
"github.com/xvzc/SpoofDPI/packet"
"github.com/xvzc/SpoofDPI/proxy" "github.com/xvzc/SpoofDPI/proxy"
"github.com/xvzc/SpoofDPI/util" "github.com/xvzc/SpoofDPI/util"
) )
func main() { func main() {
addr, port, dns, debug, banner, allowedHosts, allowedPattern := util.ParseArgs() util.ParseArgs()
config := util.GetConfig()
if(len(*allowedHosts) > 0) { p := proxy.New(*config.Addr, *config.Port)
var escapedUrls []string doh.Init(*config.Dns)
for _, host := range *allowedHosts { if *config.Debug {
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 {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} else { } else {
log.SetLevel(log.InfoLevel) log.SetLevel(log.InfoLevel)
@ -44,13 +28,13 @@ func main() {
FullTimestamp: true, FullTimestamp: true,
}) })
if banner { if *config.Banner {
util.PrintColoredBanner(addr, port, dns, debug) util.PrintColoredBanner()
} else { } 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) log.Fatal(err)
} }

View File

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

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("")
}