Add Trojan Support

This commit is contained in:
Connection Refused
2020-05-08 15:53:19 +08:00
parent 92d1cfdb50
commit 9149f23986
9 changed files with 5504 additions and 1 deletions

View File

@@ -40,6 +40,11 @@ namespace Netch.Controllers
/// </summary>
public VMessController pVMessController;
/// <summary>
/// Trojan 控制器
/// </summary>
public TrojanController pTrojanController;
/// <summary>
/// NF 控制器
/// </summary>
@@ -108,6 +113,14 @@ namespace Netch.Controllers
}
result = pVMessController.Start(server, mode);
break;
case "Trojan":
KillProcess("Trojan");
if (pTrojanController == null)
{
pTrojanController = new TrojanController();
}
result = pTrojanController.Start(server, mode);
break;
}
if (result)

View File

@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Netch.Controllers
{
public class TrojanController
{
/// <summary>
/// 进程实例
/// </summary>
public Process Instance;
/// <summary>
/// 当前状态
/// </summary>
public Models.State State = Models.State.Waiting;
/// <summary>
/// 启动
/// </summary>
/// <param name="server">服务器</param>
/// <param name="mode">模式</param>
/// <returns>是否启动成功</returns>
public bool Start(Models.Server server, Models.Mode mode)
{
Forms.MainForm.Instance.StatusText($"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting Trojan")}");
File.Delete("logging\\trojan.log");
if (!File.Exists("bin\\Trojan.exe"))
{
return false;
}
File.WriteAllText("data\\last.json", Newtonsoft.Json.JsonConvert.SerializeObject(new Models.Trojan()
{
local_addr = Global.Settings.LocalAddress,
local_port = Global.Settings.Socks5LocalPort,
remote_addr = server.Hostname,
remote_port = server.Port,
password = new List<string>()
{
server.Password
}
}));
Instance = MainController.GetProcess();
Instance.StartInfo.FileName = "bin\\Trojan.exe";
Instance.StartInfo.Arguments = "-c data\\last.json";
Instance.OutputDataReceived += OnOutputDataReceived;
Instance.ErrorDataReceived += OnOutputDataReceived;
State = Models.State.Starting;
Instance.Start();
Instance.BeginOutputReadLine();
Instance.BeginErrorReadLine();
for (var i = 0; i < 1000; i++)
{
Thread.Sleep(10);
if (State == Models.State.Started)
{
return true;
}
if (State == Models.State.Stopped)
{
Utils.Logging.Info("Trojan 进程启动失败");
Stop();
return false;
}
}
Utils.Logging.Info("Trojan 进程启动超时");
Stop();
return false;
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
try
{
if (Instance != null && !Instance.HasExited)
{
Instance.Kill();
}
}
catch (Exception e)
{
Utils.Logging.Info(e.ToString());
}
}
public void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(e.Data))
{
File.AppendAllText("logging\\trojan.log", $"{e.Data}\r\n");
if (State == Models.State.Starting)
{
if (Instance.HasExited)
{
State = Models.State.Stopped;
}
else if (e.Data.Contains("started"))
{
State = Models.State.Started;
}
else if (e.Data.Contains("exiting"))
{
State = Models.State.Stopped;
}
}
}
}
}
}

View File

