message
message
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Diagnostics;
using System.Security.Principal;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Windows;
using System.Threading;
namespace KeyAuth
{
public class api
{
public string name, ownerid, secret, version;
public static long responseTime;
/// <summary>
/// Set up your application credentials in order to use keyauth
/// </summary>
/// <param name="name">Application Name</param>
/// <param name="ownerid">Your OwnerID, found in your account
settings.</param>
/// <param name="secret">Application Secret</param>
/// <param name="version">Application Version, if version doesnt match it
will open the download link you set up in your application settings and close the
app, if empty the app will close</param>
public api(string name, string ownerid, string secret, string version)
{
if (ownerid.Length != 10 || secret.Length != 64)
{
error("Application not setup correctly. Please watch video link
found in Program.cs");
Environment.Exit(0);
}
this.name = name;
this.ownerid = ownerid;
this.secret = secret;
this.version = version;
}
#region structures
[DataContract]
private class response_structure
{
[DataMember]
public bool success { get; set; }
[DataMember]
public string sessionid { get; set; }
[DataMember]
public string contents { get; set; }
[DataMember]
public string response { get; set; }
[DataMember]
public string message { get; set; }
[DataMember]
public string download { get; set; }
[DataMember]
public List<msg> messages { get; set; }
[DataMember]
public List<users> users { get; set; }
}
[DataContract]
private class user_data_structure
{
[DataMember]
public string username { get; set; }
[DataMember]
public string ip { get; set; }
[DataMember]
public string hwid { get; set; }
[DataMember]
public string createdate { get; set; }
[DataMember]
public string lastlogin { get; set; }
[DataMember]
public List<Data> subscriptions { get; set; } // array of subscriptions
(basically multiple user ranks for user with individual expiry dates
}
[DataContract]
private class app_data_structure
{
[DataMember]
public string numUsers { get; set; }
[DataMember]
public string numOnlineUsers { get; set; }
[DataMember]
public string numKeys { get; set; }
[DataMember]
public string version { get; set; }
[DataMember]
public string customerPanelLink { get; set; }
[DataMember]
public string downloadLink { get; set; }
}
#endregion
private static string sessionid, enckey;
bool initialized;
/// <summary>
/// Initializes the connection with keyauth in order to use any of the
functions
/// </summary>
public void init()
{
string sentKey = encryption.iv_key();
enckey = sentKey + "-" + secret;
var values_to_upload = new NameValueCollection
{
["type"] = "init",
["ver"] = version,
["hash"] =
checksum(Process.GetCurrentProcess().MainModule.FileName),
["enckey"] = sentKey,
["name"] = name,
["ownerid"] = ownerid
};
if (response == "KeyAuth_Invalid")
{
error("Application not found");
Environment.Exit(0);
}
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
{
load_app_data(json.appinfo);
sessionid = json.sessionid;
initialized = true;
}
else if (json.message == "invalidver")
{
app_data.downloadLink = json.download;
}
}
/// <summary>
/// Checks if Keyauth is been Initalized
/// </summary>
public void CheckInit()
{
if (!initialized)
{
error("You must run the function KeyAuthApp.init(); first");
Environment.Exit(0);
}
}
/// <summary>
/// Converts Unix time to Days,Months,Hours
///</summary>
/// <param name="subscription">Subscription Number</param>
/// <param name="Type">You can choose between Days,Hours,Months </param>
public string expirydaysleft(string Type,int subscription)
{
CheckInit();
/// <summary>
/// Registers the user using a license and gives the user a subscription
that matches their license level
/// </summary>
/// <param name="username">Username</param>
/// <param name="pass">Password</param>
/// <param name="key">License key</param>
public void register(string username, string pass, string key, string email
= "")
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
load_user_data(json.info);
}
/// <summary>
/// Allow users to enter their account information and recieve an email to
reset their password.
/// </summary>
/// <param name="username">Username</param>
/// <param name="email">Email address</param>
public void forgot(string username, string email)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
}
/// <summary>
/// Authenticates the user using their username and password
/// </summary>
/// <param name="username">Username</param>
/// <param name="pass">Password</param>
public void login(string username, string pass)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
load_user_data(json.info);
}
start:
outputten = "handshake";
outputten = "http://localhost:1337/" + outputten + "/";
listener.Prefixes.Add(outputten);
listener.Start();
if (request.HttpMethod == "OPTIONS")
{
responsepp.StatusCode = (int)HttpStatusCode.OK;
Thread.Sleep(1); // without this, the response doesn't return to
the website, and the web buttons can't be shown
listener.Stop();
goto start;
}
listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate;
listener.UnsafeConnectionNtlmAuthentication = true;
listener.IgnoreWriteExceptions = true;
datastore = datastore2;
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
{
load_user_data(json.info);
responsepp.StatusCode = 420;
responsepp.StatusDescription = "SHEESH";
}
else
{
Console.WriteLine(json.message);
responsepp.StatusCode = (int)HttpStatusCode.OK;
responsepp.StatusDescription = json.message;
success = false;
}
responsepp.ContentLength64 = buffer.Length;
Stream output = responsepp.OutputStream;
output.Write(buffer, 0, buffer.Length);
Thread.Sleep(1); // without this, the response doesn't return to the
website, and the web buttons can't be shown
listener.Stop();
if (!success)
Environment.Exit(0);
/// <summary>
/// Use Buttons from KeyAuth Customer Panel
/// </summary>
/// <param name="button">Button Name</param>
string output;
output = button;
output = "http://localhost:1337/" + output + "/";
listener.Prefixes.Add(output);
listener.Start();
responsepp.StatusCode = 420;
responsepp.StatusDescription = "SHEESH";
listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate;
listener.UnsafeConnectionNtlmAuthentication = true;
listener.IgnoreWriteExceptions = true;
listener.Stop();
}
/// <summary>
/// Gives the user a subscription that has the same level as the key
/// </summary>
/// <param name="username">Username of the user thats going to get
upgraded</param>
/// <param name="key">License with the same level as the subscription you
want to give the user</param>
public void upgrade(string username, string key)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
json.success = false;
load_response_struct(json);
}
/// <summary>
/// Authenticate without using usernames and passwords
/// </summary>
/// <param name="key">Licence used to login with</param>
public void license(string key)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
load_user_data(json.info);
}
/// <summary>
/// Checks if the current session is validated or not
/// </summary>
public void check()
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
}
/// <summary>
/// Change the data of an existing user variable, *User must be logged in*
/// </summary>
/// <param name="var">User variable name</param>
/// <param name="data">The content of the variable</param>
public void setvar(string var, string data)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
}
/// <summary>
/// Gets the an existing user variable
/// </summary>
/// <param name="var">User Variable Name</param>
/// <returns>The content of the user variable</returns>
public string getvar(string var)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
return json.response;
return null;
}
/// <summary>
/// Bans the current logged in user
/// </summary>
public void ban(string reason = null)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
}
/// <summary>
/// Gets an existing global variable
/// </summary>
/// <param name="varid">Variable ID</param>
/// <returns>The content of the variable</returns>
public string var(string varid)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
return json.message;
return null;
}
/// <summary>
/// Fetch usernames of online users
/// </summary>
/// <returns>ArrayList of usernames</returns>
public List<users> fetchOnline()
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
return json.users;
return null;
}
/// <summary>
/// Gets the last 50 sent messages of that channel
/// </summary>
/// <param name="channelname">The channel name</param>
/// <returns>the last 50 sent messages of that channel</returns>
public List<msg> chatget(string channelname)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
{
return json.messages;
}
return null;
}
/// <summary>
/// Sends a message to the given channel name
/// </summary>
/// <param name="msg">Message</param>
/// <param name="channelname">Channel Name</param>
/// <returns>If the message was sent successfully, it returns true if not
false</returns>
public bool chatsend(string msg, string channelname)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
return true;
return false;
}
/// <summary>
/// Sends a request to a webhook that you've added in the dashboard in a
safe way without it being showed for example a http debugger
/// </summary>
/// <param name="webid">Webhook ID</param>
/// <param name="param">Parameters</param>
/// <param name="body">Body of the request, empty by default</param>
/// <param name="conttype">Content type, empty by default</param>
/// <returns>the webhook's response</returns>
public string webhook(string webid, string param, string body = "", string
conttype = "")
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
return json.response;
return null;
}
/// <summary>
/// KeyAuth acts as proxy and downlods the file in a secure way
/// </summary>
/// <param name="fileid">File ID</param>
/// <returns>The bytes of the download file</returns>
public byte[] download(string fileid)
{
CheckInit();
["type"] = "file",
["fileid"] = fileid,
["sessionid"] = sessionid,
["name"] = name,
["ownerid"] = ownerid
};
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
if (json.success)
return encryption.str_to_byte_arr(json.contents);
return null;
}
/// <summary>
/// Logs the IP address,PC Name with a message, if a discord webhook is set
up in the app settings, the log will get sent there and the dashboard if not set up
it will only be in the dashboard
/// </summary>
/// <param name="message">Message</param>
public void log(string message)
{
CheckInit();
req(values_to_upload);
}
/// <summary>
/// Change the username of a user, *User must be logged in*
/// </summary>
/// <param username="username">New username.</param>
public void changeUsername(string username)
{
CheckInit();
var json =
response_decoder.string_to_generic<response_structure>(response);
load_response_struct(json);
}
var raw_response =
client.UploadValues("https://keyauth.win/api/1.2/", post_data);
stopwatch.Stop();
responseTime = stopwatch.ElapsedMilliseconds;
ServicePointManager.ServerCertificateValidationCallback =
delegate { return true; };
sigCheck(Encoding.Default.GetString(raw_response),
client.ResponseHeaders["signature"], post_data.Get(0));
return Encoding.Default.GetString(raw_response);
}
}
catch (WebException webex)
{
var response = (HttpWebResponse)webex.Response;
switch (response.StatusCode)
{
case (HttpStatusCode)429: // client hit our rate limit
error("You're connecting too fast to loader, slow down.");
Environment.Exit(0);
return "";
default: // site won't resolve. you should use keyauth.uk
domain since it's not blocked by any ISPs
error("Connection failure. Please try again, or contact us
for help.");
Environment.Exit(0);
return "";
}
}
}
try
{
string clientComputed = encryption.HashHMAC((type == "init") ?
enckey.Substring(17, 64) : enckey, resp);
if (clientComputed != signature)
{
error("Signature checksum failed. Request was tampered with or
session ended most likely. & echo: & echo Response: " + resp);
Environment.Exit(0);
}
}
catch
{
error("Signature checksum failed. Request was tampered with or
session ended most likely. & echo: & echo Response: " + resp);
Environment.Exit(0);
}
}
#region app_data
public app_data_class app_data = new app_data_class();
#region user_data
public user_data_class user_data = new user_data_class();
#region response_struct
public response_class response = new response_class();
if (!is_serializable(object_type))
throw new Exception($"the object {current_object} isn't a
serializable");
}