www.pudn.com > HttpProxy.rar > ProxyMain.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Threading; 
using System.Diagnostics; 
using System.Configuration; 
using HttpProxy.Component; 
 
namespace HttpProxy.UI 
{ 
	///  
	/// ProxyMain 类 
	///  
	public class ProxyMain : System.Windows.Forms.Form 
	{ 
		///  
		/// 必需的设计器变量。 
		///  
		private System.ComponentModel.Container components = null; 
 
 
		private CommandBarManager commandBarManager = null; 
		private CommandBar menuBar = null; 
		private CommandBar toolBar = null; 
 
		public static System.Windows.Forms.StatusBar sbrGlobal; 
		public static System.Windows.Forms.StatusBarPanel sbpGlobal; 
		internal static ProxyMain Global; 
 
		internal static bool AddUserOpen=false; 
		internal static bool DeleteUserOpen=false; 
		internal static bool MaintenUserOpen=false; 
		internal static bool MasterLogOpen=false; 
		internal static bool DetailLogOpen=false; 
 
		internal static User ProxyUser; 
		internal static string UserName="Everyone"; 
		internal static int UserId=-1; 
		internal static bool ProxyStart=false; 
		internal static bool UseLogin=bool.Parse(ConfigurationSettings.AppSettings["Login"]); 
		internal static string ConnString=ConfigurationSettings.AppSettings["ConnectionString"]; 
		internal static int ListenCount=Convert.ToInt32(ConfigurationSettings.AppSettings["ListenCount"]); 
		internal static bool LoginSucceed=false; 
		internal static ProxyServer Server; 
		internal static bool ServerStarted=false;		 
 
