Skip to content
This repository has been archived by the owner on Sep 2, 2019. It is now read-only.

Commit

Permalink
IP v6.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCube committed Jun 1, 2017
1 parent 3991b40 commit e5b129a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 52 deletions.
119 changes: 83 additions & 36 deletions ACTWebSocket.Core/ACTWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace ACTWebSocket_Plugin
using Microsoft.Win32;
using System.Reflection;
using System.Linq;
using System.Net.NetworkInformation;

public interface PluginDirectory
{
Expand Down Expand Up @@ -419,6 +420,7 @@ private void InitializeComponent()
//
// hostnames
//
this.hostnames.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.hostnames.FormattingEnabled = true;
resources.ApplyResources(this.hostnames, "hostnames");
this.hostnames.Name = "hostnames";
Expand Down Expand Up @@ -988,6 +990,7 @@ private void InitializeComponent()

#endregion

String externalIP = null;
System.Timers.Timer overlayProcCheckTimer = null;
public ACTWebSocketMain()
{
Expand Down Expand Up @@ -1393,7 +1396,19 @@ void LoadSettings()
catch (Exception e)
{
}
hostnames.Text = Hostname;
{
int host = Array.IndexOf(addressMap.Keys.ToArray(), Hostname);
if(host >= 0)
{
hostnames.SelectedIndex = host;
}
else
{
Hostname = "Loopback (127.0.0.1)";
int host2 = Array.IndexOf(addressMap.Keys.ToArray(), Hostname);
hostnames.SelectedIndex = host2;
}
}
}
}

Expand Down Expand Up @@ -1436,24 +1451,42 @@ void SaveSettings()
String currentVersionString = null;
String latestVersionString = null;

Dictionary<String, String> addressMap = new Dictionary<String, String>();

private void ACTWebSocket_Load(object sender, EventArgs e)
{
comboBoxOverlayProcType.SelectedIndex = 0;
hostnames.Text = "127.0.0.1";
String strHostName = string.Empty;
strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);

addressMap["Any (0.0.0.0)"] = System.Net.IPAddress.Any.ToString();
addressMap["Any IPV6([::])"] = "[" + System.Net.IPAddress.IPv6Any.ToString() + "]";
addressMap["Loopback (127.0.0.1)"] = System.Net.IPAddress.Loopback.ToString();
addressMap["Loopback IPV6([::1])"] = "[" + System.Net.IPAddress.IPv6Loopback.ToString() + "]";

addrs.Clear();
addrs.Add("127.0.0.1");
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
if(ni.OperationalStatus == OperationalStatus.Up)
{
if (addr[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
addrs.Add(addr[i].ToString());
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
addrs.Add(ip.Address.ToString());
addressMap[ip.Address.ToString()] = ip.Address.ToString();
}
}
}
}
}
foreach (var i in addressMap)
{
addrs.Add(i.Key);
}
addrs = Utility.Distinct<String>(addrs);
addrs.Sort();
core.SetAddress(addrs);
Expand All @@ -1463,22 +1496,31 @@ private void ACTWebSocket_Load(object sender, EventArgs e)
hostnames.Items.Add(addr);
}

hostnames.SelectedIndex = 0;

Task task = Task.Factory.StartNew(() =>
{
String ipaddress = Utility.GetExternalIp();
if (ipaddress.Length > 0)
addrs.Add(ipaddress);

addrs = Utility.Distinct<String>(addrs);
addrs.Sort();
core.SetAddress(addrs);

hostnames.Items.Clear();
foreach (var addr in addrs)
{
hostnames.Items.Add(addr);
}
externalIP = ipaddress;
});

//Task task = Task.Factory.StartNew(() =>
//{
// String ipaddress = Utility.GetExternalIp();
// if (ipaddress.Length > 0)
// addrs.Add(ipaddress);

// addrs = Utility.Distinct<String>(addrs);
// addrs.Sort();
// core.SetAddress(addrs);

// hostnames.Items.Clear();
// foreach (var addr in addrs)
// {
// hostnames.Items.Add(addr);
// }
//});
VersionCheck();
OverlayVersionCheck(gamepath.Text);
CheckUpdate();
Expand Down Expand Up @@ -1680,19 +1722,6 @@ public void StartServer()
upnpTask.Start();
}

var addresses = Dns.GetHostAddresses(Hostname);

