@@ -71,7 +71,10 @@ namespace Shadowsocks.Controller | |||
} | |||
if (hostMatch && pathMatch) | |||
{ | |||
SendResponse(firstPacket, length, socket, useSocks); | |||
if (_config.useOnlinePac && !string.IsNullOrEmpty(_config.pacUrl)) | |||
RedirectToOnlinePAC(firstPacket, length, socket, useSocks); | |||
else | |||
SendResponse(firstPacket, length, socket, useSocks); | |||
return true; | |||
} | |||
return false; | |||
@@ -121,6 +124,30 @@ namespace Shadowsocks.Controller | |||
} | |||
} | |||
private void RedirectToOnlinePAC(byte[] firstPacket, int length, Socket socket, bool useSocks) | |||
{ | |||
try | |||
{ | |||
string friendlyMessage = "Redirect to online pac " + _config.pacUrl; | |||
string text = String.Format(@"HTTP/1.1 302 Found | |||
Server: Shadowsocks | |||
Content-Type: text/html; charset=utf-8 | |||
Location: {0} | |||
Content-Length: {1} | |||
Connection: Close | |||
", _config.pacUrl, System.Text.Encoding.UTF8.GetBytes(friendlyMessage).Length) + friendlyMessage; | |||
byte[] response = System.Text.Encoding.UTF8.GetBytes(text); | |||
socket.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), socket); | |||
Util.Utils.ReleaseMemory(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
socket.Close(); | |||
} | |||
} | |||
public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks) | |||
{ | |||
try | |||
@@ -192,6 +192,28 @@ namespace Shadowsocks.Controller | |||
} | |||
} | |||
public void SavePACUrl(string pacUrl) | |||
{ | |||
_config.pacUrl = pacUrl; | |||
UpdateSystemProxy(); | |||
SaveConfig(_config); | |||
if (ConfigChanged != null) | |||
{ | |||
ConfigChanged(this, new EventArgs()); | |||
} | |||
} | |||
public void UseOnlinePAC(bool useOnlinePac) | |||
{ | |||
_config.useOnlinePac = useOnlinePac; | |||
UpdateSystemProxy(); | |||
SaveConfig(_config); | |||
if (ConfigChanged != null) | |||
{ | |||
ConfigChanged(this, new EventArgs()); | |||
} | |||
} | |||
protected void Reload() | |||
{ | |||
// some logic in configuration updated the config when saving, we need to read it again | |||
@@ -40,6 +40,11 @@ New server=未配置的服务器 | |||
QRCode=二维码 | |||
# PAC Url Form | |||
Update Online PAC URL=更新在线 PAC 网址 | |||
PAC Url=PAC 网址 | |||
# Messages | |||
Shadowsocks Error: {0}=Shadowsocks 错误: {0} | |||
@@ -63,4 +68,5 @@ No QRCode found. Try to zoom in or move it to the center of the screen.=未发 | |||
Failed to decode QRCode=无法解析二维码 | |||
Failed to update registry=无法修改注册表 | |||
System Proxy On: =系统代理已启用: | |||
Running: Port {0}=正在运行:端口 {0} | |||
Running: Port {0}=正在运行:端口 {0} | |||
PAC Url can not be blank=PAC 网址不能为空 |
@@ -17,6 +17,8 @@ namespace Shadowsocks.Model | |||
public bool shareOverLan; | |||
public bool isDefault; | |||
public int localPort; | |||
public string pacUrl; | |||
public bool useOnlinePac; | |||
private static string CONFIG_FILE = "gui-config.json"; | |||
@@ -35,7 +35,10 @@ namespace Shadowsocks.View | |||
private MenuItem ServersItem; | |||
private MenuItem globalModeItem; | |||
private MenuItem PACModeItem; | |||
private MenuItem localPACItem; | |||
private MenuItem onlinePACItem; | |||
private ConfigForm configForm; | |||
private PACUrlForm pacUrlForm; | |||
private string _urlToOpen; | |||
public MenuViewController(ShadowsocksController controller) | |||
@@ -151,10 +154,15 @@ namespace Shadowsocks.View | |||
CreateMenuItem("Show QRCode...", new EventHandler(this.QRCodeItem_Click)), | |||
CreateMenuItem("Scan QRCode from Screen...", new EventHandler(this.ScanQRCodeItem_Click)) | |||
}), | |||
new MenuItem("-"), | |||
CreateMenuItem("Edit PAC File...", new EventHandler(this.EditPACFileItem_Click)), | |||
CreateMenuItem("Update PAC from GFWList", new EventHandler(this.UpdatePACFromGFWListItem_Click)), | |||
CreateMenuItem("Edit User Rule for GFWList...", new EventHandler(this.EditUserRuleFileForGFWListItem_Click)), | |||
CreateMenuGroup("PAC", new MenuItem[] { | |||
this.localPACItem = CreateMenuItem("Local PAC", new EventHandler(this.LocalPACItem_Click)), | |||
this.onlinePACItem = CreateMenuItem("Online PAC", new EventHandler(this.OnlinePACItem_Click)), | |||
new MenuItem("-"), | |||
CreateMenuItem("Edit PAC File...", new EventHandler(this.EditPACFileItem_Click)), | |||
CreateMenuItem("Update PAC from GFWList", new EventHandler(this.UpdatePACFromGFWListItem_Click)), | |||
CreateMenuItem("Edit User Rule for GFWList...", new EventHandler(this.EditUserRuleFileForGFWListItem_Click)), | |||
CreateMenuItem("Update Online PAC URL", new EventHandler(this.UpdateOnlinePACURLItem_Click)), | |||
}), | |||
new MenuItem("-"), | |||
this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)), | |||
this.ShareOverLANItem = CreateMenuItem("Allow Clients from LAN", new EventHandler(this.ShareOverLANItem_Click)), | |||
@@ -240,6 +248,9 @@ namespace Shadowsocks.View | |||
PACModeItem.Checked = !config.global; | |||
ShareOverLANItem.Checked = config.shareOverLan; | |||
AutoStartupItem.Checked = AutoStartup.Check(); | |||
onlinePACItem.Enabled = !string.IsNullOrEmpty(config.pacUrl); | |||
onlinePACItem.Checked = onlinePACItem.Enabled && config.useOnlinePac; | |||
localPACItem.Checked = !onlinePACItem.Checked; | |||
} | |||
private void UpdateServersMenu() | |||
@@ -481,5 +492,51 @@ namespace Shadowsocks.View | |||
MessageBox.Show(I18N.GetString("Failed to update registry")); | |||
} | |||
} | |||
private void LocalPACItem_Click(object sender, EventArgs e) | |||
{ | |||
if (!localPACItem.Checked) | |||
{ | |||
localPACItem.Checked = true; | |||
onlinePACItem.Checked = false; | |||
controller.UseOnlinePAC(false); | |||
} | |||
} | |||
private void OnlinePACItem_Click(object sender, EventArgs e) | |||
{ | |||
if (!onlinePACItem.Checked) | |||
{ | |||
localPACItem.Checked = false; | |||
onlinePACItem.Checked = true; | |||
controller.UseOnlinePAC(true); | |||
} | |||
} | |||
private void showPACUrlForm() | |||
{ | |||
if (pacUrlForm != null) | |||
{ | |||
pacUrlForm.Activate(); | |||
} | |||
else | |||
{ | |||
pacUrlForm = new PACUrlForm(controller); | |||
pacUrlForm.Show(); | |||
pacUrlForm.FormClosed += pacUrlForm_FormClosed; | |||
} | |||
} | |||
private void pacUrlForm_FormClosed(object sender, FormClosedEventArgs e) | |||
{ | |||
pacUrlForm = null; | |||
Util.Utils.ReleaseMemory(); | |||
ShowFirstTimeBalloon(); | |||
} | |||
private void UpdateOnlinePACURLItem_Click(object sender, EventArgs e) | |||
{ | |||
showPACUrlForm(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,105 @@ | |||
namespace Shadowsocks.View | |||
{ | |||
partial class PACUrlForm | |||
{ | |||
/// <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() | |||
{ | |||
this.PACUrlTextBox = new System.Windows.Forms.TextBox(); | |||
this.PACUrlLabel = new System.Windows.Forms.Label(); | |||
this.OkButton = new System.Windows.Forms.Button(); | |||
this.CancelButton = new System.Windows.Forms.Button(); | |||
this.SuspendLayout(); | |||
// | |||
// PACUrlTextBox | |||
// | |||
this.PACUrlTextBox.Location = new System.Drawing.Point(61, 12); | |||
this.PACUrlTextBox.MaxLength = 256; | |||
this.PACUrlTextBox.Name = "PACUrlTextBox"; | |||
this.PACUrlTextBox.Size = new System.Drawing.Size(245, 20); | |||
this.PACUrlTextBox.TabIndex = 4; | |||
this.PACUrlTextBox.WordWrap = false; | |||
// | |||
// PACUrlLabel | |||
// | |||
this.PACUrlLabel.AutoSize = true; | |||
this.PACUrlLabel.Location = new System.Drawing.Point(6, 15); | |||
this.PACUrlLabel.Name = "PACUrlLabel"; | |||
this.PACUrlLabel.Size = new System.Drawing.Size(44, 13); | |||
this.PACUrlLabel.TabIndex = 3; | |||
this.PACUrlLabel.Text = "PAC Url"; | |||
// | |||
// OkButton | |||
// | |||
this.OkButton.Location = new System.Drawing.Point(150, 50); | |||
this.OkButton.Name = "OkButton"; | |||
this.OkButton.Size = new System.Drawing.Size(75, 23); | |||
this.OkButton.TabIndex = 5; | |||
this.OkButton.Text = "OK"; | |||
this.OkButton.UseVisualStyleBackColor = true; | |||
this.OkButton.Click += new System.EventHandler(this.OkButton_Click); | |||
// | |||
// CancelButton | |||
// | |||
this.CancelButton.Location = new System.Drawing.Point(231, 50); | |||
this.CancelButton.Name = "CancelButton"; | |||
this.CancelButton.Size = new System.Drawing.Size(75, 23); | |||
this.CancelButton.TabIndex = 6; | |||
this.CancelButton.Text = "Cancel"; | |||
this.CancelButton.UseVisualStyleBackColor = true; | |||
this.CancelButton.Click += new System.EventHandler(this.CancelButton_Click); | |||
// | |||
// PACUrlForm | |||
// | |||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); | |||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |||
this.AutoSize = true; | |||
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; | |||
this.ClientSize = new System.Drawing.Size(327, 88); | |||
this.Controls.Add(this.CancelButton); | |||
this.Controls.Add(this.OkButton); | |||
this.Controls.Add(this.PACUrlTextBox); | |||
this.Controls.Add(this.PACUrlLabel); | |||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; | |||
this.MaximizeBox = false; | |||
this.MinimizeBox = false; | |||
this.Name = "PACUrlForm"; | |||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; | |||
this.Text = "Update Online PAC URL"; | |||
this.Load += new System.EventHandler(this.PACUrlForm_Load); | |||
this.ResumeLayout(false); | |||
this.PerformLayout(); | |||
} | |||
#endregion | |||
private System.Windows.Forms.TextBox PACUrlTextBox; | |||
private System.Windows.Forms.Label PACUrlLabel; | |||
private System.Windows.Forms.Button OkButton; | |||
private System.Windows.Forms.Button CancelButton; | |||
} | |||
} |
@@ -0,0 +1,69 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel; | |||
using System.Data; | |||
using System.Drawing; | |||
using System.Text; | |||
using System.Windows.Forms; | |||
using Shadowsocks.Controller; | |||
using Shadowsocks.Model; | |||
using Shadowsocks.Properties; | |||
namespace Shadowsocks.View | |||
{ | |||
public partial class PACUrlForm : Form | |||
{ | |||
private ShadowsocksController controller; | |||
private string orig_pacUrl; | |||
public PACUrlForm(ShadowsocksController controller) | |||
{ | |||
this.Font = System.Drawing.SystemFonts.MessageBoxFont; | |||
InitializeComponent(); | |||
UpdateTexts(); | |||
this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); | |||
this.controller = controller; | |||
controller.ConfigChanged += controller_ConfigChanged; | |||
} | |||
private void UpdateTexts() | |||
{ | |||
OkButton.Text = I18N.GetString("OK"); | |||
CancelButton.Text = I18N.GetString("Cancel"); | |||
PACUrlLabel.Text = I18N.GetString("PAC Url"); | |||
this.Text = I18N.GetString("Update Online PAC URL"); | |||
} | |||
private void controller_ConfigChanged(object sender, EventArgs e) | |||
{ | |||
orig_pacUrl = PACUrlTextBox.Text = controller.GetConfiguration().pacUrl; | |||
} | |||
private void PACUrlForm_Load(object sender, EventArgs e) | |||
{ | |||
PACUrlTextBox.Text = controller.GetConfiguration().pacUrl; | |||
} | |||
private void OkButton_Click(object sender, EventArgs e) | |||
{ | |||
string pacUrl = PACUrlTextBox.Text.Trim(); | |||
if (string.IsNullOrEmpty(pacUrl)) | |||
{ | |||
MessageBox.Show(I18N.GetString("PAC Url can not be blank")); | |||
return; | |||
} | |||
if (pacUrl != this.orig_pacUrl) | |||
{ | |||
controller.SavePACUrl(pacUrl); | |||
} | |||
this.Close(); | |||
} | |||
private void CancelButton_Click(object sender, EventArgs e) | |||
{ | |||
this.Close(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,120 @@ | |||
<?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"> | |||
<xsd:complexType> | |||
<xsd:choice maxOccurs="unbounded"> | |||
<xsd:element name="metadata"> | |||
<xsd:complexType> | |||
<xsd:sequence> | |||
<xsd:element name="value" type="xsd:string" minOccurs="0" /> | |||
</xsd:sequence> | |||
<xsd:attribute name="name" use="required" type="xsd:string" /> | |||
<xsd:attribute name="type" type="xsd:string" /> | |||
<xsd:attribute name="mimetype" type="xsd:string" /> | |||
<xsd:attribute ref="xml:space" /> | |||
</xsd:complexType> | |||
</xsd:element> | |||
<xsd:element name="assembly"> | |||
<xsd:complexType> | |||
<xsd:attribute name="alias" type="xsd:string" /> | |||
<xsd:attribute name="name" type="xsd:string" /> | |||
</xsd:complexType> | |||
</xsd:element> | |||
<xsd:element name="data"> | |||
<xsd:complexType> | |||
<xsd:sequence> | |||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | |||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> | |||
</xsd:sequence> | |||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> | |||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> | |||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> | |||
<xsd:attribute ref="xml:space" /> | |||
</xsd:complexType> | |||
</xsd:element> | |||
<xsd:element name="resheader"> | |||
<xsd:complexType> | |||
<xsd:sequence> | |||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | |||
</xsd:sequence> | |||
<xsd:attribute name="name" type="xsd:string" use="required" /> | |||
</xsd:complexType> | |||
</xsd:element> | |||
</xsd:choice> | |||
</xsd:complexType> | |||
</xsd:element> | |||
</xsd:schema> | |||
<resheader name="resmimetype"> | |||
<value>text/microsoft-resx</value> | |||
</resheader> | |||
<resheader name="version"> | |||
<value>2.0</value> | |||
</resheader> | |||
<resheader name="reader"> | |||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
<resheader name="writer"> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
</root> |
@@ -162,6 +162,12 @@ | |||
<Compile Include="Controller\ShadowsocksController.cs" /> | |||
<Compile Include="Controller\SystemProxy.cs" /> | |||
<Compile Include="View\MenuViewController.cs" /> | |||
<Compile Include="View\PACUrlForm.cs"> | |||
<SubType>Form</SubType> | |||
</Compile> | |||
<Compile Include="View\PACUrlForm.Designer.cs"> | |||
<DependentUpon>PACUrlForm.cs</DependentUpon> | |||
</Compile> | |||
<Compile Include="View\QRCodeForm.cs"> | |||
<SubType>Form</SubType> | |||
</Compile> | |||
@@ -180,6 +186,9 @@ | |||
<SubType>Designer</SubType> | |||
<LastGenOutput>Resources.Designer.cs</LastGenOutput> | |||
</EmbeddedResource> | |||
<EmbeddedResource Include="View\PACUrlForm.resx"> | |||
<DependentUpon>PACUrlForm.cs</DependentUpon> | |||
</EmbeddedResource> | |||
<EmbeddedResource Include="View\QRCodeForm.resx"> | |||
<DependentUpon>QRCodeForm.cs</DependentUpon> | |||
</EmbeddedResource> | |||