Skip to content

Commit c151b28

Browse files
committedAug 6, 2023
fix auth
1 parent e6d4055 commit c151b28

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed
 

‎Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
GOOS=linux GOARCH=amd64 go build

‎main.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ var specailIPs []string
4141
var allowedIPs []string
4242

4343
func updateSpecailIPs(reader *bufio.Reader, conn net.Conn) bool {
44-
// 使用 io.TeeReader 复制原始数据到另一个 io.Writer
45-
// buf 中保存了一份原始数据的拷贝,你可以根据需求使用它
46-
var buf strings.Builder
47-
teeReader := io.TeeReader(reader, &buf)
44+
peekData, err := reader.Peek(3)
45+
if err != nil {
46+
return false
47+
}
48+
if !strings.HasPrefix(string(peekData), "GET") {
49+
return false
50+
}
4851

4952
// 使用 http.Request 对象解析连接的请求
50-
request, err := http.ReadRequest(bufio.NewReader(teeReader))
53+
request, err := http.ReadRequest(reader)
5154
if err != nil {
5255
fmt.Println("Error reading request:", err)
5356
return false
@@ -151,7 +154,7 @@ func needAuth(conn net.Conn) bool {
151154

152155
// 如果客户端 IP 不在白名单中,需要认证
153156
if !allowed {
154-
log.Println("Connection from", clientIP, "is not allowed")
157+
// log.Println("Connection from", clientIP, "is not allowed")
155158
return true
156159
}
157160

0 commit comments

Comments
 (0)
Please sign in to comment.