67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
// Command open-proxy forwards `open`/`xdg-open` requests from a remote VM to
|
|
// the host machine that actually has the browser/GUI.
|
|
//
|
|
// On the host: open-proxy serve
|
|
// On the VM: open-proxy open <file|url|string>...
|
|
//
|
|
// The VM reaches the host's server over an SSH reverse tunnel, e.g.
|
|
//
|
|
// ssh -R 7777:127.0.0.1:7777 vm
|
|
//
|
|
// so the VM's 127.0.0.1:7777 maps to the host's server.
|
|
//
|
|
// As a convenience, when the binary is invoked under the name "open" (e.g. via
|
|
// a symlink early on $PATH), it behaves exactly like `open-proxy open`.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func main() {
|
|
// Shadowed-name mode: when symlinked to `open` or `xdg-open`, act as the
|
|
// client and fall back to that same real opener if the host is unreachable.
|
|
if base := filepath.Base(os.Args[0]); base == "open" || base == "xdg-open" {
|
|
os.Exit(runClient(os.Args[1:], base))
|
|
}
|
|
|
|
if len(os.Args) < 2 {
|
|
usage()
|
|
os.Exit(2)
|
|
}
|
|
|
|
switch os.Args[1] {
|
|
case "serve":
|
|
os.Exit(runServer(os.Args[2:]))
|
|
case "open":
|
|
os.Exit(runClient(os.Args[2:], defaultOpenerName()))
|
|
case "-h", "--help", "help":
|
|
usage()
|
|
os.Exit(0)
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "open-proxy: unknown command %q\n\n", os.Args[1])
|
|
usage()
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
|
|
func usage() {
|
|
fmt.Fprint(os.Stderr, `open-proxy - forward `+"`open`"+` from a remote VM to the host
|
|
|
|
Usage:
|
|
open-proxy serve [-addr 127.0.0.1:7777]
|
|
open-proxy open <file|url|string>...
|
|
|
|
Environment:
|
|
OPEN_PROXY_ADDR client target / server listen address (default 127.0.0.1:7777)
|
|
OPEN_PROXY_TOKEN shared secret; if set on the server, clients must match
|
|
OPEN_PROXY_TOKEN_FILE file containing the shared secret, used when OPEN_PROXY_TOKEN is unset
|
|
OPEN_PROXY_MAXSIZE max file transfer size in bytes (default 104857600)
|
|
|
|
Tip: symlink the binary to "open" and/or "xdg-open" early on the VM's $PATH.
|
|
When the host is unreachable, it falls back to the real opener on $PATH.
|
|
`)
|
|
}
|