captcha.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package captcha
  2. import (
  3. "bytes"
  4. "image"
  5. "image/png"
  6. "os"
  7. "rand"
  8. "time"
  9. crand "crypto/rand"
  10. "github.com/dchest/uniuri"
  11. "io"
  12. "container/list"
  13. "sync"
  14. )
  15. const (
  16. dotSize = 6
  17. maxSkew = 3
  18. expiration = 2 * 60 // 2 minutes
  19. collectNum = 100 // number of items that triggers collection
  20. )
  21. type expValue struct {
  22. timestamp int64
  23. id string
  24. }
  25. type storage struct {
  26. mu sync.RWMutex
  27. ids map[string][]byte
  28. exp *list.List
  29. // Number of items stored after last collection
  30. colNum int
  31. }
  32. func newStore() *storage {
  33. s := new(storage)
  34. s.ids = make(map[string][]byte)
  35. s.exp = list.New()
  36. return s
  37. }
  38. var store = newStore()
  39. func NewImage(numbers []byte) *image.NRGBA {
  40. w := numberWidth * (dotSize + 2) * len(numbers)
  41. h := numberHeight * (dotSize + 5)
  42. img := image.NewNRGBA(w, h)
  43. color := image.NRGBAColor{uint8(rand.Intn(50)), uint8(rand.Intn(50)), uint8(rand.Intn(128)), 0xFF}
  44. fillWithCircles(img, color, 40, 4)
  45. x := rand.Intn(dotSize)
  46. y := 0
  47. setRandomBrightness(&color, 180)
  48. for _, n := range numbers {
  49. y = rand.Intn(dotSize * 4)
  50. drawNumber(img, font[n], x, y, color)
  51. x += dotSize*numberWidth + rand.Intn(maxSkew) + 3
  52. }
  53. return img
  54. }
  55. func init() {
  56. rand.Seed(time.Seconds())
  57. }
  58. func randomNumbers() []byte {
  59. n := make([]byte, 6)
  60. if _, err := io.ReadFull(crand.Reader, n); err != nil {
  61. panic(err)
  62. }
  63. for i := range n {
  64. n[i] %= 10
  65. }
  66. return n
  67. }
  68. func Encode(w io.Writer) (numbers []byte, err os.Error) {
  69. numbers = randomNumbers()
  70. err = png.Encode(w, NewImage(numbers))
  71. return
  72. }
  73. func New() string {
  74. ns := randomNumbers()
  75. id := uniuri.New()
  76. store.mu.Lock()
  77. defer store.mu.Unlock()
  78. store.ids[id] = ns
  79. store.exp.PushBack(expValue{time.Seconds(), id})
  80. store.colNum++
  81. if store.colNum > collectNum {
  82. Collect()
  83. store.colNum = 0
  84. }
  85. return id
  86. }
  87. func WriteImage(w io.Writer, id string) os.Error {
  88. store.mu.RLock()
  89. defer store.mu.RUnlock()
  90. ns, ok := store.ids[id]
  91. if !ok {
  92. return os.NewError("captcha id not found")
  93. }
  94. return png.Encode(w, NewImage(ns))
  95. }
  96. func Verify(w io.Writer, id string, ns []byte) bool {
  97. store.mu.Lock()
  98. defer store.mu.Unlock()
  99. realns, ok := store.ids[id]
  100. if !ok {
  101. return false
  102. }
  103. store.ids[id] = nil, false
  104. return bytes.Equal(ns, realns)
  105. }
  106. func Collect() {
  107. now := time.Seconds()
  108. store.mu.Lock()
  109. defer store.mu.Unlock()
  110. for e := store.exp.Front(); e != nil; e = e.Next() {
  111. ev, ok := e.Value.(expValue)
  112. if !ok {
  113. return
  114. }
  115. if ev.timestamp+expiration < now {
  116. store.ids[ev.id] = nil, false
  117. store.exp.Remove(e)
  118. } else {
  119. return
  120. }
  121. }
  122. }