Merge branch 'general-devel-rest' into general-devel

This commit is contained in:
high 2011-09-06 00:00:56 -04:00
commit c72a80a51a
16 changed files with 7008 additions and 13 deletions

BIN
HttpBins/HttpServer.dll Normal file

Binary file not shown.

BIN
HttpBins/HttpServer.pdb Normal file

Binary file not shown.

6183
HttpBins/HttpServer.xml Normal file

File diff suppressed because it is too large Load diff

View file

@ -187,6 +187,18 @@ namespace TShockAPI
[Description("This will announce a player's location on join")]
public bool EnableGeoIP = false;
[Description("This will turn on a token requirement for the /status API endpoint.")]
public bool EnableTokenEndpointAuthentication = false;
[Description("This is used when the API endpoint /status is queried.")]
public string ServerNickname = "TShock Server";
[Description("Enable/Disable the rest api.")]
public bool RestApiEnabled = false;
[Description("This is the port which the rest api will listen on.")]
public int RestApiPort = 7878;
public static ConfigFile Read(string path)
{
if (!File.Exists(path))

View file

@ -36,5 +36,5 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.3.0.0904")]
[assembly: AssemblyFileVersion("3.3.0.0904")]
[assembly: AssemblyVersion("3.3.2.0905")]
[assembly: AssemblyFileVersion("3.3.2.0905")]

141
TShockAPI/Rest/Rest.cs Normal file
View file

@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using HttpServer;
using HttpServer.Headers;
using Newtonsoft.Json;
using HttpListener = HttpServer.HttpListener;
namespace Rests
{
/// <summary>
/// Rest command delegate
/// </summary>
/// <param name="parameters">Parameters in the url</param>
/// <param name="verbs">{x} in urltemplate</param>
/// <returns>Response object or null to not handle request</returns>
public delegate object RestCommandD(RestVerbs verbs, IParameterCollection parameters);
public class Rest : IDisposable
{
readonly List<RestCommand> commands = new List<RestCommand>();
HttpListener listener;
public IPAddress Ip { get; set; }
public int Port { get; set; }
public Rest(IPAddress ip, int port)
{
Ip = ip;
Port = port;
}
public virtual void Start()
{
if (listener == null)
{
listener = HttpListener.Create(Ip, Port);
listener.RequestReceived += OnRequest;
listener.Start(int.MaxValue);
}
}
public void Start(IPAddress ip, int port)
{
Ip = ip;
Port = port;
Start();
}
public virtual void Stop()
{
listener.Stop();
}
public void Register(string path, RestCommandD callback)
{
AddCommand(new RestCommand(path, callback));
}
public void Register(RestCommand com)
{
AddCommand(com);
}
protected void AddCommand(RestCommand com)
{
commands.Add(com);
}
protected virtual void OnRequest(object sender, RequestEventArgs e)
{
var obj = ProcessRequest(sender, e);
if (obj == null)
throw new NullReferenceException("obj");
var str = JsonConvert.SerializeObject(obj, Formatting.Indented);
e.Response.Connection.Type = ConnectionType.Close;
e.Response.Body.Write(Encoding.ASCII.GetBytes(str), 0, str.Length);
e.Response.Status = HttpStatusCode.OK;
return;
}
protected virtual object ProcessRequest(object sender, RequestEventArgs e)
{
var uri = e.Request.Uri.AbsolutePath;
uri = uri.TrimEnd('/');
foreach (var com in commands)
{
var verbs = new RestVerbs();
if (com.HasVerbs)
{
var match = Regex.Match(uri, com.UriVerbMatch);
if (!match.Success)
continue;
if ((match.Groups.Count - 1) != com.UriVerbs.Length)
continue;
for (int i = 0; i < com.UriVerbs.Length; i++)
verbs.Add(com.UriVerbs[i], match.Groups[i + 1].Value);
}
else if (com.UriTemplate.ToLower() != uri.ToLower())
{
continue;
}
var obj = ExecuteCommand(com, verbs, e.Request.Parameters);
if (obj != null)
return obj;
}
return new Dictionary<string, string> { { "status", "404" }, { "error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints." } };
}
protected virtual object ExecuteCommand(RestCommand cmd, RestVerbs verbs, IParameterCollection parms)
{
return cmd.Callback(verbs, parms);
}
#region Dispose
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (listener != null)
{
listener.Stop();
listener = null;
}
}
}
~Rest()
{
Dispose(false);
}
#endregion
}
}

