-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathbackend.go
286 lines (230 loc) · 8.33 KB
/
backend.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* @Copyright Reserved By Janusec (https://www.janusec.com/).
* @Author: U2
* @Date: 2018-07-14 16:38:41
* @Last Modified: U2, 2018-07-14 16:38:41
*/
package models
import (
"crypto/tls"
"database/sql"
"sync"
)
// Application i.e. Web site
type Application struct {
ID int64 `json:"id,string"`
Name string `json:"name"`
InternalScheme string `json:"internal_scheme"` // http, https
Destinations []*Destination `json:"destinations"`
// Route: map[string][]*Destination
// {"/abc/": ["192.168.1.1:8800", "192.168.1.2:8800"], ".php": [...], "/": [...]}
Route sync.Map `json:"-"`
Domains []*Domain `json:"domains"`
RedirectHTTPS bool `json:"redirect_https"`
HSTSEnabled bool `json:"hsts_enabled"`
WAFEnabled bool `json:"waf_enabled"`
// 5-second shield, v1.2.0
ShieldEnabled bool `json:"shield_enabled"`
ClientIPMethod IPMethod `json:"ip_method"`
Description string `json:"description"`
OAuthRequired bool `json:"oauth_required"`
SessionSeconds int64 `json:"session_seconds"`
Owner string `json:"owner"`
// CSP (Content Security Policy) v0.9.11
CSPEnabled bool `json:"csp_enabled"`
CSP string `json:"csp"`
// CacheEnabled cache static files v1.2.5
CacheEnabled bool `json:"cache_enabled"`
// Cookie Consent Management v1.4.1
CookieMgmtEnabled bool `json:"cookie_mgmt_enabled"`
ConciseNotice string `json:"concise_notice"`
// LongNoticeLink is not required and removed from v1.4.2fix3, ConciseNotice begin to support HTML
// LongNoticeLink string `json:"long_notice_link"`
NecessaryNotice string `json:"necessary_notice"`
FunctionalNotice string `json:"functional_notice"`
EnableFunctional bool `json:"enable_functional"`
AnalyticsNotice string `json:"analytics_notice"`
EnableAnalytics bool `json:"enable_analytics"`
MarketingNotice string `json:"marketing_notice"`
EnableMarketing bool `json:"enable_marketing"`
UnclassifiedNotice string `json:"unclassified_notice"`
EnableUnclassified bool `json:"enable_unclassified"`
Cookies []*Cookie `json:"cookies"`
// CustomHeader add by gateway, v1.4.2
CustomHeaders []*CustomHeader `json:"custom_headers"`
}
// DBApplication for storage in database
type DBApplication struct {
ID int64 `json:"id,string"`
Name string `json:"name"`
InternalScheme string `json:"internal_scheme"` // http, https
RedirectHTTPS bool `json:"redirect_https"`
HSTSEnabled bool `json:"hsts_enabled"`
WAFEnabled bool `json:"waf_enabled"`
// 5-second shield, v1.2.0
ShieldEnabled bool `json:"shield_enabled"`
ClientIPMethod IPMethod `json:"ip_method"`
Description string `json:"description"`
OAuthRequired bool `json:"oauth_required"`
SessionSeconds int64 `json:"session_seconds"`
Owner string `json:"owner"`
// CSP (Content Security Policy) v0.9.11
CSPEnabled bool `json:"csp_enabled"`
CSP string `json:"csp"`
// CacheEnabled cache static files v1.2.5
CacheEnabled bool `json:"cache_enabled"`
// Cookie Consent Management v1.4.1
CookieMgmtEnabled bool `json:"cookie_mgmt_enabled"`
ConciseNotice string `json:"concise_notice"`
// LongNoticeLink is not required and removed from v1.4.2fix3, ConciseNotice begin to support HTML
// LongNoticeLink string `json:"long_notice_link"`
NecessaryNotice string `json:"necessary_notice"`
FunctionalNotice string `json:"functional_notice"`
EnableFunctional bool `json:"enable_functional"`
AnalyticsNotice string `json:"analytics_notice"`
EnableAnalytics bool `json:"enable_analytics"`
MarketingNotice string `json:"marketing_notice"`
EnableMarketing bool `json:"enable_marketing"`
UnclassifiedNotice string `json:"unclassified_notice"`
EnableUnclassified bool `json:"enable_unclassified"`
// CustomHeaders add by gateway, v1.4.2
CustomHeaders string `json:"custom_headers"`
}
type CustomHeader struct {
Key string `json:"key"`
Value string `json:"value"`
}
type DomainRelation struct {
App *Application
Cert *CertItem
Redirect bool
Location string
}
type Domain struct {
ID int64 `json:"id,string"`
Name string `json:"name"`
AppID int64 `json:"app_id,string"`
CertID int64 `json:"cert_id,string"`
Redirect bool `json:"redirect"`
Location string `json:"location"`
App *Application `json:"-"`
Cert *CertItem `json:"-"`
}
type DBDomain struct {
ID int64 `json:"id,string"`
Name string `json:"name"`
AppID int64 `json:"app_id,string"`
CertID int64 `json:"cert_id,string"`
Redirect bool `json:"redirect"`
Location string `json:"location"`
}
// RouteType used for backend routing
type RouteType int64
const (
// ReverseProxyRoute used for secondary application /abc/ /xyz/
ReverseProxyRoute RouteType = 1
// FastCGIRoute used for PHP etc.
FastCGIRoute RouteType = 1 << 1
// StaticRoute used for static web server
StaticRoute RouteType = 1 << 2
// K8S_Ingress used for k8s pods, added on 2023.01.22
K8S_Ingress RouteType = 1 << 3
)
// Destination is used for backend routing
type Destination struct {
ID int64 `json:"id,string"`
// 0.9.8+
RouteType RouteType `json:"route_type"`
// 0.9.8+
RequestRoute string `json:"request_route"`
// 0.9.8+
BackendRoute string `json:"backend_route"`
// Destination is backend IP:Port , or static directory
// If RoutyType is K8S, this field is not used
Destination string `json:"destination"`
// PodsAPI example: http://127.0.0.1:8080/api/v1/namespaces/default/pods
PodsAPI string `json:"pods_api"`
// PodPort: port of backend pods
PodPort string `json:"pod_port"`
// Pods are all destinations
Pods string `json:"pods"`
AppID int64 `json:"app_id,string"`
NodeID int64 `json:"node_id,string"`
// Online status of Destination (IP:Port), added in V0.9.11
Online bool `json:"online"`
CheckTime int64 `json:"check_time"`
// added in 1.3.1, K8s routine updating and avoid race
Mutex sync.RWMutex `json:"-"`
IsUpdating bool `json:"-"`
}
// PODS for k8s /api/v1/namespaces/default/pods
type PODS struct {
Items []Item `json:"items"`
}
type Item struct {
Status PodStatus `json:"status"`
}
type PodStatus struct {
Phase string `json:"phase"`
PodIP string `json:"podIP"`
}
type CertItem struct {
ID int64 `json:"id,string"`
CommonName string `json:"common_name"`
CertContent string `json:"cert_content"`
PrivKeyContent string `json:"priv_key_content"`
TlsCert tls.Certificate `json:"-"`
ExpireTime int64 `json:"expire_time"`
Description string `json:"description"`
}
type DBCertItem struct {
ID int64
CommonName string
CertContent string
EncryptedPrivKey []byte
ExpireTime int64
Description sql.NullString
}
type IPMethod int64
const (
IPMethod_REMOTE_ADDR IPMethod = 1
IPMethod_X_FORWARDED_FOR IPMethod = 1 << 1
IPMethod_X_REAL_IP IPMethod = 1 << 2
IPMethod_REAL_IP IPMethod = 1 << 3
)
// VipApp configuration, added from 0.9.12, database table name forwarding_app
type VipApp struct {
ID int64 `json:"id,string"`
Name string `json:"name"`
// Port on Gateway
ListenPort int64 `json:"listen_port"`
// IsTCP: true for TCP, false for UDP
IsTCP bool `json:"is_tcp"`
// Targets, memory use only, not save to database
Targets []*VipTarget `json:"targets"`
// Route: map[port][]*VipTarget
// {3001: ["192.168.1.1:3306", "192.168.1.2:3306"], 3002: [...]}
// Route sync.Map `json:"-"`
Owner string `json:"owner"`
Description string `json:"description"`
// ExitChan used for exit, when VipApp deleted or port changed.
ExitChan chan bool `json:"-"`
}
// VipTarget added from 0.9.12
type VipTarget struct {
ID int64 `json:"id,string"`
VipAppID int64 `json:"vip_app_id,string"`
// RouteType: Reverse_Proxy, K8S_Ingress
RouteType RouteType `json:"route_type"`
// Destination is backend IP:Port
Destination string `json:"destination"`
// PodsAPI example: http://127.0.0.1:8080/api/v1/namespaces/default/pods
PodsAPI string `json:"pods_api"`
// PodPort: port of backend pods
PodPort string `json:"pod_port"`
// Pods are all destinations
Pods string `json:"pods"`
// Online status of Destination (IP:Port)
Online bool `json:"online"`
CheckTime int64 `json:"check_time"`
}