		///  
		/// Initializes a new instance of the  class. 
		///  
		public ProxyMain() 
		{ 
			if(ConfigurationSettings.AppSettings["SqlHelper"].ToLower()=="sqlserver") 
				User.Helper=new SqlServerHelper(ProxyMain.ConnString); 
			else 
				User.Helper=new AccessHelper(); 
			// 
			// Windows 窗体设计器支持所必需的 
			// 
			InitializeComponent(); 
 
			#region Menu & ToolBar 
 
			this.commandBarManager = new CommandBarManager(); 
			this.menuBar = new CommandBar(this.commandBarManager, CommandBarStyle.Menu); 
			this.toolBar = new CommandBar(this.commandBarManager, CommandBarStyle.ToolBar); 
 
			// Menu and toolbar 
			CommandBarButton startButton = new CommandBarButton(Images.Culture, "启动服务(&T)", new EventHandler(this.ServiceStart_Click), Keys.Control | Keys.T); 
			CommandBarButton delButton = new CommandBarButton(Images.Delete, "停止服务(&P)", new EventHandler(this.ServiceStop_Click), Keys.Control | Keys.P); 
			CommandBarButton resButton = new CommandBarButton(Images.Mail, "重启服务(&R)", new EventHandler(this.ServiceReStart_Click), Keys.Control | Keys.R); 
	 
			toolBar.Items.Add(startButton); 
			toolBar.Items.Add(delButton); 
			toolBar.Items.Add(resButton); 
			toolBar.Items.AddSeparator(); 
	 
			CommandBarButton adduserButton = new CommandBarButton(Images.New, "添加用户(&N)", new EventHandler(this.AddUser_Click), Keys.Control | Keys.N); 
			CommandBarButton maintenButton = new CommandBarButton(Images.Details, "维护用户(&M)", new EventHandler(this.MaintenUser_Click), Keys.Control | Keys.M); 
			CommandBarButton deleteuserButton = new CommandBarButton(Images.Stop, "删除用户(&D)", new EventHandler(this.DeleteUser_Click), Keys.Control | Keys.D); 
			 
			this.toolBar.Items.Add(adduserButton); 
			this.toolBar.Items.Add(maintenButton); 
			this.toolBar.Items.AddSeparator(); 
 
			CommandBarMenu serviceMenu = menuBar.Items.AddMenu("服务(&S)"); 
			serviceMenu.Items.Add(startButton); 
			serviceMenu.Items.Add(delButton); 
			serviceMenu.Items.Add(resButton); 
			serviceMenu.Items.AddSeparator(); 
			serviceMenu.Items.AddButton("E&xit", new EventHandler(this.Exit_Click)); 
		 
			CommandBarMenu userMenu = this.menuBar.Items.AddMenu("用户(&M)"); 
			userMenu.Items.Add(adduserButton); 
			userMenu.Items.Add(maintenButton); 
			userMenu.Items.Add(deleteuserButton); 
 
			CommandBarButton viewlogButton=new CommandBarButton(Images.Preview,"查看日志(&L)",new EventHandler(this.ViewLog_Click),Keys.Control | Keys.L); 
			CommandBarButton clearlogButton=new CommandBarButton(Images.FolderProperties,"清除日志(&K)",new EventHandler(this.ClearLog_Click),Keys.Control | Keys.K); 
			CommandBarButton fontButton=new CommandBarButton(Images.Languages,"字体(&F)...",new EventHandler(this.ChangeFont_Click)); 
 
			CommandBarMenu logMenu=this.menuBar.Items.AddMenu("工具(&L)"); 
			logMenu.Items.Add(viewlogButton); 
			logMenu.Items.Add(clearlogButton); 
			logMenu.Items.AddSeparator(); 
			logMenu.Items.Add(fontButton); 
 
			CommandBarButton toolButton=new CommandBarButton(Images.Tools,"工具栏(&T)",new EventHandler(this.Tools_Click)); 
			CommandBarButton statusButton=new CommandBarButton(Images.Icons,"状态栏(&S)",new EventHandler(this.Status_Click)); 
 
			CommandBarMenu viewMenu=this.menuBar.Items.AddMenu("视窗(&V)"); 
			viewMenu.Items.Add(toolButton); 
			viewMenu.Items.Add(statusButton); 
 
			CommandBarButton helpButton=new CommandBarButton(Images.Help,"帮助文件(&F)",new EventHandler(this.Help_Click),Keys.F1); 
			CommandBarButton aboutButton=new CommandBarButton(Images.Home,"关于(&A)...",new EventHandler(this.About_Click)); 
 
			CommandBarMenu helpMenu=this.menuBar.Items.AddMenu("帮助(&H)"); 
			helpMenu.Items.Add(helpButton); 
			helpMenu.Items.AddSeparator(); 
			helpMenu.Items.Add(aboutButton); 
 
			this.toolBar.Items.Add(viewlogButton); 
			this.toolBar.Items.AddSeparator(); 
			this.toolBar.Items.Add(helpButton); 
			this.toolBar.Items.Add(aboutButton); 
			 
			this.commandBarManager.CommandBars.Add(this.menuBar); 
			this.commandBarManager.CommandBars.Add(this.toolBar); 
			this.Controls.Add(this.commandBarManager); 
			 
			// Context menu 
			CommandBarContextMenu contextMenu = new CommandBarContextMenu(); 
			contextMenu.Items.Add(startButton); 
			contextMenu.Items.Add(delButton); 
			contextMenu.Items.AddSeparator(); 
			contextMenu.Items.Add(viewlogButton); 
			contextMenu.Items.Add(clearlogButton); 
			contextMenu.Items.AddSeparator(); 
			contextMenu.Items.Add(aboutButton); 
			this.ContextMenu = contextMenu; 
 
			// Status Bar 
			sbrGlobal = new StatusBar(); 
			sbpGlobal = new StatusBarPanel(); 
			((System.ComponentModel.ISupportInitialize)(sbpGlobal)).BeginInit(); 
			sbrGlobal.Location = new System.Drawing.Point(0, this.ClientSize.Height); 
			sbrGlobal.Name = "sbrGlobal"; 
			sbrGlobal.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {sbpGlobal}); 
			sbrGlobal.ShowPanels = true; 
			sbrGlobal.Size = new System.Drawing.Size(this.ClientSize.Width, 20); 
			sbrGlobal.SizingGrip = false; 
			sbpGlobal.Width = this.ClientSize.Width; 
			sbpGlobal.Text = " 请选择菜单进行操作..."; 
			sbpGlobal.Style=StatusBarPanelStyle.Text; 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {sbrGlobal}); 
			((System.ComponentModel.ISupportInitialize)(sbpGlobal)).EndInit(); 
 
