File: main.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 (218 lines) | stat: -rw-r--r-- 4,727 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//go:build windows
// +build windows

package main

import (
	"context"
	"fmt"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"syscall"
	"time"
	"unsafe"

	"github.com/containers/gvisor-tap-vsock/pkg/sshclient"
	"github.com/containers/gvisor-tap-vsock/pkg/types"
	"github.com/containers/gvisor-tap-vsock/pkg/utils"
	"github.com/containers/winquit/pkg/winquit"
	"github.com/sirupsen/logrus"
	"golang.org/x/sync/errgroup"
	"golang.org/x/sys/windows/svc/eventlog"
)

const (
	ERR_BAD_ARGS = 0x000A
	WM_QUIT      = 0x12
)

var (
	stateDir string
	debug    bool
)

func main() {
	args := os.Args
	if len(args) > 1 {
		switch args[1] {
		case "-version":
			version := types.NewVersion("win-sshproxy")
			fmt.Println(version.String())
			os.Exit(0)
		case "-debug":
			debug = true
			args = args[2:]
		default:
			args = args[1:]
		}
	}

	if len(args) < 5 || (len(args)-2)%3 != 0 {
		alert("Usage: " + filepath.Base(os.Args[0]) + "(-debug) [name] [statedir] ([source] [dest] [identity])...  \n\nThis facilty proxies windows pipes and unix sockets over ssh using the specified identity.")
		os.Exit(ERR_BAD_ARGS)
	}

	log, err := setupLogging(args[0])
	if err != nil {
		os.Exit(1)
	}
	defer log.Close()

	stateDir = args[1]

	var sources, dests, identities []string
	for i := 2; i < len(args)-2; i += 3 {
		sources = append(sources, args[i])
		dests = append(dests, args[i+1])
		identities = append(identities, args[i+2])
	}

	ctx, cancel := context.WithCancel(context.Background())
	group, ctx := errgroup.WithContext(ctx)

	quit := make(chan bool, 1)
	// Wait for a WM_QUIT message to exit
	winquit.NotifyOnQuit(quit)
	go func() {
		<-quit
		cancel()
	}()

	// Save thread for legacy callers which use it to post a quit
	if _, err := saveThreadId(); err != nil {
		logrus.Errorf("Error saving thread id: " + err.Error())
	}

	logrus.Debug("Setting up proxies")
	setupProxies(ctx, group, sources, dests, identities)

	// Wait for cmopletion (cancellation) or error
	if err := group.Wait(); err != nil {
		logrus.Errorf("Error occured in execution group: " + err.Error())
		os.Exit(1)
	}
}

func setupLogging(name string) (*eventlog.Log, error) {
	// Reuse the Built-in .NET Runtime Source so that we do not
	// have to provide a messaage table and modify the system
	// event configuration
	log, err := eventlog.Open(".NET Runtime")
	if err != nil {
		return nil, err
	}

	logrus.AddHook(NewEventHook(log, name))
	if debug {
		logrus.SetLevel(logrus.DebugLevel)
	} else {
		logrus.SetLevel(logrus.InfoLevel)
	}

	return log, nil
}

func setupProxies(ctx context.Context, g *errgroup.Group, sources []string, dests []string, identities []string) error {
	for i := 0; i < len(sources); i++ {
		var (
			src  *url.URL
			dest *url.URL
			err  error
		)
		if strings.Contains(sources[i], "://") {
			src, err = url.Parse(sources[i])
			if err != nil {
				return err
			}
		} else {
			src = &url.URL{
				Scheme: "unix",
				Path:   sources[i],
			}
		}

		dest, err = url.Parse(dests[i])
		if err != nil {
			return err
		}
		j := i
		g.Go(func() error {
			forward, err := sshclient.CreateSSHForward(ctx, src, dest, identities[j], nil)
			if err != nil {
				return err
			}
			go func() {
				<-ctx.Done()
				// Abort pending accepts
				forward.Close()
			}()
		loop:
			for {
				select {
				case <-ctx.Done():
					break loop
				default:
					// proceed
				}
				err := forward.AcceptAndTunnel(ctx)
				if err != nil {
					logrus.Debugf("Error occurred handling ssh forwarded connection: %q", err)
				}
			}
			return nil
		})
	}

	return nil
}

func saveThreadId() (uint32, error) {
	path := filepath.Join(stateDir, "win-sshproxy.tid")
	file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
	if err != nil {
		return 0, err
	}
	defer file.Close()

	tid, err := getThreadId()
	if err != nil {
		return 0, err
	}

	fmt.Fprintf(file, "%d:%d\n", os.Getpid(), tid)
	return tid, nil
}

func getThreadId() (uint32, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	getTid := func() (uint32, error) {
		tid := winquit.GetCurrentMessageLoopThreadId()
		if tid != 0 {
			return tid, nil
		}
		return 0, fmt.Errorf("failed to get thread ID")
	}

	return utils.Retry(ctx, getTid, "Waiting for message loop thread id")
}

// Creates an "error" style pop-up window
func alert(caption string) int {
	// Error box style
	format := 0x10

	user32 := syscall.NewLazyDLL("user32.dll")
	captionPtr, _ := syscall.UTF16PtrFromString(caption)
	titlePtr, _ := syscall.UTF16PtrFromString("winpath")
	ret, _, _ := user32.NewProc("MessageBoxW").Call(
		uintptr(0),
		uintptr(unsafe.Pointer(captionPtr)),
		uintptr(unsafe.Pointer(titlePtr)),
		uintptr(format))

	return int(ret)
}