fix: 修复forward不释放,一直创建新的forward,耗尽文件句柄

This commit is contained in:
余泓铮
2024-05-20 14:33:48 +08:00
parent 4b1cb35d75
commit 118f9cc1fa
8 changed files with 56 additions and 59 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
@@ -181,10 +182,8 @@ func (d *Device) DevicePath() (string, error) {
return resp, err
}
func (d *Device) Forward(localPort int, remoteInterface interface{}, noRebind ...bool) (err error) {
command := ""
func (d *Device) Forward(remoteInterface interface{}, noRebind ...bool) (port int, err error) {
var remote string
local := fmt.Sprintf("tcp:%d", localPort)
switch r := remoteInterface.(type) {
// for unix sockets
case string:
@@ -193,6 +192,24 @@ func (d *Device) Forward(localPort int, remoteInterface interface{}, noRebind ..
remote = fmt.Sprintf("tcp:%d", r)
}
forwardList, err := d.ForwardList()
if err != nil {
return
}
for _, forwardItem := range forwardList {
if forwardItem.Remote == remote {
return strconv.Atoi(forwardItem.Local[4:])
}
}
localPort, err := builtin.GetFreePort()
if err != nil {
return
}
command := ""
local := fmt.Sprintf("tcp:%d", localPort)
if len(noRebind) != 0 && noRebind[0] {
command = fmt.Sprintf("host-serial:%s:forward:norebind:%s;%s", d.serial, local, remote)
} else {
@@ -200,7 +217,7 @@ func (d *Device) Forward(localPort int, remoteInterface interface{}, noRebind ..
}
_, err = d.adbClient.executeCommand(command, true)
return
return localPort, nil
}
func (d *Device) ForwardList() (deviceForwardList []DeviceForward, err error) {

View File

@@ -124,8 +124,7 @@ func TestDevice_Forward(t *testing.T) {
setupDevices(t)
for _, device := range devices {
localPort := 61000
err := device.Forward(localPort, 6790)
localPort, err := device.Forward(6790)
if err != nil {
t.Fatal(err)
}