EduNetworkBuilder/EduNetworkBuilder/NetworkCardEditor.cs

160 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class NetworkCardEditor : Form
{
NetworkCard MyNicToEdit;
Button btnVPNEnd = null;
TextBox tbVPNEncrypt = null;
Label lblVPNEnd = null;
Label lblVPNEncrypt = null;
private NetworkCardEditor()
{
InitializeComponent();
LanguagifyComponents();
}
public NetworkCardEditor(NetworkCard NicToEdit)
{
InitializeComponent();
LanguagifyComponents();
if (NicToEdit.GetNicType == NicType.tun || NicToEdit.GetNicType == NicType.vpn)
{
AutoSize = false;
btnVPNEnd = new Button();
btnVPNEnd.Location = new Point(TBMacAddress.Location.X, cbDHCP.Location.Y + 28);
string btnval = "0.0.0.0";
if (NicToEdit.TunnelEndpoint != null)
btnval = NicToEdit.TunnelEndpoint.GetIPString;
btnVPNEnd.Text = btnval;
btnVPNEnd.Click += btnVPNEnd_Click;
lblVPNEnd = new Label();
lblVPNEnd.Location = new Point(10, btnVPNEnd.Location.Y);
lblVPNEnd.Text = "VPN Endpoint:";
Controls.Add(btnVPNEnd);
Controls.Add(lblVPNEnd);
this.Height = lblVPNEnd.Location.Y + lblVPNEnd.Height + 80;
if (NicToEdit.GetNicType == NicType.vpn)
{
tbVPNEncrypt = new TextBox();
tbVPNEncrypt.Location = new Point(TBMacAddress.Location.X, btnVPNEnd.Location.Y + 28);
lblVPNEncrypt = new Label();
lblVPNEncrypt.Location = new Point(10, tbVPNEncrypt.Location.Y);
lblVPNEncrypt.Text = "Encryption Key:";
Controls.Add(tbVPNEncrypt);
Controls.Add(lblVPNEncrypt);
this.Height = tbVPNEncrypt.Location.Y + tbVPNEncrypt.Height + 80;
}
Refresh();
//AutoSize = true;
}
TBMacAddress.Enabled = false;
tbNicName.Enabled = false;
tbNicType.Enabled = false;
cbCanUseDHCP.Enabled = false;
TBMacAddress.Text = NicToEdit.MAC;
tbNicName.Text = NicToEdit.NicName();
tbNicType.Text = NicToEdit.GetNicType.ToString();
MyNicToEdit = NicToEdit;
cbCanUseDHCP.Checked = NicToEdit.CanUseDHCP;
cbDHCP.Checked = NicToEdit.UsesDHCP;
if (!NicToEdit.CanUseDHCP) cbDHCP.Enabled = false;
if(NicToEdit.MustUseDHCP)
{
cbDHCP.Enabled = false;
cbCanUseDHCP.Enabled = false;
}
Network theNet = NB.GetNetwork();
NetworkDevice nd = theNet.GetDeviceFromID(NicToEdit.myID);
if(theNet.ItemIsLocked(nd.hostname, NicToEdit.NicName(), NetTestType.LockNic))
{
cbDHCP.Enabled = false;
cbCanUseDHCP.Enabled = false;
TBMacAddress.Enabled = false;
tbNicName.Enabled = false;
tbNicType.Enabled = false;
}
UpdateForm();
}
private void LanguagifyComponents()
{
Text = NB.Translate("NCE_cbCanUseDHCP");
Text = NB.Translate("NCE_cbDHCP");
Text = NB.Translate("NCE_label1");
Text = NB.Translate("NCE_label2");
Text = NB.Translate("NCE_label3");
Text = NB.Translate("NCE_btnDone");
Text = NB.Translate("NCE_lblLinkStatus");
Text = NB.Translate("NCE_Form");
}
private void UpdateForm()
{
if (btnVPNEnd != null)
{
string btnval = "0.0.0.0";
if (MyNicToEdit.TunnelEndpoint != null)
btnval = MyNicToEdit.TunnelEndpoint.GetIPString;
btnVPNEnd.Text = btnval;
}
if(tbVPNEncrypt != null)
{
tbVPNEncrypt.Text = MyNicToEdit.EncryptionKey;
}
if (MyNicToEdit.GetNicType == NicType.tun || MyNicToEdit.GetNicType == NicType.vpn ||
MyNicToEdit.GetNicType == NicType.management_interface || MyNicToEdit.GetNicType == NicType.lo)
{
lblLinkStatus.Visible = false;
}
else
lblLinkStatus.Visible = true;
if (MyNicToEdit.isConnected(true))
lblLinkStatus.Text = "Connected";
else
lblLinkStatus.Text = "Disconnected";
}
private void btnDone_Click(object sender, EventArgs e)
{
MyNicToEdit.CanUseDHCP = cbCanUseDHCP.Checked;
MyNicToEdit.UsesDHCP = cbDHCP.Checked;
if (tbVPNEncrypt != null)
{
MyNicToEdit.EncryptionKey = tbVPNEncrypt.Text;
}
Close();
}
private void btnVPNEnd_Click(object sender, EventArgs e)
{
Network theNet = NB.GetNetwork();
NetworkDevice nd = theNet.GetDeviceFromID(MyNicToEdit.myID);
if(MyNicToEdit.TunnelEndpoint == null)
{
MyNicToEdit.TunnelEndpoint = new IPAddress(NB.ZeroIPString, "255.255.255.0", IPAddressType.ip_only);
}
MyNicToEdit.TunnelEndpoint.Edit(nd,"Endpoint");
UpdateForm();
}
}
}