Created
August 22, 2014 00:29
-
-
Save jdiez17/701cabd145f70c5178a1 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 ( | |
"bufio" | |
"fmt" | |
"net" | |
"os" | |
"sync" | |
"time" | |
"io" | |
) | |
const ( | |
timeout = 10 | |
port = "5900" | |
) | |
func test(addr string, results chan<- string, wg *sync.WaitGroup) { | |
wg.Add(1) | |
defer wg.Done() | |
deadline := time.Now().Add(time.Duration(timeout) * time.Second) | |
dialer := net.Dialer{Deadline: deadline} | |
conn, err := dialer.Dial("tcp", net.JoinHostPort(addr, port)) | |
if err != nil { | |
return | |
} | |
conn.SetReadDeadline(deadline) | |
conn.SetWriteDeadline(deadline) | |
var version [1024]byte | |
var auth [1024]byte | |
n, err := conn.Read(version[:]) | |
if err != nil && (err != io.EOF || n == 0) { | |
fmt.Fprintln(os.Stderr, err.Error()) | |
return | |
} | |
n, err = conn.Write(version[:]) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err.Error()) | |
return | |
} | |
n, err = conn.Read(auth[:]) | |
if err != nil && (err != io.EOF || n == 0) { | |
fmt.Fprintln(os.Stderr, err.Error()) | |
return | |
} | |
conn.Close() | |
// interpret vnc auth packet, accept it if one | |
// of the methods it supports is "unauthenticated" | |
for i := 0; i < int(auth[0]); i++ { | |
if auth[i+1] == 1 { | |
results <- addr | |
} | |
} | |
return | |
} | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
resultsChan := make(chan string) | |
var wg sync.WaitGroup | |
go func() { | |
for ip := range resultsChan { | |
fmt.Println(ip) | |
} | |
}() | |
for scanner.Scan() { | |
go test(scanner.Text(), resultsChan, &wg) | |
} | |
close(resultsChan) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment