forked from rskang/ircb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipintel.go
186 lines (152 loc) · 4.15 KB
/
ipintel.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package ipintel
import (
"crypto/md5"
"encoding/json"
"fmt"
"github.com/jteeuwen/ircb/cmd"
"github.com/jteeuwen/ircb/plugin"
"github.com/jteeuwen/ircb/proto"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"regexp"
"strconv"
"time"
)
var regMibbit = regexp.MustCompile(`^[a-fA-F0-9]{8}$`)
const url = "http://api.neustar.biz/ipi/std/v1/ipinfo/%s?apikey=%s&sig=%s&format=json"
func init() { plugin.Register(New) }
type Plugin struct {
*plugin.Base
}
func New(profile string) plugin.Plugin {
p := new(Plugin)
p.Base = plugin.New(profile, "ipintel")
return p
}
func (p *Plugin) Load(c *proto.Client) (err error) {
err = p.Base.Load(c)
if err != nil {
return
}
ini := p.LoadConfig()
if ini == nil {
log.Fatalf("[ipintel] No configuration found.")
return
}
key := ini.Section("api").S("key", "")
if len(key) == 0 {
log.Fatalf("[ipintel] No API key found.")
return
}
shared := ini.Section("api").S("shared", "")
if len(shared) == 0 {
log.Fatalf("[ipintel] No API shared secret found.")
return
}
drift := ini.Section("api").I64("drift", 0)
if len(shared) == 0 {
log.Fatalf("[ipintel] No API shared secret found.")
return
}
w := new(cmd.Command)
w.Name = "loc"
w.Description = "Fetch geo-location data for the given IP address."
w.Restricted = false
w.Params = []cmd.Param{
{Name: "ip", Description: "IPv4 address to look up", Pattern: cmd.RegIPv4},
}
w.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
hash := md5.New()
stamp := fmt.Sprintf("%d", time.Now().UTC().Unix()+drift)
io.WriteString(hash, key+shared+stamp)
sig := fmt.Sprintf("%x", hash.Sum(nil))
target := fmt.Sprintf(url, cmd.Params[0].Value, key, sig)
resp, err := http.Get(target)
if err != nil {
log.Printf("[ipintel]: %v", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("[ipintel]: %v", err)
return
}
var data Response
err = json.Unmarshal(body, &data)
if err != nil {
log.Printf("[ipintel]: %v", err)
return
}
inf := data.IPInfo
mapsURL := fmt.Sprintf("https://maps.google.com/maps?q=%f,%f",
inf.Location.Latitude, inf.Location.Longitude)
c.PrivMsg(m.Receiver,
"%s: %s (%s), Network org.: %s, Carrier: %s, TLD: %s, SLD: %s. "+
"Location: %s/%s/%s/%s (%f, %f). Postalcode: %s, Timezone: %d, %s",
m.SenderName,
inf.IPAddress, inf.IPType,
inf.Network.Organization,
inf.Network.Carrier,
inf.Network.Domain.TLD,
inf.Network.Domain.SLD,
inf.Location.Continent,
inf.Location.CountryData.Name,
inf.Location.StateData.Name,
inf.Location.CityData.Name,
inf.Location.Latitude,
inf.Location.Longitude,
inf.Location.CityData.PostalCode,
inf.Location.CityData.TimeZone,
mapsURL,
)
}
cmd.Register(w)
w = new(cmd.Command)
w.Name = "mibbit"
w.Description = "Resolve a mibbit address to a real IP address."
w.Restricted = false
w.Params = []cmd.Param{
{Name: "hex", Description: "Mibbit hex string", Pattern: regMibbit},
}
w.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
hex := cmd.Params[0].Value
var ip [4]uint64
var err error
ip[0], err = strconv.ParseUint(hex[:2], 16, 8)
if err != nil {
c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
return
}
ip[1], err = strconv.ParseUint(hex[2:4], 16, 8)
if err != nil {
c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
return
}
ip[2], err = strconv.ParseUint(hex[4:6], 16, 8)
if err != nil {
c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
return
}
ip[3], err = strconv.ParseUint(hex[6:], 16, 8)
if err != nil {
c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
return
}
address := fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
names, err := net.LookupAddr(address)
if err != nil || len(names) == 0 {
c.PrivMsg(m.Receiver, "%s: %s is %s", m.SenderName, hex, address)
} else {
c.PrivMsg(m.Receiver, "%s: %s is %s / %s",
m.SenderName, hex, address, names[0])
}
}
cmd.Register(w)
return
}