forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.go
126 lines (117 loc) · 3.28 KB
/
gateway.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
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
package main
import (
"fmt"
"github.com/Sirupsen/logrus"
"net/http"
"net/http/httputil"
"strings"
"time"
)
type ApiError struct {
Message string
}
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
for _, sub := range config.ExcludePaths {
if strings.Contains(r.URL.Path, sub) {
success_handler(w, r, p)
return
}
}
// Check for API key existence
authHeaderValue := r.Header.Get(config.AuthHeaderName)
if authHeaderValue != "" {
// Check if API key valid
key_authorised, thisSessionState := authManager.IsKeyAuthorised(authHeaderValue)
keyExpired := authManager.IsKeyExpired(&thisSessionState)
if key_authorised {
if !keyExpired {
// If valid, check if within rate limit
forwardMessage, reason := sessionLimiter.ForwardMessage(&thisSessionState)
if forwardMessage {
success_handler(w, r, p)
} else {
if reason == 1 {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Key rate limit exceeded.")
handle_error(w, r, "Rate limit exceeded", 429)
} else if reason == 2 {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Key quota limit exceeded.")
handle_error(w, r, "Quota exceeded", 429)
}
}
authManager.UpdateSession(authHeaderValue, thisSessionState)
} else {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Attempted access from expired key.")
handle_error(w, r, "Key has expired, please renew", 403)
}
} else {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Attempted access with non-existent key.")
handle_error(w, r, "Key not authorised", 403)
}
} else {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
}).Info("Attempted access with malformed header, no auth header found.")
handle_error(w, r, "Authorisation field missing", 400)
}
}
}
func success_handler(w http.ResponseWriter, r *http.Request, p *httputil.ReverseProxy) {
if config.EnableAnalytics {
t := time.Now()
keyName := r.Header.Get(config.AuthHeaderName)
thisRecord := AnalyticsRecord{
r.Method,
r.URL.Path,
r.ContentLength,
r.Header.Get("User-Agent"),
t.Day(),
t.Month(),
t.Year(),
t.Hour(),
200,
keyName}
analytics.RecordHit(thisRecord)
}
p.ServeHTTP(w, r)
}
func handle_error(w http.ResponseWriter, r *http.Request, err string, err_code int) {
if config.EnableAnalytics {
t := time.Now()
keyName := r.Header.Get(config.AuthHeaderName)
thisRecord := AnalyticsRecord{
r.Method,
r.URL.Path,
r.ContentLength,
r.Header.Get("User-Agent"),
t.Day(),
t.Month(),
t.Year(),
t.Hour(),
err_code,
keyName}
analytics.RecordHit(thisRecord)
}
w.WriteHeader(err_code)
w.Header().Add("Content-Type", "application/json")
w.Header().Add("X-Generator", "tyk.io")
thisError := ApiError{fmt.Sprintf("%s", err)}
templates.ExecuteTemplate(w, "error.json", &thisError)
}