sessions.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package sessions
  2. import (
  3. "crypto/sha1"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "math/rand"
  8. "net/http"
  9. "os"
  10. "strconv"
  11. "time"
  12. )
  13. type Vars struct {
  14. FInt map[string]int
  15. FString map[string]string
  16. FBool map[string]bool
  17. }
  18. type Session struct {
  19. w *http.ResponseWriter
  20. r *http.Request
  21. vhost string
  22. vhosthome string
  23. remoteip string
  24. vars *Vars
  25. Ident string
  26. }
  27. func New(w *http.ResponseWriter, r *http.Request, vhost string, vhosthome string, remoteip string) *Session {
  28. return &Session{w, r, vhost, vhosthome, remoteip, &Vars{}, ""}
  29. }
  30. func (s *Session) Load() {
  31. var session, err = s.r.Cookie("fsession")
  32. if err == nil && len(session.Value) == 40 {
  33. // Load session
  34. s.Ident = session.Value
  35. StartNewSession := true
  36. fsessfile := s.vhosthome + "/tmp/" + s.Ident
  37. file, ferr := os.Open(fsessfile)
  38. if ferr == nil {
  39. defer file.Close()
  40. dec := json.NewDecoder(file)
  41. ferr = dec.Decode(&s.vars)
  42. if ferr == nil {
  43. StartNewSession = false
  44. }
  45. }
  46. if StartNewSession {
  47. sessdata := Vars{}
  48. sessdata.FInt = map[string]int{}
  49. sessdata.FString = map[string]string{}
  50. sessdata.FBool = map[string]bool{}
  51. s.vars = &sessdata
  52. }
  53. } else {
  54. // Create new session
  55. // Generate unique hash
  56. rand.Seed(time.Now().Unix())
  57. rnd := rand.Intn(9999999-99) + 99
  58. userstr := s.vhost + s.remoteip + s.r.Header.Get("User-Agent") +
  59. strconv.FormatInt((int64(time.Now().Unix())), 10) +
  60. strconv.FormatInt(int64(rnd), 10)
  61. userhashstr := fmt.Sprintf("%x", sha1.Sum([]byte(userstr)))
  62. s.Ident = userhashstr
  63. // Try to create session file
  64. sessdata := Vars{}
  65. sessdata.FInt = map[string]int{}
  66. sessdata.FString = map[string]string{}
  67. sessdata.FBool = map[string]bool{}
  68. s.vars = &sessdata
  69. // Set session cookie
  70. expiration := time.Now().Add(365 * 24 * time.Hour)
  71. cookie := http.Cookie{Name: "fsession", Value: userhashstr, Expires: expiration}
  72. http.SetCookie(*s.w, &cookie)
  73. }
  74. }
  75. func (s *Session) Save() bool {
  76. // TODO: Save session to file only if any var was modified
  77. fsessfile := s.vhosthome + "/tmp/" + s.Ident
  78. r, err := json.Marshal(s.vars)
  79. if err == nil {
  80. file, ferr := os.Create(fsessfile)
  81. if ferr == nil {
  82. defer file.Close()
  83. _, ferr = file.WriteString(string(r))
  84. return true
  85. }
  86. }
  87. return false
  88. }
  89. func (s *Session) IsSetInt(name string) bool {
  90. if _, ok := s.vars.FInt[name]; ok {
  91. return true
  92. } else {
  93. return false
  94. }
  95. }
  96. func (s *Session) IsSetString(name string) bool {
  97. if _, ok := s.vars.FString[name]; ok {
  98. return true
  99. } else {
  100. return false
  101. }
  102. }
  103. func (s *Session) IsSetBool(name string) bool {
  104. if _, ok := s.vars.FBool[name]; ok {
  105. return true
  106. } else {
  107. return false
  108. }
  109. }
  110. func (s *Session) SetInt(name string, value int) {
  111. s.vars.FInt[name] = value
  112. }
  113. func (s *Session) SetString(name string, value string) {
  114. s.vars.FString[name] = value
  115. }
  116. func (s *Session) SetBool(name string, value bool) {
  117. s.vars.FBool[name] = value
  118. }
  119. func (s *Session) GetInt(name string) (int, error) {
  120. if s.IsSetInt(name) {
  121. return s.vars.FInt[name], nil
  122. } else {
  123. return 0, errors.New("Variable is not found")
  124. }
  125. }
  126. func (s *Session) GetString(name string) (string, error) {
  127. if s.IsSetString(name) {
  128. return s.vars.FString[name], nil
  129. } else {
  130. return "", errors.New("Variable is not found")
  131. }
  132. }
  133. func (s *Session) GetBool(name string) (bool, error) {
  134. if s.IsSetBool(name) {
  135. return s.vars.FBool[name], nil
  136. } else {
  137. return false, errors.New("Variable is not found")
  138. }
  139. }
  140. func (s *Session) DelInt(name string) {
  141. if s.IsSetInt(name) {
  142. delete(s.vars.FInt, name)
  143. }
  144. }
  145. func (s *Session) DelString(name string) {
  146. if s.IsSetString(name) {
  147. delete(s.vars.FString, name)
  148. }
  149. }
  150. func (s *Session) DelBool(name string) {
  151. if s.IsSetBool(name) {
  152. delete(s.vars.FBool, name)
  153. }
  154. }