Add files via upload

This commit is contained in:
Evan Su 2021-06-03 15:56:34 -04:00 committed by GitHub
parent 971e6d22e3
commit ced8b53fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 234 additions and 0 deletions

63
src/external/browser/browser.go vendored Normal file
View File

@ -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()
}

View File

@ -0,0 +1,9 @@
package browser
import "os/exec"
func openBrowser(url string) error {
return runCmd("open", url)
}
func setFlags(cmd *exec.Cmd) {}

16
src/external/browser/browser_freebsd.go vendored Normal file
View File

@ -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) {}

23
src/external/browser/browser_linux.go vendored Normal file
View File

@ -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) {}

16
src/external/browser/browser_openbsd.go vendored Normal file
View File

@ -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) {}

View File

@ -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) {}

13
src/external/browser/browser_windows.go vendored Normal file
View File

@ -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) {
}

3
src/external/browser/go.mod vendored Normal file
View File

@ -0,0 +1,3 @@
module github.com/HACKERALERT/Picocrypt/src/external/browser
go 1.14

View File

@ -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
}