forked from microsoft/terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamically generate profile with .ssh/config
This PR looks for the presense of OpenSSH config files and creates profiles from the hosts present. CLoses microsoft#9031
- Loading branch information
1 parent
c51bb3a
commit cfc66e9
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#include "pch.h" | ||
|
||
#include "SshHostGenerator.h" | ||
#include "../../inc/DefaultSettings.h" | ||
|
||
#include "DynamicProfileUtils.h" | ||
|
||
static constexpr std::wstring_view SshHostGeneratorNamespace{ L"Windows.Terminal.SSH" }; | ||
|
||
static constexpr std::wstring_view SSH_TITLE_FMT{ L"SSH - {}" }; | ||
|
||
static constexpr std::wstring_view SSH_COMMANDLINE_FMT{ L"ssh.exe {}" }; | ||
static constexpr const wchar_t* SSH_SYSTEMCONFIG_PATH = L"%ProgramData%\\ssh\\ssh_config"; | ||
static constexpr const wchar_t* SSH_USERCONFIG_PATH = L"%UserProfile%\\.ssh\\config"; | ||
|
||
static constexpr std::wstring_view SSH_HOST_PREFIX{ L"Host " }; | ||
|
||
using namespace ::Microsoft::Terminal::Settings::Model; | ||
using namespace winrt::Microsoft::Terminal::Settings::Model; | ||
|
||
std::wstring_view SshHostGenerator::GetNamespace() const noexcept | ||
{ | ||
return SshHostGeneratorNamespace; | ||
} | ||
|
||
static winrt::com_ptr<implementation::Profile> _makeProfile(const std::wstring& hostName) | ||
{ | ||
const auto title = std::format(SSH_TITLE_FMT, hostName); | ||
|
||
const auto profile{ CreateDynamicProfile(title) }; | ||
|
||
std::wstring quotedCommandline = std::format(SSH_COMMANDLINE_FMT, hostName); | ||
profile->Commandline(winrt::hstring{ quotedCommandline }); | ||
|
||
return profile; | ||
} | ||
|
||
static void _tryGetHostNamesFromConfigFile(const std::wstring configPath, std::vector<std::wstring>& hostNames) | ||
{ | ||
std::filesystem::path resolvedConfigPath{ wil::ExpandEnvironmentStringsW<std::wstring>(configPath.c_str()) }; | ||
if (std::filesystem::exists(resolvedConfigPath) && std::filesystem::is_regular_file(resolvedConfigPath)) | ||
{ | ||
std::wifstream inputStream(resolvedConfigPath); | ||
|
||
std::wstring line; | ||
while (std::getline(inputStream, line)) | ||
{ | ||
if (line.starts_with(SSH_HOST_PREFIX)) | ||
{ | ||
hostNames.emplace_back(line.substr(SSH_HOST_PREFIX.length())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Method Description: | ||
// - Generate a list of profiles for each detected OpenSSH host. | ||
// Arguments: | ||
// - <none> | ||
// Return Value: | ||
// - <A list of SSH host profiles.> | ||
void SshHostGenerator::GenerateProfiles(std::vector<winrt::com_ptr<implementation::Profile>>& profiles) const | ||
{ | ||
try | ||
{ | ||
std::vector<std::wstring> hostNames; | ||
|
||
_tryGetHostNamesFromConfigFile(SSH_SYSTEMCONFIG_PATH, hostNames); | ||
_tryGetHostNamesFromConfigFile(SSH_USERCONFIG_PATH, hostNames); | ||
|
||
for (const auto& hostName : hostNames) | ||
{ | ||
profiles.emplace_back(_makeProfile(hostName)); | ||
} | ||
} | ||
CATCH_LOG(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*++ | ||
Copyright (c) Microsoft Corporation | ||
Licensed under the MIT license. | ||
Module Name: | ||
- SshHostGenerator | ||
Abstract: | ||
- This is the dynamic profile generator for SSH connections. Enumerates all the | ||
SSH hosts to create profiles for them. | ||
Author(s): | ||
- Jon Thysell - September 2022 | ||
--*/ | ||
|
||
#pragma once | ||
|
||
#include "IDynamicProfileGenerator.h" | ||
|
||
namespace winrt::Microsoft::Terminal::Settings::Model | ||
{ | ||
class SshHostGenerator final : public IDynamicProfileGenerator | ||
{ | ||
public: | ||
std::wstring_view GetNamespace() const noexcept override; | ||
void GenerateProfiles(std::vector<winrt::com_ptr<implementation::Profile>>& profiles) const override; | ||
}; | ||
}; |