Repository: fatedier/frp (language: go)

Issue:
0.51.1 https2http not working
### Bug Description

After I upgraded frp from 0.49.0 to 0.51.1, the https2http not working, I got no response for most of the static files.
When I using local nginx + frp tcp plugin, it works well.

### frpc Version

0.51.1

### frps Version

0.51.1

### System Architecture

linux/amd64

### Configurations

[web_virtualbox]
type = https
custom_domains = virtualbox.demo.com
plugin = https2http
plugin_local_addr = 127.0.0.1:8080
use_compression = true
plugin_crt_path = /etc/letsencrypt/live/star.demo.com/fullchain.pem
plugin_key_path = /etc/letsencrypt/live/star.demo.com/privkey.pem
plugin_host_header_rewrite = 127.0.0.1
plugin_header_X-From-Where = frp


### Logs

_No response_

### Steps to reproduce

1. 
2. 
3. 
...


### Affected area

- [ ] Docs
- [ ] Installation
- [ ] Performance and Scalability
- [ ] Security
- [ ] User Experience
- [ ] Test and Release
- [ ] Developer Infrastructure
- [ ] Client Plugin
- [ ] Server Plugin
- [ ] Extensions
- [ ] Others


Discussion / hints:
downgrade the frpc to 0.49.0 (still using frps 0.51.1) can solve the issue
frpc 0.51.0 also works well, while using frpc 0.51.1 will get '400 Bad Request' in browser.
logs
@fatedier 

-------------------------------> frps.log