@@ -86,6 +86,7 @@ namespace Netch.Forms
this.SettingsButton = new System.Windows.Forms.Button();
this.ProfileGroupBox = new System.Windows.Forms.GroupBox();
this.ProfileTable = new System.Windows.Forms.TableLayoutPanel();
this.AddTrojanServerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.MenuStrip.SuspendLayout();
this.ConfigurationGroupBox.SuspendLayout();
this.configLayoutPanel.SuspendLayout();
@@ -127,7 +128,8 @@ namespace Netch.Forms
this.AddSocks5ServerToolStripMenuItem,
this.AddShadowsocksServerToolStripMenuItem,
this.AddShadowsocksRServerToolStripMenuItem,
this.AddVMessServerToolStripMenuItem});
this.AddVMessServerToolStripMenuItem,
this.AddTrojanServerToolStripMenuItem});
this.ServerToolStripMenuItem.Margin = new System.Windows.Forms.Padding(3, 0, 0, 1);
this.ServerToolStripMenuItem.Name = "ServerToolStripMenuItem";
this.ServerToolStripMenuItem.Size = new System.Drawing.Size(57, 21);
@@ -638,6 +640,13 @@ namespace Netch.Forms
this.ProfileTable.Size = new System.Drawing.Size(599, 43);
this.ProfileTable.TabIndex = 0;
//
// AddTrojanServerToolStripMenuItem
//
this.AddTrojanServerToolStripMenuItem.Name = "AddTrojanServerToolStripMenuItem";
this.AddTrojanServerToolStripMenuItem.Size = new System.Drawing.Size(259, 22);
this.AddTrojanServerToolStripMenuItem.Text = "Add [Trojan] Server";
this.AddTrojanServerToolStripMenuItem.Click += new System.EventHandler(this.AddTrojanServerToolStripMenuItem_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
@@ -738,5 +747,6 @@ namespace Netch.Forms
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem updateACLWithProxyToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem reinstallTapDriverToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem AddTrojanServerToolStripMenuItem;
}
}

View File

