sessions.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. Int map[string]int
  15. String map[string]string
  16. Bool 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 (this *Session) Load() {
  32. var session, err = this.R.Cookie("fsession")
  33. if err == nil && len(session.Value) == 40 {
  34. // Load session
  35. this.ident = session.Value
  36. StartNewSession := true
  37. fsessfile := this.DirVHostHome + "/tmp/" + this.ident
  38. file, ferr := os.Open(fsessfile)
  39. if ferr == nil {
  40. defer file.Close()
  41. dec := json.NewDecoder(file)
  42. ferr = dec.Decode(&this.vars)
  43. if ferr == nil {
  44. StartNewSession = false
  45. }
  46. }
  47. if StartNewSession {
  48. sessdata := Vars{}
  49. sessdata.Int = map[string]int{}
  50. sessdata.String = map[string]string{}
  51. sessdata.Bool = map[string]bool{}
  52. this.vars = &sessdata
  53. this.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 := this.VHost + this.RemoteIp + this.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. this.ident = userhashstr
  65. // Try to create session file
  66. sessdata := Vars{}
  67. sessdata.Int = map[string]int{}
  68. sessdata.String = map[string]string{}
  69. sessdata.Bool = map[string]bool{}
  70. this.vars = &sessdata
  71. this.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(*this.W, &cookie)
  76. }
  77. }
  78. func (this *Session) Save() bool {
  79. if !this.changed {
  80. return false
  81. }
  82. fsessfile := this.DirVHostHome + "/tmp/" + this.ident
  83. r, err := json.Marshal(this.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. this.changed = false
  91. return true
  92. }
  93. }
  94. }
  95. return false
  96. }
  97. func (this *Session) IsSetInt(name string) bool {
  98. if _, ok := this.vars.Int[name]; ok {
  99. return true
  100. } else {
  101. return false
  102. }
  103. }
  104. func (this *Session) IsSetString(name string) bool {
  105. if _, ok := this.vars.String[name]; ok {
  106. return true
  107. } else {
  108. return false
  109. }
  110. }
  111. func (this *Session) IsSetBool(name string) bool {
  112. if _, ok := this.vars.Bool[name]; ok {
  113. return true
  114. } else {
  115. return false
  116. }
  117. }
  118. func (this *Session) SetInt(name string, value int) {
  119. this.vars.Int[name] = value
  120. this.changed = true
  121. }
  122. func (this *Session) SetString(name string, value string) {
  123. this.vars.String[name] = value
  124. this.changed = true
  125. }
  126. func (this *Session) SetBool(name string, value bool) {
  127. this.vars.Bool[name] = value
  128. this.changed = true
  129. }
  130. func (this *Session) GetInt(name string) (int, error) {
  131. if this.IsSetInt(name) {
  132. return this.vars.Int[name], nil
  133. } else {
  134. return 0, errors.New("Variable is not found")
  135. }
  136. }
  137. func (this *Session) GetString(name string) (string, error) {
  138. if this.IsSetString(name) {
  139. return this.vars.String[name], nil
  140. } else {
  141. return "", errors.New("Variable is not found")
  142. }
  143. }
  144. func (this *Session) GetBool(name string) (bool, error) {
  145. if this.IsSetBool(name) {
  146. return this.vars.Bool[name], nil
  147. } else {
  148. return false, errors.New("Variable is not found")
  149. }
  150. }
  151. func (this *Session) GetIntDef(name string, def int) int {
  152. if this.IsSetInt(name) {
  153. return this.vars.Int[name]
  154. } else {
  155. return def
  156. }
  157. }
  158. func (this *Session) GetStringDef(name string, def string) string {
  159. if this.IsSetString(name) {
  160. return this.vars.String[name]
  161. } else {
  162. return def
  163. }
  164. }
  165. func (this *Session) GetBoolDef(name string, def bool) bool {
  166. if this.IsSetBool(name) {
  167. return this.vars.Bool[name]
  168. } else {
  169. return def
  170. }
  171. }
  172. func (this *Session) DelInt(name string) {
  173. if this.IsSetInt(name) {
  174. delete(this.vars.Int, name)
  175. this.changed = true
  176. }
  177. }
  178. func (this *Session) DelString(name string) {
  179. if this.IsSetString(name) {
  180. delete(this.vars.String, name)
  181. this.changed = true
  182. }
  183. }
  184. func (this *Session) DelBool(name string) {
  185. if this.IsSetBool(name) {
  186. delete(this.vars.Bool, name)
  187. this.changed = true
  188. }
  189. }