Skip to content

Commit

Permalink
output json ip
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonz committed Jan 4, 2017
1 parent 34493ab commit 533becf
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions sni.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type SNI struct {
const (
sniIPFileName string = "sniip.txt"
sniResultFileName string = "sniip_output.txt"
sniJSONFileName string = "ip.txt"
configFileName string = "sni.json"
certFileName string = "cacert.pem"
)
Expand Down Expand Up @@ -80,9 +81,17 @@ func main() {
}
t1 := time.Now()
cost := int(t1.Sub(t0).Seconds())
ipResults := getLastOkIP()
writeIP2File(ipResults, sniResultFileName)
fmt.Printf("\ntime: %ds, ok ip count: %d\n\n", cost, len(ipResults))
iplist := getLastOkIP()
ipstr := strings.Join(iplist, "\n")
writeIP2File(ipstr, sniResultFileName)
jsonip := strings.Join(iplist, "|")
jsonip += "\n\n\n"
jsonip += `"`
jsonip += strings.Join(iplist, `","`)
jsonip += `"`
writeIP2File(jsonip, sniJSONFileName)

fmt.Printf("\ntime: %ds, ok ip count: %d\n\n", cost, len(iplist))
fmt.Scanln()
}

Expand Down Expand Up @@ -163,6 +172,10 @@ func createFile() {
_, err := os.Create(sniResultFileName)
checkErr(fmt.Sprintf("create file %s error: ", sniResultFileName), err, Error)
}
if !isFileExist(sniJSONFileName) {
_, err := os.Create(sniJSONFileName)
checkErr(fmt.Sprintf("create file %s error: ", sniJSONFileName), err, Error)
}
}

//CheckErr checks given error
Expand Down Expand Up @@ -316,9 +329,9 @@ func appendIP2File(ip, filename string) {
}

//write ip to related file
func writeIP2File(ips []string, filename string) {
func writeIP2File(ips string, filename string) {
err := os.Truncate(filename, 0)
checkErr(fmt.Sprintf("truncate file %s error: ", filename), err, Error)
err = ioutil.WriteFile(filename, []byte(strings.Join(ips, "\n")), 0755)
err = ioutil.WriteFile(filename, []byte(ips), 0755)
checkErr(fmt.Sprintf("write ip to file %s error: ", filename), err, Error)
}

0 comments on commit 533becf

Please sign in to comment.