LVSession

package
v0.0.0-...-6aeb21f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2015 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

leafveingo web http session 的封装

功能简介:

session高并发获取
自动清除session
session id 可选随机数生成或IPUUID
自动统计session生成数量、删除数量和有效数量
session token

Index

Constants

View Source
const (
	SESSION_COOKIE_NAME           = "lvsessionid" //
	SESSION_COOKIE_VALUE_BASE_LEN = 22            // session cookie value 的基本长度
	COOKIE_TOKEN_KEY_RAND_LEN     = 48            // cookie token key rand bit len
	COOKIE_TOKEN_MAXLIFE_TIME     = 300           // token default maxlife time
	GLOBAL_TOKEN_KEY_LEN          = 256           // global token key rand string bit len
	FORM_TOKEN_SLICE_MAX_LEN      = 255           // form token slice storage max len,由于验证FormToken和创建时使用uint16进行转换byte,所以需要控制一定的存储大小。
	FORM_TOKEN_SLICE_DEFAULT_LEN  = 10            // form token slice default create len,由于FormToken使用于session所以在设置session所需参数的时候控制数据的一定大小,默认同一个用户请求10个页面产生10个不同的token
	DEFAULT_SCAN_GC_TIME          = 300           // 默认清理session扫描秒数,300秒
	TEMP_SESSION_MAXLIFE_TIME     = 288           // session 临时的最大有效时间,主要针对第一次请求无法获取cookie的情况下使用

	SIDTypeRandomUUID = SIDType(0) // session id 使用随机数版本类型
	SIDTypeIPUUID     = SIDType(1) // session id 使用IP版本的类型,需要链接到网络,获取失败会抛出异常

)

Variables

View Source
var (
	ErrSessionManagerFree = errors.New("session manager is freed")
	ErrCookieWrite        = errors.New("cookie can not write...")
	ErrIPValidateFail     = "ip information can not verified : "
)

Functions

This section is empty.

Types

type FormToken

type FormToken [][]byte

formToken helper calculate storage use LRU Cache mode FormToken的创建原因是用于用户同时访问不同的页面,而form没有进行提交token验证,所存储的一个token 这样用户在打开多个设置了form token的页面时可以同时进行token的验证。 formToken使用于session,所以以最小的存储数据原则设定了创建的长度,使用了LRU缓存的模式,10个form token轮流使用。 如果用户打开了11个设置了form token的页面,第一个请求的页面验证就会失败,因为token被请求的第11个页面覆盖了。

func NewFormToken

func NewFormToken(len int) FormToken

new formToken 指定一个长度 len <= 255

func (FormToken) Add

func (f FormToken) Add(b []byte) int

添加指定数据 @b 数据信息 @return 返回添加的下标

func (FormToken) Get

func (f FormToken) Get(i int) []byte

get the FormToken []byte

func (FormToken) Remove

func (f FormToken) Remove(i int)

remove the FormToken []byte

func (FormToken) RemoveAll

func (f FormToken) RemoveAll()

remove all the FormToken []byte

type HttpSession

type HttpSession interface {
	UID() string                                   // session id
	Get(key string) (interface{}, bool)            // get session data
	Set(key string, v interface{})                 // set session data
	Delete(key string)                             // delete
	AccessTime() time.Time                         // accessed time
	AccessIP() net.IP                              // access ip
	MaxlifeTime() int32                            // max life time
	SetMaxlifeTime(second int32)                   // set max life time
	Invalidate()                                   // invalidate this session
	IPAccessRule() map[string]int                  // record ip access rule, map[<ip string>]<ip count>
	CheckFormTokenSignature(signature string) bool // check token signature, pass return true,the token is form or query value, after checking changed token
	FormTokenSignature() string                    // form token signature, get a changed once every
}

http session interface

type HttpSessionManager

type HttpSessionManager struct {
	CookieSecure           bool             // cookie secure set, default false
	CookieMaxAge           int              // cookie maxage set, default 0
	CookieTokenHash        func() hash.Hash // cookie token hmac hash, default sha256.New
	CookieTokenRandLen     int              // cookie token rand string len bit
	CookieTokenMaxlifeTime int32            // cookie token maxlife time, default 300second
	IPHeaderKey            string           // proxy to http headers set ip key, default ""
	// contains filtered or unexported fields
}

session manager

func NewSessionManager

