using System;
using System.Net;
using System.Security;
namespace PSProxmox.Session
{
///
/// Represents a connection to a Proxmox VE server.
///
public class ProxmoxConnection
{
///
/// Gets the server hostname or IP address.
///
public string Server { get; private set; }
///
/// Gets the port number used for the connection.
///
public int Port { get; private set; }
///
/// Gets a value indicating whether the connection uses HTTPS.
///
public bool UseSSL { get; private set; }
///
/// Gets a value indicating whether to skip SSL certificate validation.
///
public bool SkipCertificateValidation { get; private set; }
///
/// Gets the authentication ticket for the Proxmox API.
///
public string Ticket { get; internal set; }
///
/// Gets the CSRF prevention token for the Proxmox API.
///
public string CSRFPreventionToken { get; internal set; }
///
/// Gets the username used for authentication.
///
public string Username { get; private set; }
///
/// Gets the realm used for authentication.
///
public string Realm { get; private set; }
///
/// Gets a value indicating whether the connection is authenticated.
///
public bool IsAuthenticated => !string.IsNullOrEmpty(Ticket) && !string.IsNullOrEmpty(CSRFPreventionToken);
///
/// Gets the base URL for the Proxmox API.
///
public string ApiUrl => $"{(UseSSL ? "https" : "http")}://{Server}:{Port}/api2/json";
///
/// Initializes a new instance of the class.
///
/// The server hostname or IP address.
/// The port number.
/// Whether to use HTTPS.
/// Whether to skip SSL certificate validation.
/// The username for authentication.
/// The realm for authentication.
public ProxmoxConnection(string server, int port = 8006, bool useSSL = true, bool skipCertificateValidation = false, string username = null, string realm = "pam")
{
Server = server ?? throw new ArgumentNullException(nameof(server));
Port = port;
UseSSL = useSSL;
SkipCertificateValidation = skipCertificateValidation;
Username = username;
Realm = realm;
}
///
/// Creates a copy of the connection with updated authentication information.
///
/// The authentication ticket.
/// The CSRF prevention token.
/// A new connection object with updated authentication information.
internal ProxmoxConnection WithAuthentication(string ticket, string csrfPreventionToken)
{
var connection = new ProxmoxConnection(Server, Port, UseSSL, SkipCertificateValidation, Username, Realm)
{
Ticket = ticket,
CSRFPreventionToken = csrfPreventionToken
};
return connection;
}
}
}