Skip to content

Commit

Permalink
option for reducing memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonz committed Jun 5, 2017
1 parent fbdfb3e commit 1062e2e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
11 changes: 11 additions & 0 deletions ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ func getSNIIP() []string {
return ips
}

//get all sni ip
func getSNIIPQueue() {
ipRanges := getSNIIPRange()
for _, ipRange := range ipRanges {
parsedips := parseSNIIPRange(ipRange)
for _, ip := range parsedips {
totalips <- ip
}
}
}

func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
Expand Down
37 changes: 31 additions & 6 deletions sni.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SNI struct {
ServerName []string `json:"server_name"`
SortByDelay bool `json:"sort_by_delay"`
AlwaysCheck bool `json:"always_check_all_ip"`
SoftMode bool `json:"soft_mode"`
OutputAllHostname bool
IsOverride bool
}
Expand Down Expand Up @@ -56,6 +57,7 @@ var config SNI
var certPool *x509.CertPool
var tlsConfig *tls.Config
var dialer net.Dialer
var totalips chan string

func init() {
parseConfig()
Expand All @@ -72,6 +74,25 @@ func main() {
var ips []string
status := getStatus()
status = strings.Replace(strings.Replace(status, "\n", "", -1), "\r", "", -1)

var lastOKIP []string
for _, ip := range getLastOkIP() {
lastOKIP = append(lastOKIP, ip.Address)
}

if config.SoftMode {
totalips = make(chan string, config.Concurrency*10)
go func() {
for _, ip := range lastOKIP {
totalips <- ip
}
getSNIIPQueue()
close(totalips)
}()

goto Queue
}

if config.AlwaysCheck {
ips = getSNIIP()
} else {
Expand All @@ -88,11 +109,9 @@ func main() {
ips = getDifference(getSNIIP(), getLastNoIP())
}
}
var lastOKIP []string
for _, ip := range getLastOkIP() {
lastOKIP = append(lastOKIP, ip.Address)
}

ips = append(lastOKIP, ips...)
Queue:
err := os.Truncate(sniResultFileName, 0)
checkErr(fmt.Sprintf("truncate file %s error: ", sniResultFileName), err, Error)
write2File("false", statusFileName)
Expand All @@ -102,8 +121,14 @@ func main() {
//check all sni ip begin
t0 := time.Now()
go func() {
for _, ip := range ips {
jobs <- ip
if config.SoftMode {
for ip := range totalips {
jobs <- ip
}
} else {
for _, ip := range ips {
jobs <- ip
}
}
close(jobs)
}()
Expand Down
3 changes: 2 additions & 1 deletion sni.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"www.bing.com"
],
"sort_by_delay":true,
"always_check_all_ip":true
"always_check_all_ip":true,
"soft_mode":false
}

0 comments on commit 1062e2e

Please sign in to comment.