Skip to content

Commit

Permalink
调整结构
Browse files Browse the repository at this point in the history
  • Loading branch information
Kisesy committed Jan 5, 2018
1 parent c9ededb commit ca80af7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 58 deletions.
9 changes: 5 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@
// Ping 的参数可以在下面进行设置
"VerifyPing": false,

// 每个IP的测试次数
"ScanCountPerIP": 1,

// 是否开启备份
// 每次扫到的IP,都会在此目录下备份一份
"EnableBackup": true,
"BackupDir": "./backup",

// 扫描方式, 可以设置为下面的任意一个, 大小写都可以
"Operation": "quic",
"ScanMode": "quic",

"Ping": {
"ScanMinPingRTT": 80,
"ScanMaxPingRTT": 800,
},

"QUIC": {
// 每个IP的测试次数
"ScanCountPerIP": 1,
// ServerName 可以设置多项, 会随机选择一个
// 默认空列表,随机生成域名格式字符串
// 如果要设置为空, 可以写为 [""]
Expand Down Expand Up @@ -63,6 +62,7 @@
// 暂时只支持 google IP
"TLS": {
// 同 QUIC 说明
"ScanCountPerIP": 1,
"ServerName": [],
"HTTPVerifyHosts": ["dns.google.com"],
"HandshakeTimeout": 2500,
Expand All @@ -79,6 +79,7 @@
},

"SNI": {
"ScanCountPerIP": 1,
// 注意, SNI 需要同时验证两个不同域名的 ServerName 才能正确确认
// 当然你也可以改成一个来简单确认
// 还有, 不要填封锁严重的域名, 比如 google 的域名
Expand Down
77 changes: 40 additions & 37 deletions gscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type PingConfig struct {
}

type ScanConfig struct {
ScanCountPerIP int
ServerName []string
HTTPVerifyHosts []string
HandshakeTimeout time.Duration
Expand All @@ -35,39 +36,51 @@ type ScanConfig struct {
}

type GScanConfig struct {
ScanWorker int
VerifyPing bool
ScanCountPerIP int
EnableBackup bool
BackupDir string

Operation string
Ping PingConfig
Quic ScanConfig
Tls ScanConfig
Sni ScanConfig
ScanWorker int
VerifyPing bool
EnableBackup bool
BackupDir string

ScanMode string
Ping PingConfig
Quic ScanConfig
Tls ScanConfig
Sni ScanConfig
}

var execFolder = "./"

func init() {
rand.Seed(time.Now().Unix())

log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func initConfig(cfg *GScanConfig) {
if cfg.EnableBackup {
if strings.HasPrefix(cfg.BackupDir, "./") {
cfg.BackupDir = filepath.Join(execFolder, cfg.BackupDir)
func initConfig(cfgfile, execFolder string) *GScanConfig {
if strings.HasPrefix(cfgfile, "./") {
cfgfile = filepath.Join(execFolder, cfgfile)
}

gcfg := new(GScanConfig)
if err := readJsonConfig(cfgfile, gcfg); err != nil {
log.Panicln(err)
}

if gcfg.EnableBackup {
if strings.HasPrefix(gcfg.BackupDir, "./") {
gcfg.BackupDir = filepath.Join(execFolder, gcfg.BackupDir)
}
if _, err := os.Stat(cfg.BackupDir); os.IsNotExist(err) {
if err := os.MkdirAll(cfg.BackupDir, 0755); err != nil {
if _, err := os.Stat(gcfg.BackupDir); os.IsNotExist(err) {
if err := os.MkdirAll(gcfg.BackupDir, 0755); err != nil {
log.Println(err)
}
}
}
cfgs := []*ScanConfig{&cfg.Quic, &cfg.Tls, &cfg.Sni}

gcfg.ScanMode = strings.ToLower(gcfg.ScanMode)

gcfg.Ping.ScanMinPingRTT = gcfg.Ping.ScanMinPingRTT * time.Millisecond
gcfg.Ping.ScanMaxPingRTT = gcfg.Ping.ScanMaxPingRTT * time.Millisecond

cfgs := []*ScanConfig{&gcfg.Quic, &gcfg.Tls, &gcfg.Sni}
for _, c := range cfgs {
if strings.HasPrefix(c.InputFile, "./") {
c.InputFile = filepath.Join(execFolder, c.InputFile)
Expand All @@ -87,7 +100,9 @@ func initConfig(cfg *GScanConfig) {
c.ScanMaxRTT *= time.Millisecond
c.HandshakeTimeout *= time.Millisecond
}
return gcfg
}

func main() {
defer func() {
if r := recover(); r != nil {
Expand All @@ -108,28 +123,19 @@ func main() {
flag.StringVar(&cfgfile, "Config File", "./config.json", "Config file, json format")
flag.Parse()

var execFolder = "./"
if e, err := os.Executable(); err != nil {
log.Panicln(err)
} else {
execFolder = filepath.Dir(e)
}
// execFolder = "./"

if strings.HasPrefix(cfgfile, "./") {
cfgfile = filepath.Join(execFolder, cfgfile)
}

gcfg := new(GScanConfig)
err := readJsonConfig(cfgfile, gcfg)
if err != nil {
log.Panicln(err)
}

initConfig(gcfg)
gcfg := initConfig(cfgfile, execFolder)

var cfg *ScanConfig
operation := strings.ToLower(gcfg.Operation)
switch operation {
scanMode := gcfg.ScanMode
switch scanMode {
case "quic":
cfg = &gcfg.Quic
testIPFunc = testQuic
Expand All @@ -149,9 +155,6 @@ func main() {
log.Panicln(err)
}

gcfg.Ping.ScanMinPingRTT = gcfg.Ping.ScanMinPingRTT * time.Millisecond
gcfg.Ping.ScanMaxPingRTT = gcfg.Ping.ScanMaxPingRTT * time.Millisecond

srs := &ScanRecords{}

log.Printf("Start loading IP Range file: %s\n", iprangeFile)
Expand Down Expand Up @@ -188,7 +191,7 @@ func main() {
log.Printf("All results writed to %s\n", cfg.OutputFile)
}
if gcfg.EnableBackup {
filename := fmt.Sprintf("%s_%s_lv%d.txt", operation, time.Now().Format("20060102_150405"), cfg.Level)
filename := fmt.Sprintf("%s_%s_lv%d.txt", scanMode, time.Now().Format("20060102_150405"), cfg.Level)
bakfilename := filepath.Join(gcfg.BackupDir, filename)
if err := ioutil.WriteFile(bakfilename, b.Bytes(), 0644); err != nil {
log.Printf("Failed to write output file:%s for reason:%v\n", bakfilename, err)
Expand Down
38 changes: 21 additions & 17 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (srs *ScanRecords) AddRecord(rec *ScanRecord) {
}

func (srs *ScanRecords) IncScanCounter() {
scanCount := atomic.AddInt32(&(srs.scanCounter), 1)
scanCount := atomic.AddInt32(&srs.scanCounter, 1)
if scanCount%1000 == 0 {
log.Printf("Scanned %d IPs, Found %d records\n", scanCount, srs.RecordSize())
}
Expand All @@ -42,15 +42,26 @@ func (srs *ScanRecords) RecordSize() int {
}

func (srs *ScanRecords) ScanCount() int32 {
return atomic.LoadInt32(&(srs.scanCounter))
return atomic.LoadInt32(&srs.scanCounter)
}

var testIPFunc func(ip string, config *ScanConfig, record *ScanRecord) bool

func testip(ip string, config *ScanConfig) *ScanRecord {
record := new(ScanRecord)
for i := 0; i < config.ScanCountPerIP; i++ {
if !testIPFunc(ip, config, record) {
return nil
}
}
record.IP = ip
record.RTT = record.RTT / time.Duration(config.ScanCountPerIP)
return record
}

func testip_worker(ctx context.Context, ch chan string, gcfg *GScanConfig, cfg *ScanConfig, srs *ScanRecords, wg *sync.WaitGroup) {
defer wg.Done()

_s:
for ip := range ch {
srs.IncScanCounter()

Expand All @@ -64,22 +75,15 @@ _s:
}
}

record := new(ScanRecord)
record.IP = ip
for i := 0; i < gcfg.ScanCountPerIP; i++ {
if !testIPFunc(ip, cfg, record) {
record = nil
continue _s
record := testip(ip, cfg)
if record != nil {
select {
case <-ctx.Done():
return
default:
srs.AddRecord(record)
}
}
record.RTT = record.RTT / time.Duration(gcfg.ScanCountPerIP)

select {
case <-ctx.Done():
return
default:
srs.AddRecord(record)
}
}
}

Expand Down

0 comments on commit ca80af7

Please sign in to comment.