Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 1 | package whois |
| 2 | |
| 3 | // Support for the WHOIS protocol. |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "io/ioutil" |
| 9 | "net" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | // Query returns a semi-raw response from a WHOIS server for a given query. |
| 14 | // We only convert \r\n to \n, and then do no other transformation to the data. |
| 15 | func Query(ctx context.Context, server string, query string) (string, error) { |
| 16 | var d net.Dialer |
| 17 | conn, err := d.DialContext(ctx, "tcp", server) |
| 18 | if err != nil { |
| 19 | return "", fmt.Errorf("while dialing %q: %v", server, err) |
| 20 | } |
| 21 | |
| 22 | defer conn.Close() |
| 23 | |
| 24 | fmt.Fprintf(conn, "%s\r\n", query) |
| 25 | |
| 26 | data, err := ioutil.ReadAll(conn) |
| 27 | if err != nil { |
| 28 | return "", fmt.Errorf("while receiving data from %q: %v", server, err) |
| 29 | } |
| 30 | |
| 31 | return strings.ReplaceAll(string(data), "\r", ""), nil |
| 32 | } |