No Description

dev.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. // This file is just something I used while developing.. It's kind of an
  3. // example of usage but currently not a very clean one.
  4. import (
  5. "flag"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "sync"
  10. "time"
  11. "github.com/garyburd/redigo/redis"
  12. "github.com/thomasf/internet"
  13. )
  14. func main() {
  15. flag.Parse()
  16. pool = newPool(*redisServer, "")
  17. mainCidrr()
  18. mainIP2ASN()
  19. }
  20. func mainCidrr() {
  21. cc := internet.CIDRReport{
  22. Date: time.Now(),
  23. }
  24. log.Printf("DOWNLOAD %v", cc)
  25. err := cc.Download()
  26. if err != nil {
  27. panic(err)
  28. }
  29. if cc.IsDownloaded() {
  30. conn := pool.Get()
  31. defer conn.Close()
  32. log.Printf("IMPORT %v", cc)
  33. start := time.Now()
  34. n, err := cc.Import(conn)
  35. if err != nil {
  36. panic(err)
  37. }
  38. log.Printf("Imported %d rows from %s in %s",
  39. n, filepath.Base(cc.Path()), time.Since(start))
  40. }
  41. }
  42. func mainIP2ASN() {
  43. internet.SetDataDir(filepath.Join(os.TempDir(), "internet"))
  44. DoIndex()
  45. conn := pool.Get()
  46. defer conn.Close()
  47. internet.RefreshBGPDump(conn)
  48. DoLookup()
  49. }
  50. func DoLookup() {
  51. conn := pool.Get()
  52. defer conn.Close()
  53. q := internet.NewIP2ASNClient(conn)
  54. q2 := internet.NewASN2ASDescClient(conn)
  55. for _, i := range []string{"8.8.8.8", "5.150.255.150", "127.0.0.1"} {
  56. res, err := q.Current(i)
  57. if err != nil {
  58. panic(err)
  59. }
  60. log.Printf("current : %s: %s ", i, res)
  61. if res != nil {
  62. cur, err := q2.Current(res.ASN)
  63. if err != nil {
  64. panic(err)
  65. }
  66. log.Printf("desc : %s", cur)
  67. }
  68. log.Printf("allhistory: %s", i)
  69. allhist, err := q.AllHistory(i)
  70. if err != nil {
  71. panic(err)
  72. }
  73. for _, v := range allhist {
  74. log.Println(v.String())
  75. }
  76. }
  77. }
  78. func DoIndex() {
  79. var wg sync.WaitGroup
  80. for _, b := range []internet.BGPDump{
  81. {Date: time.Date(2009, 03, 10, 0, 0, 0, 0, time.UTC)},
  82. {Date: time.Date(2014, 03, 10, 0, 0, 0, 0, time.UTC)},
  83. // {Date: time.Date(2015, 04, 18, 0, 0, 0, 0, time.UTC)},
  84. // {Date: time.Date(2015, 01, 01, 0, 0, 0, 0, time.UTC)},
  85. // {Date: time.Date(2015, 01, 02, 0, 0, 0, 0, time.UTC)},
  86. // {Date: time.Date(2012, 03, 10, 0, 0, 0, 0, time.UTC)},
  87. // {Date: time.Date(2010, 03, 04, 0, 0, 0, 0, time.UTC)},
  88. // {Date: time.Date(2009, 03, 11, 0, 0, 0, 0, time.UTC)},
  89. } {
  90. wg.Add(1)
  91. go func(b internet.BGPDump) {
  92. defer func() {
  93. wg.Done()
  94. if r := recover(); r != nil {
  95. log.Println("Recovered in f", r, b.Path())
  96. }
  97. }()
  98. log.Printf("DOWNLOAD %s", b.Path())
  99. err := b.Download()
  100. if err != nil {
  101. panic(err)
  102. }
  103. if b.IsDownloaded() {
  104. log.Printf("IMPORT %s", b.Path())
  105. conn := pool.Get()
  106. defer conn.Close()
  107. start := time.Now()
  108. n, err := b.Import(conn)
  109. if err != nil {
  110. panic(err)
  111. }
  112. log.Printf("Imported %d rows from %s in %s",
  113. n, filepath.Base(b.Path()), time.Since(start))
  114. }
  115. }(b)
  116. }
  117. wg.Wait()
  118. }
  119. func newPool(server, password string) *redis.Pool {
  120. return &redis.Pool{
  121. MaxIdle: 10,
  122. IdleTimeout: 240 * time.Second,
  123. Dial: func() (redis.Conn, error) {
  124. c, err := redis.Dial("tcp", server)
  125. if err != nil {
  126. return nil, err
  127. }
  128. // if _, err := c.Do("AUTH", password); err != nil {
  129. // c.Close()
  130. // return nil, err
  131. // }
  132. return c, err
  133. },
  134. TestOnBorrow: func(c redis.Conn, t time.Time) error {
  135. _, err := c.Do("PING")
  136. return err
  137. },
  138. }
  139. }
  140. var (
  141. pool *redis.Pool
  142. redisServer = flag.String("redisServer", ":28743", "")
  143. // redisPassword = flag.String("redisPassword", "", "")
  144. )