File: port_forward_request.go

package info (click to toggle)
golang-github-microsoft-dev-tunnels 0.0.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,988 kB
  • sloc: cs: 9,969; java: 2,767; javascript: 328; xml: 186; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,209 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package messages

import (
	"bytes"
	"fmt"
	"io"
)

const (
	PortForwardRequestType = "tcpip-forward"
)

type PortForwardRequest struct {
	addressToBind string
	port          uint32
}

func NewPortForwardRequest(addressToBind string, port uint32) *PortForwardRequest {
	return &PortForwardRequest{
		addressToBind: addressToBind,
		port:          port,
	}
}

func (pfr *PortForwardRequest) Port() uint32 {
	return pfr.port
}

func (pfr *PortForwardRequest) Marshal() ([]byte, error) {
	buf := new(bytes.Buffer)
	if err := writeString(buf, pfr.addressToBind); err != nil {
		return nil, fmt.Errorf("error writing address to bind: %w", err)
	}
	if err := writeUint32(buf, pfr.port); err != nil {
		return nil, fmt.Errorf("error writing port: %w", err)
	}
	return buf.Bytes(), nil
}

func (pfr *PortForwardRequest) Unmarshal(buf io.Reader) error {
	addressToBind, err := readString(buf)
	if err != nil {
		return fmt.Errorf("error reading address to bind: %w", err)
	}
	port, err := readUint32(buf)
	if err != nil {
		return fmt.Errorf("error reading port: %w", err)
	}
	pfr.addressToBind = addressToBind
	pfr.port = port
	return nil
}