package bridge

import (
	
	

	
	
	
	
)

// GetSession gets session from memory and if does not exists creates a new one
func (, ,  string) (*Session, error) {
	,  := Sessions.Get(, )
	if  != nil {
		 = &Session{}
		 := .CreateShell(, , )
		if ! {
			return nil, logger.FiberError(fiber.StatusForbidden, "cannot connect to server")
		}
		Sessions.Set(, , )
	}

	.LastConnection = time.Now()
	return , nil
}

// CreateShell creates a new shell with credential type and sets it on session
func ( *Session) (, ,  string) bool {
	,  := liman.GetCredentials(&models.User{ID: }, &models.Server{ID: })
	if  != nil {
		return false
	}

	.Username = .Username
	.password = .Key
	.IpAddr = 
	.Port = .Port
	.LastConnection = time.Now()

	if .Type == "ssh" {
		,  := InitShellWithPassword(.Username, .password, .IpAddr, .Port)
		if  != nil {
			return false
		}
		.SSH = 
	} else if .Type == "ssh_certificate" {
		,  := InitShellWithCert(.Username, .password, .IpAddr, .Port)
		if  != nil {
			return false
		}
		.SSH = 
	} else if strings.Contains(.Type, "winrm") {
		 := true
		if strings.Contains(.Type, "insecure") {
			 = false
		}

		,  := InitWinRm(.Username, .password, .IpAddr, .Port, )
		if  != nil {
			return false
		}
		.WinRM = 

		,  := .Run("echo $env:TEMP")
		if  != nil {
			return false
		}

		.WindowsPath = strings.TrimSpace() + "\\"
		.WindowsLetter = .WindowsPath[0:1]
		.WindowsPath = .WindowsPath[3:]
	} else {
		return false
	}

	return true
}

// CreateRaw creates a new raw shell with credential type and sets it on session
func ( *Session) (, , , ,  string) bool {
	.Username = 
	.password = 
	.IpAddr = 
	.Port = 
	.LastConnection = time.Now()

	if  == "ssh" {
		,  := InitShellWithPassword(.Username, .password, .IpAddr, .Port)
		if  != nil {
			return false
		}
		.SSH = 
	} else if  == "ssh_certificate" {
		,  := InitShellWithCert(.Username, .password, .IpAddr, .Port)
		if  != nil {
			return false
		}
		.SSH = 
	} else if strings.Contains(, "winrm") {
		 := true
		if strings.Contains(, "insecure") {
			 = false
		}

		,  := InitWinRm(.Username, .password, .IpAddr, .Port, )
		if  != nil {
			return false
		}
		.WinRM = 

		,  := .Run("echo $env:TEMP")
		if  != nil {
			return false
		}

		.WindowsPath = strings.TrimSpace() + "\\"
		.WindowsLetter = .WindowsPath[0:1]
		.WindowsPath = .WindowsPath[3:]
	} else {
		return false
	}

	return true
}

// CreateFileSesssion creates a new file session with credential type and sets it on session
func ( *Session) (, ,  string) bool {
	,  := liman.GetCredentials(&models.User{ID: }, &models.Server{ID: })
	if  != nil {
		return false
	}

	.Username = .Username
	.password = .Key
	.IpAddr = 
	.Port = .Port
	.LastConnection = time.Now()

	if .Type == "ssh" || .Type == "ssh_certificate" {
		if .SFTP != nil {
			return true
		}

		 := .CreateShell(, , )
		if ! {
			return false
		}
		.SFTP = OpenSFTPConnection(.SSH)
	} else if strings.Contains(.Type, "winrm") {
		if .SMB != nil {
			return true
		}

		,  := OpenSmbConnection(.IpAddr, .Username, .password)
		if  != nil {
			return false
		}

		.SMB = 
	} else {
		return false
	}

	return true
}