Skip to content

Commit

Permalink
bugfix: logvisitor parse ip failed (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
zizdlp authored Aug 30, 2024
1 parent 7801da3 commit 04d8c16
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: BUILD_MAIN
on:
push:
branches:
- main
- feature/print2
jobs:
build:
runs-on: ubuntu-latest
Expand Down
12 changes: 11 additions & 1 deletion zbook_backend/gapi/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gapi
import (
"context"
"fmt"
"net"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -275,9 +276,18 @@ func (server *Server) LogRedisVisitor(ctx context.Context) (err error) {
// 分隔ClientIp并取第一个部分
ClientIpParts := strings.Split(ClientIp, ",")
ClientIp = ClientIpParts[0]

// 验证 IP 地址
if net.ParseIP(ClientIp) == nil {
return fmt.Errorf("invalid IP address when log visitor: %s", ClientIp)
}

// 替换 UserAgent 中的冒号为下划线
UserAgent = strings.ReplaceAll(UserAgent, ":", "_")

location, err := time.LoadLocation(server.config.TIMEZONE)
if err != nil {
return fmt.Errorf("failed to load location:%v", err)
return fmt.Errorf("failed to load location: %v", err)
}
today := time.Now().In(location).Format("2006-01-02")
redisKey := fmt.Sprintf("%s:%s:%s:%s", "logvisitor", ClientIp, UserAgent, today)
Expand Down
2 changes: 1 addition & 1 deletion zbook_backend/statik/statik.go

Large diffs are not rendered by default.

47 changes: 35 additions & 12 deletions zbook_backend/util/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,42 @@ func TestFilterDiffFilesByExtensions(t *testing.T) {
}

func TestExtractLogDetails(t *testing.T) {
// 测试数据
logKey := "logvisitor:::1:Mozilla/5.0:2024-08-20"
// IPv4 Test Case
logKeyIPv4 := "logvisitor:192.168.1.1:Mozilla/5.0:2024-08-20"
expectedIPIPv4 := "192.168.1.1"
expectedUserAgentIPv4 := "Mozilla/5.0"
expectedDateIPv4 := "2024-08-20"
ip, userAgent, date := ExtractLogDetails(logKeyIPv4)
require.Equal(t, expectedIPIPv4, ip, "IPv4 address not correct")
require.Equal(t, expectedUserAgentIPv4, userAgent, "User Agent not correct")
require.Equal(t, expectedDateIPv4, date, "Date not correct")

// 期望的结果
expectedIP := "::1"
expectedUserAgent := "Mozilla/5.0"
expectedDate := "2024-08-20"
// IPv6 Test Case
logKeyIPv6 := "logvisitor:2001:0db8:85a3:0000:0000:8a2e:0370:7334:Mozilla/5.0:2024-08-20"
expectedIPIPv6 := "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
expectedUserAgentIPv6 := "Mozilla/5.0"
expectedDateIPv6 := "2024-08-20"
ip, userAgent, date = ExtractLogDetails(logKeyIPv6)
require.Equal(t, expectedIPIPv6, ip, "IPv6 address not correct")
require.Equal(t, expectedUserAgentIPv6, userAgent, "User Agent not correct")
require.Equal(t, expectedDateIPv6, date, "Date not correct")

// 调用函数
ip, userAgent, date := ExtractLogDetails(logKey)
// Additional cases with colons in UserAgent
testCases := []struct {
logKey string
expectedIP string
expectedUserAgent string
expectedDate string
}{
{"logvisitor:66.249.66.168:Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.119 Mobile Safari/537.36 (compatible; GoogleOther):2024-08-26", "66.249.66.168", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.119 Mobile Safari/537.36 (compatible; GoogleOther)", "2024-08-26"},
{"logvisitor:66.249.66.168:Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.119 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http_//www.google.com/bot.html):2024-08-23", "66.249.66.168", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.119 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http_//www.google.com/bot.html)", "2024-08-23"},
// Add more test cases as needed
}

// 使用 require 进行断言
require.Equal(t, expectedIP, ip, "IP 地址不正确")
require.Equal(t, expectedUserAgent, userAgent, "User Agent 不正确")
require.Equal(t, expectedDate, date, "日期不正确")
for _, tc := range testCases {
ip, userAgent, date := ExtractLogDetails(tc.logKey)
require.Equal(t, tc.expectedIP, ip, "IP not correct for logKey: "+tc.logKey)
require.Equal(t, tc.expectedUserAgent, userAgent, "User Agent not correct for logKey: "+tc.logKey)
require.Equal(t, tc.expectedDate, date, "Date not correct for logKey: "+tc.logKey)
}
}

0 comments on commit 04d8c16

Please sign in to comment.