Skip to content

Commit

Permalink
MTProxy -> MTProto | 修复ssh私钥带密码登陆
Browse files Browse the repository at this point in the history
  • Loading branch information
next-autumn committed Aug 24, 2021
1 parent be9ae4a commit 6e61ef1
Show file tree
Hide file tree
Showing 33 changed files with 198 additions and 131 deletions.
1 change: 1 addition & 0 deletions ProxySuper.Core/Models/Hosts/Host.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Win32;
using MvvmCross.Commands;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace ProxySuper.Core.Models.Projects
{
public class MTProxyGoSettings : IProjectSettings
public class MTProtoGoSettings : IProjectSettings
{
public MTProxyGoSettings()
public MTProtoGoSettings()
{
Port = 443;

Expand Down
2 changes: 1 addition & 1 deletion ProxySuper.Core/Models/Projects/ProjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public enum ProjectType
NaiveProxy = 2,
Brook = 3,
V2ray = 4,
MTProxyGo = 5,
MTProtoGo = 5,
}
}
8 changes: 4 additions & 4 deletions ProxySuper.Core/Models/Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public Host Host
[JsonProperty("trojanGoSettings")]
public TrojanGoSettings TrojanGoSettings { get; set; }

[JsonProperty("naiveProxySettings")]
[JsonProperty("naiveProtoSettings")]
public NaiveProxySettings NaiveProxySettings { get; set; }

[JsonProperty("brook")]
public BrookSettings BrookSettings { get; set; }

[JsonProperty("mtProxyGoSettings")]
public MTProxyGoSettings MTProxyGoSettings { get; set; }
[JsonProperty("mtProtoGoSettings")]
public MTProtoGoSettings MTProtoGoSettings { get; set; }


[JsonIgnore]
Expand All @@ -65,7 +65,7 @@ public ProjectType Type

if (NaiveProxySettings != null) return ProjectType.NaiveProxy;

if (MTProxyGoSettings != null) return ProjectType.MTProxyGo;
if (MTProtoGoSettings != null) return ProjectType.MTProtoGo;

return ProjectType.Brook;
}
Expand Down
10 changes: 5 additions & 5 deletions ProxySuper.Core/ProxySuper.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<Compile Include="Models\Projects\BrookType.cs" />
<Compile Include="Models\Projects\IProjectSettings.cs" />
<Compile Include="Models\Hosts\LocalProxy.cs" />
<Compile Include="Models\Projects\MTProxyGoSettings.cs" />
<Compile Include="Models\Projects\MTProtoGoSettings.cs" />
<Compile Include="Models\Projects\NaiveProxySettings.cs" />
<Compile Include="Models\Projects\ProjectType.cs" />
<Compile Include="Models\Projects\TrojanGoSettings.cs" />
Expand All @@ -90,7 +90,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\Utils.cs" />
<Compile Include="Services\BrookService.cs" />
<Compile Include="Services\MTProxyGoService.cs" />
<Compile Include="Services\MTProtoGoService.cs" />
<Compile Include="Services\NaiveProxyService.cs" />
<Compile Include="Services\ServiceBase.cs" />
<Compile Include="Services\ShareLink.cs" />
Expand All @@ -105,9 +105,9 @@
<Compile Include="ViewModels\BrookInstallViewModel.cs" />
<Compile Include="ViewModels\EnableRootViewModel.cs" />
<Compile Include="ViewModels\HomeViewModel.cs" />
<Compile Include="ViewModels\MTProxyGoConfigViewModel.cs" />
<Compile Include="ViewModels\MTProxyGoEditorViewModel.cs" />
<Compile Include="ViewModels\MTProxyGoInstallViewModel.cs" />
<Compile Include="ViewModels\MTProtoGoConfigViewModel.cs" />
<Compile Include="ViewModels\MTProtoGoEditorViewModel.cs" />
<Compile Include="ViewModels\MTProtoGoInstallViewModel.cs" />
<Compile Include="ViewModels\NaiveProxyConfigViewModel.cs" />
<Compile Include="ViewModels\NaiveProxyEditorViewModel.cs" />
<Compile Include="ViewModels\NaiveProxyInstallViewModel.cs" />
Expand Down
3 changes: 3 additions & 0 deletions ProxySuper.Core/Services/BrookService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
using ProxySuper.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -72,6 +73,8 @@ public void Install()
AppendCommand("分享连接:");
AppendCommand(ShareLink.BuildBrook(Settings));
NavigationService.Navigate<BrookConfigViewModel, BrookSettings>(Settings);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
using ProxySuper.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,9 +10,9 @@

namespace ProxySuper.Core.Services
{
public class MTProxyGoService : ServiceBase<MTProxyGoSettings>
public class MTProtoGoService : ServiceBase<MTProtoGoSettings>
{
public MTProxyGoService(Host host, MTProxyGoSettings settings) : base(host, settings)
public MTProtoGoService(Host host, MTProtoGoSettings settings) : base(host, settings)
{
}

Expand Down Expand Up @@ -53,9 +54,9 @@ public void Install()
RunCmd($"echo \"bind-to=\\\"0.0.0.0:{Settings.Port}\\\"\" >> /etc/mtg.toml");
Progress.Percentage = 80;
Progress.Step = "7. 启动MTProxy服务";
Progress.Step = "7. 启动MTProto服务";
RunCmd($"docker run -d -v /etc/mtg.toml:/config.toml --name=mtg --restart=always -p {Settings.Port + ":" + Settings.Port} nineseconds/mtg");
Progress.Desc = "设置自启动MTProxy服务";
Progress.Desc = "设置自启动MTProto服务";
Progress.Step = "安装完成";
Progress.Percentage = 100;
Expand All @@ -64,6 +65,7 @@ public void Install()
AppendCommand("Port: " + Settings.Port);
AppendCommand("Secret: " + Settings.SecretText);
NavigationService.Navigate<MTProtoGoConfigViewModel, MTProtoGoSettings>(Settings);
}
catch (Exception ex)
{
Expand All @@ -79,7 +81,7 @@ public void Uninstall()
try
{
Progress.Percentage = 0;
Progress.Step = "卸载MTProxy";
Progress.Step = "卸载MTProto";
Progress.Desc = "检测系统环境";
EnsureRootUser();
Expand All @@ -106,9 +108,9 @@ public void UpdateSettings()
try
{
Progress.Percentage = 0;
Progress.Step = "更新MTProxy配置";
Progress.Step = "更新MTProto配置";
Progress.Desc = "暂停MTProxy服务";
Progress.Desc = "暂停MTProto服务";
var cid = RunCmd("docker ps -q --filter name=mtg");
RunCmd($"docker stop {cid}");
Progress.Percentage = 50;
Expand All @@ -122,7 +124,7 @@ public void UpdateSettings()
RunCmd($"echo \"bind-to=\\\"0.0.0.0:{Settings.Port}\\\"\" >> /etc/mtg.toml");
Progress.Percentage = 80;
Progress.Desc = "重启MTProxy服务";
Progress.Desc = "重启MTProto服务";
RunCmd($"docker restart {cid}");
Progress.Percentage = 100;
Expand Down
3 changes: 3 additions & 0 deletions ProxySuper.Core/Services/NaiveProxyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ProxySuper.Core.Models;
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
using ProxySuper.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -69,6 +70,8 @@ public void Install()
AppendCommand("分享连接:");
AppendCommand(ShareLink.BuildNaiveProxy(Settings));
NavigationService.Navigate<NaiveProxyConfigViewModel, NaiveProxySettings>(Settings);
}
catch (Exception ex)
{
Expand Down
85 changes: 59 additions & 26 deletions ProxySuper.Core/Services/ServiceBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ProxySuper.Core.Helpers;
using MvvmCross;
using MvvmCross.Navigation;
using ProxySuper.Core.Helpers;
using ProxySuper.Core.Models;
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
Expand All @@ -9,6 +11,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;

namespace ProxySuper.Core.Services
{
Expand All @@ -30,7 +34,6 @@ public abstract class ServiceBase<TSettings> where TSettings : IProjectSettings
{
private Host _host;


private SshClient _sshClient;

private ProjectProgress _progress;
Expand All @@ -41,7 +44,11 @@ public ServiceBase(Host host, TSettings settings)

Settings = settings;

_sshClient = new SshClient(CreateConnectionInfo());
var connection = CreateConnectionInfo();
if (connection != null)
{
_sshClient = new SshClient(connection);
}

_progress = new ProjectProgress();

Expand All @@ -54,6 +61,8 @@ public ServiceBase(Host host, TSettings settings)
IPv6 = string.Empty;

IsOnlyIPv6 = false;

NavigationService = Mvx.IoCProvider.Resolve<IMvxNavigationService>();
}

public string RunCmd(string command)
Expand Down Expand Up @@ -89,12 +98,20 @@ public string RunCmd(string command)

public bool IsOnlyIPv6 { get; set; }

public IMvxNavigationService NavigationService { get; set; }


#region 公用方法
public void Connect()
{
Task.Run(() =>
{
if (_sshClient == null)
{
MessageBox.Show("无法建立连接,连接参数有误!");
return;
}
if (_sshClient.IsConnected == false)
{
Progress.Desc = ("正在与服务器建立连接");
Expand All @@ -115,7 +132,7 @@ public void Disconnect()
{
Task.Run(() =>
{
_sshClient.Disconnect();
_sshClient?.Disconnect();
});
}

Expand Down Expand Up @@ -314,7 +331,6 @@ public void EnsureRootUser()
}
}


public void UninstallCaddy()
{
Progress.Desc = "关闭Caddy服务";
Expand Down Expand Up @@ -751,35 +767,52 @@ private string GetInstallCmd(string soft)

private ConnectionInfo CreateConnectionInfo()
{
var authMethods = new List<AuthenticationMethod>();

if (!string.IsNullOrEmpty(_host.Password))
try
{
authMethods.Add(new PasswordAuthenticationMethod(_host.UserName, _host.Password));
}
var authMethods = new List<AuthenticationMethod>();

if (_host.SecretType == LoginSecretType.PrivateKey)
{
authMethods.Add(new PrivateKeyAuthenticationMethod(_host.UserName, new PrivateKeyFile(_host.PrivateKeyPath)));
}
if (_host.SecretType == LoginSecretType.Password)
{
authMethods.Add(new PasswordAuthenticationMethod(_host.UserName, _host.Password));
}

if (_host.SecretType == LoginSecretType.PrivateKey)
{
PrivateKeyFile keyFile;
if (string.IsNullOrEmpty(_host.Password))
{
keyFile = new PrivateKeyFile(_host.PrivateKeyPath);
}
else
{
keyFile = new PrivateKeyFile(_host.PrivateKeyPath, _host.Password);
}
authMethods.Add(new PrivateKeyAuthenticationMethod(_host.UserName, keyFile));
}

if (_host.Proxy.Type == ProxyTypes.None)
{
return new ConnectionInfo(
host: _host.Address,
username: _host.UserName,
authenticationMethods: authMethods.ToArray());
}

if (_host.Proxy.Type == ProxyTypes.None)
{
return new ConnectionInfo(
host: _host.Address,
port: _host.Port,
username: _host.UserName,
proxyType: _host.Proxy.Type,
proxyHost: _host.Proxy.Address,
proxyPort: _host.Proxy.Port,
proxyUsername: _host.Proxy.UserName, proxyPassword: _host.Proxy.Password,
authenticationMethods: authMethods.ToArray());
}

return new ConnectionInfo(
host: _host.Address,
port: _host.Port,
username: _host.UserName,
proxyType: _host.Proxy.Type,
proxyHost: _host.Proxy.Address,
proxyPort: _host.Proxy.Port,
proxyUsername: _host.Proxy.UserName, proxyPassword: _host.Proxy.Password,
authenticationMethods: authMethods.ToArray());
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
#endregion
}
Expand Down
7 changes: 6 additions & 1 deletion ProxySuper.Core/Services/TrojanGoService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Win32;
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
using ProxySuper.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -93,6 +94,10 @@ public void Install()
{
Progress.Step = "安装成功,请上传您的 TLS 证书。";
}
else
{
NavigationService.Navigate<TrojanGoConfigViewModel, TrojanGoSettings>(Settings);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -202,12 +207,12 @@ public void ApplyForCert()
{
try
{
EnsureRootUser();
Progress.Step = "续签证书";
Progress.Percentage = 0;
Progress.Desc = "检测系统环境";
EnsureRootUser();
EnsureSystemEnv();
Progress.Percentage = 20;
Expand Down
Loading

0 comments on commit 6e61ef1

Please sign in to comment.