| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "net" |
| 9 | "net/http" |
| 10 | "net/http/pprof" |
| 11 | "net/url" |
| 12 | "os" |
| 13 | "os/signal" |
| 14 | "runtime" |
| 15 | "strconv" |
| 16 | "strings" |
| 17 | "syscall" |
| 18 | "time" |
| 19 | |
| 20 | "github.com/containers/gvisor-tap-vsock/pkg/net/stdio" |
| 21 | "github.com/containers/gvisor-tap-vsock/pkg/notification" |
| 22 | "github.com/containers/gvisor-tap-vsock/pkg/sshclient" |
| 23 | "github.com/containers/gvisor-tap-vsock/pkg/transport" |
| 24 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 25 | "github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork" |
| 26 | "github.com/containers/winquit/pkg/winquit" |
| 27 | humanize "github.com/dustin/go-humanize" |
| 28 | log "github.com/sirupsen/logrus" |
| 29 | "golang.org/x/sync/errgroup" |
| 30 | ) |
| 31 | |
| 32 | var ( |
| 33 | exitCode int |
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | |
| 38 | config, err := GvproxyInit() |
| 39 | if err != nil { |
| 40 | log.Fatal(err.Error()) |
| 41 | } |
| 42 | |
| 43 | |
| 44 | log.Info(GvproxyVersion()) |
| 45 | |
| 46 | ctx, cancel := context.WithCancel(context.Background()) |
| 47 | |
| 48 | defer log.Exit(exitCode) |
| 49 | |
| 50 | |
| 51 | if config.PIDFile != "" { |
| 52 | f, err := os.Create(config.PIDFile) |
| 53 | if err != nil { |
| 54 | log.Errorf("failed to create pidfile: %s", err.Error()) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | defer func() { |
| 59 | if err := os.Remove(config.PIDFile); err != nil { |
| 60 | log.Errorf("failed to remove pidfile: %s", err.Error()) |
| 61 | } |
| 62 | }() |
| 63 | pid := os.Getpid() |
| 64 | if _, err := f.WriteString(strconv.Itoa(pid)); err != nil { |
| 65 | log.Errorf("failed to write pidfile: %s", err.Error()) |
| 66 | return |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | groupErrs, ctx := errgroup.WithContext(ctx) |
| 71 | |
| 72 | sigChan := make(chan os.Signal, 1) |
| 73 | signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) |
| 74 | |
| 75 | |
| 76 | winquit.SimulateSigTermOnQuit(sigChan) |
| 77 | |
| 78 | groupErrs.Go(func() error { |
| 79 | return run(ctx, groupErrs, config) |
| 80 | }) |
| 81 | |
| 82 | |
| 83 | groupErrs.Go(func() error { |
| 84 | select { |
| 85 | |
| 86 | case <-sigChan: |
| 87 | cancel() |
| 88 | return errors.New("signal caught") |
| 89 | case <-ctx.Done(): |
| 90 | return nil |
| 91 | } |
| 92 | }) |
| 93 | |
| 94 | if err := groupErrs.Wait(); err != nil { |
| 95 | log.Errorf("gvproxy exiting: %v", err) |
| 96 | exitCode = 1 |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func getForwardsMap(sshPort int, sshHostPort string) map[string]string { |
| 101 | if sshPort == -1 { |
| 102 | return map[string]string{} |
| 103 | } |
| 104 | return map[string]string{ |
| 105 | fmt.Sprintf("127.0.0.1:%d", sshPort): sshHostPort, |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | type arrayFlags []string |
| 110 | |
| 111 | func (i *arrayFlags) String() string { |
| 112 | return "my string representation" |
| 113 | } |
| 114 | |
| 115 | func (i *arrayFlags) Set(value string) error { |
| 116 | *i = append(*i, value) |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func InDebugMode() bool { |
| 121 | return log.GetLevel().String() == "debug" |
| 122 | } |
| 123 | |
| 124 | func run(ctx context.Context, g *errgroup.Group, config *GvproxyConfig) error { |
| 125 | vn, err := virtualnetwork.New(&config.Stack) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | log.Info("waiting for clients...") |
| 130 | |
| 131 | |
| 132 | |
| 133 | notificationSender := notification.NewNotificationSender(config.NotificationSocket) |
| 134 | if config.NotificationSocket != "" { |
| 135 | g.Go(func() error { |
| 136 | notificationSender.Start(ctx) |
| 137 | return nil |
| 138 | }) |
| 139 | } |
| 140 | vn.SetNotificationSender(notificationSender) |
| 141 | for _, endpoint := range config.Listen { |
| 142 | log.Infof("listening %s", endpoint) |
| 143 | ln, err := transport.Listen(endpoint) |
| 144 | if err != nil { |
| 145 | return fmt.Errorf("cannot listen: %w", err) |
| 146 | } |
| 147 | httpServe(ctx, g, ln, withProfiler(vn)) |
| 148 | } |
| 149 | notificationSender.Send(types.NotificationMessage{NotificationType: types.Ready}) |
| 150 | |
| 151 | if config.Services != "" { |
| 152 | log.Infof("enabling services API. Listening %s", config.Services) |
| 153 | ln, err := transport.Listen(config.Services) |
| 154 | if err != nil { |
| 155 | return fmt.Errorf("cannot listen: %w", err) |
| 156 | } |
| 157 | httpServe(ctx, g, ln, vn.ServicesMux()) |
| 158 | } |
| 159 | |
| 160 | ln, err := vn.Listen("tcp", fmt.Sprintf("%s:80", config.Stack.GatewayIP)) |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | mux := http.NewServeMux() |
| 165 | mux.Handle("/services/forwarder/all", vn.Mux()) |
| 166 | mux.Handle("/services/forwarder/expose", vn.Mux()) |
| 167 | mux.Handle("/services/forwarder/unexpose", vn.Mux()) |
| 168 | httpServe(ctx, g, ln, mux) |
| 169 | |
| 170 | if InDebugMode() { |
| 171 | g.Go(func() error { |
| 172 | debugLog: |
| 173 | for { |
| 174 | select { |
| 175 | case <-time.After(5 * time.Second): |
| 176 | log.Debugf("%s sent to the VM, %s received from the VM\n", humanize.Bytes(vn.BytesSent()), humanize.Bytes(vn.BytesReceived())) |
| 177 | case <-ctx.Done(): |
| 178 | break debugLog |
| 179 | } |
| 180 | } |
| 181 | return nil |
| 182 | }) |
| 183 | } |
| 184 | |
| 185 | if config.Interfaces.VPNKit != "" { |
| 186 | vpnkitListener, err := transport.Listen(config.Interfaces.VPNKit) |
| 187 | if err != nil { |
| 188 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 189 | return fmt.Errorf("vpnkit listen error: %w", err) |
| 190 | } |
| 191 | g.Go(func() error { |
| 192 | vpnloop: |
| 193 | for { |
| 194 | select { |
| 195 | case <-ctx.Done(): |
| 196 | break vpnloop |
| 197 | default: |
| 198 | |
| 199 | } |
| 200 | conn, err := vpnkitListener.Accept() |
| 201 | if err != nil { |
| 202 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 203 | log.Errorf("vpnkit accept error: %s", err) |
| 204 | continue |
| 205 | } |
| 206 | g.Go(func() error { |
| 207 | return vn.AcceptVpnKit(conn) |
| 208 | }) |
| 209 | } |
| 210 | return nil |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | if config.Interfaces.Qemu != "" { |
| 215 | qemuListener, err := transport.Listen(config.Interfaces.Qemu) |
| 216 | if err != nil { |
| 217 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 218 | return fmt.Errorf("qemu listen error: %w", err) |
| 219 | } |
| 220 | |
| 221 | g.Go(func() error { |
| 222 | <-ctx.Done() |
| 223 | if err := qemuListener.Close(); err != nil { |
| 224 | log.Errorf("error closing %s: %q", config.Interfaces.Qemu, err) |
| 225 | } |
| 226 | return os.Remove(config.Interfaces.Qemu) |
| 227 | }) |
| 228 | |
| 229 | g.Go(func() error { |
| 230 | conn, err := qemuListener.Accept() |
| 231 | if err != nil { |
| 232 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 233 | return fmt.Errorf("qemu accept error: %w", err) |
| 234 | } |
| 235 | return vn.AcceptQemu(ctx, conn) |
| 236 | }) |
| 237 | } |
| 238 | |
| 239 | if config.Interfaces.Bess != "" { |
| 240 | bessListener, err := transport.Listen(config.Interfaces.Bess) |
| 241 | if err != nil { |
| 242 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 243 | return fmt.Errorf("bess listen error: %w", err) |
| 244 | } |
| 245 | |
| 246 | g.Go(func() error { |
| 247 | <-ctx.Done() |
| 248 | if err := bessListener.Close(); err != nil { |
| 249 | log.Errorf("error closing %s: %q", config.Interfaces.Bess, err) |
| 250 | } |
| 251 | return os.Remove(config.Interfaces.Bess) |
| 252 | }) |
| 253 | |
| 254 | g.Go(func() error { |
| 255 | conn, err := bessListener.Accept() |
| 256 | if err != nil { |
| 257 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 258 | return fmt.Errorf("bess accept error: %w", err) |
| 259 | } |
| 260 | return vn.AcceptBess(ctx, conn) |
| 261 | }) |
| 262 | } |
| 263 | |
| 264 | if config.Interfaces.Vfkit != "" { |
| 265 | conn, err := transport.ListenUnixgram(config.Interfaces.Vfkit) |
| 266 | if err != nil { |
| 267 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 268 | return fmt.Errorf("vfkit listen error: %w", err) |
| 269 | } |
| 270 | |
| 271 | g.Go(func() error { |
| 272 | <-ctx.Done() |
| 273 | if err := conn.Close(); err != nil { |
| 274 | log.Errorf("error closing %s: %q", config.Interfaces.Vfkit, err) |
| 275 | } |
| 276 | vfkitSocketURI, _ := url.Parse(config.Interfaces.Vfkit) |
| 277 | return os.Remove(vfkitSocketURI.Path) |
| 278 | }) |
| 279 | |
| 280 | g.Go(func() error { |
| 281 | vfkitConn, err := transport.AcceptVfkit(conn) |
| 282 | if err != nil { |
| 283 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 284 | return fmt.Errorf("vfkit accept error: %w", err) |
| 285 | } |
| 286 | |
| 287 | return vn.AcceptVfkit(ctx, vfkitConn) |
| 288 | }) |
| 289 | } |
| 290 | |
| 291 | if config.Interfaces.Stdio != "" { |
| 292 | g.Go(func() error { |
| 293 | conn := stdio.GetStdioConn() |
| 294 | if err := vn.AcceptStdio(ctx, conn); err != nil { |
| 295 | notificationSender.Send(types.NotificationMessage{NotificationType: types.HypervisorError}) |
| 296 | return err |
| 297 | } |
| 298 | return nil |
| 299 | }) |
| 300 | } |
| 301 | |
| 302 | for i := range config.Forwards { |
| 303 | var ( |
| 304 | src *url.URL |
| 305 | err error |
| 306 | ) |
| 307 | if strings.Contains(config.Forwards[i].Socket, "://") { |
| 308 | src, err = url.Parse(config.Forwards[i].Socket) |
| 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | } else { |
| 313 | src = &url.URL{ |
| 314 | Scheme: "unix", |
| 315 | Path: config.Forwards[i].Socket, |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | dest := &url.URL{ |
| 320 | Scheme: "ssh", |
| 321 | User: url.User(config.Forwards[i].User), |
| 322 | Host: sshHostPort, |
| 323 | Path: config.Forwards[i].Dest, |
| 324 | } |
| 325 | j := i |
| 326 | g.Go(func() error { |
| 327 | defer os.Remove(config.Forwards[j].Socket) |
| 328 | forward, err := sshclient.CreateSSHForward(ctx, src, dest, config.Forwards[j].Identity, vn) |
| 329 | if err != nil { |
| 330 | return err |
| 331 | } |
| 332 | go func() { |
| 333 | <-ctx.Done() |
| 334 | |
| 335 | forward.Close() |
| 336 | }() |
| 337 | loop: |
| 338 | for { |
| 339 | select { |
| 340 | case <-ctx.Done(): |
| 341 | break loop |
| 342 | default: |
| 343 | |
| 344 | } |
| 345 | err := forward.AcceptAndTunnel(ctx) |
| 346 | if err != nil { |
| 347 | log.Debugf("Error occurred handling ssh forwarded connection: %q", err) |
| 348 | } |
| 349 | } |
| 350 | return nil |
| 351 | }) |
| 352 | } |
| 353 | |
| 354 | return nil |
| 355 | } |
| 356 | |
| 357 | func httpServe(ctx context.Context, g *errgroup.Group, ln net.Listener, mux http.Handler) { |
| 358 | g.Go(func() error { |
| 359 | <-ctx.Done() |
| 360 | return ln.Close() |
| 361 | }) |
| 362 | g.Go(func() error { |
| 363 | s := &http.Server{ |
| 364 | Handler: mux, |
| 365 | ReadTimeout: 10 * time.Second, |
| 366 | WriteTimeout: 10 * time.Second, |
| 367 | } |
| 368 | err := s.Serve(ln) |
| 369 | if err != nil { |
| 370 | if err != http.ErrServerClosed { |
| 371 | return err |
| 372 | } |
| 373 | return err |
| 374 | } |
| 375 | return nil |
| 376 | }) |
| 377 | } |
| 378 | |
| 379 | func withProfiler(vn *virtualnetwork.VirtualNetwork) http.Handler { |
| 380 | mux := vn.Mux() |
| 381 | if InDebugMode() { |
| 382 | mux.HandleFunc("/debug/pprof/", pprof.Index) |
| 383 | mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) |
| 384 | mux.HandleFunc("/debug/pprof/profile", pprof.Profile) |
| 385 | mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) |
| 386 | } |
| 387 | return mux |
| 388 | } |
| 389 | |
| 390 | func searchDomains() []string { |
| 391 | if runtime.GOOS == "darwin" || runtime.GOOS == "linux" { |
| 392 | f, err := os.Open("/etc/resolv.conf") |
| 393 | if err != nil { |
| 394 | log.Errorf("open file error: %v", err) |
| 395 | return nil |
| 396 | } |
| 397 | defer f.Close() |
| 398 | sc := bufio.NewScanner(f) |
| 399 | searchPrefix := "search " |
| 400 | for sc.Scan() { |
| 401 | if strings.HasPrefix(sc.Text(), searchPrefix) { |
| 402 | return parseSearchString(sc.Text(), searchPrefix) |
| 403 | } |
| 404 | } |
| 405 | if err := sc.Err(); err != nil { |
| 406 | log.Errorf("scan file error: %v", err) |
| 407 | return nil |
| 408 | } |
| 409 | } |
| 410 | return nil |
| 411 | } |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | func parseSearchString(text, searchPrefix string) []string { |
| 417 | |
| 418 | if runtime.GOOS == "darwin" && len(text) > 256 { |
| 419 | log.Errorf("Search domains list is too long, it should not exceed 256 chars on macOS: %d", len(text)) |
| 420 | text = text[:256] |
| 421 | lastSpace := strings.LastIndex(text, " ") |
| 422 | if lastSpace != -1 { |
| 423 | text = text[:lastSpace] |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | searchDomains := strings.Split(strings.TrimPrefix(text, searchPrefix), " ") |
| 428 | log.Debugf("Using search domains: %v", searchDomains) |
| 429 | |
| 430 | |
| 431 | if runtime.GOOS == "darwin" && len(searchDomains) > 6 { |
| 432 | log.Errorf("Search domains list is too long, it should not exceed 6 domains on macOS: %d", len(searchDomains)) |
| 433 | searchDomains = searchDomains[:6] |
| 434 | } |
| 435 | |
| 436 | return searchDomains |
| 437 | } |