Skip to content

Commit

Permalink
添加升级Trojan服务端版本
Browse files Browse the repository at this point in the history
  • Loading branch information
proxysu committed May 28, 2020
1 parent 80fd2a4 commit ee8857c
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 7 deletions.
9 changes: 5 additions & 4 deletions ProxySU/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@


</Grid.RowDefinitions>
<Button x:Name="ButtonUpdateTrojan" Content="检测/升级Trojan版本" Grid.Column="0" Grid.Row="0" Margin="5" Click="ButtonUpdateTrojan_Click"></Button>
<Button x:Name="ButtonTrojanTemplate" Content="Trojan设置" Grid.Column="1" Grid.Row="0" Margin="5" Click="ButtonTrojanTemplate_Click"/>
<TextBlock Text="Trojan参数设置" Grid.Column="1" Grid.Row="1" Grid.RowSpan="2" HorizontalAlignment="Center"></TextBlock>

Expand Down Expand Up @@ -221,13 +222,13 @@
<Grid Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="4" Grid.RowSpan="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.55*"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.55*"></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.55*"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
Expand Down Expand Up @@ -384,7 +385,7 @@
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="程序版本:1.6.1" Grid.Column="0" Grid.Row="0"></TextBlock>
<TextBlock Text="程序版本:1.6.2" Grid.Column="0" Grid.Row="0"></TextBlock>

</Grid>

Expand Down
191 changes: 190 additions & 1 deletion ProxySU/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2609,7 +2609,7 @@ private void ButtonUpdateV2ray_Click(object sender, RoutedEventArgs e)
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
//升级主程序
//升级V2ray主程序
private void UpdateV2ray(ConnectionInfo connectionInfo, TextBlock textBlockName, ProgressBar progressBar)
{
string currentStatus = "正在登录远程主机......";
Expand Down Expand Up @@ -2770,6 +2770,195 @@ private void UpdateV2ray(ConnectionInfo connectionInfo, TextBlock textBlockName,
#endregion

}
//检测升级远程主机Trojan版本
private void ButtonUpdateTrojan_Click(object sender, RoutedEventArgs e)
{
ConnectionInfo connectionInfo = GenerateConnectionInfo();
if (connectionInfo == null)
{
MessageBox.Show("远程主机连接信息有误,请检查");
return;
}

Thread thread = new Thread(() => UpdateTojan(connectionInfo, TextBlockSetUpProcessing, ProgressBarSetUpProcessing));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
//升级Trojan主程序
private void UpdateTojan(ConnectionInfo connectionInfo, TextBlock textBlockName, ProgressBar progressBar)
{
string currentStatus = "正在登录远程主机......";
Action<TextBlock, ProgressBar, string> updateAction = new Action<TextBlock, ProgressBar, string>(UpdateTextBlock);
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);

try
{
#region 主机指纹,暂未启用
//byte[] expectedFingerPrint = new byte[] {
// 0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
// 0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b
// };
#endregion
using (var client = new SshClient(connectionInfo))

{
#region ssh登录验证主机指纹代码块,暂未启用
// client.HostKeyReceived += (sender, e) =>
// {
// if (expectedFingerPrint.Length == e.FingerPrint.Length)
// {
// for (var i = 0; i < expectedFingerPrint.Length; i++)
// {
// if (expectedFingerPrint[i] != e.FingerPrint[i])
// {
// e.CanTrust = false;
// break;
// }
// }
// }
// else
// {
// e.CanTrust = false;
// }
// };
#endregion

client.Connect();
if (client.IsConnected == true)
{
currentStatus = "主机登录成功";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);
}
//检测远程主机V2ray版本
currentStatus = "检测远程主机Trojan版本......";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);

string cmdTestTrojanInstalled = @"find / -name trojan";
//MessageBox.Show(cmdTestV2rayInstalled);
string resultCmdTestTrojanInstalled = client.RunCommand(cmdTestTrojanInstalled).Result;
//client.Disconnect();
//MessageBox.Show(resultCmdTestV2rayInstalled);
if (resultCmdTestTrojanInstalled.Contains("/usr/local/bin/trojan") == false)
{
MessageBoxResult messageBoxResult = MessageBox.Show("远程主机未安装Trojan!");

currentStatus = "未安装Trojan,退出";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);
return;

}
string sshcmd;
sshcmd = @"echo ""$(/usr/local/bin/trojan -v 2>&1)"" | head -n 1 | cut -d "" "" -f4";
//MessageBox.Show(sshcmd);
string trojanCurrentVersion = client.RunCommand(sshcmd).Result;//不含字母v
//MessageBox.Show(v2rayCurrentVersion);

sshcmd = @"curl -fsSL https://api.github.com/repos/trojan-gfw/trojan/releases/latest | grep tag_name | sed -E 's/.*""v(.*)"".*/\1/'";
//MessageBox.Show(sshcmd);

string trojanNewVersion = client.RunCommand(sshcmd).Result;//不包含字母v
//MessageBox.Show(v2rayNewVersion);
if (trojanNewVersion.Equals(trojanCurrentVersion) == false)
{
MessageBoxResult messageBoxResult = MessageBox.Show($"远程主机当前版本为:v{trojanCurrentVersion}\n最新版本为:{trojanNewVersion}\n是否升级为最新版本?", "", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (messageBoxResult == MessageBoxResult.No)
{
currentStatus = "升级取消,退出";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);
return;
}
else
{
currentStatus = "正在升级Trojan到最新版本......";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);

//备份配置文件
sshcmd = @"mv /usr/local/etc/trojan/config.json /usr/local/etc/trojan/config.json.bak";
client.RunCommand(sshcmd);
//升级Trojan主程序
client.RunCommand("curl -o /tmp/trojan-quickstart.sh https://raw.githubusercontent.com/trojan-gfw/trojan-quickstart/master/trojan-quickstart.sh");
client.RunCommand("yes | bash /tmp/trojan-quickstart.sh");
sshcmd = @"echo ""$(/usr/local/bin/trojan -v 2>&1)"" | head -n 1 | cut -d "" "" -f4";
//MessageBox.Show(sshcmd);
trojanCurrentVersion = client.RunCommand(sshcmd).Result;//不含字母v
if (trojanNewVersion.Equals(trojanCurrentVersion) == true)
{
//恢复原来的配置文件备份
sshcmd = @"rm -f /usr/local/etc/trojan/config.json";
client.RunCommand(sshcmd);
sshcmd = @"mv /usr/local/etc/trojan/config.json.bak /usr/local/etc/trojan/config.json";
client.RunCommand(sshcmd);
MessageBox.Show($"升级成功!!\n当前版本为:v{trojanCurrentVersion}\n最新版本为:{trojanNewVersion}");
currentStatus = "升级成功!当前已是最新版本!";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);
}
else
{
MessageBox.Show("升级失败,原因未知,请向开发者提问,以寻求支持!");
currentStatus = "升级失败!";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);
}
}
}
else
{
MessageBox.Show($"远程主机当前已是最新版本:{trojanNewVersion}\n无需升级!");
currentStatus = "已是最新版本,无需升级,退出";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
Thread.Sleep(1000);
}

