Files
orchard/internal/command/portforward/portspec.go
T
Nikolay Edigaryev 47fef47d1c Port forwarding support (#30)
* Port forwarding support

* .golangci.yml: remove and replace deprecated and archived linters

* Client: pass credentials when calling WebSocket API methods

* API: require ServiceAccountRoleComputeWrite role for port forwarding

* Use Buf

* Rename Poll() RPC method to Watch()

* Split Rendezvous into two parts: Watcher and Proxy (#32)

* Split Rendezvous into two parts: Watcher and Proxy

* Implement Proxy cancellation

* Use Protocol Buffers structure directly in Watcher

* Fix TestWatcher after switching to Protocol Buffers structure

* portForwardVM(): ensure we also check for gin's context
2023-03-14 11:31:13 -04:00

48 lines
995 B
Go

package portforward
import (
"errors"
"fmt"
"strconv"
"strings"
)
var ErrInvalidPortSpec = errors.New("invalid port-forwarding specification")
type PortSpec struct {
LocalPort uint16
RemotePort uint16
}
func NewPortSpec(portSpecRaw string) (*PortSpec, error) {
splits := strings.Split(portSpecRaw, ":")
if len(splits) > 2 {
return nil, fmt.Errorf("%w: expected no more than 2 components delimited by \":\", found %d",
ErrInvalidPortSpec, len(splits))
}
localPort, err := strconv.ParseUint(splits[0], 10, 16)
if err != nil {
return nil, err
}
remotePort := localPort
if len(splits) > 1 {
remotePort, err = strconv.ParseUint(splits[1], 10, 16)
if err != nil {
return nil, err
}
}
if localPort < 1 || remotePort < 1 || localPort > 65535 || remotePort > 65535 {
return nil, fmt.Errorf("%w: only ports in range [1, 65535] are allowed", ErrInvalidPortSpec)
}
return &PortSpec{
LocalPort: uint16(localPort),
RemotePort: uint16(remotePort),
}, nil
}