Skip to content

Commit

Permalink
🚀 Add main function
Browse files Browse the repository at this point in the history
  • Loading branch information
dwisiswant0 committed Jun 2, 2020
1 parent 7ef214b commit 3db579a
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"bufio"
"flag"
"fmt"
"net"
"os"
"sync"
)

func main() {
concurrency := 20
flag.IntVar(&concurrency, "c", 20, "Set the concurrency level")
flag.Parse()
jobs := make(chan string)
var wg sync.WaitGroup
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
for host := range jobs {
addr, err := net.LookupIP(host)
if err != nil {
continue
}
if !isCloudflare(addr[0]) {
fmt.Println(addr[0])
}
}
wg.Done()
}()
}

sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
jobs <- sc.Text()
}

close(jobs)

if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
}

wg.Wait()
}

func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}

func hosts(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}

var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}

lenIPs := len(ips)
switch {
case lenIPs < 2:
return ips, nil

default:
return ips[1 : len(ips)-1], nil
}
}

func isCloudflare(ip net.IP) bool {
cidrs := []string{"173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", "141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", "197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/12", "172.64.0.0/13", "131.0.72.0/22"}
for i := range cidrs {
hosts, err := hosts(cidrs[i])
if err != nil {
continue
}

for _, host := range hosts {
if host == ip.String() {
return true
}
}
}
return false
}

0 comments on commit 3db579a

Please sign in to comment.