2023/07/24 23:50:47 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:50:47 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:51:08 [D] [control.go:519] [c15e3fc1e27a0724] receive heartbeat
2023/07/24 23:51:13 [D] [proxy.go:265] [c15e3fc1e27a0724] [web_virtualbox] join connections closed
2023/07/24 23:51:13 [D] [vhost.go:245] [c15e3fc1e27a0724] [web_virtualbox] new request host [virtualbox.demo.cn] path [] httpUser []
2023/07/24 23:51:13 [I] [proxy.go:199] [c15e3fc1e27a0724] [web_virtualbox] get a user connection [117.129.4.151:15865]
2023/07/24 23:51:13 [D] [control.go:274] [c15e3fc1e27a0724] get work connection from pool
2023/07/24 23:51:13 [D] [proxy.go:126] [c15e3fc1e27a0724] [web_virtualbox] get a new work connection: [117.129.4.151:15699]
2023/07/24 23:51:13 [T] [proxy.go:235] [c15e3fc1e27a0724] [web_virtualbox] handler user tcp connection, use_encryption: false, use_compression: true
2023/07/24 23:51:13 [D] [proxy.go:255] [c15e3fc1e27a0724] [web_virtualbox] join connections, workConn(l[192.168.0.133:7000] r[117.129.4.151:15699]) userConn(l[192.168.0.133:8443] r[117.129.4.151:15865])
2023/07/24 23:51:13 [D] [vhost.go:245] [c15e3fc1e27a0724] [web_virtualbox] new request host [virtualbox.demo.cn] path [] httpUser []
2023/07/24 23:51:13 [I] [proxy.go:199] [c15e3fc1e27a0724] [web_virtualbox] get a user connection [117.129.4.151:15867]
2023/07/24 23:51:13 [D] [control.go:274] [c15e3fc1e27a0724] get work connection from pool
2023/07/24 23:51:13 [D] [proxy.go:126] [c15e3fc1e27a0724] [web_virtualbox] get a new work connection: [117.129.4.151:15699]
2023/07/24 23:51:13 [T] [proxy.go:235] [c15e3fc1e27a0724] [web_virtualbox] handler user tcp connection, use_encryption: false, use_compression: true
2023/07/24 23:51:13 [D] [proxy.go:255] [c15e3fc1e27a0724] [web_virtualbox] join connections, workConn(l[192.168.0.133:7000] r[117.129.4.151:15699]) userConn(l[192.168.0.133:8443] r[117.129.4.151:15867])
2023/07/24 23:51:13 [D] [proxy.go:265] [c15e3fc1e27a0724] [web_virtualbox] join connections closed
2023/07/24 23:51:13 [D] [vhost.go:245] [c15e3fc1e27a0724] [web_virtualbox] new request host [virtualbox.demo.cn] path [] httpUser []
2023/07/24 23:51:13 [I] [proxy.go:199] [c15e3fc1e27a0724] [web_virtualbox] get a user connection [117.129.4.151:15866]
2023/07/24 23:51:13 [D] [control.go:274] [c15e3fc1e27a0724] get work connection from pool
2023/07/24 23:51:13 [D] [proxy.go:126] [c15e3fc1e27a0724] [web_virtualbox] get a new work connection: [117.129.4.151:15699]
2023/07/24 23:51:13 [T] [proxy.go:235] [c15e3fc1e27a0724] [web_virtualbox] handler user tcp connection, use_encryption: false, use_compression: true
2023/07/24 23:51:13 [D] [proxy.go:255] [c15e3fc1e27a0724] [web_virtualbox] join connections, workConn(l[192.168.0.133:7000] r[117.129.4.151:15699]) userConn(l[192.168.0.133:8443] r[117.129.4.151:15866])
2023/07/24 23:51:13 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:51:13 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:51:13 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:51:13 [D] [vhost.go:245] [c15e3fc1e27a0724] [web_virtualbox] new request host [virtualbox.demo.cn] path [] httpUser []
2023/07/24 23:51:13 [I] [proxy.go:199] [c15e3fc1e27a0724] [web_virtualbox] get a user connection [117.129.4.151:15868]
2023/07/24 23:51:13 [D] [control.go:274] [c15e3fc1e27a0724] get work connection from pool
2023/07/24 23:51:13 [D] [proxy.go:126] [c15e3fc1e27a0724] [web_virtualbox] get a new work connection: [117.129.4.151:15699]
2023/07/24 23:51:13 [T] [proxy.go:235] [c15e3fc1e27a0724] [web_virtualbox] handler user tcp connection, use_encryption: false, use_compression: true
2023/07/24 23:51:13 [D] [proxy.go:255] [c15e3fc1e27a0724] [web_virtualbox] join connections, workConn(l[192.168.0.133:7000] r[117.129.4.151:15699]) userConn(l[192.168.0.133:8443] r[117.129.4.151:15868])
2023/07/24 23:51:13 [D] [vhost.go:245] [c15e3fc1e27a0724] [web_virtualbox] new request host [virtualbox.demo.cn] path [] httpUser []
2023/07/24 23:51:13 [I] [proxy.go:199] [c15e3fc1e27a0724] [web_virtualbox] get a user connection [117.129.4.151:15869]
2023/07/24 23:51:13 [D] [control.go:274] [c15e3fc1e27a0724] get work connection from pool
2023/07/24 23:51:13 [D] [proxy.go:126] [c15e3fc1e27a0724] [web_virtualbox] get a new work connection: [117.129.4.151:15699]
2023/07/24 23:51:13 [T] [proxy.go:235] [c15e3fc1e27a0724] [web_virtualbox] handler user tcp connection, use_encryption: false, use_compression: true
2023/07/24 23:51:13 [D] [proxy.go:255] [c15e3fc1e27a0724] [web_virtualbox] join connections, workConn(l[192.168.0.133:7000] r[117.129.4.151:15699]) userConn(l[192.168.0.133:8443] r[117.129.4.151:15869])
2023/07/24 23:51:13 [D] [vhost.go:245] [c15e3fc1e27a0724] [web_virtualbox] new request host [virtualbox.demo.cn] path [] httpUser []
2023/07/24 23:51:13 [I] [proxy.go:199] [c15e3fc1e27a0724] [web_virtualbox] get a user connection [117.129.4.151:15870]
2023/07/24 23:51:13 [D] [control.go:274] [c15e3fc1e27a0724] get work connection from pool
2023/07/24 23:51:13 [D] [proxy.go:126] [c15e3fc1e27a0724] [web_virtualbox] get a new work connection: [117.129.4.151:15699]
2023/07/24 23:51:13 [T] [proxy.go:235] [c15e3fc1e27a0724] [web_virtualbox] handler user tcp connection, use_encryption: false, use_compression: true
2023/07/24 23:51:13 [D] [proxy.go:255] [c15e3fc1e27a0724] [web_virtualbox] join connections, workConn(l[192.168.0.133:7000] r[117.129.4.151:15699]) userConn(l[192.168.0.133:8443] r[117.129.4.151:15870])
2023/07/24 23:51:13 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:51:13 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:51:13 [D] [proxy.go:265] [c15e3fc1e27a0724] [web_virtualbox] join connections closed
2023/07/24 23:51:13 [D] [proxy.go:265] [c15e3fc1e27a0724] [web_virtualbox] join connections closed
2023/07/24 23:51:13 [D] [control.go:245] [c15e3fc1e27a0724] new work connection registered
2023/07/24 23:51:13 [D] [vhost.go:245] [c15e3fc1e27a0724] [web_virtualbox] new request host [virtualbox.demo.cn] path [] httpUser []
2023/07/24 23:51:13 [I] [proxy.go:199] [c15e3fc1e27a0724] [web_virtualbox] get a user connection [117.129.4.151:15871]
2023/07/24 23:51:13 [D] [control.go:274] [c15e3fc1e27a0724] get work connection from pool
2023/07/24 23:51:13 [D] [proxy.go:126] [c15e3fc1e27a0724] [web_virtualbox] get a new work connection: [117.129.4.151:15699]
2023/07/24 23:51:13 [T] [proxy.go:235] [c15e3fc1e27a0724] [web_virtualbox] handler user tcp connection, use_encryption: false, use_compression: true
2023/07/24 23:51:13 [D] [proxy.go:255] [c15e3fc1e27a0724] [web_virtualbox] join connections, workConn(l[192.168.0.133:7000] r[117.129.4.151:15699]) userConn(l[192.168.0.133:8443] r[117.129.4.151:15871])


