-
Notifications
You must be signed in to change notification settings - Fork 28
/
whois.go
75 lines (57 loc) · 1.3 KB
/
whois.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// -------------------------
//
// Copyright 2015, undiabler
//
// git: github.com/undiabler/golang-whois
//
// http://undiabler.com
//
// Released under the Apache License, Version 2.0
//
//--------------------------
package whois
import (
"fmt"
"io/ioutil"
"net"
"strings"
"time"
)
//Simple connection to whois servers with default timeout 5 sec
func GetWhois(domain string) (string, error) {
return GetWhoisTimeout(domain, time.Second*5)
}
//Connection to whois servers with various time.Duration
func GetWhoisTimeout(domain string, timeout time.Duration) (result string, err error) {
var (
parts []string
zone string
buffer []byte
connection net.Conn
)
parts = strings.Split(domain, ".")
if len(parts) < 2 {
err = fmt.Errorf("Domain(%s) name is wrong!", domain)
return
}
//last part of domain is zome
zone = parts[len(parts)-1]
server, ok := servers[zone]
if !ok {
err = fmt.Errorf("No such server for zone %s. Domain %s.", zone, domain)
return
}
connection, err = net.DialTimeout("tcp", net.JoinHostPort(server, "43"), timeout)
if err != nil {
//return net.Conn error
return
}
defer connection.Close()
connection.Write([]byte(domain + "\r\n"))
buffer, err = ioutil.ReadAll(connection)
if err != nil {
return
}
result = string(buffer[:])
return
}