using InnovEnergy.SysTools.Edges; using InnovEnergy.SysTools.Utils; namespace InnovEnergy.SysTools.Remote; public readonly struct SshHost { public const Int32 DefaultPort = 22; public String User { get; } public String Address { get; } public Int32 Port { get; } public SysPath? KeyFile { get; } public SshHost(String user, String address, Int32 port = DefaultPort) { User = user; Address = address; KeyFile = null; Port = port; } public SshHost(String user, String address, SysPath? keyFile, Int32 port = DefaultPort) { User = user; Address = address; KeyFile = keyFile; Port = port; } public static SshHost Parse(String userAtAddressColonPort) { var addressPort = userAtAddressColonPort.AfterFirst('@').Split(':'); var user = userAtAddressColonPort.UntilFirst('@'); var address = addressPort[0]; var port = addressPort.Length < 2 ? DefaultPort : Int32.Parse(addressPort[1]); return new SshHost(user, address, null, port); } public static SshHost Parse(String userAtAddressColonPort, SysPath keyFile) { var addressPort = userAtAddressColonPort.AfterFirst('@').Split(':'); var user = userAtAddressColonPort.UntilFirst('@'); var address = addressPort[0]; var port = addressPort.Length < 2 ? DefaultPort : Int32.Parse(addressPort[1]); return new SshHost(user, address, keyFile, port); } public static SshHost Localhost { get; } = new SshHost(Environment.UserName, "127.0.0.1"); public Boolean IsLocalhost => Equals(Localhost); public override String ToString() => $"{User}@{Address}"; #region equality public Boolean Equals(SshHost other) { return User == other.User && Address == other.Address && Port == other.Port && Nullable.Equals(KeyFile, other.KeyFile); } public override Boolean Equals(Object obj) { return obj is SshHost other && Equals(other); } public override Int32 GetHashCode() { unchecked { var hashCode = (User != null ? User.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Address != null ? Address.GetHashCode() : 0); hashCode = (hashCode * 397) ^ Port; hashCode = (hashCode * 397) ^ KeyFile.GetHashCode(); return hashCode; } } #endregion public static RemotePath operator / (SshHost host, SysPath path) => host.WithPath(path); public static RemotePath operator / (SshHost host, String path) { return host.WithPath(path.ToPath().ToAbsolute()); } public void Deconstruct(out String user, out String address, out SysPath? keyFile) { user = User; address = Address; keyFile = KeyFile; } }