aboutsummaryrefslogtreecommitdiff
path: root/session.go
blob: 75444438f4c10802fb38501aab797e7b67a171ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"net/http"
	"sync"
	"time"
)

type TSession struct {
	SessionID []byte
	IPAddress string
	Expires   time.Time
	AccountID int
}

// IMPORTANT(fusion): Ideally you'd save sessions in a database to reduce memory
// usage and to make them persistent with server restarts. In reality, we should
// have a low amount of sessions and a high server uptime, making the memory usage
// here minimal. We can always turn this into a LRU cache with a set maximum number
// of sessions.

var (
	g_SessionsMutex sync.Mutex
	g_Sessions      []TSession
)

func GetRequestSessionID(Request *http.Request) []byte {
	Cookie, Err := Request.Cookie("GOSESSID")
	if Err != nil {
		return nil
	}

	SessionID, Err := hex.DecodeString(Cookie.Value)
	if Err != nil {
		g_LogErr.Printf("Failed to decode session id: %v", Err)
		return nil
	}

	if len(SessionID) != 32 {
		g_LogErr.Printf("Invalid session id size %v (expected 32)", len(SessionID))
		return nil
	}

	return SessionID
}

func SessionLookup(SessionID []byte, IPAddress string) int {
	AccountID := 0
	if SessionID != nil && IPAddress != "" {
		g_SessionsMutex.Lock()
		defer g_SessionsMutex.Unlock()
		for Index := 0; Index < len(g_Sessions); Index += 1 {
			Session := &g_Sessions[Index]

			if time.Until(Session.Expires) <= 0 {
				g_Sessions = SwapAndPop(g_Sessions, Index)
				Index -= 1
				continue
			}

			if bytes.Equal(Session.SessionID, SessionID) && Session.IPAddress == IPAddress {
				AccountID = Session.AccountID
				break
			}
		}
	}
	return AccountID
}

func SessionStart(Context *THttpRequestContext, AccountID int) {
	if AccountID <= 0 {
		g_LogErr.Printf("Trying to start session with invalid account id %v", AccountID)
		return
	}

	SessionID := make([]byte, 32)
	if _, Err := rand.Read(SessionID); Err != nil {
		g_LogErr.Printf("Failed to generate session id: %v", Err)
		return
	}

	Context.SessionID = SessionID
	Context.AccountID = AccountID
	Expires := time.Now().Add(time.Hour)
	http.SetCookie(Context.Writer, &http.Cookie{
		Name:     "GOSESSID",
		Value:    hex.EncodeToString(SessionID),
		Path:     "/",
		Expires:  Expires,
		Secure:   false, // TODO(fusion): Enable this when HTTPS is enabled (?).
		HttpOnly: true,
	})

	g_SessionsMutex.Lock()
	defer g_SessionsMutex.Unlock()
	g_Sessions = append(g_Sessions,
		TSession{
			SessionID: SessionID,
			IPAddress: Context.IPAddress,
			Expires:   Expires,
			AccountID: AccountID,
		})
}

func SessionEnd(Context *THttpRequestContext) {
	if Context.SessionID == nil {
		return
	}

	http.SetCookie(Context.Writer, &http.Cookie{
		Name:    "GOSESSID",
		Value:   "",
		Path:    "/",
		Expires: time.Unix(0, 0),
	})

	g_SessionsMutex.Lock()
	defer g_SessionsMutex.Unlock()
	for Index := 0; Index < len(g_Sessions); Index += 1 {
		Session := &g_Sessions[Index]
		if bytes.Equal(Session.SessionID, Context.SessionID) && Session.IPAddress == Context.IPAddress {
			g_Sessions[Index] = g_Sessions[len(g_Sessions)-1]
			g_Sessions[len(g_Sessions)-1] = TSession{}
			g_Sessions = g_Sessions[:len(g_Sessions)-1]
			break
		}
	}
}