func (h *handler) handleDownloadTarFunc(c echo.Context) error {
	pathToSend := c.QueryParam("path")
	fileFilter := c.QueryParam("file-filter")
	if strings.TrimSpace(pathToSend) == "" {
		return fmt.Errorf("Request does not contain query 'path' value")
	}

	var tarProvider tar_io.TarProvider
	if isDir, err := path_utils.DirectoryExists(pathToSend); err != nil {
		return fmt.Errorf("Unable to determine if path '%s' is a directory, error: %s", pathToSend, err.Error())
	} else if isDir {
		tarProvider = tar_io.Factories.TarProvider.Dir(pathToSend, fileFilter)
		h.logger.Infof("Now starting to send dir '%s'", pathToSend)
	} else if isFile, err := path_utils.FileExists(pathToSend); err != nil {
		return fmt.Errorf("Unable to determine if path '%s' is a file, error: %s", pathToSend, err.Error())
	} else if isFile {
		tarProvider = tar_io.Factories.TarProvider.File(pathToSend)
		h.logger.Infof("Now starting to send file '%s'", pathToSend)
	} else {
		return fmt.Errorf("Path '%s' is not an existing file or directory", pathToSend)
	}

	handler := &sendTarHandler{writer: c.Response()}
	err := tar_io.UploadProvider(tarProvider, handler)
	if err != nil {
		return fmt.Errorf("Unable to send file, error: %s", err.Error())
	}

	return nil
}
Example #2
0
func assertFileExistance(actual interface{}, expected ...interface{}) string {
	if fail := need(1, expected); fail != success {
		return fail
	}

	filePath, filePathIsString := actual.(string)
	mustExist, mustExistIsBool := expected[0].(bool)

	if !filePathIsString {
		return fmt.Sprintf(shouldBeString, reflect.TypeOf(actual))
	}

	if !mustExistIsBool {
		return fmt.Sprintf(shouldBeBool, reflect.TypeOf(expected[0]))
	}

	if fileExist, err := path_utils.FileExists(filePath); err != nil {
		return err.Error()
	} else if fileExist != mustExist {
		if mustExist {
			return fmt.Sprintf(shouldHaveBeenTrue, fileExist)
		} else {
			return fmt.Sprintf(shouldHaveBeenFalse, fileExist)
		}
	}

	return success
}
Example #3
0
func (h *handler) handleStartFunc(c echo.Context) error {
	req := c.Request()
	resp := c.Response()

	dto := &dtos.ExecDto{}
	err := h.getDto(c, dto)
	if err != nil {
		return err
	}

	if exeExists, err := path_utils.FileExists(dto.Exe); err != nil {
		return fmt.Errorf("Unable to check if Exe path '%s' exists, error: %s", dto.Exe, err.Error())
	} else if !exeExists {
		return c.String(http.StatusBadRequest, fmt.Sprintf("Exe path '%s' does not exist. Please specify the full Exe path to run.", dto.Exe))
	}

	ip := getIPFromRequest(req)
	hostNames, err := getHostNamesFromIP(ip)
	if err != nil {
		h.logger.Warningf("Unable to find hostname(s) for IP '%s', error: %s", ip, err.Error())
	}

	h.logger.Infof(
		"Starting command (remote ip %s, hostnames = %+v), exe = '%s', args = '%#v' (working dir '%s')",
		ip, hostNames, dto.Exe, dto.Args, dto.WorkingDir)

	executor, err := execstreamer.NewExecutorFromName(dto.Executor)
	if err != nil {
		return err
	}

	cmd := executor.GetCommand(dto.Exe, dto.Args...)
	cmd.Dir = dto.WorkingDir

	err = cmd.Start()
	if err != nil {
		return err
	}

	resp.Header().Set(shared.PROCESS_ID_HTTP_HEADER_NAME, fmt.Sprintf("%d", cmd.Process.Pid))
	return c.String(200, "The command was successfully started. Pid in header: "+shared.PROCESS_ID_HTTP_HEADER_NAME)
}