client.Disconnect();
return;
}
}
catch (Exception ex1)//例外处理
#region 例外处理
{
//MessageBox.Show(ex1.Message);
if (ex1.Message.Contains("连接尝试失败") == true)
{
MessageBox.Show($"{ex1.Message}\n请检查主机地址及端口是否正确,如果通过代理,请检查代理是否正常工作");
}

else if (ex1.Message.Contains("denied (password)") == true)
{
MessageBox.Show($"{ex1.Message}\n密码错误或用户名错误");
}
else if (ex1.Message.Contains("Invalid private key file") == true)
{
MessageBox.Show($"{ex1.Message}\n所选密钥文件错误或者格式不对");
}
else if (ex1.Message.Contains("denied (publickey)") == true)
{
MessageBox.Show($"{ex1.Message}\n使用密钥登录,密钥文件错误或用户名错误");
}
else if (ex1.Message.Contains("目标计算机积极拒绝") == true)
{
MessageBox.Show($"{ex1.Message}\n主机地址错误,如果使用了代理,也可能是连接代理的端口错误");
}
else
{
MessageBox.Show("发生错误");
MessageBox.Show(ex1.Message);
}
currentStatus = "主机登录失败";
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);

}
#endregion

}



//生成三合一的v2ray路径
private void ButtonV2rayPath3in1_Click(object sender, RoutedEventArgs e)
{
Expand Down
4 changes: 2 additions & 2 deletions ProxySU/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.1.0")]
[assembly: AssemblyFileVersion("1.6.1.0")]
[assembly: AssemblyVersion("1.6.2.0")]
[assembly: AssemblyFileVersion("1.6.2.0")]
Binary file modified ProxySU/bin/Beta/Beta.zip
Binary file not shown.

0 comments on commit ee8857c

Please sign in to comment.