-------------------------------> frpc.log
2023/07/24 23:48:47 [D] [proxy_wrapper.go:255] [c15e3fc1e27a0724] [web_virtualbox] start a new work connection, localAddr: 192.168.50.110:57129 remoteAddr: x.x.x.x:7000
2023/07/24 23:48:47 [T] [proxy.go:135] [c15e3fc1e27a0724] [web_virtualbox] handle tcp work connection, use_encryption: false, use_compression: true
2023/07/24 23:48:47 [D] [proxy.go:186] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin: https2http
2023/07/24 23:48:47 [D] [proxy.go:188] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin finished
2023/07/24 23:48:47 [D] [proxy_wrapper.go:255] [c15e3fc1e27a0724] [web_virtualbox] start a new work connection, localAddr: 192.168.50.110:57129 remoteAddr: x.x.x.x:7000
2023/07/24 23:48:47 [T] [proxy.go:135] [c15e3fc1e27a0724] [web_virtualbox] handle tcp work connection, use_encryption: false, use_compression: true
2023/07/24 23:48:47 [D] [proxy.go:186] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin: https2http
2023/07/24 23:48:47 [D] [proxy.go:188] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin finished
2023/07/24 23:48:47 [D] [proxy_wrapper.go:255] [c15e3fc1e27a0724] [web_virtualbox] start a new work connection, localAddr: 192.168.50.110:57129 remoteAddr: x.x.x.x:7000
2023/07/24 23:48:47 [T] [proxy.go:135] [c15e3fc1e27a0724] [web_virtualbox] handle tcp work connection, use_encryption: false, use_compression: true
2023/07/24 23:48:47 [D] [proxy.go:186] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin: https2http
2023/07/24 23:48:47 [D] [proxy.go:188] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin finished
2023/07/24 23:48:47 [D] [proxy_wrapper.go:255] [c15e3fc1e27a0724] [web_virtualbox] start a new work connection, localAddr: 192.168.50.110:57129 remoteAddr: x.x.x.x:7000
2023/07/24 23:48:47 [T] [proxy.go:135] [c15e3fc1e27a0724] [web_virtualbox] handle tcp work connection, use_encryption: false, use_compression: true
2023/07/24 23:48:47 [D] [proxy.go:186] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin: https2http
2023/07/24 23:48:47 [D] [proxy.go:188] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin finished
2023/07/24 23:48:47 [D] [proxy_wrapper.go:255] [c15e3fc1e27a0724] [web_virtualbox] start a new work connection, localAddr: 192.168.50.110:57129 remoteAddr: x.x.x.x:7000
2023/07/24 23:48:47 [T] [proxy.go:135] [c15e3fc1e27a0724] [web_virtualbox] handle tcp work connection, use_encryption: false, use_compression: true
2023/07/24 23:48:47 [D] [proxy.go:186] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin: https2http
2023/07/24 23:48:47 [D] [proxy.go:188] [c15e3fc1e27a0724] [web_virtualbox] handle by plugin finished
2023/07/24 23:49:08 [D] [control.go:296] [c15e3fc1e27a0724] send heartbeat to server
2023/07/24 23:49:08 [D] [control.go:329] [c15e3fc1e27a0724] receive heartbeat from server

