-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngao.go
95 lines (77 loc) · 2 KB
/
ngao.go
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
/*
Package ngao (shield) is a reverse proxy that limits the maximum number of connections
to an upstream host.
Usage:
Get the package
go get github.com/peteretelej/ngao
Import into your application:
import "github.com/peteretelej/ngao"
Set your configuration details, and run ngao:
func main() {
c := &ngao.Config{
ListenAddr: ":9015", // your listen address e.g ":9010"
Host: "etelej.com", // the backend server to reverseproxy
Scheme: "https", // protocol scheme of backend host e.g. https, http
TotalAllowed: 4, // Maximum client sessions allowed
ClearInterval: 60 * 5, // Interval to clear older sessions (secs)
}
ngao.Run(c)
}
ngao.Run(c)
ngao was written by Peter Etelej <peter@etelej.com>
*/
package ngao
import (
"log"
"strconv"
"sync"
"time"
"github.com/gorilla/sessions"
)
// Config allows configuring of ngao's options
type Config struct {
ListenAddr string
Host string
Scheme string
TotalAllowed int
ClearInterval int
}
var store = sessions.NewCookieStore([]byte("ngao"))
var totalAllowed, clearInterval int
// Run launches ngao reverse proxy
func Run(c *Config) {
log.Print("Starting Ngao server")
log.Printf("Listening on '%s', Serving: %s://%s",
c.ListenAddr, c.Scheme, c.Host)
log.Printf("Max sessions: %d. Older sessions cleared every: %d secs",
c.TotalAllowed, c.ClearInterval)
totalAllowed = c.TotalAllowed
clearInterval = c.ClearInterval
initConf()
var wg sync.WaitGroup
wg.Add(1)
go func() {
rProxy(c.ListenAddr, c.Host, c.Scheme)
wg.Done()
}()
wg.Add(1)
go func() {
// Launch session queues manager
manager()
wg.Done()
}()
wg.Wait()
}
func initConf() {
t := time.Now().Format("20060102150405")
i, _ := strconv.Atoi(t)
uniqgen = &uniqData{
uniqID: i,
}
nconfig = &conf{
AllowedMap: make(map[int]int),
Allowed: make([]int, 0),
WaitingMap: make(map[int]string),
Waiting: make([]int, 0),
}
}