-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWinSCPService.cs
223 lines (204 loc) · 7.02 KB
/
WinSCPService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Microsoft.Win32;
namespace Flow.Launcher.Plugin.WinSCP
{
internal class SessionEntry
{
/// <summary>
/// The identifier with path
/// </summary>
public string Identifier { get; set; }
/// <summary>
/// Decoded title from indentifier
/// </summary>
public string Title { get; set; }
/// <summary>
/// The string version of protocol (scp, sftp, s3, etc.)
/// </summary>
public string Protocol { get; set; }
/// <summary>
/// The optional Username
/// </summary>
public string Username { get; set; }
/// <summary>
/// The Hostname
/// </summary>
public string Hostname { get; set; }
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
public override string ToString()
{
if (string.IsNullOrEmpty(Username))
{
return string.Format("{0}://{1}", Protocol, Hostname);
}
return string.Format("{0}://{1}@{2}", Protocol, Username, Hostname);
}
}
internal class WinSCPService
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private readonly PluginInitContext _context;
private readonly Settings _settings;
private readonly List<SessionEntry> _entries;
private DateTime _lastCheck = DateTime.MinValue;
private DateTime _lastConfigChange = DateTime.MinValue;
public WinSCPService(PluginInitContext context, Settings settings)
{
_context = context;
_settings = settings;
_entries = new List<SessionEntry>();
}
private void ReloadFromFile(string file)
{
SessionEntry entry = null;
bool isSession = false;
foreach (var line in File.ReadAllLines(file))
{
if (line.StartsWith('[') && line.EndsWith(']'))
{
if (entry is not null)
{
_entries.Add(entry);
entry = null;
}
if (line.StartsWith("[Sessions\\"))
{
isSession = true;
entry = new() { Identifier = line[10..^1], Protocol = "sftp", };
entry.Title = HttpUtility.UrlDecode(entry.Identifier);
}
else
{
isSession = false;
}
}
else if (isSession && entry is not null)
{
if (line.StartsWith("HostName="))
{
entry.Hostname = line[9..];
}
else if (line.StartsWith("UserName="))
{
entry.Username = line[9..];
}
else if (line.StartsWith("FSProtocol="))
{
entry.Protocol = line[11..] switch
{
"0" => "scp",
"7" => "s3",
_ => "sftp",
};
}
}
}
if (entry is not null)
{
_entries.Add(entry);
}
}
private void ReloadFromRegistry()
{
using var root = Registry.CurrentUser.OpenSubKey(
"Software\\Martin Prikryl\\WinSCP 2\\Sessions"
);
if (root is null)
{
return;
}
foreach (var subKey in root.GetSubKeyNames())
{
using var SessionSubKey = root.OpenSubKey(subKey);
if (SessionSubKey == null)
{
continue;
}
try
{
SessionEntry entry = new()
{
Identifier = subKey,
Title = HttpUtility.UrlDecode(subKey),
Hostname = SessionSubKey.GetValue("HostName").ToString(),
Protocol = "sftp",
};
Object protocol = SessionSubKey.GetValue("FSProtocol");
if(protocol != null)
{
entry.Protocol = protocol.ToString() switch
{
"0" => "scp",
"7" => "s3",
_ => "sftp"
};
}
Object username = SessionSubKey.GetValue("UserName");
if (username != null)
{
entry.Username = username.ToString();
}
_entries.Add(entry);
}
catch (Exception)
{
// ignore
}
}
}
private void CheckAndReload()
{
if (_entries.Count == 0 || _lastCheck.AddMinutes(5) < DateTime.Now)
{
_entries.Clear();
try
{
_lastCheck = DateTime.Now;
Logger.Info("Reloading WinSCP config");
string configPath = Path.Combine(
Path.GetDirectoryName(_settings.WinSCPExePath),
"WinSCP.ini"
);
if (!Path.Exists(configPath))
{
configPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"WinSCP.ini"
);
}
if (Path.Exists(configPath))
{
if (_lastConfigChange != File.GetLastWriteTime(configPath)) {
ReloadFromFile(configPath);
}
_lastConfigChange = File.GetLastWriteTime(configPath);
}
else
{
ReloadFromRegistry();
}
}
catch (System.Exception e)
{
string errorTitle = _context.API.GetTranslation(
"flowlauncher_plugin_winscp_error"
);
_context.API.ShowMsg(errorTitle, e.ToString(), "");
}
}
}
internal List<SessionEntry> GetAll()
{
CheckAndReload();
return _entries;
}
}
}