diff --git a/MCGalaxy/Commands/Moderation/CmdLocation.cs b/MCGalaxy/Commands/Moderation/CmdLocation.cs
index 167d272b3..320ee748c 100644
--- a/MCGalaxy/Commands/Moderation/CmdLocation.cs
+++ b/MCGalaxy/Commands/Moderation/CmdLocation.cs
@@ -18,9 +18,10 @@
using System;
using System.IO;
using System.Net;
+using MCGalaxy.Network;
namespace MCGalaxy.Commands.Moderation {
- public class CmdLocation : Command {
+ public class CmdLocation : Command {
public override string name { get { return "location"; } }
public override string shortcut { get { return "lo"; } }
public override string type { get { return CommandTypes.Moderation; } }
@@ -44,8 +45,11 @@ namespace MCGalaxy.Commands.Moderation {
Player.Message(p, Colors.red + "Player has an internal IP, cannot trace"); return;
}
- string country = new WebClient().DownloadString("http://ipinfo.io/" + ip + "/country");
- country = country.Replace("\n", "");
+ string country = null;
+ using (WebClient client = HttpUtil.CreateWebClient()) {
+ country = client.DownloadString("http://ipinfo.io/" + ip + "/country");
+ country = country.Replace("\n", "");
+ }
Player.Message(p, "The IP of &a" + target + " %Shas been traced to: &b" + country);
}
diff --git a/MCGalaxy/Generator/HeightmapGen.cs b/MCGalaxy/Generator/HeightmapGen.cs
index 1ce773a82..58b62928b 100644
--- a/MCGalaxy/Generator/HeightmapGen.cs
+++ b/MCGalaxy/Generator/HeightmapGen.cs
@@ -19,6 +19,7 @@ using System;
using System.Drawing;
using System.IO;
using System.Net;
+using MCGalaxy.Network;
namespace MCGalaxy.Generator {
public static class HeightmapGen {
@@ -35,7 +36,7 @@ namespace MCGalaxy.Generator {
}
try {
- using (WebClient client = new WebClient()) {
+ using (WebClient client = HttpUtil.CreateWebClient()) {
Player.Message(p, "Downloading file from: &f" + url);
client.DownloadFile(uri, dir + "tempImage_" + p.name + ".bmp");
}
diff --git a/MCGalaxy/MCGalaxy_.csproj b/MCGalaxy/MCGalaxy_.csproj
index b25c879e4..fd145032b 100644
--- a/MCGalaxy/MCGalaxy_.csproj
+++ b/MCGalaxy/MCGalaxy_.csproj
@@ -574,6 +574,7 @@
+
diff --git a/MCGalaxy/Network/Heartbeat/ClassiCube.cs b/MCGalaxy/Network/Heartbeat/ClassiCube.cs
index 446b04922..4f1a282f5 100644
--- a/MCGalaxy/Network/Heartbeat/ClassiCube.cs
+++ b/MCGalaxy/Network/Heartbeat/ClassiCube.cs
@@ -93,7 +93,7 @@ namespace MCGalaxy.Network {
return count;
}
- public override void OnRequest(HttpWebRequest request) {
+ public override void OnRequest(HttpWebRequest request) {
#if !NET_20
request.Host = "www.classicube.net";
#else
diff --git a/MCGalaxy/Network/Heartbeat/Heartbeat.cs b/MCGalaxy/Network/Heartbeat/Heartbeat.cs
index 032c7e675..8f5bfe0d9 100644
--- a/MCGalaxy/Network/Heartbeat/Heartbeat.cs
+++ b/MCGalaxy/Network/Heartbeat/Heartbeat.cs
@@ -85,7 +85,7 @@ namespace MCGalaxy.Network {
for (int i = 0; i < MAX_RETRIES; i++) {
try {
- HttpWebRequest req = WebRequest.Create(beat.URL) as HttpWebRequest;
+ HttpWebRequest req = HttpUtil.CreateRequest(beat.URL);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
diff --git a/MCGalaxy/Network/Utils/HttpUtil.cs b/MCGalaxy/Network/Utils/HttpUtil.cs
new file mode 100644
index 000000000..0ce8dacca
--- /dev/null
+++ b/MCGalaxy/Network/Utils/HttpUtil.cs
@@ -0,0 +1,57 @@
+/*
+ Copyright 2015 MCGalaxy
+
+ Dual-licensed under the Educational Community License, Version 2.0 and
+ the GNU General Public License, Version 3 (the "Licenses"); you may
+ not use this file except in compliance with the Licenses. You may
+ obtain a copy of the Licenses at
+
+ http://www.opensource.org/licenses/ecl2.php
+ http://www.gnu.org/licenses/gpl-3.0.html
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the Licenses are distributed on an "AS IS"
+ BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ or implied. See the Licenses for the specific language governing
+ permissions and limitations under the Licenses.
+ */
+using System;
+using System.Net;
+
+namespace MCGalaxy.Network {
+ /// Static class for assisting with making web requests.
+ public static class HttpUtil {
+
+ public static WebClient CreateWebClient() { return new CustomWebClient(); }
+
+ public static HttpWebRequest CreateRequest(string uri) {
+ HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
+ req.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
+ req.UserAgent = Server.SoftwareNameVersioned;
+ return req;
+ }
+
+
+ class CustomWebClient : WebClient {
+ protected override WebRequest GetWebRequest(Uri address) {
+ HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(address);
+ req.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
+ req.UserAgent = Server.SoftwareNameVersioned;
+ return (WebRequest)req;
+ }
+ }
+
+ static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) {
+ IPAddress localIP = null;
+ if (Server.Listener != null) {
+ localIP = Server.Listener.LocalIP;
+ } else if (!IPAddress.TryParse(Server.listenIP, out localIP)) {
+ return null;
+ }
+
+ // can only use same family for local bind IP
+ if (remoteEndPoint.AddressFamily != localIP.AddressFamily) return null;
+ return new IPEndPoint(localIP, 0);
+ }
+ }
+}
\ No newline at end of file
diff --git a/MCGalaxy/Server/Server.cs b/MCGalaxy/Server/Server.cs
index 531a8483e..608c42515 100644
--- a/MCGalaxy/Server/Server.cs
+++ b/MCGalaxy/Server/Server.cs
@@ -27,6 +27,7 @@ using MCGalaxy.Drawing;
using MCGalaxy.Eco;
using MCGalaxy.Events;
using MCGalaxy.Games;
+using MCGalaxy.Network;
using MCGalaxy.Tasks;
using MCGalaxy.Util;
using Newtonsoft.Json;
@@ -50,8 +51,9 @@ namespace MCGalaxy {
Log(file + " doesn't exist, Downloading");
try {
- using (WebClient web = new WebClient())
- web.DownloadFile(Updater.BaseURL + file + "?raw=true", file);
+ using (WebClient client = HttpUtil.CreateWebClient()) {
+ client.DownloadFile(Updater.BaseURL + file + "?raw=true", file);
+ }
if (File.Exists(file))
Log(file + " download succesful!");
} catch {
diff --git a/MCGalaxy/Server/Tasks/InitTasks.cs b/MCGalaxy/Server/Tasks/InitTasks.cs
index a400be9e5..f19b45a8f 100644
--- a/MCGalaxy/Server/Tasks/InitTasks.cs
+++ b/MCGalaxy/Server/Tasks/InitTasks.cs
@@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Net;
+using MCGalaxy.Network;
namespace MCGalaxy.Tasks {
internal static class InitTasks {
@@ -25,8 +26,8 @@ namespace MCGalaxy.Tasks {
const string staffUrl = Updater.BaseURL + "Uploads/devs.txt";
internal static void UpdateStaffList() {
try {
- using (WebClient web = new WebClient()) {
- string[] result = web.DownloadString(staffUrl).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
+ using (WebClient client = HttpUtil.CreateWebClient()) {
+ string[] result = client.DownloadString(staffUrl).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in result) {
string type = line.Split(':')[0].ToLower();
List list = (type == "devs") ? Server.Devs : (type == "mods") ? Server.Mods : null;
diff --git a/MCGalaxy/Server/Updater.cs b/MCGalaxy/Server/Updater.cs
index fd6b08a4a..a241c3951 100644
--- a/MCGalaxy/Server/Updater.cs
+++ b/MCGalaxy/Server/Updater.cs
@@ -21,6 +21,7 @@ using System.IO;
using System.Net;
using System.Reflection;
using System.Windows.Forms;
+using MCGalaxy.Network;
using MCGalaxy.Tasks;
namespace MCGalaxy {
@@ -44,10 +45,10 @@ namespace MCGalaxy {
public static void UpdateCheck(Player p = null) {
CurrentUpdate = true;
if (!Server.checkUpdates) return;
- WebClient Client = new WebClient();
+ WebClient client = HttpUtil.CreateWebClient();
try {
- string raw = Client.DownloadString(CurrentVersionFile);
+ string raw = client.DownloadString(CurrentVersionFile);
Version latestVersion = new Version(raw);
if (latestVersion <= Server.Version) {
Player.Message(p, "No update found!");
@@ -78,7 +79,7 @@ namespace MCGalaxy {
Logger.WriteError(e);
}
- Client.Dispose();
+ client.Dispose();
CurrentUpdate = false;
}
@@ -120,7 +121,7 @@ namespace MCGalaxy {
} catch {
}
- WebClient client = new WebClient();
+ WebClient client = HttpUtil.CreateWebClient();
client.DownloadFile(DLLLocation, "MCGalaxy_.update");
client.DownloadFile(EXELocation, "MCGalaxy.update");
client.DownloadFile(ChangelogLocation, "Changelog.txt");