goadb/adb/device_forward.go

64 lines
1.3 KiB
Go
Raw Normal View History

2024-06-12 11:57:38 +00:00
package adb
import (
"strings"
"github.com/timoxa0/goadb/internal/errors"
)
2024-06-14 10:56:58 +00:00
type ForwardType int8
2024-06-12 11:57:38 +00:00
const (
TypeInvalid ForwardType = iota
2024-06-14 10:56:58 +00:00
ForwardTCP
ForwardLOCAL
2024-06-12 11:57:38 +00:00
)
var forwardTypeStrings = map[string]ForwardType{
2024-06-14 10:56:58 +00:00
"tcp": ForwardTCP,
"local": ForwardLOCAL,
2024-06-12 11:57:38 +00:00
}
type ForwardTarget string
type ForwardLocal struct {
2024-06-14 10:56:58 +00:00
FType ForwardType
FTarget ForwardTarget
2024-06-12 11:57:38 +00:00
}
type ForwardRemote struct {
2024-06-14 10:56:58 +00:00
FType ForwardType
FTarget ForwardTarget
2024-06-12 11:57:38 +00:00
}
type Forward struct {
2024-06-14 10:56:58 +00:00
Local ForwardLocal
Remote ForwardRemote
2024-06-12 11:57:38 +00:00
}
func parseForward(str string, deviceSerial string) ([]Forward, error) {
var forwards []Forward
for _, forwardStr := range strings.Split(str, "\n") {
if strings.Trim(forwardStr, "\n\t ") == "" {
continue
}
raw_forward := strings.Split(forwardStr, " ")
serial, local, remote := raw_forward[0], raw_forward[1], raw_forward[2]
if serial == deviceSerial {
raw_local := strings.Split(local, ":")
raw_remote := strings.Split(remote, ":")
forwards = append(forwards, Forward{
2024-06-14 10:56:58 +00:00
Local: ForwardLocal{
FType: forwardTypeStrings[raw_local[0]],
FTarget: ForwardTarget(raw_local[1]),
2024-06-12 11:57:38 +00:00
},
2024-06-14 10:56:58 +00:00
Remote: ForwardRemote{
FType: forwardTypeStrings[raw_remote[0]],
FTarget: ForwardTarget(raw_remote[1]),
2024-06-12 11:57:38 +00:00
},
})
}
}
return forwards, errors.Errorf(errors.ParseError, "invalid device forward")
}