sessions.go 3.9 KB

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