sinaip.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package dispatch
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "io"
  6. "os"
  7. "syscall"
  8. )
  9. type SinaIP struct {
  10. country map[uint16]*Country
  11. province map[uint16]*Province
  12. city map[uint16]*City
  13. isp map[uint16]*ISP
  14. district map[uint16]*District
  15. segment []*IPSegment
  16. }
  17. type IPDetail struct {
  18. Country string
  19. Province string
  20. City string
  21. ISP string
  22. District string
  23. Latitude float64
  24. Longitude float64
  25. }
  26. type IPSegment struct {
  27. start uint32
  28. end uint32
  29. country *Country
  30. province *Province
  31. city *City
  32. isp *ISP
  33. district *District
  34. latitude float64
  35. longitude float64
  36. }
  37. type Country struct {
  38. name string
  39. }
  40. type Province struct {
  41. name string
  42. }
  43. type City struct {
  44. name string
  45. }
  46. type ISP struct {
  47. name string
  48. }
  49. type District struct {
  50. name string
  51. }
  52. func NewSinaIP(file string) (*SinaIP, error) {
  53. f, err := os.Open(file)
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer f.Close()
  58. info, err := f.Stat()
  59. if err != nil {
  60. return nil, err
  61. }
  62. data, err := syscall.Mmap(int(f.Fd()), 0, int(info.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE)
  63. if err != nil {
  64. return nil, err
  65. }
  66. defer syscall.Munmap(data)
  67. var (
  68. segmentOffset uint32
  69. countryOffset uint32
  70. provinceOffset uint32
  71. cityOffset uint32
  72. ispOffset uint32
  73. districtOffset uint32
  74. )
  75. reader := bytes.NewReader(data[0:24])
  76. if err := binary.Read(reader, binary.BigEndian, &segmentOffset); err != nil {
  77. return nil, err
  78. }
  79. if err := binary.Read(reader, binary.BigEndian, &countryOffset); err != nil {
  80. return nil, err
  81. }
  82. if err := binary.Read(reader, binary.BigEndian, &provinceOffset); err != nil {
  83. return nil, err
  84. }
  85. if err := binary.Read(reader, binary.BigEndian, &cityOffset); err != nil {
  86. return nil, err
  87. }
  88. if err := binary.Read(reader, binary.BigEndian, &ispOffset); err != nil {
  89. return nil, err
  90. }
  91. if err := binary.Read(reader, binary.BigEndian, &districtOffset); err != nil {
  92. return nil, err
  93. }
  94. biliIP := new(SinaIP)
  95. if err := biliIP.loadCity(data[cityOffset:]); err != nil {
  96. return nil, err
  97. }
  98. if err := biliIP.loadCountry(data[countryOffset:]); err != nil {
  99. return nil, err
  100. }
  101. if err := biliIP.loadProvince(data[provinceOffset:]); err != nil {
  102. return nil, err
  103. }
  104. if err := biliIP.loadISP(data[ispOffset:]); err != nil {
  105. return nil, err
  106. }
  107. if err := biliIP.loadDistrict(data[districtOffset:]); err != nil {
  108. return nil, err
  109. }
  110. if err := biliIP.loadIPSegment(data[segmentOffset:]); err != nil {
  111. return nil, err
  112. }
  113. return biliIP, nil
  114. }
  115. func (b *SinaIP) DoQuery(ip uint32) *IPDetail {
  116. left := 0
  117. right := len(b.segment) - 1
  118. var r *IPDetail
  119. for left <= right {
  120. middle := left + (right-left)/2
  121. s := b.segment[middle]
  122. if ip >= s.start && ip <= s.end {
  123. r = new(IPDetail)
  124. if s.country != nil {
  125. r.Country = s.country.name
  126. }
  127. if s.province != nil {
  128. r.Province = s.province.name
  129. }
  130. if s.city != nil {
  131. r.City = s.city.name
  132. }
  133. if s.isp != nil {
  134. r.ISP = s.isp.name
  135. }
  136. if s.district != nil {
  137. r.District = s.district.name
  138. }
  139. r.Latitude = s.latitude
  140. r.Longitude = s.longitude
  141. break
  142. } else if ip < s.start {
  143. right = middle - 1
  144. } else if ip > s.end {
  145. left = middle + 1
  146. }
  147. }
  148. return r
  149. }
  150. func (b *SinaIP) loadCountry(data []byte) error {
  151. reader := bytes.NewReader(data)
  152. var count uint16
  153. if err := binary.Read(reader, binary.BigEndian, &count); err != nil {
  154. return err
  155. }
  156. b.country = make(map[uint16]*Country, count)
  157. for i := uint16(0); i < count; i++ {
  158. var code uint16
  159. if err := binary.Read(reader, binary.BigEndian, &code); err != nil {
  160. return err
  161. }
  162. var length uint8
  163. if err := binary.Read(reader, binary.BigEndian, &length); err != nil {
  164. return err
  165. }
  166. country := make([]byte, length)
  167. if _, err := io.ReadFull(reader, country); err != nil {
  168. return err
  169. }
  170. b.country[code] = &Country{
  171. name: string(country),
  172. }
  173. }
  174. return nil
  175. }
  176. func (b *SinaIP) loadProvince(data []byte) error {
  177. reader := bytes.NewReader(data)
  178. var count uint16
  179. if err := binary.Read(reader, binary.BigEndian, &count); err != nil {
  180. return err
  181. }
  182. b.province = make(map[uint16]*Province, count)
  183. for i := uint16(0); i < count; i++ {
  184. var code uint16
  185. if err := binary.Read(reader, binary.BigEndian, &code); err != nil {
  186. return err
  187. }
  188. var length uint8
  189. if err := binary.Read(reader, binary.BigEndian, &length); err != nil {
  190. return err
  191. }
  192. province := make([]byte, length)
  193. if _, err := io.ReadFull(reader, province); err != nil {
  194. return err
  195. }
  196. b.province[code] = &Province{
  197. name: string(province),
  198. }
  199. }
  200. return nil
  201. }
  202. func (b *SinaIP) loadCity(data []byte) error {
  203. reader := bytes.NewReader(data)
  204. var count uint16
  205. if err := binary.Read(reader, binary.BigEndian, &count); err != nil {
  206. return err
  207. }
  208. b.city = make(map[uint16]*City, count)
  209. for i := uint16(0); i < count; i++ {
  210. var code uint16
  211. if err := binary.Read(reader, binary.BigEndian, &code); err != nil {
  212. return err
  213. }
  214. var length uint8
  215. if err := binary.Read(reader, binary.BigEndian, &length); err != nil {
  216. return err
  217. }
  218. city := make([]byte, length)
  219. if _, err := io.ReadFull(reader, city); err != nil {
  220. return err
  221. }
  222. b.city[code] = &City{
  223. name: string(city),
  224. }
  225. }
  226. return nil
  227. }
  228. func (b *SinaIP) loadISP(data []byte) error {
  229. reader := bytes.NewReader(data)
  230. var count uint16
  231. if err := binary.Read(reader, binary.BigEndian, &count); err != nil {
  232. return err
  233. }
  234. b.isp = make(map[uint16]*ISP, count)
  235. for i := uint16(0); i < count; i++ {
  236. var code uint16
  237. if err := binary.Read(reader, binary.BigEndian, &code); err != nil {
  238. return err
  239. }
  240. var length uint8
  241. if err := binary.Read(reader, binary.BigEndian, &length); err != nil {
  242. return err
  243. }
  244. isp := make([]byte, length)
  245. if _, err := io.ReadFull(reader, isp); err != nil {
  246. return err
  247. }
  248. b.isp[code] = &ISP{
  249. name: string(isp),
  250. }
  251. }
  252. return nil
  253. }
  254. func (b *SinaIP) loadDistrict(data []byte) error {
  255. reader := bytes.NewReader(data)
  256. var count uint16
  257. if err := binary.Read(reader, binary.BigEndian, &count); err != nil {
  258. return err
  259. }
  260. b.district = make(map[uint16]*District, count)
  261. for i := uint16(0); i < count; i++ {
  262. var code uint16
  263. if err := binary.Read(reader, binary.BigEndian, &code); err != nil {
  264. return err
  265. }
  266. var length uint8
  267. if err := binary.Read(reader, binary.BigEndian, &length); err != nil {
  268. return err
  269. }
  270. district := make([]byte, length)
  271. if _, err := io.ReadFull(reader, district); err != nil {
  272. return err
  273. }
  274. b.district[code] = &District{
  275. name: string(district),
  276. }
  277. }
  278. return nil
  279. }
  280. func (b *SinaIP) loadIPSegment(data []byte) error {
  281. reader := bytes.NewReader(data)
  282. var count uint32
  283. if err := binary.Read(reader, binary.BigEndian, &count); err != nil {
  284. return err
  285. }
  286. b.segment = make([]*IPSegment, 0, count)
  287. for i := uint32(0); i < count; i++ {
  288. segment := new(IPSegment)
  289. if err := binary.Read(reader, binary.BigEndian, &segment.start); err != nil {
  290. return err
  291. }
  292. if err := binary.Read(reader, binary.BigEndian, &segment.end); err != nil {
  293. return err
  294. }
  295. var (
  296. countryCode uint16
  297. provinceCode uint16
  298. cityCode uint16
  299. ispCode uint16
  300. districtCode uint16
  301. latitude int32
  302. longitude int32
  303. )
  304. if err := binary.Read(reader, binary.BigEndian, &countryCode); err != nil {
  305. return err
  306. } else {
  307. segment.country = b.country[countryCode]
  308. }
  309. if err := binary.Read(reader, binary.BigEndian, &provinceCode); err != nil {
  310. return err
  311. } else {
  312. segment.province = b.province[provinceCode]
  313. }
  314. if err := binary.Read(reader, binary.BigEndian, &cityCode); err != nil {
  315. return err
  316. } else {
  317. segment.city = b.city[cityCode]
  318. }
  319. if err := binary.Read(reader, binary.BigEndian, &ispCode); err != nil {
  320. return err
  321. } else {
  322. segment.isp = b.isp[ispCode]
  323. }
  324. if err := binary.Read(reader, binary.BigEndian, &districtCode); err != nil {
  325. return err
  326. } else {
  327. segment.district = b.district[districtCode]
  328. }
  329. if err := binary.Read(reader, binary.BigEndian, &latitude); err != nil {
  330. return err
  331. } else {
  332. segment.latitude = float64(latitude) / float64(10000)
  333. }
  334. if err := binary.Read(reader, binary.BigEndian, &longitude); err != nil {
  335. return err
  336. } else {
  337. segment.longitude = float64(longitude) / float64(10000)
  338. }
  339. b.segment = append(b.segment, segment)
  340. }
  341. return nil
  342. }