1
- using ProxySuper . Core . Helpers ;
1
+ using MvvmCross ;
2
+ using MvvmCross . Navigation ;
3
+ using ProxySuper . Core . Helpers ;
2
4
using ProxySuper . Core . Models ;
3
5
using ProxySuper . Core . Models . Hosts ;
4
6
using ProxySuper . Core . Models . Projects ;
9
11
using System . Linq ;
10
12
using System . Text ;
11
13
using System . Threading . Tasks ;
14
+ using System . Windows ;
15
+ using System . Windows . Navigation ;
12
16
13
17
namespace ProxySuper . Core . Services
14
18
{
@@ -30,7 +34,6 @@ public abstract class ServiceBase<TSettings> where TSettings : IProjectSettings
30
34
{
31
35
private Host _host ;
32
36
33
-
34
37
private SshClient _sshClient ;
35
38
36
39
private ProjectProgress _progress ;
@@ -41,7 +44,11 @@ public ServiceBase(Host host, TSettings settings)
41
44
42
45
Settings = settings ;
43
46
44
- _sshClient = new SshClient ( CreateConnectionInfo ( ) ) ;
47
+ var connection = CreateConnectionInfo ( ) ;
48
+ if ( connection != null )
49
+ {
50
+ _sshClient = new SshClient ( connection ) ;
51
+ }
45
52
46
53
_progress = new ProjectProgress ( ) ;
47
54
@@ -54,6 +61,8 @@ public ServiceBase(Host host, TSettings settings)
54
61
IPv6 = string . Empty ;
55
62
56
63
IsOnlyIPv6 = false ;
64
+
65
+ NavigationService = Mvx . IoCProvider . Resolve < IMvxNavigationService > ( ) ;
57
66
}
58
67
59
68
public string RunCmd ( string command )
@@ -89,12 +98,20 @@ public string RunCmd(string command)
89
98
90
99
public bool IsOnlyIPv6 { get ; set ; }
91
100
101
+ public IMvxNavigationService NavigationService { get ; set ; }
102
+
92
103
93
104
#region 公用方法
94
105
public void Connect ( )
95
106
{
96
107
Task . Run ( ( ) =>
97
108
{
109
+ if ( _sshClient == null )
110
+ {
111
+ MessageBox . Show ( "无法建立连接,连接参数有误!" ) ;
112
+ return ;
113
+ }
114
+
98
115
if ( _sshClient . IsConnected == false )
99
116
{
100
117
Progress . Desc = ( "正在与服务器建立连接" ) ;
@@ -115,7 +132,7 @@ public void Disconnect()
115
132
{
116
133
Task . Run ( ( ) =>
117
134
{
118
- _sshClient . Disconnect ( ) ;
135
+ _sshClient ? . Disconnect ( ) ;
119
136
} ) ;
120
137
}
121
138
@@ -314,7 +331,6 @@ public void EnsureRootUser()
314
331
}
315
332
}
316
333
317
-
318
334
public void UninstallCaddy ( )
319
335
{
320
336
Progress . Desc = "关闭Caddy服务" ;
@@ -751,35 +767,52 @@ private string GetInstallCmd(string soft)
751
767
752
768
private ConnectionInfo CreateConnectionInfo ( )
753
769
{
754
- var authMethods = new List < AuthenticationMethod > ( ) ;
755
-
756
- if ( ! string . IsNullOrEmpty ( _host . Password ) )
770
+ try
757
771
{
758
- authMethods . Add ( new PasswordAuthenticationMethod ( _host . UserName , _host . Password ) ) ;
759
- }
772
+ var authMethods = new List < AuthenticationMethod > ( ) ;
760
773
761
- if ( _host . SecretType == LoginSecretType . PrivateKey )
762
- {
763
- authMethods . Add ( new PrivateKeyAuthenticationMethod ( _host . UserName , new PrivateKeyFile ( _host . PrivateKeyPath ) ) ) ;
764
- }
774
+ if ( _host . SecretType == LoginSecretType . Password )
775
+ {
776
+ authMethods . Add ( new PasswordAuthenticationMethod ( _host . UserName , _host . Password ) ) ;
777
+ }
778
+
779
+ if ( _host . SecretType == LoginSecretType . PrivateKey )
780
+ {
781
+ PrivateKeyFile keyFile ;
782
+ if ( string . IsNullOrEmpty ( _host . Password ) )
783
+ {
784
+ keyFile = new PrivateKeyFile ( _host . PrivateKeyPath ) ;
785
+ }
786
+ else
787
+ {
788
+ keyFile = new PrivateKeyFile ( _host . PrivateKeyPath , _host . Password ) ;
789
+ }
790
+ authMethods . Add ( new PrivateKeyAuthenticationMethod ( _host . UserName , keyFile ) ) ;
791
+ }
792
+
793
+ if ( _host . Proxy . Type == ProxyTypes . None )
794
+ {
795
+ return new ConnectionInfo (
796
+ host : _host . Address ,
797
+ username : _host . UserName ,
798
+ authenticationMethods : authMethods . ToArray ( ) ) ;
799
+ }
765
800
766
- if ( _host . Proxy . Type == ProxyTypes . None )
767
- {
768
801
return new ConnectionInfo (
769
802
host : _host . Address ,
803
+ port : _host . Port ,
770
804
username : _host . UserName ,
805
+ proxyType : _host . Proxy . Type ,
806
+ proxyHost : _host . Proxy . Address ,
807
+ proxyPort : _host . Proxy . Port ,
808
+ proxyUsername : _host . Proxy . UserName , proxyPassword : _host . Proxy . Password ,
771
809
authenticationMethods : authMethods . ToArray ( ) ) ;
772
810
}
773
-
774
- return new ConnectionInfo (
775
- host : _host . Address ,
776
- port : _host . Port ,
777
- username : _host . UserName ,
778
- proxyType : _host . Proxy . Type ,
779
- proxyHost : _host . Proxy . Address ,
780
- proxyPort : _host . Proxy . Port ,
781
- proxyUsername : _host . Proxy . UserName , proxyPassword : _host . Proxy . Password ,
782
- authenticationMethods : authMethods . ToArray ( ) ) ;
811
+ catch ( Exception ex )
812
+ {
813
+ MessageBox . Show ( ex . Message ) ;
814
+ return null ;
815
+ }
783
816
}
784
817
#endregion
785
818
}
0 commit comments