Created
July 30, 2013 03:12
-
-
Save davecheney/6109911 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "flag" | |
| "io" | |
| "log" | |
| "net" | |
| "os" | |
| "strings" | |
| "time" | |
| ) | |
| var ( | |
| a, b, c, d int // start ip | |
| e, f, g, h int // finish ip | |
| ) | |
| func init() { | |
| flag.IntVar(&a, "a", 1, "") | |
| flag.IntVar(&b, "b", 1, "") | |
| flag.IntVar(&c, "c", 1, "") | |
| flag.IntVar(&d, "d", 1, "") | |
| flag.IntVar(&e, "e", 255, "") | |
| flag.IntVar(&f, "f", 255, "") | |
| flag.IntVar(&g, "g", 255, "") | |
| flag.IntVar(&h, "h", 255, "") | |
| flag.Parse() | |
| } | |
| func gen(addr chan<- *net.TCPAddr) { | |
| for { | |
| for A := a; A < 256; A++ { | |
| for B := b; B < 256; B++ { | |
| for C := c; C < 256; C++ { | |
| for D := d; D < 256; D++ { | |
| addr <- &net.TCPAddr{IP: net.IP{byte(A), byte(B), byte(C), byte(D)}, Port: 80} | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| const request = "GET / HTTP/1.0\r\n\r\n" | |
| const timeout = 20 * time.Second | |
| func probe(c <-chan *net.TCPAddr) { | |
| for addr := range c { | |
| conn, err := net.DialTimeout("tcp4", addr.String(), 3*time.Second) | |
| if err != nil { | |
| continue | |
| } | |
| conn.SetDeadline(time.Now().Add(timeout)) | |
| if _, err := io.Copy(conn, strings.NewReader(request)); err != nil { | |
| conn.Close() | |
| continue | |
| } | |
| f, err := os.Create(addr.String()) | |
| if err != nil { | |
| conn.Close() | |
| continue | |
| } | |
| n, err := io.Copy(f, conn) | |
| log.Printf("%v: copied %d bytes, err %v", addr.String(), n, err) | |
| conn.Close() | |
| f.Close() | |
| if n == 0 { | |
| os.Remove(f.Name()) | |
| } | |
| } | |
| } | |
| const N = 64 | |
| func main() { | |
| c := make(chan *net.TCPAddr) | |
| for i := 0; i < N; i++ { | |
| go probe(c) | |
| } | |
| gen(c) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment