Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 2874c7b

Browse files
committed
[proxy]请求、日志增加若干个变量
1 parent 4bb8ab6 commit 2874c7b

6 files changed

Lines changed: 96 additions & 2 deletions

File tree

tealogs/accesslogs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
~~~bash
2+
easyjson -all access_log.go
3+
~~~

tealogs/accesslogs/access_log.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ type AccessLog struct {
3434
RewriteId string `var:"rewriteId" bson:"rewriteId" json:"rewriteId"` // 重写规则ID
3535

3636
TeaVersion string `var:"teaVersion" bson:"teaVersion" json:"teaVersion"` // TeaWeb版本
37-
RemoteAddr string `var:"remoteAddr" bson:"remoteAddr" json:"remoteAddr"` // 终端地址,通常是:ip:port
37+
RemoteAddr string `var:"remoteAddr" bson:"remoteAddr" json:"remoteAddr"` // 终端地址
38+
RawRemoteAddr string `var:"rawRemoteAddr" bson:"rawRemoteAddr" json:"rawRemoteAddr"` // 直接连接服务器的终端地址
3839
RemotePort int `var:"remotePort" bson:"remotePort" json:"remotePort"` // 终端端口
3940
RemoteUser string `var:"remoteUser" bson:"remoteUser" json:"remoteUser"` // 终端用户,基于BasicAuth认证
4041
RequestURI string `var:"requestURI" bson:"requestURI" json:"requestURI"` // 请求URI
@@ -71,9 +72,12 @@ type AccessLog struct {
7172
ServerName string `var:"serverName" bson:"serverName" json:"serverName"` // 接收请求的服务器名
7273
ServerPort int `var:"serverPort" bson:"serverPort" json:"serverPort"` // 服务器端口
7374
ServerProtocol string `var:"serverProtocol" bson:"serverProtocol" json:"serverProtocol"` // 服务器协议,类似于HTTP/1.0”
75+
Hostname string `var:"hostname" bson:"hostname" json:"hostname"`
7476

7577
// 代理相关
7678
BackendAddress string `var:"backendAddress" bson:"backendAddress" json:"backendAddress"` // 代理的后端的地址
79+
BackendCode string `var:"backendCode" bson:"backendCode" json:"backendCode"` // 后端代号
80+
BackendScheme string `var:"backendScheme" bson:"backendScheme" json:"backendScheme"` // 后端协议
7781
FastcgiAddress string `var:"fastcgiAddress" bson:"fastcgiAddress" json:"fastcgiAddress"` // Fastcgi后端地址
7882

7983
// 调试用
@@ -269,6 +273,28 @@ func (this *AccessLog) Format(format string) string {
269273
}
270274
}
271275

276+
// backend
277+
if strings.HasPrefix(varName, "backend.") {
278+
subVarName := varName[8:]
279+
switch subVarName {
280+
case "id":
281+
return this.BackendId
282+
case "address":
283+
return this.BackendAddress
284+
case "host":
285+
index := strings.Index(this.BackendAddress, ":")
286+
if index > -1 {
287+
return this.BackendAddress[:index]
288+
} else {
289+
return ""
290+
}
291+
case "scheme":
292+
return this.BackendScheme
293+
case "code":
294+
return this.BackendCode
295+
}
296+
}
297+
272298
return "${" + varName + "}"
273299
})
274300

tealogs/accesslogs/access_log_easyjson.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tealogs/accesslogs/access_log_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,21 @@ func TestAccessLog_JSON(t *testing.T) {
331331
t.Log(string(data))
332332
}
333333

334+
func TestAccessLog_Backend(t *testing.T) {
335+
accessLog := NewAccessLog()
336+
accessLog.BackendId = "1234"
337+
accessLog.BackendAddress = "127.0.0.1:8001"
338+
accessLog.BackendCode = "web001"
339+
accessLog.BackendScheme = "http"
340+
accessLog.Hostname = "Web001"
341+
t.Log(accessLog.Format("${backend.address} ${backend.host} ${backend.id} ${backend.code} ${backend.scheme} ${hostname}"))
342+
data, err := easyjson.Marshal(accessLog)
343+
if err != nil {
344+
t.Fatal(err)
345+
}
346+
t.Log(string(data))
347+
}
348+
334349
func TestAccessLog_JSON_compare(t *testing.T) {
335350
accessLog := &AccessLog{
336351
RequestPath: "/hello",

teaproxy/request.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ var bytePool1k = teautils.NewBytePool(20480, 1024)
5454
var bytePool32k = teautils.NewBytePool(20480, 32*1024)
5555
var bytePool128k = teautils.NewBytePool(20480, 128*1024)
5656

57+
// 环境变量
58+
var HOSTNAME, _ = os.Hostname()
59+
5760
// 请求定义
5861
// HTTP HEADER RFC: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
5962
type Request struct {
@@ -1270,6 +1273,8 @@ func (this *Request) Format(source string) string {
12701273
return this.serverName
12711274
case "serverPort":
12721275
return strconv.Itoa(this.requestServerPort())
1276+
case "hostname":
1277+
return HOSTNAME
12731278
case "documentRoot":
12741279
if this.locationContext != nil && len(this.locationContext.Root) > 0 {
12751280
return this.locationContext.Root
@@ -1305,6 +1310,13 @@ func (this *Request) Format(source string) string {
13051310
switch suffix {
13061311
case "address":
13071312
return this.backend.Address
1313+
case "host":
1314+
index := strings.Index(this.backend.Address, ":")
1315+
if index > -1 {
1316+
return this.backend.Address[:index]
1317+
} else {
1318+
return ""
1319+
}
13081320
case "id":
13091321
return this.backend.Id
13101322
case "scheme":
@@ -1432,9 +1444,16 @@ func (this *Request) log() {
14321444
cookies[cookie.Name] = cookie.Value
14331445
}
14341446

1447+
addr := this.raw.RemoteAddr
1448+
host, _, err := net.SplitHostPort(addr)
1449+
if err == nil {
1450+
addr = host
1451+
}
1452+
14351453
accessLog := &accesslogs.AccessLog{
14361454
TeaVersion: teaconst.TeaVersion,
14371455
RemoteAddr: this.requestRemoteAddr(),
1456+
RawRemoteAddr: addr,
14381457
RemotePort: this.requestRemotePort(),
14391458
RemoteUser: this.requestRemoteUser(),
14401459
RequestURI: this.requestURI(),
@@ -1469,6 +1488,7 @@ func (this *Request) log() {
14691488
HasErrors: len(this.errors) > 0,
14701489
Extend: &accesslogs.AccessLogExtend{},
14711490
Attrs: this.attrs,
1491+
Hostname: HOSTNAME,
14721492
}
14731493

14741494
// 日志和统计
@@ -1501,6 +1521,8 @@ func (this *Request) log() {
15011521
if this.backend != nil {
15021522
accessLog.BackendAddress = this.backend.Address
15031523
accessLog.BackendId = this.backend.Id
1524+
accessLog.BackendScheme = this.backend.Scheme
1525+
accessLog.BackendCode = this.backend.Code
15041526
}
15051527

15061528
if this.fastcgi != nil {

teawaf/checkpoints/cc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (this *CCCheckpoint) Start() {
2828
if this.grid != nil {
2929
this.grid.Destroy()
3030
}
31-
this.grid = teamemory.NewGrid(100)
31+
this.grid = teamemory.NewGrid(32)
3232
}
3333

3434
func (this *CCCheckpoint) RequestValue(req *requests.Request, param string, options map[string]string) (value interface{}, sysErr error, userErr error) {

0 commit comments

Comments
 (0)