View file

@ -0,0 +1,47 @@
using System.Linq;
using System.Text.RegularExpressions;
namespace Rests
{
public class RestCommand
{
public string Name { get; protected set; }
public string UriTemplate { get; protected set; }
public string UriVerbMatch { get; protected set; }
public string[] UriVerbs { get; protected set; }
public RestCommandD Callback { get; protected set; }
public bool RequiesToken { get; set; }
/// <summary>
///
/// </summary>
/// <param name="name">Used for identification</param>
/// <param name="uritemplate">Url template</param>
/// <param name="callback">Rest Command callback</param>
public RestCommand(string name, string uritemplate, RestCommandD callback)
{
Name = name;
UriTemplate = uritemplate;
UriVerbMatch = string.Format("^{0}$", string.Join("([^/]*)", Regex.Split(uritemplate, "\\{[^\\{\\}]*\\}")));
var matches = Regex.Matches(uritemplate, "\\{([^\\{\\}]*)\\}");
UriVerbs = (from Match match in matches select match.Groups[1].Value).ToArray();
Callback = callback;
RequiesToken = true;
}
/// <summary>
///
/// </summary>
/// <param name="uritemplate">Url template</param>
/// <param name="callback">Rest Command callback</param>
public RestCommand(string uritemplate, RestCommandD callback)
: this(string.Empty, uritemplate, callback)
{
}
public bool HasVerbs
{
get { return UriVerbs.Length > 0; }
}
}
}

View file