bool localhostOnly = false;
for(int i=0;i< addresses.Length;++i)
{
var ip = addresses[i];
if (IPAddress.IsLoopback(ip))
{
localhostOnly = true;
break;
}
}

if (RandomURL)
{
ACTWebSocketCore.randomDir = Guid.NewGuid().ToString();
Expand All @@ -1703,13 +1732,18 @@ public void StartServer()
}
try
{
String address = IPAddress.Any.ToString();
if(Array.IndexOf(addressMap.Keys.ToArray(), Hostname) >=0)
{
address = addressMap[Hostname];
}
if (UseUPnP)
{
core.StartServer(localhostOnly ? "127.0.0.1" : "0.0.0.0", Port, UPnPPort, Hostname, SkinOnAct, UseSSL);
core.StartServer(address, Port, UPnPPort, Hostname, SkinOnAct, UseSSL);
}
else
{
core.StartServer(localhostOnly ? "127.0.0.1" : "0.0.0.0", Port, Port, Hostname, SkinOnAct, UseSSL);
core.StartServer(address, Port, Port, Hostname, SkinOnAct, UseSSL);
}
}
catch (Exception e)
Expand Down Expand Up @@ -1913,7 +1947,20 @@ public string getURLPath(string skinPath = "", bool withRandomURL = true)
{
string url = "";
{
url = "://" + Hostname + ":" + Port + "/";
String address = IPAddress.Any.ToString();
if (Array.IndexOf(addressMap.Keys.ToArray(), Hostname) >= 0)
{
address = addressMap[Hostname];
if (address == "[::]")
{
address = (externalIP != null) ? externalIP : "[::1]";
}
if (address == "0.0.0.0")
{
address = (externalIP != null) ? externalIP : "127.0.0.1";
}
}
url = "://" + address + ":" + Port + "/";
}
if (withRandomURL)
{
Expand All @@ -1926,7 +1973,7 @@ public string getURLPath(string skinPath = "", bool withRandomURL = true)
{
try
{
Uri uri = new Uri(skinPath + "?HOST_PORT=" + "ws" + url);
Uri uri = new Uri(skinPath + "?HOST_PORT=" + (UseSSL ? "wss" : "ws") + url);
return uri.ToString();
}
catch (Exception e)
Expand All @@ -1940,7 +1987,7 @@ public string getURLPath(string skinPath = "", bool withRandomURL = true)
{
try
{
string fullURL = ("http") + url + Uri.EscapeDataString(skinPath);
string fullURL = (UseSSL ? "https" : "http") + url + Uri.EscapeDataString(skinPath);
fullURL = fullURL.Replace("%5C", "/");
fullURL = fullURL.Replace("%2F", "/");
return fullURL;
Expand Down
30 changes: 14 additions & 16 deletions ACTWebSocket.Core/ACTWebSocketCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,18 @@ internal void StartServer(string address, int port, int extPort, string domain =
{
httpServer.SslConfiguration.ServerCertificate = GenerateCertificate(domain);
}

try
{
// for Parser's MemoryError
var field = typeof(WebSocket).GetField("FragmentLength",
BindingFlags.Static |
BindingFlags.NonPublic);
int value = (int)field.GetValue(null);
field.SetValue(null, Int32.MaxValue - 14);
}
catch(Exception e)
{
}
// TODO : SSL
//wssv = new WebSocketServer(System.Net.IPAddress.Parse(address), port, true);
//wssv.SslConfiguration.ServerCertificate =
Expand Down Expand Up @@ -208,27 +219,14 @@ internal void StartServer(string address, int port, int extPort, string domain =
{
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
string host = "";
if (address == "127.0.0.1" || address == "localhost")
{
host = "localhost";
}
else if (domain != null && domain.Length > 0)
{
host = domain;
}

string host_port = host + ":" + extPort.ToString();
string host_port = req.Url.Host+":"+req.Url.Port.ToString();// host + ":" + extPort.ToString();
if (context.User != null)
{
string username = context.User.Identity.Name;
NetworkCredential cred = httpServer.UserCredentialsFinder(context.User.Identity);
string password = cred.Password;
host_port = username + ":" + password + "@" + host + ":" + extPort.ToString();
}
else
{
host_port = host + ":" + extPort.ToString();
host_port = username + ":" + password + "@" + host_port;
}
host_port += parent_path;
res.SetCookie(new Cookie("HOST_PORT", host_port));
Expand Down

0 comments on commit e5b129a

Please sign in to comment.