File: port_forward_channel_open.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 (87 lines) | stat: -rw-r--r-- 2,362 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package messages

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

const PortForwardChannelType = "forwarded-tcpip"

type PortForwardChannel struct {
	channelOpen         *channelOpen
	host                string
	port                uint32
	originatorIPAddress string
	originatorPort      uint32
}

func NewPortForwardChannel(senderChannel uint32, host string, port uint32, originatorIPAddress string, originatorPort uint32) *PortForwardChannel {
	co := newChannelOpen(senderChannel, 0, 0)

	return &PortForwardChannel{
		channelOpen:         co,
		host:                host,
		port:                port,
		originatorIPAddress: originatorIPAddress,
		originatorPort:      originatorPort,
	}
}

func (pfc *PortForwardChannel) Type() string {
	return PortForwardChannelType
}

func (pfc *PortForwardChannel) Port() uint32 {
	return pfc.port
}

// Marshal returns the byte representation of the PortForwardChannel.
// This does not include the channelOpen as it is already included in the ssh message.
func (pfc *PortForwardChannel) Marshal() ([]byte, error) {
	var buff []byte
	buf := bytes.NewBuffer(buff)
	if err := writeString(buf, pfc.host); err != nil {
		return nil, fmt.Errorf("error writing host: %w", err)
	}
	if err := writeUint32(buf, pfc.port); err != nil {
		return nil, fmt.Errorf("error writing port: %w", err)
	}
	if err := writeString(buf, pfc.originatorIPAddress); err != nil {
		return nil, fmt.Errorf("error writing originator ip address: %w", err)
	}
	if err := writeUint32(buf, pfc.originatorPort); err != nil {
		return nil, fmt.Errorf("error writing originator port: %w", err)
	}

	return buf.Bytes(), nil
}

// Unmarshal parses the byte representation of the PortForwardChannel.
// This does not include the channelOpen.
func (pfc *PortForwardChannel) Unmarshal(buf io.Reader) (err error) {
	pfc.host, err = readString(buf)
	if err != nil {
		return fmt.Errorf("error reading host: %w", err)
	}

	pfc.port, err = readUint32(buf)
	if err != nil {
		return fmt.Errorf("error reading port: %w", err)
	}

	pfc.originatorIPAddress, err = readString(buf)
	if err != nil {
		return fmt.Errorf("error reading originator ip address: %w", err)
	}

	pfc.originatorPort, err = readUint32(buf)
	if err != nil {
		return fmt.Errorf("error reading originator port: %w", err)
	}

	return nil
}