package handlers

import (
	
	
	
	
	
)

// CommandRunner runs command on specified server
func ( *fiber.Ctx) error {
	if len(.FormValue("server_id")) < 1 {
		return logger.FiberError(fiber.StatusBadRequest, "server not found")
	}

	,  := liman.GetServer(&models.Server{ID: .FormValue("server_id")})
	if  != nil {
		return 
	}

	,  := bridge.GetSession(
		.Locals("user_id").(string),
		.ID,
		.IPAddress,
	)
	if  != nil {
		return 
	}

	,  := .Run(.FormValue("command"))
	if  != nil {
		return logger.FiberError(fiber.StatusForbidden, "cannot run command")
	}

	return .SendString()
}

// OutsideCommandRunner runs command on external endpoints
func ( *fiber.Ctx) error {
	 := []string{
		"command",
		"connection_type",
		"remote_host",
		"remote_port",
		"username",
		"password",
		"disconnect",
	}

	for ,  := range  {
		if len(.FormValue()) < 1 {
			return logger.FiberError(fiber.StatusBadRequest, +" parameter is missing")
		}
	}

	,  := bridge.Sessions.GetRaw(
		.Locals("user_id").(string),
		.FormValue("remote_host"),
		.FormValue("username"),
	)
	if  != nil {
		 = &bridge.Session{}
		 := .CreateRaw(
			.FormValue("connection_type"),
			.FormValue("username"),
			.FormValue("password"),
			.FormValue("remote_host"),
			.FormValue("remote_port"),
		)

		if  {
			bridge.Sessions.SetRaw(
				.Locals("user_id").(string),
				.FormValue("remote_host"),
				.FormValue("username"),
				,
			)
		} else {
			return logger.FiberError(fiber.StatusInternalServerError, "cannot create connection")
		}
	}

	,  := .Run(.FormValue("command"))
	if  != nil {
		return logger.FiberError(fiber.StatusForbidden, "cannot run command")
	}

	if .FormValue("disconnect") == "1" {
		.CloseAllConnections()
		bridge.Sessions.Delete(.Locals("user_id").(string) + .FormValue("remote_host") + .FormValue("username"))
	}

	return .SendString()
}