From 8b3e328ed4eccd8b761c847fadecc6bda234316e Mon Sep 17 00:00:00 2001 From: huhu415 Date: Sun, 24 Nov 2024 17:05:32 +0800 Subject: [PATCH] fix: peek size change to 8 --- .goreleaser.yaml | 2 +- handle/handle.go | 10 +++-- handle/tools.go | 18 ++++----- handle/tools_test.go | 94 ++++++++++++++++++++++---------------------- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index b6ab937..d03d62a 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -59,7 +59,7 @@ release: header: | ## 🥳Changes - - 增加了http to websocket功能 + - 修复了pg数据库连接不上问题 ```sh tar -xzvf uaProxy-linux-xxxxxxx.tar.gz # 解压 diff --git a/handle/handle.go b/handle/handle.go index b7a8e2e..c4a7417 100644 --- a/handle/handle.go +++ b/handle/handle.go @@ -11,9 +11,11 @@ import ( "github.com/sirupsen/logrus" ) +const PEEKSIZE = 8 + func HandleConnection(clientConn net.Conn) { defer clientConn.Close() - logrus.Debugf("clientConn. remoteAdd: %s", clientConn.RemoteAddr().String()) + // logrus.Debugf("clientConn. remoteAdd: %s", clientConn.RemoteAddr().String()) serverConn, err := GetDestConn(clientConn) if err != nil { @@ -23,11 +25,11 @@ func HandleConnection(clientConn net.Conn) { defer serverConn.Close() bufioReader := bufio.NewReader(clientConn) - peekBuff, _ := bufioReader.Peek(10) - logrus.Debug(string(peekBuff)) + peekBuff, _ := bufioReader.Peek(PEEKSIZE) + logrus.Debugf("locationIp: %s, remoteIp: %s, peekBuff: %v", clientConn.RemoteAddr().String(), serverConn.RemoteAddr().String(), peekBuff) go io.Copy(clientConn, serverConn) - if len(peekBuff) > 0 && isEnglishLetter(peekBuff[0]) && isHTTP(peekBuff) { + if len(peekBuff) > 0 && isEnglishBigLetter(peekBuff[0]) && isHTTP(peekBuff) { handleHTTPConnection(bufioReader, serverConn) } else { handleNonHTTPConnection(bufioReader, serverConn) diff --git a/handle/tools.go b/handle/tools.go index 4e71244..91dee77 100644 --- a/handle/tools.go +++ b/handle/tools.go @@ -10,6 +10,7 @@ import ( "github.com/v2fly/v2ray-core/v5/transport/internet/tcp" ) +// 超过 PEEKSIZE 的就没有意义了, 因为只能输入 PEEKSIZE个字节 var anyMethodSet = map[string]struct{}{ http.MethodGet: {}, http.MethodPost: {}, @@ -42,13 +43,7 @@ var anyMethodSet = map[string]struct{}{ } func isHTTP(peek []byte) bool { - tempPeekString := strings.ToUpper(string(peek)) - logrus.Debug(tempPeekString) - - first, _, ok := strings.Cut(tempPeekString, " ") - if !ok { - return false - } + first, _, _ := strings.Cut(string(peek), " ") if _, ok := anyMethodSet[first]; !ok { return false @@ -56,9 +51,9 @@ func isHTTP(peek []byte) bool { return true } -func isEnglishLetter(b byte) bool { - // 检查是否为大写字母或小写字母 - return (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') +func isEnglishBigLetter(b byte) bool { + // 检查是否为大写字母 + return (b >= 'A' && b <= 'Z') } func GetDestConn(clientConn net.Conn) (net.Conn, error) { @@ -68,9 +63,10 @@ func GetDestConn(clientConn net.Conn) (net.Conn, error) { dest, err = tcp.GetOriginalDestination(clientConn) if err != nil { logrus.Errorf("failed to get original destination") + return nil, err } - logrus.Debugf("%s, ip: %s, port: %s", dest.Network, dest.Address.IP().String(), dest.Port) + logrus.Debugf("clientIp: %s, remoteIp: %s:%s", clientConn.RemoteAddr().String(), dest.Address.IP().String(), dest.Port) return dialDestination(dest) } diff --git a/handle/tools_test.go b/handle/tools_test.go index ec686c0..7f5bc46 100644 --- a/handle/tools_test.go +++ b/handle/tools_test.go @@ -1,54 +1,56 @@ package handle -import "testing" +import ( + "testing" +) -func Test_isHTTP(t *testing.T) { - tests := []struct { - peek []byte - want bool - }{ - { - peek: []byte("GET /path "), // 10 bytes - want: true, - }, - { - peek: []byte("POST /path "), // 10 bytes - want: true, - }, - { - peek: []byte("put /path "), // 10 bytes - want: true, - }, - { - peek: []byte(" "), // 10 spaces - want: false, - }, - { - peek: []byte("INVALID /p "), // 10 bytes - want: false, - }, - { - peek: []byte("GET "), // 10 bytes - want: true, - }, - { - peek: []byte("GET /p?k=v"), // 10 bytes - want: true, - }, - { - peek: []byte("DELETE /pa"), // 10 bytes - want: true, - }, - { - peek: []byte("PROPPATCH 123123"), // 10 bytes - want: true, - }, +func Test_isHTTP_Fail(t *testing.T) { + // 失败测试用例 - 这些都不是有效的 HTTP 请求 + failTests := [][]byte{ + []byte("INVALID /path HTTP/1.1"), + []byte("NOT-HTTP /index.html"), + []byte("HELLO WORLD"), + []byte("SSL-13"), + []byte("12345"), + []byte("PROPPATCH /api/data HTTP/1.1"), // 因为太长 + []byte("SUBSCRIBE /api/data HTTP/1.1"), // 因为太长 + []byte("UNSUBSCRIBE /api/data HTTP/1.1"), // 因为方法太长 } - for _, tt := range tests { - t.Run("isHttp", func(t *testing.T) { - if got := isHTTP(tt.peek); got != tt.want { - t.Errorf("isHTTP() = %v, want %v", got, tt.want) + // 测试失败用例 + for _, tt := range failTests { + methodLine := tt + if len(tt) > PEEKSIZE { + methodLine = tt[:PEEKSIZE] + } + t.Run("Should Fail: "+string(methodLine), func(t *testing.T) { + if got := isHTTP(methodLine); got != false { + t.Errorf("raw = %v, input = %v, want false for input: %s", tt, got, string(methodLine)) + } + }) + } +} + +func Test_isHTTP_Success(t *testing.T) { + // 成功测试用例 - 这些都是有效的 HTTP 请求开头 + successTests := [][]byte{ + []byte("CHECKOUT /api/data HTTP/1.1"), + []byte("CHECKOUT /api/data HTTP/1.1"), + []byte("GET /index.html HTTP/1.1"), + []byte("POST /api/data HTTP/1.1"), + []byte("HEAD /test HTTP/1.0"), + []byte("PUT /update HTTP/1.1"), + []byte("DELETE /remove HTTP/1.1"), + []byte("OPTIONS /check HTTP/1.1"), + []byte("PATCH /modify HTTP/1.1"), + } + + // 测试成功用例 + for _, tt := range successTests { + methodLine := tt[:PEEKSIZE] + t.Run("Should Success: "+string(methodLine), func(t *testing.T) { + if got := isHTTP(methodLine); got != true { + t.Errorf("raw = %v, input = %v, want true for input: %s", tt, got, string(methodLine)) } }) }