@ -0,0 +1,360 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HttpServer;
using Rests;
using Terraria;
namespace TShockAPI
{
public class RestManager
{
private Rest Rest;
public RestManager(Rest rest)
{
Rest = rest;
}
public void RegisterRestfulCommands()
{
Rest.Register(new RestCommand("/status", Status) { RequiesToken = false });
Rest.Register(new RestCommand("/tokentest", TokenTest) { RequiesToken = true });
Rest.Register(new RestCommand("/users/read/{user}/info", UserInfo) { RequiesToken = true });
Rest.Register(new RestCommand("/users/destroy/{user}", UserDestroy) { RequiesToken = true });
Rest.Register(new RestCommand("/users/update/{user}", UserUpdate) { RequiesToken = true });
Rest.Register(new RestCommand("/bans/create", BanCreate) { RequiesToken = true });
Rest.Register(new RestCommand("/bans/read/{user}/info", BanInfo) { RequiesToken = true });
Rest.Register(new RestCommand("/bans/destroy/{user}", BanDestroy) { RequiesToken = true });
Rest.Register(new RestCommand("/lists/players", UserList) { RequiesToken = true });
Rest.Register(new RestCommand("/world/read", WorldRead) { RequiesToken = true });
Rest.Register(new RestCommand("/world/meteor", WorldMeteor) { RequiesToken = true });
Rest.Register(new RestCommand("/world/bloodmoon/{bool}", WorldBloodmoon) { RequiesToken = true });
//RegisterExamples();
}
#region RestMethods
object TokenTest(RestVerbs verbs, IParameterCollection parameters)
{
return new Dictionary<string, string> { { "status", "200" }, { "response", "Token is valid and was passed through correctly." } };
}
object Status(RestVerbs verbs, IParameterCollection parameters)
{
if (TShock.Config.EnableTokenEndpointAuthentication)
return new RestObject("403") { Error = "Server settings require a token for this API call." };
var activeplayers = Main.player.Where(p => p != null && p.active).ToList();
string currentPlayers = string.Join(", ", activeplayers.Select(p => p.name));
var ret = new RestObject("200");
ret["name"] = TShock.Config.ServerNickname;
ret["port"] = Convert.ToString(TShock.Config.ServerPort);
ret["playercount"] = Convert.ToString(activeplayers.Count());
ret["players"] = currentPlayers;
return ret;
}
#endregion
#region RestUserMethods
object UserList(RestVerbs verbs, IParameterCollection parameters)
{
var activeplayers = Main.player.Where(p => p != null && p.active).ToList();
string currentPlayers = string.Join(", ", activeplayers.Select(p => p.name));
var ret = new RestObject("200");
ret["players"] = currentPlayers;
return ret;
}
object UserUpdate(RestVerbs verbs, IParameterCollection parameters)
{
var returnBlock = new Dictionary<string, string>();
var password = parameters["password"];
var group = parameters["group"];
if (group == null && password == null)
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "No parameters were passed.");
return returnBlock;
}
var user = TShock.Users.GetUserByName(verbs["user"]);
if (user == null)
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "The specefied user doesn't exist.");
return returnBlock;
}
if (password != null)
{
TShock.Users.SetUserPassword(user, password);
returnBlock.Add("password-response", "Password updated successfully.");
}
if (group != null)
{
TShock.Users.SetUserGroup(user, group);
returnBlock.Add("group-response", "Group updated successfully.");
}
returnBlock.Add("status", "200");
return returnBlock;
}
object UserDestroy(RestVerbs verbs, IParameterCollection parameters)
{
var user = TShock.Users.GetUserByName(verbs["user"]);
if (user == null)
{
return new Dictionary<string, string> { { "status", "400" }, { "error", "The specified user account does not exist." } };
}
var returnBlock = new Dictionary<string, string>();
try
{
TShock.Users.RemoveUser(user);
}
catch (Exception)
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "The specified user was unable to be removed.");
return returnBlock;
}
returnBlock.Add("status", "200");
returnBlock.Add("response", "User deleted successfully.");
return returnBlock;
}
object UserInfo(RestVerbs verbs, IParameterCollection parameters)
{
var user = TShock.Users.GetUserByName(verbs["user"]);
if (user == null)
{
return new Dictionary<string, string> { { "status", "400" }, { "error", "The specified user account does not exist." } };
}
var returnBlock = new Dictionary<string, string>();
returnBlock.Add("status", "200");
returnBlock.Add("group", user.Group);
returnBlock.Add("id", user.ID.ToString());
return returnBlock;
}
#endregion
#region RestBanMethods
object BanCreate(RestVerbs verbs, IParameterCollection parameters)
{
var returnBlock = new Dictionary<string, string>();
var ip = parameters["ip"];
var name = parameters["name"];
var reason = parameters["reason"];
if (ip == null && name == null)
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "Required parameters were missing from this API endpoint.");
return returnBlock;
}
if (ip == null)
{
ip = "";
}
if (name == null)
{
name = "";
}
if (reason == null)
{
reason = "";
}
try
{
TShock.Bans.AddBan(ip, name, reason);
}
catch (Exception)
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "The specified ban was unable to be created.");
return returnBlock;
}
returnBlock.Add("status", "200");
returnBlock.Add("response", "Ban created successfully.");
return returnBlock;
}
object BanDestroy(RestVerbs verbs, IParameterCollection parameters)
{
var returnBlock = new Dictionary<string, string>();
var type = parameters["type"];
if (type == null)
{
returnBlock.Add("Error", "Invalid Type");
return returnBlock;
}
var ban = new DB.Ban();
if (type == "ip") ban = TShock.Bans.GetBanByIp(verbs["user"]);
else if (type == "name") ban = TShock.Bans.GetBanByName(verbs["user"]);
else
{
returnBlock.Add("Error", "Invalid Type");
return returnBlock;
}
if (ban == null)
{
return new Dictionary<string, string> { { "status", "400" }, { "error", "The specified ban does not exist." } };
}
try
{
TShock.Bans.RemoveBan(ban.IP);
}
catch (Exception)
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "The specified ban was unable to be removed.");
return returnBlock;
}
returnBlock.Add("status", "200");
returnBlock.Add("response", "Ban deleted successfully.");
return returnBlock;
}
object BanInfo(RestVerbs verbs, IParameterCollection parameters)
{
var returnBlock = new Dictionary<string, string>();
var type = parameters["type"];
if (type == null)
{
returnBlock.Add("Error", "Invalid Type");
return returnBlock;
}
var ban = new DB.Ban();
if (type == "ip") ban = TShock.Bans.GetBanByIp(verbs["user"]);
else if (type == "name") ban = TShock.Bans.GetBanByName(verbs["user"]);
else
{
returnBlock.Add("Error", "Invalid Type");
return returnBlock;
}
if (ban == null)
{
return new Dictionary<string, string> { { "status", "400" }, { "error", "The specified ban does not exist." } };
}
returnBlock.Add("status", "200");
returnBlock.Add("name", ban.Name);
returnBlock.Add("ip", ban.IP);
returnBlock.Add("reason", ban.Reason);
return returnBlock;
}
#endregion
#region RestWorldMethods
object WorldRead(RestVerbs verbs, IParameterCollection parameters)
{
var returnBlock = new Dictionary<string, string>();
returnBlock.Add("status", "200");
returnBlock.Add("name", Main.worldName);
returnBlock.Add("size", Main.maxTilesX + "*" + Main.maxTilesY);
returnBlock.Add("time", Main.time.ToString());
returnBlock.Add("daytime", Main.dayTime.ToString());
returnBlock.Add("bloodmoon", Main.bloodMoon.ToString());
returnBlock.Add("invasionsize", Main.invasionSize.ToString());
return returnBlock;
}
object WorldMeteor(RestVerbs verbs, IParameterCollection parameters)
{
WorldGen.dropMeteor();
var returnBlock = new Dictionary<string, string>();
returnBlock.Add("status", "200");
returnBlock.Add("response", "Meteor has been spawned.");
return returnBlock;
}
object WorldBloodmoon(RestVerbs verbs, IParameterCollection parameters)
{
var returnBlock = new Dictionary<string, string>();
var bloodmoonVerb = verbs["bool"];
bool bloodmoon;
if (bloodmoonVerb == null)
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "No parameter was passed.");
return returnBlock;
}
if (!bool.TryParse(bloodmoonVerb, out bloodmoon))
{
returnBlock.Add("status", "400");
returnBlock.Add("error", "Unable to parse parameter.");
return returnBlock;
}
Main.bloodMoon = bloodmoon;
returnBlock.Add("status", "200");
returnBlock.Add("response", "Blood Moon has been set to " + bloodmoon.ToString());
return returnBlock;
}
#endregion
#region RestExampleMethods
public void RegisterExamples()
{
Rest.Register(new RestCommand("/HelloWorld/name/{username}", UserTest) { RequiesToken = false });
Rest.Register(new RestCommand("/wizard/{username}", Wizard) { RequiesToken = false });
}
//The Wizard example, for demonstrating the response convention:
object Wizard(RestVerbs verbs, IParameterCollection parameters)
{
var returnBack = new Dictionary<string, string>();
returnBack.Add("status", "200"); //Keep this in everything, 200 = ok, etc. Standard http status codes.
returnBack.Add("error", "(If this failed, you would have a different status code and provide the error object.)"); //And only include this if the status isn't 200 or a failure
returnBack.Add("Verified Wizard", "You're a wizard, " + verbs["username"]); //Outline any api calls and possible responses in some form of documentation somewhere
return returnBack;
}
//http://127.0.0.1:8080/HelloWorld/name/{username}?type=status
object UserTest(RestVerbs verbs, IParameterCollection parameters)
{
var ret = new Dictionary<string, string>();
var type = parameters["type"];
if (type == null)
{
ret.Add("Error", "Invalid Type");
return ret;
}
if (type == "status")
{
ret.Add("Users", "Info here");
return ret;
}
return null;
}
#endregion
}
}