Can not reproduce in my env and the logs seems no error.
When making concurrent requests, it is possible to reproduce the issue. It requires time to investigate the cause.

Fix patch:
diff --git a/Release.md b/Release.md
index 8b59bafa523..f8fcfb2b1a4 100644
--- a/Release.md
+++ b/Release.md
@@ -5,3 +5,4 @@
 ### Fixes
 
 * fix a goroutine leak issue caused by Login plugin timeout.
+* Fix an issue introduced in version 0.51.1, enabling `use_compression` will cause some requests to fail.
diff --git a/client/proxy/proxy.go b/client/proxy/proxy.go
index 62d02fcfc23..62b3cbba51f 100644
--- a/client/proxy/proxy.go
+++ b/client/proxy/proxy.go
@@ -142,10 +142,9 @@ func (pxy *BaseProxy) HandleTCPWorkConnection(workConn net.Conn, m *msg.StartWor
 			return
 		}
 	}
+	var compressionResourceRecycleFn func()
 	if baseConfig.UseCompression {
-		var releaseFn func()
-		remote, releaseFn = libio.WithCompressionFromPool(remote)
-		defer releaseFn()
+		remote, compressionResourceRecycleFn = libio.WithCompressionFromPool(remote)
 	}
 
 	// check if we need to send proxy protocol info
@@ -215,4 +214,7 @@ func (pxy *BaseProxy) HandleTCPWorkConnection(workConn net.Conn, m *msg.StartWor
 	if len(errs) > 0 {
 		xl.Trace("join connections errors: %v", errs)
 	}
+	if compressionResourceRecycleFn != nil {
+		compressionResourceRecycleFn()
+	}
 }
diff --git a/client/proxy/sudp.go b/client/proxy/sudp.go
index fbbe87cab2b..347b86a6227 100644
--- a/client/proxy/sudp.go
+++ b/client/proxy/sudp.go
@@ -97,9 +97,7 @@ func (pxy *SUDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
 		}
 	}
 	if pxy.cfg.UseCompression {
-		var releaseFn func()
-		rwc, releaseFn = libio.WithCompressionFromPool(rwc)
-		defer releaseFn()
+		rwc = libio.WithCompression(rwc)
 	}
 	conn = utilnet.WrapReadWriteCloserToConn(rwc, conn)
 
diff --git a/client/proxy/udp.go b/client/proxy/udp.go
index 58f30cb28e0..c0edc31256b 100644
--- a/client/proxy/udp.go
+++ b/client/proxy/udp.go
@@ -108,9 +108,7 @@ func (pxy *UDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
 		}
 	}
 	if pxy.cfg.UseCompression {
-		var releaseFn func()
-		rwc, releaseFn = libio.WithCompressionFromPool(rwc)
-		defer releaseFn()
+		rwc = libio.WithCompression(rwc)
 	}
 	conn = utilnet.WrapReadWriteCloserToConn(rwc, conn)
 
diff --git a/client/visitor/stcp.go b/client/visitor/stcp.go
index 86e09ac0b73..56d55055bf7 100644
--- a/client/visitor/stcp.go
+++ b/client/visitor/stcp.go
@@ -126,9 +126,9 @@ func (sv *STCPVisitor) handleConn(userConn net.Conn) {
 	}
 
 	if sv.cfg.UseCompression {
-		var releaseFn func()
-		remote, releaseFn = libio.WithCompressionFromPool(remote)
-		defer releaseFn()
+		var recycleFn func()
+		remote, recycleFn = libio.WithCompressionFromPool(remote)
+		defer recycleFn()
 	}
 
 	libio.Join(userConn, remote)
diff --git a/client/visitor/xtcp.go b/client/visitor/xtcp.go
index fe71147c41b..dc5b807f768 100644
--- a/client/visitor/xtcp.go
+++ b/client/visitor/xtcp.go
@@ -200,9 +200,9 @@ func (sv *XTCPVisitor) handleConn(userConn net.Conn) {
 		}
 	}
 	if sv.cfg.UseCompression {
-		var releaseFn func()
-		muxConnRWCloser, releaseFn = libio.WithCompressionFromPool(muxConnRWCloser)
-		defer releaseFn()
+		var recycleFn func()
+		muxConnRWCloser, recycleFn = libio.WithCompressionFromPool(muxConnRWCloser)
+		defer recycleFn()
 	}
 
 	_, _, errs := libio.Join(userConn, muxConnRWCloser)
