fix: update user-agent regex to use regexp2 and handle match errors

This commit is contained in:
SunBK201 2024-11-02 20:41:07 +08:00
parent 7b1377d5e9
commit 75e76a0394
9 changed files with 21 additions and 14 deletions

View File

@ -1,7 +1,7 @@
#!/bin/sh
project_name="ua3f"
release_version="0.5.0"
release_version="0.5.1"
target=cmd/ua3f.go
dist=./dist
release_dir=./bin

View File

@ -7,21 +7,21 @@ import (
"fmt"
"io"
"net"
"regexp"
"slices"
"strings"
"time"
"ua3f/http"
"ua3f/log"
"github.com/dlclark/regexp2"
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/sirupsen/logrus"
)
var version = "0.5.0"
var version = "0.5.1"
var payloadByte []byte
var uaPattern string
var uaRegexp *regexp.Regexp
var uaRegexp *regexp2.Regexp
var cache *expirable.LRU[string, string]
var HTTP_METHOD = []string{"GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT"}
var whitelist = []string{
@ -77,7 +77,7 @@ func main() {
return
}
logrus.Info(fmt.Sprintf("Listen on %s:%d", addr, port))
uaRegexp, err = regexp.Compile(uaPattern)
uaRegexp, err = regexp2.Compile(uaPattern, regexp2.None)
if err != nil {
logrus.Fatal("Invalid User-Agent Regex Pattern: ", err)
return
@ -440,7 +440,11 @@ func CopyPileline(dst io.Writer, src io.Reader, destAddrPort string) {
isInWhiteList := false
isMatchUaPattern := true
if uaPattern != "" {
isMatchUaPattern = uaRegexp.MatchString(uaStr)
isMatchUaPattern, err = uaRegexp.MatchString(uaStr)
if err != nil {
logrus.Error(fmt.Sprintf("[%s][%s] User-Agent Regex Pattern Match Error: %s", destAddrPort, src.(*net.TCPConn).RemoteAddr().String(), err.Error()))
isMatchUaPattern = true
}
}
if slices.Contains(whitelist, uaStr) {
isInWhiteList = true

1
go.mod
View File

@ -8,6 +8,7 @@ require (
)
require (
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/stretchr/testify v1.8.2 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

4
go.sum
View File

@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -9,7 +11,7 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=

View File

@ -20,7 +20,7 @@ ckcmd() {
cd /root
getcpucore
version=0.5.0
version=0.5.1
ua3f_tar=ua3f-$version-$cpucore.tar.gz
if id -u shellclash >/dev/null 2>&1; then

View File

@ -3,7 +3,7 @@ local uci = require("luci.model.uci").cursor()
ua3f = Map("ua3f",
"UA3F",
[[
<a href="https://github.com/SunBK201/UA3F" target="_blank">Version: 0.5.0</a>
<a href="https://github.com/SunBK201/UA3F" target="_blank">Version: 0.5.1</a>
<br>
Across the Campus we can reach every corner in the world.
]]

View File

@ -1,5 +1,5 @@
Package: ua3f
Version: 0.5.0-1
Version: 0.5.1-1
Depends: libc, luci-compat
Source: /feed/openwrt
SourceName: UA3F
@ -7,5 +7,5 @@ License: GPL-3.0-only
Section: net
SourceDateEpoch: 1711267200
Architecture: all
Installed-Size: 2641920
Installed-Size: 2775040
Description: Implementation of the new generation of HTTP User-Agent modification methodology.

View File

@ -1,5 +1,5 @@
Package: ua3f
Version: 0.5.0-1
Version: 0.5.1-1
Depends: libc, luci-compat
Source: /feed/openwrt
SourceName: UA3F
@ -7,5 +7,5 @@ License: GPL-3.0-only
Section: net
SourceDateEpoch: 1711267200
Architecture: all
Installed-Size: 2641920
Installed-Size: 2703360
Description: Implementation of the new generation of HTTP User-Agent modification methodology.

View File

@ -3,7 +3,7 @@ local uci = require("luci.model.uci").cursor()
ua3f = Map("ua3f",
"UA3F",
[[
<a href="https://github.com/SunBK201/UA3F" target="_blank">Version: 0.5.0</a>
<a href="https://github.com/SunBK201/UA3F" target="_blank">Version: 0.5.1</a>
<br>
Across the Campus we can reach every corner in the world.
]]