summaryrefslogtreecommitdiff
path: root/whois.go
blob: c2f4f7692854c7b2c0901a922ec8e88716140cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"fmt"
	"io/ioutil"
	"net"
	"time"
)

const (
	WHOIS_SERVER = /*"whois-servers.net"*/ "whois.cymru.com"
	WHOIS_PORT   = "43"
	TIMEOUT      = 30
	RETRIES      = 1
)

func main() {
	domains := []string{"109.105.104.100", "123.123.123.123"}
	res, err := whois(domains, WHOIS_SERVER)
	if err != nil {
		return
	}
	fmt.Println(res)
}

/* implements the whois protocal described at https://en.wikipedia.org/wiki/Whois#Protocol */
func whois(domains []string, server string) (answer string, err error) {
	var conn net.Conn
	for i := 0; i < RETRIES; i++ {
		conn, err = net.DialTimeout("tcp", net.JoinHostPort(server, WHOIS_PORT), time.Second*TIMEOUT)
		if err != nil {
			return
		}
	}
	defer conn.Close()

	//conn.Write([]byte(" -p " + domain + "\r\n"))
	conn.Write([]byte(" -p -o -f -u begin\r\n"))
	for _, d := range domains {
		conn.Write([]byte(d + "\r\n"))
	}
	conn.Write([]byte("end\r\n"))

	buffer, err := ioutil.ReadAll(conn)
	if err != nil {
		return
	}

	answer = string(buffer[:])
	return
}