Http requests also use local IP (if specified) as bind IP, also send software name as user agent

This commit is contained in:
UnknownShadow200 2017-05-31 22:08:28 +10:00
parent 9bc4650504
commit 5411ee4f97
9 changed files with 81 additions and 14 deletions

View File

@ -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);
}

View File

@ -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");
}

View File

@ -574,6 +574,7 @@
<Compile Include="Network\Socket\TcpListen.cs" />
<Compile Include="Network\Socket\TcpSocket.cs" />
<Compile Include="Network\Utils\BufferedBlockSender.cs" />
<Compile Include="Network\Utils\HttpUtil.cs" />
<Compile Include="Network\Utils\LevelChunkStream.cs" />
<Compile Include="Network\Utils\NetUtils.cs" />
<Compile Include="Player\Group\Group.cs" />

View File

@ -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

View File

@ -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);

View File

@ -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 {
/// <summary> Static class for assisting with making web requests. </summary>
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);
}
}
}

View File

@ -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 {

View File

@ -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<string> list = (type == "devs") ? Server.Devs : (type == "mods") ? Server.Mods : null;

View File

@ -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");