@@ -493,6 +493,12 @@ namespace Netch.Forms
Hide();
}
private void AddTrojanServerToolStripMenuItem_Click(object sender, EventArgs e)
{
new Server.Trojan().Show();
Hide();
}
private void CreateProcessModeToolStripButton_Click(object sender, EventArgs e)
{
new Mode.Process().Show();

176
Netch/Forms/Server/Trojan.Designer.cs generated Normal file
View File

@@ -0,0 +1,176 @@
namespace Netch.Forms.Server
{
partial class Trojan
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Trojan));
this.ConfigurationGroupBox = new System.Windows.Forms.GroupBox();
this.PasswordTextBox = new System.Windows.Forms.TextBox();
this.PasswordLabel = new System.Windows.Forms.Label();
this.AddressLabel = new System.Windows.Forms.Label();
this.PortTextBox = new System.Windows.Forms.TextBox();
this.AddressTextBox = new System.Windows.Forms.TextBox();
this.RemarkTextBox = new System.Windows.Forms.TextBox();
this.RemarkLabel = new System.Windows.Forms.Label();
this.PortLabel = new System.Windows.Forms.Label();
this.ControlButton = new System.Windows.Forms.Button();
this.ConfigurationGroupBox.SuspendLayout();
this.SuspendLayout();
//
// ConfigurationGroupBox
//
this.ConfigurationGroupBox.Controls.Add(this.PasswordTextBox);
this.ConfigurationGroupBox.Controls.Add(this.PasswordLabel);
this.ConfigurationGroupBox.Controls.Add(this.AddressLabel);
this.ConfigurationGroupBox.Controls.Add(this.PortTextBox);
this.ConfigurationGroupBox.Controls.Add(this.AddressTextBox);
this.ConfigurationGroupBox.Controls.Add(this.RemarkTextBox);
this.ConfigurationGroupBox.Controls.Add(this.RemarkLabel);
this.ConfigurationGroupBox.Controls.Add(this.PortLabel);
this.ConfigurationGroupBox.Location = new System.Drawing.Point(12, 12);
this.ConfigurationGroupBox.Name = "ConfigurationGroupBox";
this.ConfigurationGroupBox.Size = new System.Drawing.Size(420, 109);
this.ConfigurationGroupBox.TabIndex = 0;
this.ConfigurationGroupBox.TabStop = false;
this.ConfigurationGroupBox.Text = "Configuration";
//
// PasswordTextBox
//
this.PasswordTextBox.Location = new System.Drawing.Point(120, 77);
this.PasswordTextBox.Name = "PasswordTextBox";
this.PasswordTextBox.Size = new System.Drawing.Size(294, 23);
this.PasswordTextBox.TabIndex = 7;
this.PasswordTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// PasswordLabel
//
this.PasswordLabel.AutoSize = true;
this.PasswordLabel.Location = new System.Drawing.Point(10, 80);
this.PasswordLabel.Name = "PasswordLabel";
this.PasswordLabel.Size = new System.Drawing.Size(64, 17);
this.PasswordLabel.TabIndex = 6;
this.PasswordLabel.Text = "Password";
//
// AddressLabel
//
this.AddressLabel.AutoSize = true;
this.AddressLabel.Location = new System.Drawing.Point(10, 51);
this.AddressLabel.Name = "AddressLabel";
this.AddressLabel.Size = new System.Drawing.Size(56, 17);
this.AddressLabel.TabIndex = 2;
this.AddressLabel.Text = "Address";
//
// PortTextBox
//
this.PortTextBox.Location = new System.Drawing.Point(358, 48);
this.PortTextBox.Name = "PortTextBox";
this.PortTextBox.Size = new System.Drawing.Size(56, 23);
this.PortTextBox.TabIndex = 5;
this.PortTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// AddressTextBox
//
this.AddressTextBox.Location = new System.Drawing.Point(120, 48);
this.AddressTextBox.Name = "AddressTextBox";
this.AddressTextBox.Size = new System.Drawing.Size(232, 23);
this.AddressTextBox.TabIndex = 3;
this.AddressTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// RemarkTextBox
//
this.RemarkTextBox.Location = new System.Drawing.Point(120, 19);
this.RemarkTextBox.Name = "RemarkTextBox";
this.RemarkTextBox.Size = new System.Drawing.Size(294, 23);
this.RemarkTextBox.TabIndex = 1;
this.RemarkTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// RemarkLabel
//
this.RemarkLabel.AutoSize = true;
this.RemarkLabel.Location = new System.Drawing.Point(10, 22);
this.RemarkLabel.Name = "RemarkLabel";
this.RemarkLabel.Size = new System.Drawing.Size(53, 17);
this.RemarkLabel.TabIndex = 0;
this.RemarkLabel.Text = "Remark";
//
// PortLabel
//
this.PortLabel.AutoSize = true;
this.PortLabel.Location = new System.Drawing.Point(351, 51);
this.PortLabel.Name = "PortLabel";
this.PortLabel.Size = new System.Drawing.Size(11, 17);
this.PortLabel.TabIndex = 4;
this.PortLabel.Text = ":";
//
// ControlButton
//
this.ControlButton.Location = new System.Drawing.Point(357, 127);
this.ControlButton.Name = "ControlButton";
this.ControlButton.Size = new System.Drawing.Size(75, 23);
this.ControlButton.TabIndex = 1;
this.ControlButton.Text = "Save";
this.ControlButton.UseVisualStyleBackColor = true;
this.ControlButton.Click += new System.EventHandler(this.ControlButton_Click);
//
// Trojan
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(444, 161);
this.Controls.Add(this.ControlButton);
this.Controls.Add(this.ConfigurationGroupBox);
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.MaximizeBox = false;
this.Name = "Trojan";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Trojan";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Trojan_FormClosing);
this.Load += new System.EventHandler(this.Trojan_Load);
this.ConfigurationGroupBox.ResumeLayout(false);
this.ConfigurationGroupBox.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox ConfigurationGroupBox;
private System.Windows.Forms.Button ControlButton;
private System.Windows.Forms.Label RemarkLabel;
private System.Windows.Forms.TextBox RemarkTextBox;
private System.Windows.Forms.Label PortLabel;
private System.Windows.Forms.TextBox AddressTextBox;
private System.Windows.Forms.TextBox PortTextBox;
private System.Windows.Forms.Label AddressLabel;
private System.Windows.Forms.TextBox PasswordTextBox;
private System.Windows.Forms.Label PasswordLabel;
}
}

View File

@@ -0,0 +1,106 @@
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Netch.Forms.Server
{
public partial class Trojan : Form
{
public int Index;
/// <summary>
/// 初始化
/// </summary>
/// <param name="index">需要编辑的索引</param>
public Trojan(int index = -1)
{
InitializeComponent();
Index = index;
}
private void Trojan_Load(object sender, EventArgs e)
{
ConfigurationGroupBox.Text = Utils.i18N.Translate(ConfigurationGroupBox.Text);
RemarkLabel.Text = Utils.i18N.Translate(RemarkLabel.Text);
AddressLabel.Text = Utils.i18N.Translate(AddressLabel.Text);
PasswordLabel.Text = Utils.i18N.Translate(PasswordLabel.Text);
ControlButton.Text = Utils.i18N.Translate(ControlButton.Text);
if (Index != -1)
{
RemarkTextBox.Text = Global.Settings.Server[Index].Remark;
AddressTextBox.Text = Global.Settings.Server[Index].Hostname;
PortTextBox.Text = Global.Settings.Server[Index].Port.ToString();
PasswordTextBox.Text = Global.Settings.Server[Index].Password;
}
}
private void Trojan_FormClosing(object sender, FormClosingEventArgs e)
{
Global.MainForm.Show();
}
private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
var cbx = sender as ComboBox;
if (cbx != null)
{
e.DrawBackground();
if (e.Index >= 0)
{
var sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
var brush = new SolidBrush(cbx.ForeColor);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
brush = SystemBrushes.HighlightText as SolidBrush;
}
e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf);
}
}
}
private void ControlButton_Click(object sender, EventArgs e)
{
if (!Regex.Match(PortTextBox.Text, "^[0-9]+$").Success)
{
return;
}
if (Index == -1)
{
Global.Settings.Server.Add(new Models.Server
{
Remark = RemarkTextBox.Text,
Type = "Trojan",
Hostname = AddressTextBox.Text,
Port = int.Parse(PortTextBox.Text),
Password = PasswordTextBox.Text
});
}
else
{
Global.Settings.Server[Index] = new Models.Server
{
Remark = RemarkTextBox.Text,
Group = Global.Settings.Server[Index].Group,
Type = "Trojan",
Hostname = AddressTextBox.Text,
Port = int.Parse(PortTextBox.Text),
Password = PasswordTextBox.Text
};
}
Utils.Configuration.Save();
MessageBox.Show(Utils.i18N.Translate("Saved"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
Global.MainForm.InitServer();
Close();
}
}
}

File diff suppressed because it is too large Load Diff

76
Netch/Models/Trojan.cs Normal file
View File

@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Netch.Models
{
public class Trojan
{
/// <summary>
/// 启动类型
/// </summary>
public string run_type = "client";
/// <summary>
/// 监听地址
/// </summary>
public string local_addr = "127.0.0.1";
/// <summary>
/// 监听端口
/// </summary>
public int local_port = 2801;
/// <summary>
/// 远端地址
/// </summary>
public string remote_addr;
/// <summary>
/// 远端端口
/// </summary>
public int remote_port;
/// <summary>
/// 密码
/// </summary>
public List<string> password;
/// <summary>
/// 日志级别
/// </summary>
public int log_level = 1;
public TrojanSSL ssl = new TrojanSSL();
public TrojanTCP tcp = new TrojanTCP();
}
public class TrojanSSL
{
public bool verify = false;
public bool verify_hostname = false;
public string cert;
public string cipher = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:AES128-SHA:AES256-SHA:DES-CBC3-SHA";
public string cipher_tls13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384";
public string sni;
public List<string> alpn = new List<string>()
{
"h2",
"http/1.1"
};
public bool reuse_session = true;
public bool session_ticket = true;
public string curves = "";
}
public class TrojanTCP
{
public bool no_delay = false;
public bool keep_alive = true;
public bool reuse_port = false;
public bool fast_open = true;
public int fast_open_qlen = 20;
}
}

View File

@@ -66,6 +66,9 @@
</ItemGroup>
<ItemGroup>
<Compile Update="Forms\Server\Trojan.cs">
<SubType>Form</SubType>
</Compile>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>