grs.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package grs
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "net/url"
  7. "regexp"
  8. "time"
  9. "gopkg.in/jmcvetta/napping.v3"
  10. "gopkg.in/mgo.v2/bson"
  11. )
  12. const (
  13. GRS_URL = "http://rest.db.ripe.net/search.json"
  14. )
  15. type GRS struct {
  16. ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
  17. Source string `bson:"source" json:"source"`
  18. IpFrom string `bson:"ip_from" json:"ip_from"`
  19. IpTo string `bson:"ip_to" json:"ip_to"`
  20. IpFromRaw []byte `bson:"ip_from_raw" json:"ip_from_raw"`
  21. IpToRaw []byte `bson:"ip_to_raw" json:"ip_to_raw"`
  22. Description string `bson:"description" json:"description"`
  23. Country string `bson:"country" json:"country"`
  24. Status string `bson:"status" json:"status"`
  25. Cidr string `bson:"cidr" json:"cidr"`
  26. Name string `bson:"name" json:"name"`
  27. Org string `bson:"org" json:"org"`
  28. CreatedAt time.Time `bson:"created_at" json:"created_at"`
  29. }
  30. type GRSRaw struct {
  31. Service Name `json:"service"`
  32. Parameters Parameters `json:"parameters"`
  33. Objects Objects `json:"objects"`
  34. Terms Terms `json:"terms-and-conditions"`
  35. ErrorMessages ErrorMessages `json:"errormessages"`
  36. }
  37. type Name struct {
  38. Name string `json:"name"`
  39. }
  40. type Parameters struct {
  41. InverseLookup map[string]interface{} `json:"inverse-lookup"`
  42. TypeFilters map[string]interface{} `json:"type-filters"`
  43. Flags map[string]interface{} `json:"flags"`
  44. QueryStrings QueryString `json:"query-strings"`
  45. Sources Sources `json:"sources"`
  46. }
  47. type QueryString struct {
  48. QueryString []Value `json:"query-string"`
  49. }
  50. type Value struct {
  51. Value string `json:"value"`
  52. }
  53. type Sources struct {
  54. Ids []Id `json:"source"`
  55. }
  56. type Id struct {
  57. ID string `json:"id"`
  58. }
  59. type Objects struct {
  60. Objects []Object `json:"object"`
  61. }
  62. type Object struct {
  63. Type string `json:"type"`
  64. Link Link `json:"link"`
  65. Source Id `json:source"`
  66. PrimaryKey Attributes `json:"primary-key"`
  67. Attributes Attributes `json:"attributes"`
  68. Tags Tags `json:"tags"`
  69. }
  70. type Link struct {
  71. Type string `json:"type"`
  72. Href string `json:"href"`
  73. }
  74. type Attributes struct {
  75. Attributes []Attribute `json:"attribute"`
  76. }
  77. type Attribute struct {
  78. Link Link `json:"link"`
  79. Name string `json:"name"`
  80. Value string `json:"value"`
  81. ReferencedType string `json:"referenced-type"`
  82. }
  83. type Tags struct {
  84. Tags []Tag `json:"tag"`
  85. }
  86. type Tag struct {
  87. ID string `json:"id"`
  88. Data string `json:"data"`
  89. }
  90. type Terms struct {
  91. Type string `json:"type"`
  92. Href string `json:"href"`
  93. }
  94. type ErrorMessages struct {
  95. ErrorMessage []ErrorMessage `json:"errormessage"`
  96. }
  97. type ErrorMessage struct {
  98. Severity string `json:"severity"`
  99. Text string `json:"text"`
  100. }
  101. func Lookup(ip string) (GRS, error) {
  102. // var data map[string]interface{}
  103. var data GRSRaw
  104. args := url.Values{}
  105. args.Set("query-string", ip)
  106. args.Add("source", "ripe-grs")
  107. args.Add("source", "arin-grs")
  108. args.Add("source", "apnic-grs")
  109. args.Add("source", "lacnic-grs")
  110. args.Add("source", "afrinic-grs")
  111. resp, err := napping.Get(GRS_URL, &args, &data, nil)
  112. if err != nil {
  113. return GRS{}, errors.New(fmt.Sprint("Failed to load data from ", resp.Url, ": ", err))
  114. }
  115. if resp.Status() != 200 {
  116. return GRS{}, errors.New(fmt.Sprint("Failed to load data from GRS server: ", resp.Status()))
  117. }
  118. if len(data.ErrorMessages.ErrorMessage) > 0 {
  119. return GRS{}, errors.New(fmt.Sprint("No GRS data for ", ip, ": ", data.ErrorMessages.ErrorMessage[0].Text))
  120. }
  121. grs := GRS{}
  122. var grs_inetnum *Object
  123. var grs_route *Object
  124. var inetnum string
  125. for i, e := range data.Objects.Objects {
  126. switch e.Type {
  127. case "inetnum":
  128. grs_inetnum = &data.Objects.Objects[i]
  129. case "inet6num":
  130. grs_inetnum = &data.Objects.Objects[i]
  131. case "route":
  132. grs_route = &data.Objects.Objects[i]
  133. }
  134. }
  135. // Inetnum fields
  136. //
  137. if grs_inetnum != nil {
  138. for _, e := range grs_inetnum.Attributes.Attributes {
  139. switch e.Name {
  140. case "inetnum":
  141. inetnum = e.Value
  142. case "inet6num":
  143. inetnum = e.Value
  144. case "netname":
  145. grs.Name = e.Value
  146. case "descr":
  147. grs.Description = e.Value
  148. case "country":
  149. grs.Country = e.Value
  150. case "status":
  151. grs.Status = e.Value
  152. case "org":
  153. grs.Org = e.Value
  154. }
  155. }
  156. }
  157. // Route fields
  158. //
  159. if grs_route != nil {
  160. for _, e := range grs_route.Attributes.Attributes {
  161. switch e.Name {
  162. case "route":
  163. grs.Cidr = e.Value
  164. case "descr":
  165. grs.Description = e.Value
  166. }
  167. }
  168. }
  169. var ip_from net.IP
  170. var ip_to net.IP
  171. if inetnum != "" {
  172. _, network, err := net.ParseCIDR(inetnum)
  173. if err == nil {
  174. ip_from, ip_to = networkRange(network)
  175. grs.IpFrom = ip_from.String()
  176. grs.IpTo = ip_to.String()
  177. if grs.Cidr == "" {
  178. grs.Cidr = network.String()
  179. }
  180. } else {
  181. reg := regexp.MustCompile(`^\s*(\d+\.\d+\.\d+\.\d+)\s*-\s*(\d+\.\d+\.\d+\.\d+)\s*$`)
  182. matches := reg.FindAllStringSubmatch(inetnum, -1)
  183. if matches != nil && matches[0] != nil && matches[0][1] != "" && matches[0][2] != "" {
  184. grs.IpFrom = matches[0][1]
  185. grs.IpTo = matches[0][2]
  186. ip_from = net.ParseIP(grs.IpFrom)
  187. ip_to = net.ParseIP(grs.IpTo)
  188. }
  189. }
  190. grs.IpFromRaw = ip_from.To16()
  191. grs.IpToRaw = ip_to.To16()
  192. }
  193. return grs, nil
  194. }
  195. // Calculates the first and last IP addresses in an IPNet
  196. func networkRange(network *net.IPNet) (net.IP, net.IP) {
  197. netIP := network.IP.To16()
  198. firstIP := netIP.Mask(network.Mask)
  199. lastIP := net.ParseIP("::").To16()
  200. for i := 0; i < len(lastIP); i++ {
  201. lastIP[i] = netIP[i] | ^network.Mask[i]
  202. }
  203. return firstIP, lastIP
  204. }