-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dcad1f7
Showing
8 changed files
with
4,196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go-checkip | ||
go-checkip.exe | ||
okip.txt |
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"concurrency" : 100, | ||
"httptimeout" : 7000, | ||
"server" : [ | ||
"gws", | ||
"gvs 1.0" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/golang/glog" | ||
"io/ioutil" | ||
"os" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
const ( | ||
defaultFileMode = 0644 | ||
) | ||
|
||
var sep string | ||
|
||
func init() { | ||
if runtime.GOOS == "windows" { | ||
sep = "\r\n" | ||
} else { | ||
sep = "\n" | ||
} | ||
} | ||
|
||
func readIPFile(file string) []string { | ||
bytes, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
|
||
return strings.Split(string(bytes), sep) | ||
} | ||
|
||
func writeOKIP(okIPList IPList) { | ||
f, err := os.Create("./okip.txt") | ||
checkErr(err) | ||
defer f.Close() | ||
for _, ip := range okIPList.IPList { | ||
f.WriteString(fmt.Sprintf("%s %d %s %s%s", ip.addr, ip.timeDelay, ip.commonName, ip.serverName, sep)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type IP struct { | ||
addr string | ||
commonName string | ||
orgName string | ||
serverName string | ||
timeDelay int | ||
} | ||
|
||
type IPList struct { | ||
sort.Interface | ||
IPList []IP | ||
} | ||
|
||
func (iplist IPList) Len() int { | ||
return len(iplist.IPList) | ||
} | ||
|
||
func (iplist IPList) Less(i, j int) bool { | ||
if iplist.IPList[i].timeDelay <= iplist.IPList[j].timeDelay { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (iplist *IPList) Swap(i, j int) { | ||
t := *new(IP) | ||
t = (*iplist).IPList[i] | ||
(*iplist).IPList[i] = (*iplist).IPList[j] | ||
(*iplist).IPList[j] = t | ||
} | ||
|
||
func inc(ip net.IP) { | ||
for i := len(ip) - 1; i >= 0; i-- { | ||
ip[i]++ | ||
if ip[i] > 0 { | ||
break | ||
} | ||
} | ||
} | ||
|
||
func parseIPRange(iprange string) []string { | ||
var ipStrList []string | ||
if strings.Contains(iprange, "-") { | ||
parts := strings.Split(iprange, "-") | ||
startip := net.ParseIP(parts[0]) | ||
endip := net.ParseIP(parts[1]) | ||
for ip := startip; bytes.Compare(ip, endip) <= 0; inc(ip) { | ||
ipStrList = append(ipStrList, ip.String()) | ||
} | ||
if strings.HasSuffix(ipStrList[0], ".0") { | ||
ipStrList = ipStrList[1:] | ||
} | ||
if strings.HasSuffix(ipStrList[len(ipStrList)-1], ".0") { | ||
ipStrList = ipStrList[:len(ipStrList)-2] | ||
} | ||
return ipStrList | ||
|
||
} else if strings.Contains(iprange, "/") { | ||
ip, ipnet, err := net.ParseCIDR(iprange) | ||
checkErr(err) | ||
for ip = ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) { | ||
ipStrList = append(ipStrList, ip.String()) | ||
} | ||
return ipStrList[1 : len(ipStrList)-1] | ||
} else { | ||
ipStrList = append(ipStrList, iprange) | ||
return ipStrList | ||
} | ||
|
||
} | ||
|
||
func getAllIP(file string) []string { | ||
alliprange := readIPFile(file) | ||
var allip []string | ||
for _, iprange := range alliprange { | ||
allip = append(allip, parseIPRange(iprange)...) | ||
} | ||
return allip | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
74.125.1.0/24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"github.com/golang/glog" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"sort" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type config struct { | ||
Concurrency int | ||
Httptimeout int | ||
Server []string | ||
} | ||
|
||
var ( | ||
httpOkIPList []IP | ||
conf config | ||
cacertPool *x509.CertPool | ||
tlsConfig = &tls.Config{ | ||
RootCAs: cacertPool, | ||
InsecureSkipVerify: true, | ||
} | ||
|
||
httpClient = http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: tlsConfig, | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
configJson, err := ioutil.ReadFile("./config.json") | ||
checkErr(err) | ||
err = json.Unmarshal(configJson, &conf) | ||
cacertFile, err := ioutil.ReadFile("./cacert.pem") | ||
checkErr(err) | ||
cacertPool = x509.NewCertPool() | ||
cacertPool.AppendCertsFromPEM(cacertFile) | ||
httpClient.Timeout = time.Millisecond * time.Duration(conf.Httptimeout) | ||
} | ||
|
||
func checkErr(err error) { | ||
if err != nil { | ||
glog.Fatalln(err) | ||
} | ||
} | ||
|
||
func httpCheckip(ip string, sem chan bool) { | ||
defer func() { <-sem }() | ||
start := time.Now() | ||
resp, err := httpClient.Head(fmt.Sprintf("https://%s", ip)) | ||
end := time.Now() | ||
|
||
if err != nil { | ||
if !strings.Contains(err.Error(), "www.google.com") { | ||
glog.Infof("%s %s", ip, err) | ||
return | ||
} | ||
checkedip := IP{ | ||
addr: ip, | ||
commonName: "google.com", | ||
orgName: "Google Inc", | ||
serverName: "gws", | ||
timeDelay: int(end.Sub(start).Seconds() * 1000), | ||
} | ||
glog.Infof("%s %s %d", checkedip.addr, checkedip.serverName, checkedip.timeDelay) | ||
|
||
if strInSlice(checkedip.serverName, conf.Server) { | ||
httpOkIPList = append(httpOkIPList, checkedip) | ||
} | ||
return | ||
} | ||
|
||
peerCertSubject := resp.TLS.PeerCertificates[0].Subject | ||
commonName := peerCertSubject.CommonName | ||
orgName := "" | ||
if len(peerCertSubject.Organization) > 0 { | ||
orgName = peerCertSubject.Organization[0] | ||
} | ||
serverName := resp.Header.Get("Server") | ||
checkedip := IP{ | ||
addr: ip, | ||
commonName: commonName, | ||
orgName: orgName, | ||
serverName: serverName, | ||
timeDelay: int(end.Sub(start).Seconds() * 1000), | ||
} | ||
|
||
glog.Infof("%s %s %d", checkedip.addr, checkedip.serverName, checkedip.timeDelay) | ||
|
||
if strInSlice(checkedip.serverName, conf.Server) { | ||
httpOkIPList = append(httpOkIPList, checkedip) | ||
} | ||
|
||
} | ||
|
||
func strInSlice(str string, strslice []string) bool { | ||
for _, s := range strslice { | ||
if str == s { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func main() { | ||
defer writeOKIPFile() | ||
|
||
fmt.Printf("Concurrency: %d%sHttptimeout: %d%s", conf.Concurrency, sep, conf.Httptimeout, sep) | ||
fmt.Printf("%#v%s", conf.Server, sep) | ||
|
||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt) | ||
go func() { | ||
for _ = range c { | ||
writeOKIPFile() | ||
os.Exit(1) | ||
} | ||
}() | ||
flag.Set("logtostderr", "true") | ||
flag.Parse() | ||
|
||
sem := make(chan bool, conf.Concurrency) | ||
var ipstrlist []string | ||
ipstrlist = getAllIP("./iprange.txt") | ||
fmt.Printf("IP to be checked: %d\n", len(ipstrlist)) | ||
time.Sleep(time.Second * 2) | ||
|
||
for _, ip := range ipstrlist { | ||
sem <- true | ||
go httpCheckip(ip, sem) | ||
} | ||
|
||
for i := 0; i < cap(sem); i++ { | ||
sem <- true | ||
} | ||
|
||
println(len(httpOkIPList)) | ||
|
||
} | ||
|
||
func writeOKIPFile() { | ||
if len(httpOkIPList) > 0 { | ||
okips := &IPList{ | ||
IPList: httpOkIPList, | ||
} | ||
sort.Sort(okips) | ||
writeOKIP(*okips) | ||
} | ||
} |