diff --git a/go.mod b/go.mod
index 0d301f84be8..97bf2ba9c43 100644
--- a/go.mod
+++ b/go.mod
@@ -6,7 +6,7 @@ require (
 	github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
 	github.com/coreos/go-oidc/v3 v3.6.0
 	github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb
-	github.com/fatedier/golib v0.1.1-0.20230720124328-204db2e322f8
+	github.com/fatedier/golib v0.1.1-0.20230725122706-dcbaee8eef40
 	github.com/fatedier/kcp-go v2.0.4-0.20190803094908-fe8645b0a904+incompatible
 	github.com/go-playground/validator/v10 v10.14.1
 	github.com/google/uuid v1.3.0
diff --git a/go.sum b/go.sum
index faf7b922288..0fcd840fd74 100644
--- a/go.sum
+++ b/go.sum
@@ -26,8 +26,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
 github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
 github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb h1:wCrNShQidLmvVWn/0PikGmpdP0vtQmnvyRg3ZBEhczw=
 github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb/go.mod h1:wx3gB6dbIfBRcucp94PI9Bt3I0F2c/MyNEWuhzpWiwk=
-github.com/fatedier/golib v0.1.1-0.20230720124328-204db2e322f8 h1:8JYzwZ26k1zEsjgits6GHsQxm2tl3K3FYgMMLcIKBKU=
-github.com/fatedier/golib v0.1.1-0.20230720124328-204db2e322f8/go.mod h1:Lmi9U4VfvdRvonSMh1FgXVy1hCXycVyJk4E9ktokknE=
+github.com/fatedier/golib v0.1.1-0.20230725122706-dcbaee8eef40 h1:BVdpWT6viE/mpuRa6txNyRNjtHa1Efrii9Du6/gHfJ0=
+github.com/fatedier/golib v0.1.1-0.20230725122706-dcbaee8eef40/go.mod h1:Lmi9U4VfvdRvonSMh1FgXVy1hCXycVyJk4E9ktokknE=
 github.com/fatedier/kcp-go v2.0.4-0.20190803094908-fe8645b0a904+incompatible h1:ssXat9YXFvigNge/IkkZvFMn8yeYKFX+uI6wn2mLJ74=
 github.com/fatedier/kcp-go v2.0.4-0.20190803094908-fe8645b0a904+incompatible/go.mod h1:YpCOaxj7vvMThhIQ9AfTOPW2sfztQR5WDfs7AflSy4s=
 github.com/fatedier/yamux v0.0.0-20230628132301-7aca4898904d h1:ynk1ra0RUqDWQfvFi5KtMiSobkVQ3cNc0ODb8CfIETo=
diff --git a/pkg/util/version/version.go b/pkg/util/version/version.go
index 670e07fcec5..7ea3a25289f 100644
--- a/pkg/util/version/version.go
+++ b/pkg/util/version/version.go
@@ -19,7 +19,7 @@ import (
 	"strings"
 )
 
-var version = "0.51.1"
+var version = "0.51.2"
 
 func Full() string {
 	return version
diff --git a/server/proxy/http.go b/server/proxy/http.go
index 81139167eeb..432dbc101b3 100644
--- a/server/proxy/http.go
+++ b/server/proxy/http.go
@@ -175,9 +175,7 @@ func (pxy *HTTPProxy) GetRealConn(remoteAddr string) (workConn net.Conn, err err
 		}
 	}
 	if pxy.cfg.UseCompression {
-		var releaseFn func()
-		rwc, releaseFn = libio.WithCompressionFromPool(rwc)
-		defer releaseFn()
+		rwc = libio.WithCompression(rwc)
 	}
 
 	if pxy.GetLimiter() != nil {
diff --git a/server/proxy/proxy.go b/server/proxy/proxy.go
index 9d6ee993553..d1e6625523a 100644
--- a/server/proxy/proxy.go
+++ b/server/proxy/proxy.go
@@ -241,9 +241,9 @@ func (pxy *BaseProxy) handleUserTCPConnection(userConn net.Conn) {
 		}
 	}
 	if cfg.UseCompression {
-		var releaseFn func()
-		local, releaseFn = libio.WithCompressionFromPool(local)
-		defer releaseFn()
+		var recycleFn func()
+		local, recycleFn = libio.WithCompressionFromPool(local)
+		defer recycleFn()
 	}
 
 	if pxy.GetLimiter() != nil {


Test patch: