func main() { var dsn string // To tcp root:aaa@tcp(104.236.128.162:3306)/domaindb flag.StringVar(&dsn, "dsn", "root@unix(/tmp/mysql.sock)/domaindb", "Data source name") flag.Parse() db, err := sql.Open("mysql", dsn) if err != nil { log.Fatal(err) } //For now, don't defer to make sure we don't close it when only scanning 1 item. defer db.Close() err = db.Ping() if err != nil { log.Fatal(err) } query := fmt.Sprintf("SELECT name FROM pinyin WHERE status != %v AND Status != %v", DOMAIN_STATUS_REGISTERED, DOMAIN_STATUS_NOT_REGISTERED) //query := fmt.Sprintf("SELECT name FROM pinyin") rows, err := db.Query(query) if err != nil { log.Fatal(err) } // Default max_connections is 151 rateSem := make(chan int, 100) var wg sync.WaitGroup for rows.Next() { var domain string if err := rows.Scan(&domain); err != nil { log.Fatal(err) } rateSem <- 1 wg.Add(1) go func() { defer func() { <-rateSem wg.Done() }() whoisRaw, whoisErr := whois.Query(domain) if whoisErr != nil { log.Printf("Whois failed for: %s err: %v", domain, whoisErr) } else { var s Status if strings.Contains(strings.ToUpper(whoisRaw), strings.ToUpper("No match for \""+domain+"\"")) { s = DOMAIN_STATUS_NOT_REGISTERED } else { s = DOMAIN_STATUS_REGISTERED } if _, localErr := db.Exec(fmt.Sprintf("UPDATE pinyin set status = %v WHERE name = '%s'", s, domain)); localErr != nil { log.Println("Update Error:", localErr) } } }() } wg.Wait() }
func main() { var domain string flag.StringVar(&domain, "domain", "", "domain name to check") flag.Parse() if len(domain) == 0 { flag.Usage() return } whoisRaw, err := whois.Query(domain) if err != nil { log.Fatal("Failed to query domain", err) } fmt.Println("Raw", whoisRaw) }