hooks

package
v0.0.0-...-fa0bf4e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FileHook = HookComponents{
	func(path string, _ tftp.Request) (*HookResult, error) {
		file, err := os.Open(path)
		if err != nil {
			return nil, err
		}

		stat, err := file.Stat()
		if err != nil {
			return nil, err
		}
		return newHookResult(file, nil, int(stat.Size()), nil), nil
	},
	func(s string, _ config.HookExtraArgs) (string, error) {
		return pathEscape.ReplaceAllLiteralString(s, ""), nil
	},
}
View Source
var HTTPHook = HookComponents{
	func(url string, tftpReq tftp.Request) (*HookResult, error) {
		client := &http.Client{
			CheckRedirect: func(req *http.Request, via []*http.Request) error {
				if len(via) == 0 {
					return nil
				}

				for key, val := range via[0].Header {
					if key != "Referer" {
						req.Header[key] = val
					}
				}

				return nil
			},
		}

		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			return nil, err
		}

		req.Header.Set("User-Agent", "hooktftp v0.9.1")
		req.Header.Set("X-Forwarded-For", (*tftpReq.Addr).String())

		res, err := client.Do(req)
		if err != nil {
			return nil, err
		}

		if res.StatusCode != http.StatusOK {
			res.Body.Close()
			return nil, fmt.Errorf("Bad response '%v' from %v", res.Status, url)
		}

		return newHookResult(res.Body, nil, int(res.ContentLength), nil), nil

	},
	func(s string, extra config.HookExtraArgs) (string, error) {
		shouldUrlDecode := extra["urldecode"].(bool)

		if !shouldUrlDecode {
			return s, nil
		}

		unescaped, err := url.PathUnescape(s)
		if err != nil {
			return "", err
		}
		return unescaped, nil
	},
}
View Source
var ShellHook = HookComponents{
	func(command string, request tftp.Request) (*HookResult, error) {

		if len(command) == 0 {
			return nil, errors.New("Empty shell command")
		}

		split, err := shlex.Split(command)
		if err != nil {
			return nil, err
		}

		cmd := exec.Command(split[0], split[1:]...)

		stdout, err := cmd.StdoutPipe()
		if err != nil {
			return nil, err
		}

		stderr, err := cmd.StderrPipe()
		if err != nil {
			return nil, err
		}

		env := os.Environ()
		env = append(env, fmt.Sprintf("CLIENT_ADDR=%s", (*request.Addr).String()))
		cmd.Env = env

		err = cmd.Start()

		return newHookResult(stdout, stderr, -1, cmd.Wait), err
	},
	func(s string, _ config.HookExtraArgs) (string, error) {
		return shellEscape.ReplaceAllStringFunc(s, func(s string) string {
			return "\\" + s
		}), nil
	},
}

Functions

This section is empty.

Types

type Hook

type Hook func(path string, request tftp.Request) (*HookResult, error)

func CompileHook

func CompileHook(hookDef iHookDef) (Hook, error)

type HookComponents

type HookComponents struct {
	Execute Hook
	// contains filtered or unexported fields
}

type HookFinalizer

type HookFinalizer func() error

type HookResult

type HookResult struct {
	Stdout   io.ReadCloser
	Stderr   io.ReadCloser
	Length   int
	Finalize HookFinalizer
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL