Skip to content

Commit 4011ea2

Browse files
committed
feat: Allow search in tag list view #297
[WIP] working on multi credentials #301, auto address switching.
1 parent 462e07a commit 4011ea2

File tree

12 files changed

+257
-80
lines changed

12 files changed

+257
-80
lines changed

Ui/Model/Protocol/Base/Credential.cs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Sockets;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Shawn.Utils;
@@ -13,10 +14,11 @@ public object Clone()
1314
{
1415
return MemberwiseClone();
1516
}
16-
}
17+
public Credential CloneMe()
18+
{
19+
return (MemberwiseClone() as Credential)!;
20+
}
1721

18-
public class CredentialWithAddressPortUserPwd : Credential
19-
{
2022
private string _name = "";
2123
public string Name
2224
{
@@ -56,5 +58,69 @@ public string Password
5658
get => _password;
5759
set => SetAndNotifyIfChanged(ref _password, value);
5860
}
61+
62+
public static bool TestAddressPortIsAvailable(ProtocolBaseWithAddressPortUserPwd protocol, Credential credential, int timeOutMillisecond = 0)
63+
{
64+
if (string.IsNullOrEmpty(credential.Address) && string.IsNullOrEmpty(credential.Port))
65+
{
66+
return false;
67+
}
68+
69+
string address = string.IsNullOrEmpty(credential.Address) ? protocol.Address : credential.Address;
70+
string port = string.IsNullOrEmpty(credential.Port) ? protocol.Port : credential.Port;
71+
try
72+
{
73+
74+
var iport = int.Parse(port);
75+
var client = new TcpClient();
76+
if (timeOutMillisecond > 0)
77+
{
78+
if (!client.ConnectAsync(address, iport).Wait(timeOutMillisecond))
79+
{
80+
throw new Exception();
81+
}
82+
}
83+
else
84+
{
85+
client.Connect(address, iport);
86+
}
87+
client.Close();
88+
89+
return true;
90+
}
91+
catch (Exception)
92+
{
93+
// ignored
94+
}
95+
96+
SimpleLogHelper.Error($"TXT:{address}:{port} 未打开");
97+
98+
return false;
99+
}
100+
101+
102+
103+
public void SetCredential(in Credential credential)
104+
{
105+
if (!string.IsNullOrEmpty(credential.Address))
106+
{
107+
Address = credential.Address;
108+
}
109+
110+
if (!string.IsNullOrEmpty(credential.Port))
111+
{
112+
Port = credential.Port;
113+
}
114+
115+
if (!string.IsNullOrEmpty(credential.UserName))
116+
{
117+
UserName = credential.UserName;
118+
}
119+
120+
if (!string.IsNullOrEmpty(credential.Password))
121+
{
122+
Password = credential.Password;
123+
}
124+
}
59125
}
60126
}

Ui/Model/Protocol/Base/ProtocolBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,13 @@ public ProtocolBase Clone()
282282
if (this is ProtocolBaseWithAddressPortUserPwd p
283283
&& clone is ProtocolBaseWithAddressPortUserPwd c)
284284
{
285-
if (p.Credentials != null)
285+
if (p.AlternateCredentials != null)
286286
{
287-
c.Credentials = new ObservableCollection<CredentialWithAddressPortUserPwd>(p.Credentials.Select(x => x.Clone() as CredentialWithAddressPortUserPwd));
287+
c.AlternateCredentials = new ObservableCollection<Credential>(p.AlternateCredentials.Select(x => x.Clone() as Credential));
288288
}
289289
else
290290
{
291-
c.Credentials = null;
291+
c.AlternateCredentials = null;
292292
}
293293
}
294294
return clone;

Ui/Model/Protocol/Base/ProtocolBaseWithAddressPort.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ protected override string GetSubTitle()
4040
return $"{Address}:{Port}";
4141
}
4242

43-
private ObservableCollection<CredentialWithAddressPortUserPwd>? _credentials = new ObservableCollection<CredentialWithAddressPortUserPwd>();
44-
public virtual ObservableCollection<CredentialWithAddressPortUserPwd>? Credentials
45-
{
46-
get => _credentials;
47-
set => SetAndNotifyIfChanged(ref _credentials, value);
48-
}
49-
5043
#endregion Conn
5144
}
5245
}

Ui/Model/Protocol/Base/ProtocolBaseWithAddressPortUserPwd.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Shawn.Utils;
1+
using CredentialManagement;
2+
using Shawn.Utils;
3+
using System.Collections.ObjectModel;
24

35
namespace _1RM.Model.Protocol.Base
46
{
@@ -31,6 +33,56 @@ protected override string GetSubTitle()
3133
return string.IsNullOrEmpty(UserName) ? base.GetSubTitle() : $"{Address}:{Port} ({UserName})";
3234
}
3335

36+
private ObservableCollection<Credential>? _alternateCredentials = new ObservableCollection<Credential>();
37+
public ObservableCollection<Credential>? AlternateCredentials
38+
{
39+
get => _alternateCredentials;
40+
set => SetAndNotifyIfChanged(ref _alternateCredentials, value);
41+
}
42+
43+
44+
private bool? _isAutoAlternateAddressSwitching = false;
45+
public bool? IsAutoAlternateAddressSwitching
46+
{
47+
get => _isAutoAlternateAddressSwitching;
48+
set => SetAndNotifyIfChanged(ref _isAutoAlternateAddressSwitching, value);
49+
}
50+
51+
public Credential GetCredential()
52+
{
53+
var c = new Credential()
54+
{
55+
Address = Address,
56+
Port = Port,
57+
Password = Password,
58+
UserName = UserName,
59+
};
60+
return c;
61+
}
62+
63+
public void SetCredential(in Credential credential)
64+
{
65+
if (!string.IsNullOrEmpty(credential.Address))
66+
{
67+
Address = credential.Address;
68+
}
69+
70+
if (!string.IsNullOrEmpty(credential.Port))
71+
{
72+
Port = credential.Port;
73+
}
74+
75+
if (!string.IsNullOrEmpty(credential.UserName))
76+
{
77+
UserName = credential.UserName;
78+
}
79+
80+
if (!string.IsNullOrEmpty(credential.Password))
81+
{
82+
Password = credential.Password;
83+
}
84+
}
85+
3486
#endregion
3587
}
3688
}

Ui/Model/Protocol/FileTransmit/Transmitters/TransmitterFtp.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ private void InitClient()
253253
{
254254
_ftp?.Dispose();
255255
_ftp = new FtpClient(Hostname, Port, Username, Password);
256-
//_ftp.Credentials = new NetworkCredential(Username, Password);
257256
_ftp.Connect();
258257
}
259258
}

Ui/Model/ProtocolAction.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public static List<ProtocolAction> GetActions(this ProtocolBase server)
4848
));
4949
}
5050

51-
if (server is ProtocolBaseWithAddressPortUserPwd { Credentials.Count: > 0 } protocol)
51+
if (server is ProtocolBaseWithAddressPortUserPwd { AlternateCredentials.Count: > 0 } protocol)
5252
{
53-
foreach (var credential in protocol.Credentials)
53+
foreach (var credential in protocol.AlternateCredentials)
5454
{
5555
actions.Add(new ProtocolAction(
5656
actionName: IoC.Get<ILanguageService>().Translate("Connect") + $" (TXT:with credential {credential.Name})",
57-
action: () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server, fromView: $"{nameof(LauncherWindowView)} - Action - Credentials", assignCredentialName: credential.Name); }
57+
action: () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server, fromView: $"{nameof(LauncherWindowView)} - Action - AlternateCredentials", assignCredentialName: credential.Name); }
5858
));
5959
}
6060
}
@@ -76,11 +76,6 @@ public static List<ProtocolAction> GetActions(this ProtocolBase server)
7676
}
7777
}
7878

79-
//else
80-
//{
81-
// actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Connect"), () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, fromView: nameof(LauncherWindowView)); }));
82-
//}
83-
8479
if (writable)
8580
{
8681
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Edit"), () =>
@@ -89,12 +84,12 @@ public static List<ProtocolAction> GetActions(this ProtocolBase server)
8984
IoC.Get<MainWindowViewModel>()?.ShowMe();
9085
GlobalEventHelper.OnRequestGoToServerEditPage?.Invoke(server: server, showAnimation: false);
9186
}));
92-
//actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("server_card_operate_duplicate"), () =>
93-
//{
94-
// if (GlobalEventHelper.OnRequestGoToServerEditPage == null)
95-
// IoC.Get<MainWindowViewModel>()?.ShowMe();
96-
// GlobalEventHelper.OnRequestGoToServerDuplicatePage?.Invoke(server: server, showAnimation: false);
97-
//}));
87+
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("server_card_operate_duplicate"), () =>
88+
{
89+
if (GlobalEventHelper.OnRequestGoToServerEditPage == null)
90+
IoC.Get<MainWindowViewModel>()?.ShowMe();
91+
GlobalEventHelper.OnRequestGoToServerDuplicatePage?.Invoke(server: server, showAnimation: false);
92+
}));
9893
}
9994
};
10095

@@ -151,11 +146,6 @@ public static List<ProtocolAction> GetActions(this ProtocolBase server)
151146
}
152147
}
153148

154-
//if (writable)
155-
//{
156-
// actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Delete"), () => { GlobalEventHelper.OnRequestDeleteServer?.Invoke(server); }));
157-
//}
158-
159149
#endregion Build Actions
160150

161151
return actions;

Ui/Service/DataBaseService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public static void EncryptToDatabaseLevel(this ProtocolBase server)
3434
var s = (ProtocolBaseWithAddressPortUserPwd)server;
3535
s.Password = EncryptOnce(s.Password);
3636

37-
if (s.Credentials != null)
38-
foreach (var credential in s.Credentials)
37+
if (s.AlternateCredentials != null)
38+
foreach (var credential in s.AlternateCredentials)
3939
{
4040
credential.Password = EncryptOnce(credential.Password);
4141
}
@@ -62,8 +62,8 @@ public static void DecryptToConnectLevel(this ProtocolBase server)
6262
var s = (ProtocolBaseWithAddressPortUserPwd)server;
6363
s.Password = DecryptOrReturnOriginalString(s.Password);
6464

65-
if (s.Credentials != null)
66-
foreach (var credential in s.Credentials)
65+
if (s.AlternateCredentials != null)
66+
foreach (var credential in s.AlternateCredentials)
6767
{
6868
credential.Password = DecryptOrReturnOriginalString(credential.Password);
6969
}

0 commit comments

Comments
 (0)