2023-05-07 05:57:37 +00:00
package util
import (
"flag"
"fmt"
"regexp"
"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
)
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
2024-08-05 08:07:53 +00:00
SystemProxy * bool
2023-09-08 08:35:41 +00:00
Timeout * int
2024-08-06 08:48:18 +00:00
AllowedPattern [ ] * regexp . Regexp
2024-07-22 04:49:18 +00:00
WindowSize * int
2024-07-22 10:59:11 +00:00
Version * bool
2024-08-08 18:47:34 +00:00
BufferSize * int
2023-05-07 05:57:37 +00:00
}
2024-08-06 08:48:18 +00:00
type StringArray [ ] string
2023-05-07 05:57:37 +00:00
2024-08-06 08:48:18 +00:00
func ( arr * StringArray ) String ( ) string {
return fmt . Sprintf ( "%s" , * arr )
2023-05-07 05:57:37 +00:00
}
2024-08-06 08:48:18 +00:00
func ( arr * StringArray ) Set ( value string ) error {
* arr = append ( * arr , value )
2023-05-07 05:57:37 +00:00
return nil
}
var config * Config
func GetConfig ( ) * Config {
return config
}
func ParseArgs ( ) {
config = & Config { }
2024-07-23 00:41:13 +00:00
config . Addr = flag . String ( "addr" , "127.0.0.1" , "listen address" )
2023-05-07 05:57:37 +00:00
config . Port = flag . Int ( "port" , 8080 , "port" )
2024-07-23 00:41:13 +00:00
config . DnsAddr = flag . String ( "dns-addr" , "8.8.8.8" , "dns address" )
config . DnsPort = flag . Int ( "dns-port" , 53 , "port number for dns" )
2024-08-06 08:48:18 +00:00
config . EnableDoh = flag . Bool ( "enable-doh" , false , "enable 'dns-over-https'" )
2024-07-23 00:41:13 +00:00
config . Debug = flag . Bool ( "debug" , false , "enable debug output" )
config . NoBanner = flag . Bool ( "no-banner" , false , "disable banner" )
2024-08-05 07:49:02 +00:00
config . SystemProxy = flag . Bool ( "system-proxy" , true , "enable system-wide proxy" )
2024-08-06 08:48:18 +00:00
config . Timeout = flag . Int ( "timeout" , 0 , "timeout in milliseconds; no timeout when not given" )
2024-08-03 06:58:17 +00:00
config . WindowSize = flag . Int ( "window-size" , 0 , ` chunk size , in number of bytes , for fragmented client hello ,
2024-07-23 00:41:13 +00:00
try lower values if the default value doesn ' t bypass the DPI ;
2024-08-03 06:58:17 +00:00
when not given , the client hello packet will be sent in two parts :
fragmentation for the first data packet and the rest
` )
2024-08-06 08:48:18 +00:00
config . Version = flag . Bool ( "v" , false , "print spoof-dpi's version; this may contain some other relevant information" )
2024-08-08 18:47:34 +00:00
config . BufferSize = flag . Int ( "buffer-size" , 1024 , "buffer size, in number of bytes, is the maximum amount of data that can be read at once from a remote resource" )
2024-08-06 08:48:18 +00:00
var allowedPattern StringArray
flag . Var (
& allowedPattern ,
2023-05-07 05:57:37 +00:00
"pattern" ,
2024-08-06 08:48:18 +00:00
"bypass DPI only on packets matching this regex pattern; can be given multiple times" ,
2023-05-07 05:57:37 +00:00
)
flag . Parse ( )
2024-08-06 08:48:18 +00:00
for _ , pattern := range allowedPattern {
config . AllowedPattern = append ( config . AllowedPattern , regexp . MustCompile ( pattern ) )
2023-05-07 05:57:37 +00:00
}
}
func PrintColoredBanner ( ) {
2024-07-22 04:49:18 +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 ( )
}
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 ( "" )
}