View file

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
namespace Rests
{
[Serializable]
public class RestObject : Dictionary<string, string>
{
public string Status
{
get { return this["status"]; }
set { this["status"] = value; }
}
public string Error
{
get { return this["error"]; }
set { this["error"] = value; }
}
public string Response
{
get { return this["response"]; }
set { this["response"] = value; }
}
public RestObject(string status)
{
Status = status;
}
/// <summary>
/// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns null if key does not exist.</returns>
public new string this[string key]
{
get
{
string ret;
if (TryGetValue(key, out ret))
return ret;
return null;
}
set
{
if (!ContainsKey(key))
{
if (value == null)
return;
Add(key, value);
}
else
{
if (value != null)
base[key] = value;
else
Remove(key);
}
}
}
}
}

View file

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
namespace Rests
{
[Serializable]
public class RestVerbs : Dictionary<string, string>
{
/// <summary>
/// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns null if key does not exist.</returns>
public new string this[string key]
{
get
{
string ret;
if (TryGetValue(key, out ret))
return ret;
return null;
}
set
{
if (!ContainsKey(key))
{
if (value == null)
return;
Add(key, value);
}
else
{
if (value != null)
base[key] = value;
else
Remove(key);
}
}
}
}
}

View file

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using HttpServer;
using TShockAPI;
namespace Rests
{
/// <summary>
///
/// </summary>
/// <param name="username">Username to verify</param>
/// <param name="password">Password to verify</param>
/// <returns>Returning a restobject with a null error means a successful verification.</returns>
public delegate RestObject VerifyD(string username, string password);
public class SecureRest : Rest
{
public Dictionary<string, object> Tokens { get; protected set; }
public event VerifyD Verify;
public SecureRest(IPAddress ip, int port)
: base(ip, port)
{
Tokens = new Dictionary<string, object>();
Register(new RestCommand("/token/create/{username}/{password}", NewToken) { RequiesToken = false });
Register(new RestCommand("/token/destroy/{token}", DestroyToken) { RequiesToken = true });
}
object DestroyToken(RestVerbs verbs, IParameterCollection parameters)
{
var token = verbs["token"];
try
{
Tokens.Remove(token);
}
catch (Exception)
{
return new Dictionary<string, string> { { "status", "400" }, { "error", "The specified token queued for destruction failed to be deleted." } };
}
return new Dictionary<string, string> { { "status", "200" }, { "response", "Requested token was successfully destroyed." } };
}
object NewToken(RestVerbs verbs, IParameterCollection parameters)
{
var user = verbs["username"];
var pass = verbs["password"];
RestObject obj = null;
if (Verify != null)
obj = Verify(user, pass);
if (obj == null)
obj = new RestObject("401") { Error = "Invalid username/password combination provided. Please re-submit your query with a correct pair." };
if (obj.Error != null)
return obj;
string hash;
var rand = new Random();
var randbytes = new byte[32];
do
{
rand.NextBytes(randbytes);
hash = randbytes.Aggregate("", (s, b) => s + b.ToString("X2"));
} while (Tokens.ContainsKey(hash));
Tokens.Add(hash, user);
obj["token"] = hash;
return obj;
}
protected override object ExecuteCommand(RestCommand cmd, RestVerbs verbs, IParameterCollection parms)
{
if (cmd.RequiesToken)
{
var strtoken = parms["token"];
if (strtoken == null)
return new Dictionary<string, string> { { "status", "401" }, { "error", "Not authorized. The specified API endpoint requires a token." } };
object token;
if (!Tokens.TryGetValue(strtoken, out token))
return new Dictionary<string, string> { { "status", "403" }, { "error", "Not authorized. The specified API endpoint requires a token, but the provided token was not valid." } };
}
return base.ExecuteCommand(cmd, verbs, parms);
}
}
}

