-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipwhois.go
116 lines (98 loc) · 2.67 KB
/
ipwhois.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ipwhois
import (
"encoding/json"
"errors"
"fmt"
"os/exec"
)
var ErrRateLimit = errors.New("call was rate-limited")
var pyCheckImport = `\
try:
import %s
except ImportError, e:
print "not installed"
`
var pyWhoisQuery = `\
import sys
import json
from ipwhois import IPWhois
from ipwhois.exceptions import HTTPRateLimitError
# Change the user agent
from urllib2 import build_opener
opener = build_opener()
opener.addheaders = [('User-Agent', '%s')]
obj = IPWhois("%s", proxy_opener=opener)
try:
results = obj.lookup_rdap(depth=1, retry_count=0)
print json.dumps(results)
except HTTPRateLimitError:
print "rate-limit"
except:
raise
`
// execPythonScript executes a given python script
func execPythonScript(script string) (string, error) {
cmd := exec.Command("python", "-c", script)
out, err := cmd.Output()
if err != nil {
return "", err
}
return string(out), nil
}
func checkPythonInstalled() bool {
res, err := execPythonScript(`print "Hello"`)
if err != nil || res != "Hello\n" {
return false
}
return true
}
func checkPythonPackageInstalled(name string) bool {
s := fmt.Sprintf(pyCheckImport, name)
res, err := execPythonScript(s)
if err != nil || res == "not installed\n" {
return false
}
return true
}
var (
// PreqreqsMet means that the prerequisites for running this package have been met
// meaning python and py-ipwhois are installed
PreqreqsMet = false
// defaultUserAgent is the default user-agent the request to rdap whois will be made with
defaultUserAgent = "ipwhois"
)
func init() {
// Check if python is present
// Check if py-ipwhois is present
if checkPythonInstalled() && checkPythonPackageInstalled("ipwhois") {
PreqreqsMet = true
}
}
// LookupIP performs an ip whois lookup on the given ip and returns a similar result
// for all RIRs as specified by the py-ipwhois package
func LookupIP(ip string) (*Response, error) {
return LookupIPWithUA(ip, defaultUserAgent)
}
// LookupIPWithUA is similar to LookupIP but allows you to specify the User-Agent
// the request to the RDAP whois server will be made with
func LookupIPWithUA(ip string, ua string) (*Response, error) {
if !PreqreqsMet {
panic("calling `LookupIP` requires you have `python` and the `ipwhois` python package installed")
}
// call python's ipwhois
s := fmt.Sprintf(pyWhoisQuery, ua, ip)
strRes, err := execPythonScript(s)
if err != nil {
return nil, fmt.Errorf("call to py-whois failed: %s", err)
}
// Check if the python error is due to rate-limiting
if strRes == "rate-limit\n" {
return nil, ErrRateLimit
}
// convert string response to struct
var res Response
if err := json.Unmarshal([]byte(strRes), &res); err != nil {
return nil, err
}
return &res, nil
}