sessions.go 4.5 KB

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