-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
224 lines (196 loc) · 6.31 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package middleware
import (
"errors"
"mjpclab.dev/ehfs/src/param"
"mjpclab.dev/ehfs/src/util"
"mjpclab.dev/ghfs/src/middleware"
baseParam "mjpclab.dev/ghfs/src/param"
"mjpclab.dev/ghfs/src/serverError"
)
var errInvalidParamValue = errors.New("invalid param value")
var errParamCountNotMatch = errors.New("base-param count is not equal to param count")
func ParamToMiddlewares(baseParam *baseParam.Param, param *param.Param) (preMids, postMids []middleware.Middleware, errs []error) {
var mid middleware.Middleware
var err error
var es []error
// headers
headerMids := make([]middleware.Middleware, 0, len(param.HeaderAdds)+len(param.HeaderSets))
for i := range param.HeaderAdds {
mid, err = getHeaderAddMiddleware(param.HeaderAdds[i])
errs = serverError.AppendError(errs, err)
if mid != nil {
headerMids = append(headerMids, mid)
}
}
for i := range param.HeaderSets {
mid, err = getHeaderSetMiddleware(param.HeaderSets[i])
errs = serverError.AppendError(errs, err)
if mid != nil {
headerMids = append(headerMids, mid)
}
}
// dependent: gzip static
gzipStaticMids := make([]middleware.Middleware, 0, 1)
if param.GzipStatic {
mid, err = getGzipStaticMiddleware()
errs = serverError.AppendError(errs, err)
if mid != nil {
gzipStaticMids = append(gzipStaticMids, mid)
}
}
// dependent: status pages
statusPageMids := make([]middleware.Middleware, 0, len(param.StatusPages))
for i := range param.StatusPages {
mid, err = getStatusPageMiddleware(param.StatusPages[i], param.GzipStatic)
errs = serverError.AppendError(errs, err)
if mid != nil {
statusPageMids = append(statusPageMids, mid)
}
}
// ip allows
ipAllowMids := make([]middleware.Middleware, 0, 1)
mid, es = getIPAllowMiddleware(param.IPAllows, param.IPAllowFiles, statusPageMids)
errs = append(errs, es...)
if mid != nil {
ipAllowMids = append(ipAllowMids, mid)
}
// ip denies
ipDenyMids := make([]middleware.Middleware, 0, 1)
mid, es = getIPDenyMiddleware(param.IPDenies, param.IPDenyFiles, statusPageMids)
errs = append(errs, es...)
if mid != nil {
ipDenyMids = append(ipDenyMids, mid)
}
// rewrite hosts
rewriteHostMids := make([]middleware.Middleware, 0, len(param.RewriteHosts))
for i := range param.RewriteHosts {
mid, err = getRewriteHostMiddleware(param.RewriteHosts[i], middleware.GoNext)
errs = serverError.AppendError(errs, err)
if mid != nil {
rewriteHostMids = append(rewriteHostMids, mid)
}
}
// rewrite hosts post
rewriteHostPostMids := make([]middleware.Middleware, 0, len(param.RewriteHostsPost))
for i := range param.RewriteHostsPost {
mid, err = getRewriteHostMiddleware(param.RewriteHostsPost[i], middleware.GoNext)
errs = serverError.AppendError(errs, err)
if mid != nil {
rewriteHostPostMids = append(rewriteHostPostMids, mid)
}
}
// rewrite hosts end
rewriteHostEndMids := make([]middleware.Middleware, 0, len(param.RewriteHostsEnd))
for i := range param.RewriteHostsEnd {
mid, err = getRewriteHostMiddleware(param.RewriteHostsEnd[i], middleware.SkipRests)
errs = serverError.AppendError(errs, err)
if mid != nil {
rewriteHostEndMids = append(rewriteHostEndMids, mid)
}
}
// rewrites
rewriteMids := make([]middleware.Middleware, 0, len(param.Rewrites))
for i := range param.Rewrites {
mid, err = getRewriteMiddleware(param.Rewrites[i], middleware.GoNext)
errs = serverError.AppendError(errs, err)
if mid != nil {
rewriteMids = append(rewriteMids, mid)
}
}
// rewrites post
rewritePostMids := make([]middleware.Middleware, 0, len(param.RewritesPost))
for i := range param.RewritesPost {
mid, err = getRewriteMiddleware(param.RewritesPost[i], middleware.GoNext)
errs = serverError.AppendError(errs, err)
if mid != nil {
rewritePostMids = append(rewritePostMids, mid)
}
}
// rewrites end
rewriteEndMids := make([]middleware.Middleware, 0, len(param.RewritesEnd))
for i := range param.RewritesEnd {
mid, err = getRewriteMiddleware(param.RewritesEnd[i], middleware.SkipRests)
errs = serverError.AppendError(errs, err)
if mid != nil {
rewriteEndMids = append(rewriteEndMids, mid)
}
}
// redirects
redirectMids := make([]middleware.Middleware, 0, len(param.Redirects))
for i := range param.Redirects {
mid, err = getRedirectMiddleware(param.Redirects[i])
errs = serverError.AppendError(errs, err)
if mid != nil {
redirectMids = append(redirectMids, mid)
}
}
// proxies
proxyMids := make([]middleware.Middleware, 0, len(param.Proxies))
for i := range param.Proxies {
mid, err = getProxyMiddleware(param.Proxies[i], headerMids)
errs = serverError.AppendError(errs, err)
if mid != nil {
proxyMids = append(proxyMids, mid)
}
}
// returns
returnMids := make([]middleware.Middleware, 0, len(param.Returns))
for i := range param.Returns {
mid, err = getReturnStatusMiddleware(param.Returns[i], util.Concat(headerMids, statusPageMids))
errs = serverError.AppendError(errs, err)
if mid != nil {
returnMids = append(returnMids, mid)
}
}
// headers (moved to dependent)
// to statuses
toStatusMids := make([]middleware.Middleware, 0, len(param.ToStatuses))
for i := range param.ToStatuses {
mid, err = getReturnStatusMiddleware(param.ToStatuses[i], statusPageMids)
errs = serverError.AppendError(errs, err)
if mid != nil {
toStatusMids = append(toStatusMids, mid)
}
}
// status pages (moved to dependent)
// gzip static (moved to dependent)
// pki validation skip to-https
pkiValidationSkipToHttpsMids := make([]middleware.Middleware, 0, 1)
if baseParam.ToHttps {
mid = getPkiValidationSkipToHttpsMiddleware()
pkiValidationSkipToHttpsMids = append(pkiValidationSkipToHttpsMids, mid)
}
// combine mids
preMids = util.Concat(
ipAllowMids,
ipDenyMids,
rewriteHostMids,
rewriteMids,
redirectMids,
rewriteHostPostMids,
rewritePostMids,
rewriteHostEndMids,
rewriteEndMids,
proxyMids,
returnMids,
pkiValidationSkipToHttpsMids,
)
postMids = util.Concat(
headerMids,
toStatusMids,
statusPageMids,
gzipStaticMids,
)
return
}
func ApplyMiddlewares(baseParams []*baseParam.Param, params []*param.Param) (errs []error) {
if len(baseParams) != len(params) {
return []error{errParamCountNotMatch}
}
for i := range baseParams {
var es []error
baseParams[i].PreMiddlewares, baseParams[i].PostMiddlewares, es = ParamToMiddlewares(baseParams[i], params[i])
errs = append(errs, es...)
}
return
}