File: gvproxy_command.go

package info (click to toggle)
golang-github-containers-gvisor-tap-vsocks 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 800 kB
  • sloc: sh: 95; makefile: 59
file content (197 lines) | stat: -rw-r--r-- 4,512 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package types

import (
	"os/exec"
	"strconv"
)

type GvproxyCommand struct {
	// Print packets on stderr
	Debug bool

	// Length of packet
	// Larger packets means less packets to exchange for the same amount of data (and less protocol overhead)
	MTU int

	// Values passed in by forward-xxx flags in commandline (forward-xxx:info)
	forwardInfo map[string][]string

	// List of endpoints the user wants to listen to
	endpoints []string

	// Map of different sockets provided by user (socket-type flag:socket)
	sockets map[string]string

	// Logfile where gvproxy should redirect logs
	LogFile string

	// File where gvproxy's pid is stored
	PidFile string

	// SSHPort to access the guest VM
	SSHPort int
}

func NewGvproxyCommand() GvproxyCommand {
	return GvproxyCommand{
		MTU:         1500,
		SSHPort:     2222,
		endpoints:   []string{},
		forwardInfo: map[string][]string{},
		sockets:     map[string]string{},
	}
}

func (c *GvproxyCommand) checkSocketsInitialized() {
	if len(c.sockets) < 1 {
		c.sockets = map[string]string{}
	}
}

func (c *GvproxyCommand) checkForwardInfoInitialized() {
	if len(c.forwardInfo) < 1 {
		c.forwardInfo = map[string][]string{}
	}
}

func (c *GvproxyCommand) AddEndpoint(endpoint string) {
	if len(c.endpoints) < 1 {
		c.endpoints = []string{}
	}

	c.endpoints = append(c.endpoints, endpoint)
}

func (c *GvproxyCommand) AddVpnkitSocket(socket string) {
	c.checkSocketsInitialized()
	c.sockets["listen-vpnkit"] = socket
}

func (c *GvproxyCommand) AddQemuSocket(socket string) {
	c.checkSocketsInitialized()
	c.sockets["listen-qemu"] = socket
}

func (c *GvproxyCommand) AddBessSocket(socket string) {
	c.checkSocketsInitialized()
	c.sockets["listen-bess"] = socket
}

func (c *GvproxyCommand) AddStdioSocket(socket string) {
	c.checkSocketsInitialized()
	c.sockets["listen-stdio"] = socket
}

func (c *GvproxyCommand) AddVfkitSocket(socket string) {
	c.checkSocketsInitialized()
	c.sockets["listen-vfkit"] = socket
}

func (c *GvproxyCommand) addForwardInfo(flag, value string) {
	c.forwardInfo[flag] = append(c.forwardInfo[flag], value)
}

func (c *GvproxyCommand) AddForwardSock(socket string) {
	c.checkForwardInfoInitialized()
	c.addForwardInfo("forward-sock", socket)
}

func (c *GvproxyCommand) AddForwardDest(dest string) {
	c.checkForwardInfoInitialized()
	c.addForwardInfo("forward-dest", dest)
}

func (c *GvproxyCommand) AddForwardUser(user string) {
	c.checkForwardInfoInitialized()
	c.addForwardInfo("forward-user", user)
}

func (c *GvproxyCommand) AddForwardIdentity(identity string) {
	c.checkForwardInfoInitialized()
	c.addForwardInfo("forward-identity", identity)
}

// socketsToCmdline converts Command.sockets to a commandline format
func (c *GvproxyCommand) socketsToCmdline() []string {
	args := []string{}

	for socketFlag, socket := range c.sockets {
		if socket != "" {
			args = append(args, "-"+socketFlag, socket)
		}
	}

	return args
}

// forwardInfoToCmdline converts Command.forwardInfo to a commandline format
func (c *GvproxyCommand) forwardInfoToCmdline() []string {
	args := []string{}

	for forwardInfoFlag, forwardInfo := range c.forwardInfo {
		for _, i := range forwardInfo {
			if i != "" {
				args = append(args, "-"+forwardInfoFlag, i)
			}
		}
	}

	return args
}

// endpointsToCmdline converts Command.endpoints to a commandline format
func (c *GvproxyCommand) endpointsToCmdline() []string {
	args := []string{}

	for _, endpoint := range c.endpoints {
		if endpoint != "" {
			args = append(args, "-listen", endpoint)
		}
	}

	return args
}

// ToCmdline converts Command to a properly formatted command for gvproxy based
// on its fields
func (c *GvproxyCommand) ToCmdline() []string {
	args := []string{}

	// listen (endpoints)
	args = append(args, c.endpointsToCmdline()...)

	// debug
	if c.Debug {
		args = append(args, "-debug")
	}

	// mtu
	args = append(args, "-mtu", strconv.Itoa(c.MTU))

	// ssh-port
	args = append(args, "-ssh-port", strconv.Itoa(c.SSHPort))

	// sockets
	args = append(args, c.socketsToCmdline()...)

	// forward info
	args = append(args, c.forwardInfoToCmdline()...)

	// pid-file
	if c.PidFile != "" {
		args = append(args, "-pid-file", c.PidFile)
	}

	// log-file
	if c.LogFile != "" {
		args = append(args, "-log-file", c.LogFile)
	}

	return args
}

// Cmd converts Command to a commandline format and returns an exec.Cmd which
// can be executed by os/exec
func (c *GvproxyCommand) Cmd(gvproxyPath string) *exec.Cmd {
	return exec.Command(gvproxyPath, c.ToCmdline()...) // #nosec G204
}