			Global=this; 
 
			#endregion 
 
		} 
 
		#region Instance Method 
 
		///  
		/// 清理所有正在使用的资源。 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null)  
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows 窗体设计器生成的代码 
		///  
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
		/// 此方法的内容。 
		///  
		private void InitializeComponent() 
		{ 
			//  
			// ProxyMain 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.ClientSize = new System.Drawing.Size(642, 448); 
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; 
			this.IsMdiContainer = true; 
			this.Name = "ProxyMain"; 
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
			this.Text = "Http 代理 v1.0"; 
			this.Load += new System.EventHandler(this.ProxyMain_Load); 
			this.Activated += new System.EventHandler(this.ProxyMain_Activated); 
 
		} 
		#endregion 
 
		///  
		/// Processes the CMD key. 
		///  
		/// The MSG. 
		/// The key data. 
		///  
		protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
		{ 
			if (this.commandBarManager.PreProcessMessage(ref msg)) 
			{ 
				return true; 
			} 
 
			return base.ProcessCmdKey(ref msg, keyData); 
		} 
	 
		///  
		/// Handles the Click event of the Exit control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void Exit_Click(object sender, EventArgs e) 
		{ 
			this.Close(); 
		} 
 
		///  
		/// Handles the Click event of the About control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void About_Click(object sender, EventArgs e) 
		{ 
			Form child =new About(); 
			child.ShowDialog(); 
		} 
 
		///  
		/// Handles the Click event of the Tools control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void Tools_Click(object sender, EventArgs e) 
		{ 
			this.toolBar.Visible=!this.toolBar.Visible; 
		} 
 
		///  
		/// Handles the Click event of the Status control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void Status_Click(object sender, EventArgs e) 
		{ 
			sbrGlobal.Visible=!sbrGlobal.Visible; 
		} 
 
		///  
		/// Handles the Click event of the AddUser control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void AddUser_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.UseLogin==false) 
			{ 
				MessageBox.Show("未启用用户登陆!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
 
			} 
 
			if(ProxyMain.AddUserOpen==false) 
			{ 
				AddUser child=new AddUser(); 
				child.MdiParent=this; 
				child.Show(); 
				ProxyMain.AddUserOpen=true; 
			} 
			else 
			{ 
				for(int i=0;i 
		/// Handles the Click event of the MaintenUser control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void MaintenUser_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.UseLogin==false) 
			{ 
				MessageBox.Show("未启用用户登陆!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
 
			if(ProxyMain.MaintenUserOpen==false) 
			{ 
				MaintenUser child=new MaintenUser(); 
				child.MdiParent=this; 
				child.Show(); 
				ProxyMain.MaintenUserOpen=true; 
			} 
			else 
			{ 
				for(int i=0;i 
		/// Handles the Click event of the DeleteUser control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void DeleteUser_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.UseLogin==false) 
			{ 
				MessageBox.Show("未启用用户登陆!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
 
			if(ProxyMain.DeleteUserOpen==false) 
			{ 
				DeleteUser child=new DeleteUser(); 
				child.MdiParent=this; 
				child.Show(); 
				ProxyMain.DeleteUserOpen=true; 
			} 
			else 
			{ 
				for(int i=0;i 
		/// Handles the Click event of the ViewLog control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ViewLog_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.ServerStarted==false) 
			{ 
				MessageBox.Show("请先启动代理服务!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
 
			if(ProxyMain.MasterLogOpen==false) 
			{ 
				MasterLog child=new MasterLog(); 
				child.MdiParent=this; 
				child.Show(); 
				ProxyMain.MasterLogOpen=true; 
			} 
			else 
			{ 
				for(int i=0;i 
		/// Handles the Click event of the ClearLog control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ClearLog_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.ServerStarted==false) 
			{ 
				MessageBox.Show("请先启动代理服务!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
 
			ProxyServer.dsLog.Clear(); 
 
			if(ProxyMain.MasterLogOpen==false) 
			{ 
				MasterLog child=new MasterLog(); 
				child.MdiParent=this; 
				child.Show(); 
				ProxyMain.MasterLogOpen=true; 
			} 
			else 
			{ 
				for(int i=1;i 
		/// Handles the Click event of the ServiceStart control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ServiceStart_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.ServerStarted==false) 
			{ 
				Port child=new Port(); 
				child.ShowDialog(); 
			} 
			else 
				MessageBox.Show("代理已启动!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
		} 
 
		///  
		/// Handles the Click event of the ServiceStop control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ServiceStop_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.ServerStarted==false) 
			{ 
				MessageBox.Show("代理服务未启动!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
 
			if(MessageBox.Show("确认停止代理服务?","HttpProxy",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes) 
			{ 
				this.Cursor=Cursors.WaitCursor; 
				foreach(object o in ProxyServer.arrThread) 
				{ 
					((Thread)o).Abort(); 
				} 
				ProxyMain.Server.Dispose(); 
				for(int i=0;i 
		/// Handles the Click event of the ServiceReStart control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ServiceReStart_Click(object sender, EventArgs e) 
		{ 
			if(ProxyMain.ServerStarted==false) 
			{ 
				MessageBox.Show("代理服务未启动!","HttpProxy",MessageBoxButtons.OK,MessageBoxIcon.Information); 
				return; 
			} 
 
			if(MessageBox.Show("确认重启代理服务?","HttpProxy",MessageBoxButtons.YesNo,MessageBoxIcon.Information)!=DialogResult.Yes) 
				return; 
 
			this.Cursor=Cursors.WaitCursor; 
			foreach(object o in ProxyServer.arrThread) 
			{ 
				((Thread)o).Abort(); 
			} 
			int port=ProxyMain.Server.Port; 
			ProxyMain.Server.Dispose(); 
			for(int i=0;i 
		/// Handles the Click event of the ChangeFont control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ChangeFont_Click(object sender, EventArgs e) 
		{ 
			FontDialog dialog = new FontDialog(); 
			dialog.Font = menuBar.Font; 
			 
			if (dialog.ShowDialog() == DialogResult.OK) 
			{ 
				menuBar.Font = dialog.Font; 
				toolBar.Font = dialog.Font; 
			} 
		} 
 
		private void Help_Click(object sender, EventArgs e) 
		{ 
			Process.Start(Application.StartupPath+@"\"+ConfigurationSettings.AppSettings["Help"]); 
		} 
 
		#endregion 
 
		///  
		/// 应用程序的主入口点 
		///  
		[STAThread] 
		static void Main()  
		{ 
			Application.ThreadException+=new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 
			Application.Run(new ProxyMain()); 
		} 
 
		///  
		/// Handles the Load event of the ProxyMain control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ProxyMain_Load(object sender, System.EventArgs e) 
		{ 
			if(UseLogin!=false) 
			{ 
				Login child=new Login(); 
				child.ShowDialog(); 
			} 
		} 
 
		///  
		/// Handles the Activated event of the ProxyMain control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private void ProxyMain_Activated(object sender, System.EventArgs e) 
		{ 
			sbpGlobal.Text = " 请选择菜单进行操作..."; 
			if(UseLogin!=false && LoginSucceed==false) 
			{ 
				Application.Exit(); 
			} 
		} 
 
		///  
		/// Handles the ThreadException event of the Application control. 
		///  
		/// The source of the event. 
		/// The  instance containing the event data. 
		private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
		{ 
			MessageBox.Show("待处理!!!","HttpProxy 错误",MessageBoxButtons.OK,MessageBoxIcon.Warning); 
		} 
	} 
}