From ced8b53fe3d541f7405e6f8b8c63547c37dab80f Mon Sep 17 00:00:00 2001 From: Evan Su <48808396+HACKERALERT@users.noreply.github.com> Date: Thu, 3 Jun 2021 15:56:34 -0400 Subject: [PATCH] Add files via upload --- src/external/browser/browser.go | 63 +++++++++++++++++ src/external/browser/browser_darwin.go | 9 +++ src/external/browser/browser_freebsd.go | 16 +++++ src/external/browser/browser_linux.go | 23 +++++++ src/external/browser/browser_openbsd.go | 16 +++++ src/external/browser/browser_unsupported.go | 15 ++++ src/external/browser/browser_windows.go | 13 ++++ src/external/browser/go.mod | 3 + src/external/browser/zbrowser_windows.go | 76 +++++++++++++++++++++ 9 files changed, 234 insertions(+) create mode 100644 src/external/browser/browser.go create mode 100644 src/external/browser/browser_darwin.go create mode 100644 src/external/browser/browser_freebsd.go create mode 100644 src/external/browser/browser_linux.go create mode 100644 src/external/browser/browser_openbsd.go create mode 100644 src/external/browser/browser_unsupported.go create mode 100644 src/external/browser/browser_windows.go create mode 100644 src/external/browser/go.mod create mode 100644 src/external/browser/zbrowser_windows.go diff --git a/src/external/browser/browser.go b/src/external/browser/browser.go new file mode 100644 index 0000000..3e59690 --- /dev/null +++ b/src/external/browser/browser.go @@ -0,0 +1,63 @@ +// Package browser provides helpers to open files, readers, and urls in a browser window. +// +// The choice of which browser is started is entirely client dependant. +package browser + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "os/exec" + "path/filepath" +) + +// Stdout is the io.Writer to which executed commands write standard output. +var Stdout io.Writer = os.Stdout + +// Stderr is the io.Writer to which executed commands write standard error. +var Stderr io.Writer = os.Stderr + +// OpenFile opens new browser window for the file path. +func OpenFile(path string) error { + path, err := filepath.Abs(path) + if err != nil { + return err + } + return OpenURL("file://" + path) +} + +// OpenReader consumes the contents of r and presents the +// results in a new browser window. +func OpenReader(r io.Reader) error { + f, err := ioutil.TempFile("", "browser") + if err != nil { + return fmt.Errorf("browser: could not create temporary file: %v", err) + } + if _, err := io.Copy(f, r); err != nil { + f.Close() + return fmt.Errorf("browser: caching temporary file failed: %v", err) + } + if err := f.Close(); err != nil { + return fmt.Errorf("browser: caching temporary file failed: %v", err) + } + oldname := f.Name() + newname := oldname + ".html" + if err := os.Rename(oldname, newname); err != nil { + return fmt.Errorf("browser: renaming temporary file failed: %v", err) + } + return OpenFile(newname) +} + +// OpenURL opens a new browser window pointing to url. +func OpenURL(url string) error { + return openBrowser(url) +} + +func runCmd(prog string, args ...string) error { + cmd := exec.Command(prog, args...) + cmd.Stdout = Stdout + cmd.Stderr = Stderr + setFlags(cmd) + return cmd.Run() +} diff --git a/src/external/browser/browser_darwin.go b/src/external/browser/browser_darwin.go new file mode 100644 index 0000000..6dff040 --- /dev/null +++ b/src/external/browser/browser_darwin.go @@ -0,0 +1,9 @@ +package browser + +import "os/exec" + +func openBrowser(url string) error { + return runCmd("open", url) +} + +func setFlags(cmd *exec.Cmd) {} diff --git a/src/external/browser/browser_freebsd.go b/src/external/browser/browser_freebsd.go new file mode 100644 index 0000000..8cc0a7f --- /dev/null +++ b/src/external/browser/browser_freebsd.go @@ -0,0 +1,16 @@ +package browser + +import ( + "errors" + "os/exec" +) + +func openBrowser(url string) error { + err := runCmd("xdg-open", url) + if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound { + return errors.New("xdg-open: command not found - install xdg-utils from ports(8)") + } + return err +} + +func setFlags(cmd *exec.Cmd) {} diff --git a/src/external/browser/browser_linux.go b/src/external/browser/browser_linux.go new file mode 100644 index 0000000..ab9b4f6 --- /dev/null +++ b/src/external/browser/browser_linux.go @@ -0,0 +1,23 @@ +package browser + +import ( + "os/exec" + "strings" +) + +func openBrowser(url string) error { + providers := []string{"xdg-open", "x-www-browser", "www-browser"} + + // There are multiple possible providers to open a browser on linux + // One of them is xdg-open, another is x-www-browser, then there's www-browser, etc. + // Look for one that exists and run it + for _, provider := range providers { + if _, err := exec.LookPath(provider); err == nil { + return runCmd(provider, url) + } + } + + return &exec.Error{Name: strings.Join(providers, ","), Err: exec.ErrNotFound} +} + +func setFlags(cmd *exec.Cmd) {} diff --git a/src/external/browser/browser_openbsd.go b/src/external/browser/browser_openbsd.go new file mode 100644 index 0000000..8cc0a7f --- /dev/null +++ b/src/external/browser/browser_openbsd.go @@ -0,0 +1,16 @@ +package browser + +import ( + "errors" + "os/exec" +) + +func openBrowser(url string) error { + err := runCmd("xdg-open", url) + if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound { + return errors.New("xdg-open: command not found - install xdg-utils from ports(8)") + } + return err +} + +func setFlags(cmd *exec.Cmd) {} diff --git a/src/external/browser/browser_unsupported.go b/src/external/browser/browser_unsupported.go new file mode 100644 index 0000000..5eb17b0 --- /dev/null +++ b/src/external/browser/browser_unsupported.go @@ -0,0 +1,15 @@ +// +build !linux,!windows,!darwin,!openbsd,!freebsd + +package browser + +import ( + "fmt" + "os/exec" + "runtime" +) + +func openBrowser(url string) error { + return fmt.Errorf("openBrowser: unsupported operating system: %v", runtime.GOOS) +} + +func setFlags(cmd *exec.Cmd) {} diff --git a/src/external/browser/browser_windows.go b/src/external/browser/browser_windows.go new file mode 100644 index 0000000..a2b30d3 --- /dev/null +++ b/src/external/browser/browser_windows.go @@ -0,0 +1,13 @@ +//go:generate mkwinsyscall -output zbrowser_windows.go browser_windows.go +//sys ShellExecute(hwnd int, verb string, file string, args string, cwd string, showCmd int) (err error) = shell32.ShellExecuteW +package browser + +import "os/exec" +const SW_SHOWNORMAL = 1 + +func openBrowser(url string) error { + return ShellExecute(0, "", url, "", "", SW_SHOWNORMAL) +} + +func setFlags(cmd *exec.Cmd) { +} diff --git a/src/external/browser/go.mod b/src/external/browser/go.mod new file mode 100644 index 0000000..69290c1 --- /dev/null +++ b/src/external/browser/go.mod @@ -0,0 +1,3 @@ +module github.com/HACKERALERT/Picocrypt/src/external/browser + +go 1.14 diff --git a/src/external/browser/zbrowser_windows.go b/src/external/browser/zbrowser_windows.go new file mode 100644 index 0000000..e1b1d47 --- /dev/null +++ b/src/external/browser/zbrowser_windows.go @@ -0,0 +1,76 @@ +// Code generated by 'go generate'; DO NOT EDIT. + +package browser + +import ( + "syscall" + "unsafe" + + "github.com/HACKERALERT/Picocrypt/src/external/sys/windows" +) + +var _ unsafe.Pointer + +// Do the interface allocations only once for common +// Errno values. +const ( + errnoERROR_IO_PENDING = 997 +) + +var ( + errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING) + errERROR_EINVAL error = syscall.EINVAL +) + +// errnoErr returns common boxed Errno values, to prevent +// allocations at runtime. +func errnoErr(e syscall.Errno) error { + switch e { + case 0: + return errERROR_EINVAL + case errnoERROR_IO_PENDING: + return errERROR_IO_PENDING + } + // TODO: add more here, after collecting data on the common + // error values see on Windows. (perhaps when running + // all.bat?) + return e +} + +var ( + modshell32 = windows.NewLazySystemDLL("shell32.dll") + + procShellExecuteW = modshell32.NewProc("ShellExecuteW") +) + +func ShellExecute(hwnd int, verb string, file string, args string, cwd string, showCmd int) (err error) { + var _p0 *uint16 + _p0, err = syscall.UTF16PtrFromString(verb) + if err != nil { + return + } + var _p1 *uint16 + _p1, err = syscall.UTF16PtrFromString(file) + if err != nil { + return + } + var _p2 *uint16 + _p2, err = syscall.UTF16PtrFromString(args) + if err != nil { + return + } + var _p3 *uint16 + _p3, err = syscall.UTF16PtrFromString(cwd) + if err != nil { + return + } + return _ShellExecute(hwnd, _p0, _p1, _p2, _p3, showCmd) +} + +func _ShellExecute(hwnd int, verb *uint16, file *uint16, args *uint16, cwd *uint16, showCmd int) (err error) { + r1, _, e1 := syscall.Syscall6(procShellExecuteW.Addr(), 6, uintptr(hwnd), uintptr(unsafe.Pointer(verb)), uintptr(unsafe.Pointer(file)), uintptr(unsafe.Pointer(args)), uintptr(unsafe.Pointer(cwd)), uintptr(showCmd)) + if r1 == 0 { + err = errnoErr(e1) + } + return +}