func NewSessionManager(autoGC bool) *HttpSessionManager

*

  • new session manager *
  • @param autoGC is auto gc

func NewSessionManagerAtGCTime

func NewSessionManagerAtGCTime(gcTimeSecond int64, autoGC bool) *HttpSessionManager

*

  • new session manager *
  • @param gcTimeSecond gc operate time, minimum 60 second
  • @param autoGC is auto gc

func (*HttpSessionManager) AddSessionWillInvalidateHandlerFunc

func (sm *HttpSessionManager) AddSessionWillInvalidateHandlerFunc(handlerFunc func(session HttpSession))

the invalidate session handler func session will invalidate before use goroutine call need to set up a function parameter session is copy value

func (*HttpSessionManager) Contains

func (sm *HttpSessionManager) Contains(uid string) bool

is contains session

func (*HttpSessionManager) DeleteSession

func (sm *HttpSessionManager) DeleteSession(uid string)

func (*HttpSessionManager) Free

func (sm *HttpSessionManager) Free()

*

  • free session manager
  • will stop auto gc, clear all http session *

func (*HttpSessionManager) GC

func (sm *HttpSessionManager) GC()

gc clear session

func (*HttpSessionManager) GetSession

func (sm *HttpSessionManager) GetSession(rw http.ResponseWriter, req *http.Request, maxlifeTime int32, resetToken bool) (HttpSession, error)

获取session,根据cookie会自动进行创建 每个session会根据有效时间存储在不同的列队中 @rw @req @maxlifeTime session的最大有效时间,秒为单位。 @resetToken 是否重新设置cookie session token

func (*HttpSessionManager) GlobalTokenKey

func (sm *HttpSessionManager) GlobalTokenKey() []byte

get cookie token key

func (*HttpSessionManager) IsGC

func (sm *HttpSessionManager) IsGC() bool

GC operation is running

func (*HttpSessionManager) SessionCreateNum

func (sm *HttpSessionManager) SessionCreateNum() int64

the create session total number

func (*HttpSessionManager) SessionDeleteNum

func (sm *HttpSessionManager) SessionDeleteNum() int64

the delete session total number

func (*HttpSessionManager) SessionEffectivenNum

func (sm *HttpSessionManager) SessionEffectivenNum() int64

the session effectiven number

func (*HttpSessionManager) SetGlobalTokenKey

func (sm *HttpSessionManager) SetGlobalTokenKey(key []byte)

set cookie token key (len > 0) 设置全局的token key,默认是使用 crypto/rand 进行生成的随机数

func (*HttpSessionManager) SetSIDType

func (sm *HttpSessionManager) SetSIDType(t SIDType, urlIPApi string)

设置session id 的生成类型 @ t SIDTypeRandomUUID or SIDTypeIPUUID @ urlIPAPi 如果选择SIDTypeRandomUUID可以直接设置为 "" 空。

如果选择SIDTypeIPUUID,可以选择性的设置获取ip的URL地址,传递 "" 空则使用默认地址处理。
也可以根据自己需求,设置url, url必须能直接获取得到网络的IP信息不需要任何解析操作。
由于IPUUID需要连接网络,出现获取不了或解析不了IP的情况下会抛出异常(panic)

func (*HttpSessionManager) SetScanGCTime

func (sm *HttpSessionManager) SetScanGCTime(second int64)

设置CG HttpSession清理的间隔时间,每段时间会进行一次HttpSession的清理 @second 秒单位,大于或等于60m

func (*HttpSessionManager) SetTempSessMaxlifeTime

func (sm *HttpSessionManager) SetTempSessMaxlifeTime(second int32)
	设置临时HttpSession的最大有效时间,主要是针对第一次请求创建HttpSession所用,
 主要为了避免垃圾HttpSession的创建,有些访问了一次并且创建了HttpSession就离
	开了,或则Cookie无法写入时,在或者一些人的并发攻击产生大量的HttpSession所使
	用的一个机制,在第二次访问的时候就会设置会原来的时间。
	创建一个HttpSession最小大约占用246 bit

	避免cookie无法写入时,使用一个临时短暂最大有效时间来控制session的清理。
	再第二次请求时如果cookie能够获取得到将还原调用者设置session的最大有效时间。
	@second 秒单位,大于或等于60m

type SIDType

type SIDType byte

session id 类型

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL