Last active
August 18, 2016 15:50
-
-
Save artefactop/c1928a5f1ba13f619cb21cde165f63c6 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 ( | |
"fmt" | |
"log" | |
"net" | |
"os" | |
"strconv" | |
"time" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Fprintln(os.Stderr, "Usage: xmpp-srv-tester <NAME> [TIMEOUT]") | |
os.Exit(1) | |
} | |
srv := os.Args[1] | |
fmt.Fprintln(os.Stderr, "name:", srv) | |
cname, addr, err := lookupSRV(srv) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "[ERROR]", err) | |
} | |
fmt.Println("cname:", cname) | |
for _, a := range addr { | |
fmt.Printf(" %d %d %s %d\n", a.Priority, a.Weight, a.Target, a.Port) | |
} | |
for _, a := range addr { | |
go checkTCP(a) | |
} | |
var timeout time.Duration = 1 | |
if len(os.Args) >= 3 { | |
argTimeout := os.Args[2] | |
i, err := strconv.Atoi(argTimeout) | |
if err == nil { | |
timeout = time.Duration(i) | |
} | |
} | |
time.Sleep(time.Second * timeout) | |
} | |
func lookupSRV(srv string) (string, []*net.SRV, error) { | |
defer timeTrack(time.Now(), "DNS request") | |
return net.LookupSRV("xmpp-client", "tcp", srv) | |
} | |
func checkTCP(addr *net.SRV) { | |
defer timeTrack(time.Now(), fmt.Sprintf("Dial to %s:%d", addr.Target[0:len(addr.Target)-1], addr.Port)) | |
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", addr.Target[0:len(addr.Target)-1], addr.Port)) | |
//fmt.Printf("DNS %s remote address %s\n", addr.Target, conn.RemoteAddr().String()) | |
defer conn.Close() | |
if err != nil { | |
fmt.Println("Error", err) | |
} | |
} | |
func timeTrack(start time.Time, name string) { | |
elapsed := time.Since(start) | |
log.Printf("%s took %s", name, elapsed) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment