add tun mode

This commit is contained in:
2dust
2022-04-03 20:16:01 +08:00
parent 88070725a0
commit 94883a0a20
11 changed files with 194 additions and 39 deletions

View File

@@ -31,6 +31,7 @@
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AddProfileForm));
this.btnClose = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.chkEnableTun = new System.Windows.Forms.CheckBox();
this.chkEnabled = new System.Windows.Forms.CheckBox();
this.txtUserAgent = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
@@ -62,6 +63,7 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.chkEnableTun);
this.groupBox1.Controls.Add(this.chkEnabled);
this.groupBox1.Controls.Add(this.txtUserAgent);
this.groupBox1.Controls.Add(this.label2);
@@ -80,6 +82,12 @@
this.groupBox1.Name = "groupBox1";
this.groupBox1.TabStop = false;
//
// chkEnableTun
//
resources.ApplyResources(this.chkEnableTun, "chkEnableTun");
this.chkEnableTun.Name = "chkEnableTun";
this.chkEnableTun.UseVisualStyleBackColor = true;
//
// chkEnabled
//
resources.ApplyResources(this.chkEnabled, "chkEnabled");
@@ -217,5 +225,6 @@
private System.Windows.Forms.TextBox txtUrl;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.CheckBox chkEnabled;
private System.Windows.Forms.CheckBox chkEnableTun;
}
}

View File

@@ -48,6 +48,7 @@ namespace clashN.Forms
txtUrl.Text = profileItem.url;
chkEnabled.Checked = profileItem.enabled;
txtUserAgent.Text = profileItem.userAgent;
chkEnableTun.Checked = profileItem.enableTun;
if (profileItem.coreType == null)
{
@@ -86,6 +87,7 @@ namespace clashN.Forms
profileItem.url = txtUrl.Text.TrimEx();
profileItem.enabled = chkEnabled.Checked;
profileItem.userAgent = txtUserAgent.Text.TrimEx();
profileItem.enableTun = chkEnableTun.Checked;
if (Utils.IsNullOrEmpty(cmbCoreType.Text))
{

View File

@@ -83,28 +83,61 @@
<data name="&gt;&gt;btnClose.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="chkEnabled.AutoSize" type="System.Boolean, mscorlib">
<data name="chkEnableTun.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="chkEnableTun.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="chkEnableTun.Location" type="System.Drawing.Point, System.Drawing">
<value>261, 266</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="chkEnableTun.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 4, 4, 4</value>
</data>
<data name="chkEnableTun.Size" type="System.Drawing.Size, System.Drawing">
<value>219, 21</value>
</data>
<data name="chkEnableTun.TabIndex" type="System.Int32, mscorlib">
<value>49</value>
</data>
<data name="chkEnableTun.Text" xml:space="preserve">
<value>Enable tun mode (Run As Admin)</value>
</data>
<data name="&gt;&gt;chkEnableTun.Name" xml:space="preserve">
<value>chkEnableTun</value>
</data>
<data name="&gt;&gt;chkEnableTun.Type" xml:space="preserve">
<value>System.Windows.Forms.CheckBox, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;chkEnableTun.Parent" xml:space="preserve">
<value>groupBox1</value>
</data>
<data name="&gt;&gt;chkEnableTun.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="chkEnabled.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
</data>
<data name="chkEnabled.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="chkEnabled.Location" type="System.Drawing.Point, System.Drawing">
<value>274, 267</value>
<value>143, 309</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="chkEnabled.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 4, 4, 4</value>
</data>
<data name="chkEnabled.Size" type="System.Drawing.Size, System.Drawing">
<value>66, 21</value>
<value>188, 21</value>
</data>
<data name="chkEnabled.TabIndex" type="System.Int32, mscorlib">
<value>48</value>
</data>
<data name="chkEnabled.Text" xml:space="preserve">
<value>Enable</value>
<value>Enable Update subscription</value>
</data>
<data name="&gt;&gt;chkEnabled.Name" xml:space="preserve">
<value>chkEnabled</value>
@@ -116,7 +149,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;chkEnabled.ZOrder" xml:space="preserve">
<value>0</value>
<value>1</value>
</data>
<data name="txtUserAgent.Location" type="System.Drawing.Point, System.Drawing">
<value>143, 222</value>
@@ -140,7 +173,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;txtUserAgent.ZOrder" xml:space="preserve">
<value>1</value>
<value>2</value>
</data>
<data name="label2.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
@@ -173,7 +206,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;label2.ZOrder" xml:space="preserve">
<value>2</value>
<value>3</value>
</data>
<data name="txtUrl.Location" type="System.Drawing.Point, System.Drawing">
<value>143, 62</value>
@@ -203,7 +236,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;txtUrl.ZOrder" xml:space="preserve">
<value>3</value>
<value>4</value>
</data>
<data name="label3.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
@@ -236,7 +269,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;label3.ZOrder" xml:space="preserve">
<value>4</value>
<value>5</value>
</data>
<data name="btnEdit.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
@@ -263,7 +296,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;btnEdit.ZOrder" xml:space="preserve">
<value>5</value>
<value>6</value>
</data>
<data name="cmbCoreType.Location" type="System.Drawing.Point, System.Drawing">
<value>143, 264</value>
@@ -284,7 +317,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;cmbCoreType.ZOrder" xml:space="preserve">
<value>6</value>
<value>7</value>
</data>
<data name="labCoreType.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
@@ -314,7 +347,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;labCoreType.ZOrder" xml:space="preserve">
<value>7</value>
<value>8</value>
</data>
<data name="btnBrowse.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
@@ -341,7 +374,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;btnBrowse.ZOrder" xml:space="preserve">
<value>8</value>
<value>9</value>
</data>
<data name="txtAddress.Location" type="System.Drawing.Point, System.Drawing">
<value>143, 123</value>
@@ -365,7 +398,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;txtAddress.ZOrder" xml:space="preserve">
<value>9</value>
<value>10</value>
</data>
<data name="label13.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
@@ -392,7 +425,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;label13.ZOrder" xml:space="preserve">
<value>10</value>
<value>11</value>
</data>
<data name="txtRemarks.Location" type="System.Drawing.Point, System.Drawing">
<value>143, 23</value>
@@ -413,7 +446,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;txtRemarks.ZOrder" xml:space="preserve">
<value>11</value>
<value>12</value>
</data>
<data name="label6.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
@@ -440,7 +473,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;label6.ZOrder" xml:space="preserve">
<value>12</value>
<value>13</value>
</data>
<data name="label1.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
@@ -467,7 +500,7 @@
<value>groupBox1</value>
</data>
<data name="&gt;&gt;label1.ZOrder" xml:space="preserve">
<value>13</value>
<value>14</value>
</data>
<data name="groupBox1.Dock" type="System.Windows.Forms.DockStyle, System.Windows.Forms">
<value>Fill</value>
@@ -496,18 +529,6 @@
<data name="&gt;&gt;groupBox1.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="btnOK.Location" type="System.Drawing.Point, System.Drawing">
<value>357, 17</value>
</data>
<data name="btnOK.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 23</value>
</data>
<data name="btnOK.TabIndex" type="System.Int32, mscorlib">
<value>5</value>
</data>
<data name="btnOK.Text" xml:space="preserve">
<value>&amp;OK</value>
</data>
<data name="&gt;&gt;btnOK.Name" xml:space="preserve">
<value>btnOK</value>
</data>
@@ -544,6 +565,30 @@
<data name="&gt;&gt;panel2.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<data name="btnOK.Location" type="System.Drawing.Point, System.Drawing">
<value>357, 17</value>
</data>
<data name="btnOK.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 23</value>
</data>
<data name="btnOK.TabIndex" type="System.Int32, mscorlib">
<value>5</value>
</data>
<data name="btnOK.Text" xml:space="preserve">
<value>&amp;OK</value>
</data>
<data name="&gt;&gt;btnOK.Name" xml:space="preserve">
<value>btnOK</value>
</data>
<data name="&gt;&gt;btnOK.Type" xml:space="preserve">
<value>System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;btnOK.Parent" xml:space="preserve">
<value>panel2</value>
</data>
<data name="&gt;&gt;btnOK.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<data name="panel1.Dock" type="System.Windows.Forms.DockStyle, System.Windows.Forms">
<value>Top</value>
</data>

View File

@@ -1,4 +1,64 @@
<root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
@@ -68,7 +128,7 @@
<value>51, 21</value>
</data>
<data name="chkEnabled.Text" xml:space="preserve">
<value>启用</value>
<value>启用更新订阅</value>
</data>
<data name="label3.Size" type="System.Drawing.Size, System.Drawing">
<value>79, 17</value>
@@ -112,4 +172,7 @@
<data name="$this.Text" xml:space="preserve">
<value>配置文件</value>
</data>
<data name="chkEnableTun.Text" xml:space="preserve">
<value>启用 tun 模式 (以管理员身份运行)</value>
</data>
</root>

View File

@@ -35,7 +35,7 @@ namespace clashN.Forms
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
HideForm();
this.Text = Utils.GetVersion();
this.Text = Utils.GetVersion(); //Utils.IsAdministrator();
Global.processJob = new Job();
Application.ApplicationExit += (sender, args) =>

View File

@@ -16,6 +16,8 @@ namespace clashN.Handler
/// </summary>
class CoreConfigHandler
{
private static string SampleTun = "clashN.Sample.SampleTun.yaml";
/// <summary>
/// 生成配置文件
/// </summary>
@@ -74,6 +76,11 @@ namespace clashN.Handler
var config = LazyConfig.Instance.GetConfig();
var fileContent = Utils.FromYaml<Dictionary<string, object>>(File.ReadAllText(fileName));
if (fileContent == null)
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
//port
ModifyContent(fileContent, "port", config.httpPort);
//socks-port
@@ -93,6 +100,17 @@ namespace clashN.Handler
ModifyContent(fileContent, "allow-lan", "false");
}
//enable tun mode
if (node.enableTun)
{
string tun = Utils.GetEmbedText(SampleTun);
if (!Utils.IsNullOrEmpty(tun))
{
var tunContent = Utils.FromYaml<Dictionary<string, object>>(tun);
ModifyContent(fileContent, "tun", tunContent["tun"]);
}
}
File.WriteAllText(fileName, Utils.ToYaml(fileContent));
msg = string.Format(ResUI.SuccessfulConfiguration, $"{node.GetSummary()}");

View File

@@ -48,7 +48,7 @@ namespace clashN.Handler
else
{
ShowMsg(true, msg);
CoreRestart();
CoreRestart(item);
}
}
}
@@ -57,10 +57,10 @@ namespace clashN.Handler
/// <summary>
/// Core重启
/// </summary>
private void CoreRestart()
private void CoreRestart(ProfileItem item)
{
CoreStop();
CoreStart();
CoreStart(item);
}
/// <summary>
@@ -159,7 +159,7 @@ namespace clashN.Handler
/// <summary>
/// Core启动
/// </summary>
private void CoreStart()
private void CoreStart(ProfileItem item)
{
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString()));
@@ -182,6 +182,10 @@ namespace clashN.Handler
StandardOutputEncoding = Encoding.UTF8
}
};
if (item.enableTun)
{
p.StartInfo.Verb = "runas";
}
p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))

View File

@@ -113,6 +113,7 @@ namespace clashN.Mode
remarks = string.Empty;
testResult = string.Empty;
groupId = string.Empty;
enableTun = false;
}
#region function
@@ -173,6 +174,8 @@ namespace clashN.Mode
{
get; set;
} = string.Empty;
public bool enableTun { get; set; }
}
[Serializable]

View File

@@ -0,0 +1,6 @@
tun:
enable: true
stack: gvisor
dns-hijack:
- 0.0.0.0:53
auto-route: true

View File

@@ -88,6 +88,7 @@
<ItemGroup>
<Content Include="clashN.ico" />
<Content Include="Resources\help.png" />
<EmbeddedResource Include="Sample\SampleTun.yaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="log4net">
@@ -108,6 +109,7 @@
</ItemGroup>
<PropertyGroup />
<ItemGroup>
<None Remove="Sample\SampleTun.yaml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">

View File

@@ -47,6 +47,9 @@
<EmbeddedResource Update="Forms\AddProfileForm.resx">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Update="Forms\AddProfileForm.zh-Hans.resx">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Update="Forms\MainForm.resx">
<SubType>Designer</SubType>
</EmbeddedResource>