sessions.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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{
  75. Name: "fsession",
  76. Value: userhashstr,
  77. Path: "/",
  78. Expires: expiration,
  79. }
  80. http.SetCookie(*this.W, &cookie)
  81. }
  82. }
  83. func (this *Session) Save() bool {
  84. if !this.changed {
  85. return false
  86. }
  87. fsessfile := this.DirVHostHome + "/tmp/" + this.ident
  88. r, err := json.Marshal(this.vars)
  89. if err == nil {
  90. file, ferr := os.Create(fsessfile)
  91. if ferr == nil {
  92. defer file.Close()
  93. _, ferr = file.WriteString(string(r))
  94. if ferr == nil {
  95. this.changed = false
  96. return true
  97. }
  98. }
  99. }
  100. return false
  101. }
  102. func (this *Session) IsSetInt(name string) bool {
  103. if _, ok := this.vars.Int[name]; ok {
  104. return true
  105. } else {
  106. return false
  107. }
  108. }
  109. func (this *Session) IsSetString(name string) bool {
  110. if _, ok := this.vars.String[name]; ok {
  111. return true
  112. } else {
  113. return false
  114. }
  115. }
  116. func (this *Session) IsSetBool(name string) bool {
  117. if _, ok := this.vars.Bool[name]; ok {
  118. return true
  119. } else {
  120. return false
  121. }
  122. }
  123. func (this *Session) SetInt(name string, value int) {
  124. this.vars.Int[name] = value
  125. this.changed = true
  126. }
  127. func (this *Session) SetString(name string, value string) {
  128. this.vars.String[name] = value
  129. this.changed = true
  130. }
  131. func (this *Session) SetBool(name string, value bool) {
  132. this.vars.Bool[name] = value
  133. this.changed = true
  134. }
  135. func (this *Session) GetInt(name string) (int, error) {
  136. if this.IsSetInt(name) {
  137. return this.vars.Int[name], nil
  138. } else {
  139. return 0, errors.New("Variable is not found")
  140. }
  141. }
  142. func (this *Session) GetString(name string) (string, error) {
  143. if this.IsSetString(name) {
  144. return this.vars.String[name], nil
  145. } else {
  146. return "", errors.New("Variable is not found")
  147. }
  148. }
  149. func (this *Session) GetBool(name string) (bool, error) {
  150. if this.IsSetBool(name) {
  151. return this.vars.Bool[name], nil
  152. } else {
  153. return false, errors.New("Variable is not found")
  154. }
  155. }
  156. func (this *Session) GetIntDef(name string, def int) int {
  157. if this.IsSetInt(name) {
  158. return this.vars.Int[name]
  159. } else {
  160. return def
  161. }
  162. }
  163. func (this *Session) GetStringDef(name string, def string) string {
  164. if this.IsSetString(name) {
  165. return this.vars.String[name]
  166. } else {
  167. return def
  168. }
  169. }
  170. func (this *Session) GetBoolDef(name string, def bool) bool {
  171. if this.IsSetBool(name) {
  172. return this.vars.Bool[name]
  173. } else {
  174. return def
  175. }
  176. }
  177. func (this *Session) DelInt(name string) {
  178. if this.IsSetInt(name) {
  179. delete(this.vars.Int, name)
  180. this.changed = true
  181. }
  182. }
  183. func (this *Session) DelString(name string) {
  184. if this.IsSetString(name) {
  185. delete(this.vars.String, name)
  186. this.changed = true
  187. }
  188. }
  189. func (this *Session) DelBool(name string) {
  190. if this.IsSetBool(name) {
  191. delete(this.vars.Bool, name)
  192. this.changed = true
  193. }
  194. }