View file

@ -34,9 +34,12 @@ using System.IO;
using System.Net;
using System.Reflection;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using Community.CsharpSqlite.SQLiteClient;
using HttpServer;
using MySql.Data.MySqlClient;
using Rests;
using Terraria;
using TerrariaAPI;
using TerrariaAPI.Hooks;
@ -67,6 +70,8 @@ namespace TShockAPI
public static bool OverridePort;
public static PacketBufferer PacketBuffer;
public static MaxMind.GeoIPCountry Geo;
public static SecureRest RestApi;
public static RestManager RestManager;
/// <summary>
/// Called after TShock is initialized. Useful for plugins that needs hooks before tshock but also depend on tshock being loaded.
@ -172,8 +177,15 @@ namespace TShockAPI
Regions = new RegionManager(DB);
Itembans = new ItemManager(DB);
RememberedPos = new RemeberedPosManager(DB);
if (Config.EnableGeoIP)
Geo = new MaxMind.GeoIPCountry(Path.Combine(SavePath, "GeoIP.dat"));
RestApi = new SecureRest(IPAddress.Any, 8080);
RestApi.Verify += RestApi_Verify;
RestApi.Port = Config.RestApiPort;
RestManager = new RestManager(RestApi);
RestManager.RegisterRestfulCommands();
var geoippath = Path.Combine(SavePath, "GeoIP.dat");
if (Config.EnableGeoIP && File.Exists(geoippath))
Geo = new MaxMind.GeoIPCountry(geoippath);
Log.ConsoleInfo(string.Format("TShock Version {0} ({1}) now running.", Version, VersionCodename));
@ -209,6 +221,27 @@ namespace TShockAPI
}
}
RestObject RestApi_Verify(string username, string password)
{
var userAccount = TShock.Users.GetUserByName(username);
if (userAccount == null)
{
return new RestObject("401") { Error = "Invalid username/password combination provided. Please re-submit your query with a correct pair." };
}
if (Tools.HashPassword(password).ToUpper() != userAccount.Password.ToUpper())
{
return new RestObject("401") { Error = "Invalid username/password combination provided. Please re-submit your query with a correct pair." };
}
if (!Tools.GetGroup(userAccount.Group).HasPermission("api") && userAccount.Group != "superadmin")
{
return new RestObject("403") { Error = "Although your account was successfully found and identified, your account lacks the permission required to use the API. (api)" };
}
return new RestObject("200") { Response = "Successful login" }; //Maybe return some user info too?
}
public override void DeInitialize()
{
GameHooks.PostInitialize -= OnPostInit;
@ -226,6 +259,7 @@ namespace TShockAPI
Console.WriteLine("Thanks for using TShock! Process ID file is now being destroyed.");
File.Delete(Path.Combine(SavePath, "tshock.pid"));
}
RestApi.Dispose();
//RconHandler.ShutdownAllThreads();
}
@ -276,6 +310,7 @@ namespace TShockAPI
if (IPAddress.TryParse(parms[++i], out ip))
{
Netplay.serverListenIP = ip;
RestApi.Ip = ip;
Console.Write("Using IP: {0}", ip);
}
else
@ -353,6 +388,8 @@ namespace TShockAPI
AuthToken = 0;
}
Regions.ReloadAllRegions();
if (Config.RestApiEnabled)
RestApi.Start();
}
@ -625,7 +662,7 @@ namespace TShockAPI
NetMessage.SendData((int)PacketTypes.TimeSet, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY);
NetMessage.syncPlayers();
if (Config.EnableGeoIP)
if (Config.EnableGeoIP && Geo != null)
{
var code = Geo.TryGetCountryCode(IPAddress.Parse(player.IP));
player.Country = code == null ? "N/A" : MaxMind.GeoIPCountry.GetCountryNameByCode(code);

View file

@ -52,6 +52,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\SqlBins\Community.CsharpSqlite.SQLiteClient.dll</HintPath>
</Reference>
<Reference Include="HttpServer">
<HintPath>..\HttpBins\HttpServer.dll</HintPath>
</Reference>
<Reference Include="MySql.Data, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\SqlBins\MySql.Data.dll</HintPath>
@ -121,6 +124,12 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Rest\Rest.cs" />
<Compile Include="Rest\RestCommand.cs" />
<Compile Include="Rest\RestManager.cs" />
<Compile Include="Rest\RestObject.cs" />
<Compile Include="Rest\RestVerbs.cs" />
<Compile Include="Rest\SecureRest.cs" />
<Compile Include="Tools.cs" />
<Compile Include="TShock.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@ -177,7 +186,7 @@
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View file

@ -535,23 +535,33 @@ namespace TShockAPI
/// <summary>
/// Returns a Sha256 string for a given string
/// </summary>
/// <param name="password">string password</param>
/// <param name="bytes">bytes to hash</param>
/// <returns>string sha256</returns>
public static string HashPassword(string password)
public static string HashPassword(byte[] bytes)
{
if (string.IsNullOrEmpty(password) || password == "non-existant password")
return "non-existant password";
if (bytes == null)
throw new NullReferenceException("bytes");
Func<HashAlgorithm> func;
if (!HashTypes.TryGetValue(HashAlgo.ToLower(), out func))
throw new NotSupportedException("Hashing algorithm {0} is not supported".SFormat(HashAlgo.ToLower()));
using (var hash = func())
{
var bytes = hash.ComputeHash(Encoding.ASCII.GetBytes(password));
return bytes.Aggregate("", (s, b) => s + b.ToString("X2"));
var ret = hash.ComputeHash(bytes);
return ret.Aggregate("", (s, b) => s + b.ToString("X2"));
}
}
/// <summary>
/// Returns a Sha256 string for a given string
/// </summary>
/// <param name="bytes">bytes to hash</param>
/// <returns>string sha256</returns>
public static string HashPassword(string password)
{
if (string.IsNullOrEmpty(password) || password == "non-existant password")
return "non-existant password";
return HashPassword(Encoding.UTF8.GetBytes(password));
}
/// <summary>
/// Checks if the string contains any unprintable characters