App refactoring almost done

- Created specific namespaces and folders for each app brick
- Added proxy support using Starksoft's Biko Library
- App bricks: Main, ChatBots, Crypto, Protocol, Proxy
- Each class is now in its own file (Aes streams, chatbots)
- Used "Bridge" design pattern for Crypto, Protocol, Proxy
- Added back support for Minecraft 1.4.6 to 1.6.4 (MCC 1.6.2)
- Need to fully re-test everything and fix bugs
- To Fix : Server pinging is slow on SpigotMC
- To Do : Add Minecraft 1.2.5 (MCC 1.3) and maybe 1.3 to 1.4.5
This commit is contained in:
ORelio 2014-05-31 01:59:03 +02:00
parent 9be1d99ca0
commit d2ec2f48b7
43 changed files with 6039 additions and 2178 deletions

File diff suppressed because it is too large Load diff

269
MinecraftClient/ChatBot.cs Normal file
View file

@ -0,0 +1,269 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient
{
///
/// Welcome to the Bot API file !
/// The virtual class "ChatBot" contains anything you need for creating chat bots
/// Inherit from this class while adding your bot class to the folder "Bots".
/// Once your bot is created, read the explanations below to start using it in the MinecraftClient app.
///
/// Pieces of code to add in other parts of the program for your bot. Line numbers are approximative.
/// McTcpClient:110 | if (Settings.YourBot_Enabled) { handler.BotLoad(new ChatBots.YourBot()); }
/// Settings.cs:73 | public static bool YourBot_Enabled = false;
/// Settings.cs:74 | private enum ParseMode { /* [...] */, YourBot };
/// Settings.cs:106 | case "yourbot": pMode = ParseMode.YourBot; break;
/// Settings.cs:197 | case ParseMode.YourBot: switch (argName.ToLower()) { case "enabled": YourBot_Enabled = str2bool(argValue); break; } break;
/// Settings.cs:267 | + "[YourBot]\r\n" + "enabled=false\r\n"
/// Here your are. Now you will have a setting in MinecraftClient.ini for enabling your brand new bot.
/// Delete MinecraftClient.ini to re-generate it or add the lines [YourBot] and enabled=true to the existing one.
///
/// <summary>
/// The virtual class containing anything you need for creating chat bots.
/// </summary>
public abstract class ChatBot
{
public enum DisconnectReason { InGameKick, LoginRejected, ConnectionLost };
//Will be automatically set on bot loading, don't worry about this
public void SetHandler(McTcpClient handler) { this.handler = handler; }
private McTcpClient handler;
/* ================================================== */
/* Main methods to override for creating your bot */
/* ================================================== */
/// <summary>
/// Anything you want to initialize your bot, will be called on load by MinecraftCom
/// </summary>
public virtual void Initialize() { }
/// <summary>
/// Will be called every ~100ms (10fps) if loaded in MinecraftCom
/// </summary>
public virtual void Update() { }
/// <summary>
/// Any text sent by the server will be sent here by MinecraftCom
/// </summary>
/// <param name="text">Text from the server</param>
public virtual void GetText(string text) { }
/// <summary>
/// Is called when the client has been disconnected fom the server
/// </summary>
/// <param name="reason">Disconnect Reason</param>
/// <param name="message">Kick message, if any</param>
/// <returns>Return TRUE if the client is about to restart</returns>
public virtual bool OnDisconnect(DisconnectReason reason, string message) { return false; }
/* =================================================================== */
/* ToolBox - Methods below might be useful while creating your bot. */
/* You should not need to interact with other classes of the program. */
/* =================================================================== */
/// <summary>
/// Send text to the server. Can be anything such as chat messages or commands
/// </summary>
/// <param name="text">Text to send to the server</param>
protected void SendText(string text)
{
ConsoleIO.WriteLineFormatted("§8BOT:" + text, false);
handler.SendChatMessage(text);
}
/// <summary>
/// Remove color codes ("§c") from a text message received from the server
/// </summary>
protected static string getVerbatim(string text)
{
if ( String.IsNullOrEmpty(text) )
return String.Empty;
int idx = 0;
var data = new char[text.Length];
for ( int i = 0; i < text.Length; i++ )
if ( text[i] != '§' )
data[idx++] = text[i];
else
i++;
return new string(data, 0, idx);
}
/// <summary>
/// Verify that a string contains only a-z A-Z 0-9 and _ characters.
/// </summary>
protected static bool isValidName(string username)
{
if ( String.IsNullOrEmpty(username) )
return false;
foreach ( char c in username )
if ( !((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '_') )
return false;
return true;
}
/// <summary>
/// Returns true is the text passed is a private message sent to the bot
/// </summary>
/// <param name="text">text to test</param>
/// <param name="message">if it's a private message, this will contain the message</param>
/// <param name="sender">if it's a private message, this will contain the player name that sends the message</param>
/// <returns>Returns true if the text is a private message</returns>
protected static bool isPrivateMessage(string text, ref string message, ref string sender)
{
if (text == "") { return false; }
string[] tmp = text.Split(' ');
try
{
//Detect vanilla /tell messages
//Someone whispers to you: message
if (tmp.Length > 2 && tmp[1] == "whispers")
{
message = text.Substring(tmp[0].Length + 18);
sender = tmp[0];
return isValidName(sender);
}
//Detect Essentials (Bukkit) /m messages
//[Someone -> me] message
else if (text[0] == '[' && tmp.Length > 3 && tmp[1] == "->"
&& (tmp[2] == "me]" || tmp[2] == "moi]")) //'me' is replaced by 'moi' in french servers
{
message = text.Substring(tmp[0].Length + 4 + tmp[2].Length + 1);
sender = tmp[0].Substring(1);
if (sender[0] == '~') { sender = sender.Substring(1); }
return isValidName(sender);
}
else return false;
}
catch (IndexOutOfRangeException) { return false; }
}
/// <summary>
/// Returns true is the text passed is a public message written by a player on the chat
/// </summary>
/// <param name="text">text to test</param>
/// <param name="message">if it's message, this will contain the message</param>
/// <param name="sender">if it's message, this will contain the player name that sends the message</param>
/// <returns>Returns true if the text is a chat message</returns>
protected static bool isChatMessage(string text, ref string message, ref string sender)
{
//Detect chat messages
//<Someone> message
//<*Faction Someone> message
//<*Faction Someone>: message
//<*Faction ~Nicknamed>: message
if (text == "") { return false; }
if (text[0] == '<')
{
try
{
text = text.Substring(1);
string[] tmp = text.Split('>');
sender = tmp[0];
message = text.Substring(sender.Length + 2);
if (message.Length > 1 && message[0] == ' ')
{ message = message.Substring(1); }
tmp = sender.Split(' ');
sender = tmp[tmp.Length - 1];
if (sender[0] == '~') { sender = sender.Substring(1); }
return isValidName(sender);
}
catch (IndexOutOfRangeException) { return false; }
}
else return false;
}
/// <summary>
/// Writes some text in the console. Nothing will be sent to the server.
/// </summary>
/// <param name="text">Log text to write</param>
public static void LogToConsole(string text)
{
ConsoleIO.WriteLineFormatted("§8[BOT] " + text, true);
}
/// <summary>
/// Disconnect from the server and restart the program
/// It will unload & reload all the bots and then reconnect to the server
/// </summary>
protected void ReconnectToTheServer() { ReconnectToTheServer(3); }
/// <summary>
/// Disconnect from the server and restart the program
/// It will unload & reload all the bots and then reconnect to the server
/// </summary>
/// <param name="attempts">If connection fails, the client will make X extra attempts</param>
protected void ReconnectToTheServer(int ExtraAttempts)
{
McTcpClient.AttemptsLeft = ExtraAttempts;
Program.Restart();
}
/// <summary>
/// Disconnect from the server and exit the program
/// </summary>
protected void DisconnectAndExit()
{
Program.Exit();
}
/// <summary>
/// Unload the chatbot, and release associated memory.
/// </summary>
protected void UnloadBot()
{
handler.BotUnLoad(this);
}
/// <summary>
/// Send a private message to a player
/// </summary>
/// <param name="player">Player name</param>
/// <param name="message">Message</param>
protected void SendPrivateMessage(string player, string message)
{
SendText("/tell " + player + ' ' + message);
}
/// <summary>
/// Run a script from a file using a Scripting bot
/// </summary>
/// <param name="filename">File name</param>
/// <param name="playername">Player name to send error messages, if applicable</param>
protected void RunScript(string filename, string playername = "")
{
handler.BotLoad(new ChatBots.Script(filename, playername));
}
}
}

View file

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot make the console beep on some specified words. Useful to detect when someone is talking to you, for example.
/// </summary>
public class Alerts : ChatBot
{
private string[] dictionary = new string[0];
private string[] excludelist = new string[0];
public override void Initialize()
{
if (System.IO.File.Exists(Settings.Alerts_MatchesFile))
{
dictionary = System.IO.File.ReadAllLines(Settings.Alerts_MatchesFile);
for (int i = 0; i < dictionary.Length; i++)
{
dictionary[i] = dictionary[i].ToLower();
}
}
else LogToConsole("File not found: " + Settings.Alerts_MatchesFile);
if (System.IO.File.Exists(Settings.Alerts_ExcludesFile))
{
excludelist = System.IO.File.ReadAllLines(Settings.Alerts_ExcludesFile);
for (int i = 0; i < excludelist.Length; i++)
{
excludelist[i] = excludelist[i].ToLower();
}
}
else LogToConsole("File not found : " + Settings.Alerts_ExcludesFile);
}
public override void GetText(string text)
{
text = getVerbatim(text);
string comp = text.ToLower();
foreach (string alert in dictionary)
{
if (comp.Contains(alert))
{
bool ok = true;
foreach (string exclusion in excludelist)
{
if (comp.Contains(exclusion))
{
ok = false;
break;
}
}
if (ok)
{
if (Settings.Alerts_Beep_Enabled) { Console.Beep(); } //Text found !
if (ConsoleIO.basicIO) { ConsoleIO.WriteLine(comp.Replace(alert, "§c" + alert + "§r")); }
else
{
#region Displaying the text with the interesting part highlighted
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.White;
//Will be used for text displaying
string[] temp = comp.Split(alert.Split(','), StringSplitOptions.RemoveEmptyEntries);
int p = 0;
//Special case : alert in the beginning of the text
string test = "";
for (int i = 0; i < alert.Length; i++)
{
test += comp[i];
}
if (test == alert)
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < alert.Length; i++)
{
ConsoleIO.Write(text[p]);
p++;
}
}
//Displaying the rest of the text
for (int i = 0; i < temp.Length; i++)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.White;
for (int j = 0; j < temp[i].Length; j++)
{
ConsoleIO.Write(text[p]);
p++;
}
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Red;
try
{
for (int j = 0; j < alert.Length; j++)
{
ConsoleIO.Write(text[p]);
p++;
}
}
catch (IndexOutOfRangeException) { }
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Gray;
ConsoleIO.Write('\n');
#endregion
}
}
}
}
}
}
}

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot sends a command every 60 seconds in order to stay non-afk.
/// </summary>
public class AntiAFK : ChatBot
{
private int count;
private int timeping;
/// <summary>
/// This bot sends a /ping command every X seconds in order to stay non-afk.
/// </summary>
/// <param name="pingparam">Time amount between each ping (10 = 1s, 600 = 1 minute, etc.)</param>
public AntiAFK(int pingparam)
{
count = 0;
timeping = pingparam;
if (timeping < 10) { timeping = 10; } //To avoid flooding
}
public override void Update()
{
count++;
if (count == timeping)
{
SendText(Settings.AntiAFK_Command);
count = 0;
}
}
}
}

View file

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot automatically re-join the server if kick message contains predefined string (Server is restarting ...)
/// </summary>
public class AutoRelog : ChatBot
{
private string[] dictionary = new string[0];
private int attempts;
private int delay;
/// <summary>
/// This bot automatically re-join the server if kick message contains predefined string
/// </summary>
/// <param name="DelayBeforeRelog">Delay before re-joining the server (in seconds)</param>
/// <param name="retries">Number of retries if connection fails (-1 = infinite)</param>
public AutoRelog(int DelayBeforeRelog, int retries)
{
attempts = retries;
if (attempts == -1) { attempts = int.MaxValue; }
McTcpClient.AttemptsLeft = attempts;
delay = DelayBeforeRelog;
if (delay < 1) { delay = 1; }
}
public override void Initialize()
{
McTcpClient.AttemptsLeft = attempts;
if (System.IO.File.Exists(Settings.AutoRelog_KickMessagesFile))
{
dictionary = System.IO.File.ReadAllLines(Settings.AutoRelog_KickMessagesFile);
for (int i = 0; i < dictionary.Length; i++)
{
dictionary[i] = dictionary[i].ToLower();
}
}
else LogToConsole("File not found: " + Settings.AutoRelog_KickMessagesFile);
}
public override bool OnDisconnect(DisconnectReason reason, string message)
{
message = getVerbatim(message);
string comp = message.ToLower();
foreach (string msg in dictionary)
{
if (comp.Contains(msg))
{
LogToConsole("Waiting " + delay + " seconds before reconnecting...");
System.Threading.Thread.Sleep(delay * 1000);
McTcpClient.AttemptsLeft = attempts;
ReconnectToTheServer();
return true;
}
}
return false;
}
}
}

View file

@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot saves the received messages in a text file.
/// </summary>
public class ChatLog : ChatBot
{
public enum MessageFilter { AllText, AllMessages, OnlyChat, OnlyWhispers };
private bool dateandtime;
private bool saveOther = true;
private bool saveChat = true;
private bool savePrivate = true;
private string logfile;
/// <summary>
/// This bot saves the messages received in the specified file, with some filters and date/time tagging.
/// </summary>
/// <param name="file">The file to save the log in</param>
/// <param name="filter">The kind of messages to save</param>
/// <param name="AddDateAndTime">Add a date and time before each message</param>
public ChatLog(string file, MessageFilter filter, bool AddDateAndTime)
{
dateandtime = AddDateAndTime;
logfile = file;
switch (filter)
{
case MessageFilter.AllText:
saveOther = true;
savePrivate = true;
saveChat = true;
break;
case MessageFilter.AllMessages:
saveOther = false;
savePrivate = true;
saveChat = true;
break;
case MessageFilter.OnlyChat:
saveOther = false;
savePrivate = false;
saveChat = true;
break;
case MessageFilter.OnlyWhispers:
saveOther = false;
savePrivate = true;
saveChat = false;
break;
}
}
public static MessageFilter str2filter(string filtername)
{
switch (filtername.ToLower())
{
case "all": return MessageFilter.AllText;
case "messages": return MessageFilter.AllMessages;
case "chat": return MessageFilter.OnlyChat;
case "private": return MessageFilter.OnlyWhispers;
default: return MessageFilter.AllText;
}
}
public override void GetText(string text)
{
text = getVerbatim(text);
string sender = "";
string message = "";
if (saveChat && isChatMessage(text, ref message, ref sender))
{
save("Chat " + sender + ": " + message);
}
else if (savePrivate && isPrivateMessage(text, ref message, ref sender))
{
save("Private " + sender + ": " + message);
}
else if (saveOther)
{
save("Other: " + text);
}
}
private void save(string tosave)
{
if (dateandtime)
{
int day = DateTime.Now.Day, month = DateTime.Now.Month;
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
string D = day < 10 ? "0" + day : "" + day;
string M = month < 10 ? "0" + month : "" + day;
string Y = "" + DateTime.Now.Year;
string h = hour < 10 ? "0" + hour : "" + hour;
string m = minute < 10 ? "0" + minute : "" + minute;
string s = second < 10 ? "0" + second : "" + second;
tosave = "" + D + '-' + M + '-' + Y + ' ' + h + ':' + m + ':' + s + ' ' + tosave;
}
System.IO.FileStream stream = new System.IO.FileStream(logfile, System.IO.FileMode.OpenOrCreate);
System.IO.StreamWriter writer = new System.IO.StreamWriter(stream);
stream.Seek(0, System.IO.SeekOrigin.End);
writer.WriteLine(tosave);
writer.Dispose();
stream.Close();
}
}
}

View file

@ -0,0 +1,188 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// In-Chat Hangman game
/// </summary>
public class HangmanGame : ChatBot
{
private int vie = 0;
private int vie_param = 10;
private int compteur = 0;
private int compteur_param = 3000; //5 minutes
private bool running = false;
private bool[] discovered;
private string word = "";
private string letters = "";
private bool English;
/// <summary>
/// Le jeu du Pendu / Hangman Game
/// </summary>
/// <param name="english">if true, the game will be in english. If false, the game will be in french.</param>
public HangmanGame(bool english)
{
English = english;
}
public override void Update()
{
if (running)
{
if (compteur > 0)
{
compteur--;
}
else
{
SendText(English ? "You took too long to try a letter." : "Temps imparti écoulé !");
SendText(English ? "Game canceled." : "Partie annulée.");
running = false;
}
}
}
public override void GetText(string text)
{
string message = "";
string username = "";
text = getVerbatim(text);
if (isPrivateMessage(text, ref message, ref username))
{
if (Settings.Bots_Owners.Contains(username.ToLower()))
{
switch (message)
{
case "start":
start();
break;
case "stop":
running = false;
break;
default:
break;
}
}
}
else
{
if (running && isChatMessage(text, ref message, ref username))
{
if (message.Length == 1)
{
char letter = message.ToUpper()[0];
if (letter >= 'A' && letter <= 'Z')
{
if (letters.Contains(letter))
{
SendText(English ? ("Letter " + letter + " has already been tried.") : ("Le " + letter + " a déjà été proposé."));
}
else
{
letters += letter;
compteur = compteur_param;
if (word.Contains(letter))
{
for (int i = 0; i < word.Length; i++) { if (word[i] == letter) { discovered[i] = true; } }
SendText(English ? ("Yes, the word contains a " + letter + '!') : ("Le " + letter + " figurait bien dans le mot :)"));
}
else
{
vie--;
if (vie == 0)
{
SendText(English ? "Game Over! :]" : "Perdu ! Partie terminée :]");
SendText(English ? ("The word was: " + word) : ("Le mot était : " + word));
running = false;
}
else SendText(English ? ("The " + letter + "? No.") : ("Le " + letter + " ? Non."));
}
if (running)
{
SendText(English ? ("Mysterious word: " + word_cached + " (lives : " + vie + ")")
: ("Mot mystère : " + word_cached + " (vie : " + vie + ")"));
}
if (winner)
{
SendText(English ? ("Congrats, " + username + '!') : ("Félicitations, " + username + " !"));
running = false;
}
}
}
}
}
}
}
private void start()
{
vie = vie_param;
running = true;
letters = "";
word = chooseword();
compteur = compteur_param;
discovered = new bool[word.Length];
SendText(English ? "Hangman v1.0 - By ORelio" : "Pendu v1.0 - Par ORelio");
SendText(English ? ("Mysterious word: " + word_cached + " (lives : " + vie + ")")
: ("Mot mystère : " + word_cached + " (vie : " + vie + ")"));
SendText(English ? ("Try some letters ... :)") : ("Proposez une lettre ... :)"));
}
private string chooseword()
{
if (System.IO.File.Exists(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR))
{
string[] dico = System.IO.File.ReadAllLines(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR);
return dico[new Random().Next(dico.Length)];
}
else
{
LogToConsole(English ? "File not found: " + Settings.Hangman_FileWords_EN : "Fichier introuvable : " + Settings.Hangman_FileWords_FR);
return English ? "WORDSAREMISSING" : "DICOMANQUANT";
}
}
private string word_cached
{
get
{
string printed = "";
for (int i = 0; i < word.Length; i++)
{
if (discovered[i])
{
printed += word[i];
}
else printed += '_';
}
return printed;
}
}
private bool winner
{
get
{
for (int i = 0; i < discovered.Length; i++)
{
if (!discovered[i])
{
return false;
}
}
return true;
}
}
}
}

View file

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot sends a /list command every X seconds and save the result.
/// </summary>
public class PlayerListLogger : ChatBot
{
private int count;
private int timeping;
private string file;
/// <summary>
/// This bot sends a /list command every X seconds and save the result.
/// </summary>
/// <param name="pingparam">Time amount between each list ping (10 = 1s, 600 = 1 minute, etc.)</param>
public PlayerListLogger(int pingparam, string filetosavein)
{
count = 0;
file = filetosavein;
timeping = pingparam;
if (timeping < 10) { timeping = 10; } //To avoid flooding
}
public override void Update()
{
count++;
if (count == timeping)
{
SendText("/list");
count = 0;
}
}
public override void GetText(string text)
{
if (text.Contains("Joueurs en ligne") || text.Contains("Connected:") || text.Contains("online:"))
{
LogToConsole("Saving Player List");
DateTime now = DateTime.Now;
string TimeStamp = "[" + now.Year + '/' + now.Month + '/' + now.Day + ' ' + now.Hour + ':' + now.Minute + ']';
System.IO.File.AppendAllText(file, TimeStamp + "\n" + getVerbatim(text) + "\n\n");
}
}
}
}

View file

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// Allow to perform operations using whispers to the bot
/// </summary>
public class RemoteControl : ChatBot
{
public override void GetText(string text)
{
text = getVerbatim(text);
string command = "", sender = "";
if (isPrivateMessage(text, ref command, ref sender) && Settings.Bots_Owners.Contains(sender.ToLower()))
{
string cmd_name = command.Split(' ')[0];
switch (cmd_name.ToLower())
{
case "exit":
DisconnectAndExit();
break;
case "reco":
ReconnectToTheServer();
break;
case "script":
if (command.Length >= 8)
RunScript(command.Substring(7), sender);
break;
case "send":
if (command.Length >= 6)
SendText(command.Substring(5));
break;
case "connect":
if (command.Length >= 9)
{
Settings.ServerIP = command.Substring(8);
ReconnectToTheServer();
}
break;
case "help":
if (command.Length >= 6)
{
string help_cmd_name = command.Substring(5).ToLower();
switch (help_cmd_name)
{
case "exit": SendPrivateMessage(sender, "exit: disconnect from the server."); break;
case "reco": SendPrivateMessage(sender, "reco: restart and reconnct to the server."); break;
case "script": SendPrivateMessage(sender, "script <scriptname>: run a script file."); break;
case "send": SendPrivateMessage(sender, "send <text>: send a chat message or command."); break;
case "connect": SendPrivateMessage(sender, "connect <serverip>: connect to the specified server."); break;
case "help": SendPrivateMessage(sender, "help <cmdname>: show brief help about a command."); break;
default: SendPrivateMessage(sender, "help: unknown command '" + help_cmd_name + "'."); break;
}
}
else SendPrivateMessage(sender, "help <cmdname>. Available commands: exit, reco, script, send, connect.");
break;
default:
SendPrivateMessage(sender, "Unknown command '" + cmd_name + "'. Use 'help' for help.");
break;
}
}
}
}
}

View file

@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// Runs a list of commands
/// </summary>
public class Script : ChatBot
{
private string file;
private string[] lines = new string[0];
private int sleepticks = 10;
private int sleepticks_interval = 10;
private int nextline = 0;
private string owner;
public Script(string filename)
{
file = filename;
}
public Script(string filename, string ownername)
: this(filename)
{
if (ownername != "")
owner = ownername;
}
public static bool lookForScript(ref string filename)
{
//Automatically look in subfolders and try to add ".txt" file extension
string[] files = new string[]
{
filename,
filename + ".txt",
"scripts\\" + filename,
"scripts\\" + filename + ".txt",
"config\\" + filename,
"config\\" + filename + ".txt",
};
foreach (string possible_file in files)
{
if (System.IO.File.Exists(possible_file))
{
filename = possible_file;
return true;
}
}
return false;
}
public override void Initialize()
{
//Load the given file from the startup parameters
if (lookForScript(ref file))
{
lines = System.IO.File.ReadAllLines(file);
if (owner != null) { SendPrivateMessage(owner, "Script '" + file + "' loaded."); }
}
else
{
LogToConsole("File not found: '" + file + "'");
if (owner != null)
SendPrivateMessage(owner, "File not found: '" + file + "'");
UnloadBot(); //No need to keep the bot active
}
}
public override void Update()
{
if (sleepticks > 0) { sleepticks--; }
else
{
if (nextline < lines.Length) //Is there an instruction left to interpret?
{
string instruction_line = lines[nextline].Trim(); // Removes all whitespaces at start and end of current line
nextline++; //Move the cursor so that the next time the following line will be interpreted
sleepticks = sleepticks_interval; //Used to delay next command sending and prevent from beign kicked for spamming
if (instruction_line.Length > 1)
{
if (instruction_line[0] != '#' && instruction_line[0] != '/' && instruction_line[1] != '/')
{
string instruction_name = instruction_line.Split(' ')[0];
switch (instruction_name.ToLower())
{
case "send":
SendText(instruction_line.Substring(5, instruction_line.Length - 5));
break;
case "wait":
int ticks = 10;
try
{
ticks = Convert.ToInt32(instruction_line.Substring(5, instruction_line.Length - 5));
}
catch { }
sleepticks = ticks;
break;
case "disconnect":
DisconnectAndExit();
break;
case "exit": //Exit bot & stay connected to the server
UnloadBot();
break;
case "connect":
if (instruction_line.Length >= 9)
{
Settings.ServerIP = instruction_line.Substring(8);
ReconnectToTheServer();
}
break;
default:
sleepticks = 0; Update(); //Unknown command : process next line immediately
break;
}
}
else { sleepticks = 0; Update(); } //Comment: process next line immediately
}
}
else
{
//No more instructions to interpret
UnloadBot();
}
}
}
}
}

View file

@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// Trigger scripts on specific events
/// </summary>
public class ScriptScheduler : ChatBot
{
private class TaskDesc
{
public string script_file = null;
public bool triggerOnFirstLogin = false;
public bool triggerOnLogin = false;
public bool triggerOnTime = false;
public List<DateTime> triggerOnTime_Times = new List<DateTime>();
public bool alreadyTriggered = false;
}
private static bool firstlogin_done = false;
private string tasksfile;
private bool serverlogin_done;
private List<TaskDesc> tasks = new List<TaskDesc>();
private int verifytasks_timeleft = 10;
private int verifytasks_delay = 10;
public ScriptScheduler(string tasksfile)
{
this.tasksfile = tasksfile;
serverlogin_done = false;
}
public override void Initialize()
{
//Load the given file from the startup parameters
if (System.IO.File.Exists(tasksfile))
{
TaskDesc current_task = null;
String[] lines = System.IO.File.ReadAllLines(tasksfile);
foreach (string lineRAW in lines)
{
string line = lineRAW.Split('#')[0].Trim();
if (line.Length > 0)
{
if (line[0] == '[' && line[line.Length - 1] == ']')
{
switch (line.Substring(1, line.Length - 2).ToLower())
{
case "task":
checkAddTask(current_task);
current_task = new TaskDesc(); //Create a blank task
break;
}
}
else if (current_task != null)
{
string argName = line.Split('=')[0];
if (line.Length > (argName.Length + 1))
{
string argValue = line.Substring(argName.Length + 1);
switch (argName.ToLower())
{
case "triggeronfirstlogin": current_task.triggerOnFirstLogin = Settings.str2bool(argValue); break;
case "triggeronlogin": current_task.triggerOnLogin = Settings.str2bool(argValue); break;
case "triggerontime": current_task.triggerOnTime = Settings.str2bool(argValue); break;
case "timevalue": try { current_task.triggerOnTime_Times.Add(DateTime.ParseExact(argValue, "HH:mm", CultureInfo.InvariantCulture)); }
catch { } break;
case "script": current_task.script_file = argValue; break;
}
}
}
}
}
checkAddTask(current_task);
}
else
{
LogToConsole("File not found: '" + tasksfile + "'");
UnloadBot(); //No need to keep the bot active
}
}
private void checkAddTask(TaskDesc current_task)
{
if (current_task != null)
{
//Check if we built a valid task before adding it
if (current_task.script_file != null && Script.lookForScript(ref current_task.script_file) //Check if file exists
&& (current_task.triggerOnLogin || (current_task.triggerOnTime && current_task.triggerOnTime_Times.Count > 0))) //Look for a valid trigger
{
tasks.Add(current_task);
}
}
}
public override void Update()
{
if (verifytasks_timeleft <= 0)
{
verifytasks_timeleft = verifytasks_delay;
if (serverlogin_done)
{
foreach (TaskDesc task in tasks)
{
if (task.triggerOnTime)
{
foreach (DateTime time in task.triggerOnTime_Times)
{
if (time.Hour == DateTime.Now.Hour && time.Minute == DateTime.Now.Minute)
{
if (!task.alreadyTriggered)
{
task.alreadyTriggered = true;
RunScript(task.script_file);
}
}
}
}
else task.alreadyTriggered = false;
}
}
else
{
foreach (TaskDesc task in tasks)
{
if (task.triggerOnLogin || (firstlogin_done == false && task.triggerOnFirstLogin))
RunScript(task.script_file);
}
firstlogin_done = true;
serverlogin_done = true;
}
}
else verifytasks_timeleft--;
}
}
}

View file

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// Example of message receiving.
/// </summary>
public class TestBot : ChatBot
{
public override void GetText(string text)
{
string message = "";
string username = "";
text = getVerbatim(text);
if (isPrivateMessage(text, ref message, ref username))
{
ConsoleIO.WriteLine("Bot: " + username + " told me : " + message);
}
else if (isChatMessage(text, ref message, ref username))
{
ConsoleIO.WriteLine("Bot: " + username + " said : " + message);
}
}
}
}

View file

@ -25,7 +25,10 @@ namespace MinecraftClient
private static bool reading_lock = false; private static bool reading_lock = false;
private static bool writing_lock = false; private static bool writing_lock = false;
#region Read User Input /// <summary>
/// Read a password from the standard input
/// </summary>
public static string ReadPassword() public static string ReadPassword()
{ {
string password = ""; string password = "";
@ -71,6 +74,10 @@ namespace MinecraftClient
return password; return password;
} }
/// <summary>
/// Read a line from the standard input
/// </summary>
public static string ReadLine() public static string ReadLine()
{ {
if (basicIO) { return Console.ReadLine(); } if (basicIO) { return Console.ReadLine(); }
@ -174,9 +181,11 @@ namespace MinecraftClient
previous.AddLast(buffer + buffer2); previous.AddLast(buffer + buffer2);
return buffer + buffer2; return buffer + buffer2;
} }
#endregion
/// <summary>
/// Write a string to the standard output, without newline character
/// </summary>
#region Console Output
public static void Write(string text) public static void Write(string text)
{ {
if (basicIO) { Console.Write(text); return; } if (basicIO) { Console.Write(text); return; }
@ -216,16 +225,79 @@ namespace MinecraftClient
writing_lock = false; writing_lock = false;
} }
/// <summary>
/// Write a string to the standard output with a trailing newline
/// </summary>
public static void WriteLine(string line) public static void WriteLine(string line)
{ {
Write(line + '\n'); Write(line + '\n');
} }
/// <summary>
/// Write a single character to the standard output
/// </summary>
public static void Write(char c) public static void Write(char c)
{ {
Write("" + c); Write("" + c);
} }
#endregion
/// <summary>
/// Write a Minecraft-Formatted string to the standard output, using §c color codes
/// </summary>
/// <param name="str">String to write</param>
/// <param name="acceptnewlines">If false, space are printed instead of newlines</param>
public static void WriteLineFormatted(string str, bool acceptnewlines)
{
if (basicIO) { Console.WriteLine(str); return; }
if (!String.IsNullOrEmpty(str))
{
if (Settings.chatTimeStamps)
{
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
ConsoleIO.Write(hour.ToString("00") + ':' + minute.ToString("00") + ':' + second.ToString("00") + ' ');
}
if (!acceptnewlines) { str = str.Replace('\n', ' '); }
if (ConsoleIO.basicIO) { ConsoleIO.WriteLine(str); return; }
string[] subs = str.Split(new char[] { '§' });
if (subs[0].Length > 0) { ConsoleIO.Write(subs[0]); }
for (int i = 1; i < subs.Length; i++)
{
if (subs[i].Length > 0)
{
switch (subs[i][0])
{
case '0': Console.ForegroundColor = ConsoleColor.Gray; break; //Should be Black but Black is non-readable on a black background
case '1': Console.ForegroundColor = ConsoleColor.DarkBlue; break;
case '2': Console.ForegroundColor = ConsoleColor.DarkGreen; break;
case '3': Console.ForegroundColor = ConsoleColor.DarkCyan; break;
case '4': Console.ForegroundColor = ConsoleColor.DarkRed; break;
case '5': Console.ForegroundColor = ConsoleColor.DarkMagenta; break;
case '6': Console.ForegroundColor = ConsoleColor.DarkYellow; break;
case '7': Console.ForegroundColor = ConsoleColor.Gray; break;
case '8': Console.ForegroundColor = ConsoleColor.DarkGray; break;
case '9': Console.ForegroundColor = ConsoleColor.Blue; break;
case 'a': Console.ForegroundColor = ConsoleColor.Green; break;
case 'b': Console.ForegroundColor = ConsoleColor.Cyan; break;
case 'c': Console.ForegroundColor = ConsoleColor.Red; break;
case 'd': Console.ForegroundColor = ConsoleColor.Magenta; break;
case 'e': Console.ForegroundColor = ConsoleColor.Yellow; break;
case 'f': Console.ForegroundColor = ConsoleColor.White; break;
case 'r': Console.ForegroundColor = ConsoleColor.White; break;
}
if (subs[i].Length > 1)
{
ConsoleIO.Write(subs[i].Substring(1, subs[i].Length - 1));
}
}
}
ConsoleIO.Write('\n');
}
Console.ForegroundColor = ConsoleColor.Gray;
}
#region Subfunctions #region Subfunctions
private static void ClearLineAndBuffer() private static void ClearLineAndBuffer()

View file

@ -3,14 +3,15 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.IO;
namespace MinecraftClient namespace MinecraftClient.Crypto
{ {
/// <summary> /// <summary>
/// Methods for handling all the crypto stuff: RSA (Encryption Key Request), AES (Encrypted Stream), SHA-1 (Server Hash). /// Methods for handling all the crypto stuff: RSA (Encryption Key Request), AES (Encrypted Stream), SHA-1 (Server Hash).
/// </summary> /// </summary>
public class Crypto public class CryptoHandler
{ {
/// <summary> /// <summary>
/// Get a cryptographic service for encrypting data using the server's RSA public key /// Get a cryptographic service for encrypting data using the server's RSA public key
@ -192,249 +193,20 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// Interface for AES stream /// Get a new AES-encrypted stream for wrapping a non-encrypted stream.
/// Allows to use any object which has a Read() and Write() method.
/// </summary> /// </summary>
/// <param name="underlyingStream">Stream to encrypt</param>
/// <param name="AesKey">Key to use</param>
/// <param name="paddingProvider">Padding provider for Mono implementation</param>
/// <returns>Return an appropriate stream depending on the framework being used</returns>
public interface IAesStream public static IAesStream getAesStream(Stream underlyingStream, byte[] AesKey, IPaddingProvider paddingProvider)
{ {
int Read(byte[] buffer, int offset, int count); if (Program.isUsingMono)
void Write(byte[] buffer, int offset, int count);
}
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// This is the regular AesStream class used with the regular .NET framework from Microsoft.
/// </summary>
public class AesStream : System.IO.Stream, IAesStream
{
CryptoStream enc;
CryptoStream dec;
public AesStream(System.IO.Stream stream, byte[] key)
{ {
BaseStream = stream; return new Streams.MonoAesStream(underlyingStream, AesKey, paddingProvider);
enc = new CryptoStream(stream, GenerateAES(key).CreateEncryptor(), CryptoStreamMode.Write);
dec = new CryptoStream(stream, GenerateAES(key).CreateDecryptor(), CryptoStreamMode.Read);
}
public System.IO.Stream BaseStream { get; set; }
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
BaseStream.Flush();
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override int ReadByte()
{
return dec.ReadByte();
}
public override int Read(byte[] buffer, int offset, int count)
{
return dec.Read(buffer, offset, count);
}
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void WriteByte(byte b)
{
enc.WriteByte(b);
}
public override void Write(byte[] buffer, int offset, int count)
{
enc.Write(buffer, offset, count);
}
private RijndaelManaged GenerateAES(byte[] key)
{
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;
cipher.KeySize = 128;
cipher.FeedbackSize = 8;
cipher.Key = key;
cipher.IV = key;
return cipher;
}
}
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// This is a mono-compatible adaptation which only sends and receive 16 bytes at a time, and manually transforms blocks.
/// Data is cached before reaching the 128bits block size necessary for mono which is not CFB-8 compatible.
/// </summary>
public class MonoAesStream : System.IO.Stream, IAesStream
{
ICryptoTransform enc;
ICryptoTransform dec;
List<byte> dec_cache = new List<byte>();
List<byte> tosend_cache = new List<byte>();
public MonoAesStream(System.IO.Stream stream, byte[] key)
{
BaseStream = stream;
RijndaelManaged aes = GenerateAES(key);
enc = aes.CreateEncryptor();
dec = aes.CreateDecryptor();
}
public System.IO.Stream BaseStream { get; set; }
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
BaseStream.Flush();
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override int ReadByte()
{
byte[] temp = new byte[1];
Read(temp, 0, 1);
return temp[0];
}
public override int Read(byte[] buffer, int offset, int count)
{
while (dec_cache.Count < count)
{
byte[] temp_in = new byte[16];
byte[] temp_out = new byte[16];
int read = 0;
while (read < 16)
read += BaseStream.Read(temp_in, read, 16 - read);
dec.TransformBlock(temp_in, 0, 16, temp_out, 0);
foreach (byte b in temp_out)
dec_cache.Add(b);
}
for (int i = offset; i - offset < count; i++)
{
buffer[i] = dec_cache[0];
dec_cache.RemoveAt(0);
}
return count;
}
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void WriteByte(byte b)
{
Write(new byte[] { b }, 0, 1);
}
public override void Write(byte[] buffer, int offset, int count)
{
for (int i = offset; i - offset < count; i++)
tosend_cache.Add(buffer[i]);
if (tosend_cache.Count < 16)
tosend_cache.AddRange(MinecraftCom.getPaddingPacket());
while (tosend_cache.Count > 16)
{
byte[] temp_in = new byte[16];
byte[] temp_out = new byte[16];
for (int i = 0; i < 16; i++)
{
temp_in[i] = tosend_cache[0];
tosend_cache.RemoveAt(0);
}
enc.TransformBlock(temp_in, 0, 16, temp_out, 0);
BaseStream.Write(temp_out, 0, 16);
}
}
private RijndaelManaged GenerateAES(byte[] key)
{
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;
cipher.KeySize = 128;
cipher.FeedbackSize = 8;
cipher.Key = key;
cipher.IV = key;
return cipher;
} }
else return new Streams.RegularAesStream(underlyingStream, AesKey);
} }
} }
} }

View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Crypto
{
/// <summary>
/// Interface for AES stream
/// Allows to use a different implementation depending on the framework being used.
/// </summary>
public interface IAesStream
{
int Read(byte[] buffer, int offset, int count);
void Write(byte[] buffer, int offset, int count);
}
}

View file

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Crypto
{
/// <summary>
/// Interface for padding provider
/// Allow to get a padding plugin message from the current network protocol implementation.
/// </summary>
public interface IPaddingProvider
{
byte[] getPaddingPacket();
}
}

View file

@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace MinecraftClient.Crypto.Streams
{
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// This is a mono-compatible adaptation which only sends and receive 16 bytes at a time, and manually transforms blocks.
/// Data is cached before reaching the 128bits block size necessary for mono which is not CFB-8 compatible.
/// </summary>
public class MonoAesStream : Stream, IAesStream
{
IPaddingProvider pad;
ICryptoTransform enc;
ICryptoTransform dec;
List<byte> dec_cache = new List<byte>();
List<byte> tosend_cache = new List<byte>();
public MonoAesStream(System.IO.Stream stream, byte[] key, IPaddingProvider provider)
{
BaseStream = stream;
RijndaelManaged aes = GenerateAES(key);
enc = aes.CreateEncryptor();
dec = aes.CreateDecryptor();
}
public System.IO.Stream BaseStream { get; set; }
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
BaseStream.Flush();
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override int ReadByte()
{
byte[] temp = new byte[1];
Read(temp, 0, 1);
return temp[0];
}
public override int Read(byte[] buffer, int offset, int count)
{
while (dec_cache.Count < count)
{
byte[] temp_in = new byte[16];
byte[] temp_out = new byte[16];
int read = 0;
while (read < 16)
read += BaseStream.Read(temp_in, read, 16 - read);
dec.TransformBlock(temp_in, 0, 16, temp_out, 0);
foreach (byte b in temp_out)
dec_cache.Add(b);
}
for (int i = offset; i - offset < count; i++)
{
buffer[i] = dec_cache[0];
dec_cache.RemoveAt(0);
}
return count;
}
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void WriteByte(byte b)
{
Write(new byte[] { b }, 0, 1);
}
public override void Write(byte[] buffer, int offset, int count)
{
for (int i = offset; i - offset < count; i++)
tosend_cache.Add(buffer[i]);
if (tosend_cache.Count < 16)
tosend_cache.AddRange(pad.getPaddingPacket());
while (tosend_cache.Count > 16)
{
byte[] temp_in = new byte[16];
byte[] temp_out = new byte[16];
for (int i = 0; i < 16; i++)
{
temp_in[i] = tosend_cache[0];
tosend_cache.RemoveAt(0);
}
enc.TransformBlock(temp_in, 0, 16, temp_out, 0);
BaseStream.Write(temp_out, 0, 16);
}
}
private RijndaelManaged GenerateAES(byte[] key)
{
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;
cipher.KeySize = 128;
cipher.FeedbackSize = 8;
cipher.Key = key;
cipher.IV = key;
return cipher;
}
}
}

View file

@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace MinecraftClient.Crypto.Streams
{
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// This is the regular AesStream class used with the regular .NET framework from Microsoft.
/// </summary>
public class RegularAesStream : Stream, IAesStream
{
CryptoStream enc;
CryptoStream dec;
public RegularAesStream(Stream stream, byte[] key)
{
BaseStream = stream;
enc = new CryptoStream(stream, GenerateAES(key).CreateEncryptor(), CryptoStreamMode.Write);
dec = new CryptoStream(stream, GenerateAES(key).CreateDecryptor(), CryptoStreamMode.Read);
}
public System.IO.Stream BaseStream { get; set; }
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
BaseStream.Flush();
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override int ReadByte()
{
return dec.ReadByte();
}
public override int Read(byte[] buffer, int offset, int count)
{
return dec.Read(buffer, offset, count);
}
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void WriteByte(byte b)
{
enc.WriteByte(b);
}
public override void Write(byte[] buffer, int offset, int count)
{
enc.Write(buffer, offset, count);
}
private RijndaelManaged GenerateAES(byte[] key)
{
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;
cipher.KeySize = 128;
cipher.FeedbackSize = 8;
cipher.Key = key;
cipher.IV = key;
return cipher;
}
}
}

View file

@ -6,50 +6,63 @@ using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.IO; using System.IO;
using System.Net; using System.Net;
using MinecraftClient.Protocol;
using MinecraftClient.Proxy;
namespace MinecraftClient namespace MinecraftClient
{ {
/// <summary> /// <summary>
/// The main client class, used to connect to a Minecraft server. /// The main client class, used to connect to a Minecraft server.
/// It allows message sending and text receiving.
/// </summary> /// </summary>
class McTcpClient public class McTcpClient : IMinecraftComHandler
{ {
private List<ChatBot> bots = new List<ChatBot>();
private static List<ChatBots.Script> scripts_on_hold = new List<ChatBots.Script>();
public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); Settings.SingleCommand = ""; }
public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); }
public void BotClear() { bots.Clear(); }
public static int AttemptsLeft = 0; public static int AttemptsLeft = 0;
string host; private string host;
int port; private int port;
string username; private string username;
string text; private string uuid;
Thread t_updater; private string sessionid;
Thread t_sender;
public int getServerPort() { return port; }
public string getServerHost() { return host; }
public string getUsername() { return username; }
public string getUserUUID() { return uuid; }
public string getSessionID() { return sessionid; }
TcpClient client; TcpClient client;
MinecraftCom handler; IMinecraftCom handler;
/// <summary> /// <summary>
/// Starts the main chat client, wich will login to the server using the MinecraftCom class. /// Starts the main chat client
/// </summary> /// </summary>
/// <param name="username">The chosen username of a premium Minecraft Account</param> /// <param name="username">The chosen username of a premium Minecraft Account</param>
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param> /// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param> /// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
public McTcpClient(string username, string uuid, string sessionID, string server_port, MinecraftCom handler) public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_port)
{ {
StartClient(username, uuid, sessionID, server_port, false, handler, ""); StartClient(username, uuid, sessionID, server_port, protocolversion, false, "");
} }
/// <summary> /// <summary>
/// Starts the main chat client in single command sending mode, wich will login to the server using the MinecraftCom class, send the command and close. /// Starts the main chat client in single command sending mode
/// </summary> /// </summary>
/// <param name="username">The chosen username of a premium Minecraft Account</param> /// <param name="username">The chosen username of a premium Minecraft Account</param>
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param> /// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param> /// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
/// <param name="command">The text or command to send.</param> /// <param name="command">The text or command to send.</param>
public McTcpClient(string username, string uuid, string sessionID, string server_port, MinecraftCom handler, string command) public McTcpClient(string username, string uuid, string sessionID, string server_port, int protocolversion, string command)
{ {
StartClient(username, uuid, sessionID, server_port, true, handler, command); StartClient(username, uuid, sessionID, server_port, protocolversion, true, command);
} }
/// <summary> /// <summary>
@ -61,12 +74,15 @@ namespace MinecraftClient
/// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param> /// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param>
/// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param> /// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param>
private void StartClient(string user, string uuid, string sessionID, string server_port, bool singlecommand, MinecraftCom handler, string command) private void StartClient(string user, string uuid, string sessionID, string server_port, int protocolversion, bool singlecommand, string command)
{ {
this.handler = handler;
username = user;
string[] sip = server_port.Split(':'); string[] sip = server_port.Split(':');
host = sip[0];
this.sessionid = sessionID;
this.uuid = uuid;
this.username = user;
this.host = sip[0];
if (sip.Length == 1) if (sip.Length == 1)
{ {
port = 25565; port = 25565;
@ -80,47 +96,44 @@ namespace MinecraftClient
catch (FormatException) { port = 25565; } catch (FormatException) { port = 25565; }
} }
if (!singlecommand)
{
if (Settings.AntiAFK_Enabled) { BotLoad(new ChatBots.AntiAFK(Settings.AntiAFK_Delay)); }
if (Settings.Hangman_Enabled) { BotLoad(new ChatBots.HangmanGame(Settings.Hangman_English)); }
if (Settings.Alerts_Enabled) { BotLoad(new ChatBots.Alerts()); }
if (Settings.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.ChatLog_File.Replace("%username%", Settings.Username), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.PlayerLog_File.Replace("%username%", Settings.Username))); }
if (Settings.AutoRelog_Enabled) { BotLoad(new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); }
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.ScriptScheduler_TasksFile.Replace("%username%", Settings.Username))); }
if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); }
}
try try
{ {
Console.WriteLine("Logging in..."); client = ProxyHandler.newTcpClient(host, port);
client = new TcpClient(host, port);
client.ReceiveBufferSize = 1024 * 1024; client.ReceiveBufferSize = 1024 * 1024;
handler.setClient(client); handler = Protocol.ProtocolHandler.getProtocolHandler(client, protocolversion, this);
if (handler.Login(user, uuid, sessionID, host, port)) Console.WriteLine("Version is supported.");
Console.WriteLine("Logging in...");
if (handler.Login())
{ {
//Single command sending
if (singlecommand) if (singlecommand)
{ {
handler.SendChatMessage(command); handler.SendChatMessage(command);
Console.Write("Command "); ConsoleIO.WriteLineFormatted("§7Command §8" + command + "§7 sent.", false);
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(command);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(" sent.");
Thread.Sleep(5000); Thread.Sleep(5000);
handler.Disconnect("disconnect.quitting"); handler.Disconnect();
Thread.Sleep(1000); Thread.Sleep(1000);
} }
else else
{ {
foreach (ChatBot bot in scripts_on_hold) { bots.Add(bot); }
scripts_on_hold.Clear();
Console.WriteLine("Server was successfully joined.\nType '/quit' to leave the server."); Console.WriteLine("Server was successfully joined.\nType '/quit' to leave the server.");
StartTalk();
//Command sending thread, allowing user input
t_sender = new Thread(new ThreadStart(StartTalk));
t_sender.Name = "CommandSender";
t_sender.Start();
//Data receiving thread, allowing text receiving
t_updater = new Thread(new ThreadStart(Updater));
t_updater.Name = "PacketHandler";
t_updater.Start();
} }
} }
else
{
Console.WriteLine("Login failed.");
if (!singlecommand && !handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, "Login failed.")) { Program.ReadLineReconnect(); }
}
} }
catch (SocketException) catch (SocketException)
{ {
@ -136,14 +149,14 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Allows the user to send chat messages, commands, and to leave the server. /// Allows the user to send chat messages, commands, and to leave the server.
/// Will be automatically called on a separate Thread by StartClient()
/// </summary> /// </summary>
private void StartTalk() private void StartTalk()
{ {
try try
{ {
//Needed if the player is dead string text = "";
Thread.Sleep(500);
handler.SendRespawnPacket(); handler.SendRespawnPacket();
while (client.Client.Connected) while (client.Client.Connected)
@ -175,7 +188,7 @@ namespace MinecraftClient
} }
else if (text.ToLower().StartsWith("/script ")) else if (text.ToLower().StartsWith("/script "))
{ {
handler.BotLoad(new Bots.Script(text.Substring(8))); BotLoad(new ChatBots.Script(text.Substring(8)));
} }
else if (text != "") else if (text != "")
{ {
@ -190,7 +203,7 @@ namespace MinecraftClient
} }
else else
{ {
//Send the message splitted in several messages //Send the message splitted into several messages
while (text.Length > 100) while (text.Length > 100)
{ {
handler.SendChatMessage(text.Substring(0, 100)); handler.SendChatMessage(text.Substring(0, 100));
@ -213,44 +226,84 @@ namespace MinecraftClient
catch (IOException) { } catch (IOException) { }
} }
/// <summary>
/// Receive the data (including chat messages) from the server, and keep the connection alive.
/// Will be automatically called on a separate Thread by StartClient()
/// </summary>
private void Updater()
{
try
{
//handler.DebugDump();
do
{
Thread.Sleep(100);
} while (handler.Update());
}
catch (IOException) { }
catch (SocketException) { }
catch (ObjectDisposedException) { }
if (!handler.HasBeenKicked)
{
ConsoleIO.WriteLine("Connection has been lost.");
if (!handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "Connection has been lost.") && !Program.ReadLineReconnect()) { t_sender.Abort(); }
}
else if (Program.ReadLineReconnect()) { t_sender.Abort(); }
}
/// <summary> /// <summary>
/// Disconnect the client from the server /// Disconnect the client from the server
/// </summary> /// </summary>
public void Disconnect() public void Disconnect()
{ {
handler.Disconnect("disconnect.quitting"); foreach (ChatBot bot in bots)
if (bot is ChatBots.Script)
scripts_on_hold.Add((ChatBots.Script)bot);
handler.Disconnect();
handler.Dispose();
Thread.Sleep(1000); Thread.Sleep(1000);
if (t_updater != null) { t_updater.Abort(); }
if (t_sender != null) { t_sender.Abort(); }
if (client != null) { client.Close(); } if (client != null) { client.Close(); }
} }
/// <summary>
/// Received some text from the server
/// </summary>
/// <param name="text">Text received</param>
public void OnTextReceived(string text)
{
ConsoleIO.WriteLineFormatted(text, false);
foreach (ChatBot bot in bots)
bot.GetText(text);
}
/// <summary>
/// When connection has been lost
/// </summary>
public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
{
bool will_restart = false;
switch (reason)
{
case ChatBot.DisconnectReason.ConnectionLost:
message = "Connection has been lost.";
ConsoleIO.WriteLine(message);
break;
case ChatBot.DisconnectReason.InGameKick:
ConsoleIO.WriteLine("Disconnected by Server :");
ConsoleIO.WriteLineFormatted(message, true);
break;
case ChatBot.DisconnectReason.LoginRejected:
ConsoleIO.WriteLine("Login failed :");
ConsoleIO.WriteLineFormatted(message, true);
break;
}
foreach (ChatBot bot in bots)
will_restart |= bot.OnDisconnect(reason, message);
if (!will_restart) { Program.ReadLineReconnect(); }
}
/// <summary>
/// Called ~10 times per second by the protocol handler
/// </summary>
public void OnUpdate()
{
for (int i = 0; i < bots.Count; i++)
bots[i].Update();
}
/// <summary>
/// Send a chat message to the server
/// </summary>
public void SendChatMessage(string message)
{
handler.SendChatMessage(message);
}
} }
} }

View file

@ -53,7 +53,7 @@
<SignManifests>false</SignManifests> <SignManifests>false</SignManifests>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<ApplicationIcon>resources\appicon.ico</ApplicationIcon> <ApplicationIcon>Resources\AppIcon.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<StartupObject>MinecraftClient.Program</StartupObject> <StartupObject>MinecraftClient.Program</StartupObject>
@ -70,14 +70,42 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Bots.cs" /> <Compile Include="ChatBots\Alerts.cs" />
<Compile Include="ChatBots\AntiAFK.cs" />
<Compile Include="ChatBots\AutoRelog.cs" />
<Compile Include="ChatBots\ChatLog.cs" />
<Compile Include="ChatBots\HangmanGame.cs" />
<Compile Include="ChatBots\PlayerListLogger.cs" />
<Compile Include="ChatBots\RemoteControl.cs" />
<Compile Include="ChatBots\Script.cs" />
<Compile Include="ChatBots\ScriptScheduler.cs" />
<Compile Include="ChatBots\TestBot.cs" />
<Compile Include="ChatBot.cs" />
<Compile Include="ConsoleIO.cs" /> <Compile Include="ConsoleIO.cs" />
<Compile Include="Crypto.cs" /> <Compile Include="Crypto\Streams\MonoAesStream.cs" />
<Compile Include="ChatParser.cs" /> <Compile Include="Crypto\Streams\RegularAesStream.cs" />
<Compile Include="MinecraftCom.cs" /> <Compile Include="Crypto\CryptoHandler.cs" />
<Compile Include="Protocol\Handlers\ChatParser.cs" />
<Compile Include="Crypto\IAesStream.cs" />
<Compile Include="Crypto\IPaddingProvider.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="McTcpClient.cs" /> <Compile Include="McTcpClient.cs" />
<Compile Include="Protocol\Handlers\Protocol16.cs" />
<Compile Include="Protocol\IMinecraftCom.cs" />
<Compile Include="Protocol\IMinecraftComHandler.cs" />
<Compile Include="Protocol\Handlers\Protocol17.cs" />
<Compile Include="Protocol\ProtocolHandler.cs" />
<Compile Include="Proxy\ProxyHandler.cs" />
<Compile Include="Proxy\Handlers\EventArgs\CreateConnectionAsyncCompletedEventArgs.cs" />
<Compile Include="Proxy\Handlers\Exceptions\ProxyException.cs" />
<Compile Include="Proxy\Handlers\HttpProxyClient.cs" />
<Compile Include="Proxy\Handlers\IProxyClient.cs" />
<Compile Include="Proxy\Handlers\ProxyClientFactory.cs" />
<Compile Include="Proxy\Handlers\Socks4aProxyClient.cs" />
<Compile Include="Proxy\Handlers\Socks4ProxyClient.cs" />
<Compile Include="Proxy\Handlers\Socks5ProxyClient.cs" />
<Compile Include="Proxy\Handlers\Utils.cs" />
<Compile Include="Settings.cs" /> <Compile Include="Settings.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -103,7 +131,7 @@
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="resources\appicon.ico" /> <Content Include="Resources\AppIcon.ico" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View file

@ -1,620 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace MinecraftClient
{
/// <summary>
/// The class containing all the core functions needed to communicate with a Minecraft server.
/// </summary>
public class MinecraftCom : IAutoComplete
{
#region Login to Minecraft.net and get a new session ID
public enum LoginResult { OtherError, ServiceUnavailable, SSLError, Success, WrongPassword, AccountMigrated, NotPremium };
/// <summary>
/// Allows to login to a premium Minecraft account using the Yggdrasil authentication scheme.
/// </summary>
/// <param name="user">Login</param>
/// <param name="pass">Password</param>
/// <param name="accesstoken">Will contain the access token returned by Minecraft.net, if the login is successful</param>
/// <param name="uuid">Will contain the player's UUID, needed for multiplayer</param>
/// <returns>Returns the status of the login (Success, Failure, etc.)</returns>
public static LoginResult GetLogin(ref string user, string pass, ref string accesstoken, ref string uuid)
{
try
{
WebClient wClient = new WebClient();
wClient.Headers.Add("Content-Type: application/json");
string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + user + "\", \"password\": \"" + pass + "\" }";
string result = wClient.UploadString("https://authserver.mojang.com/authenticate", json_request);
if (result.Contains("availableProfiles\":[]}"))
{
return LoginResult.NotPremium;
}
else
{
string[] temp = result.Split(new string[] { "accessToken\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { accesstoken = temp[1].Split('"')[0]; }
temp = result.Split(new string[] { "name\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { user = temp[1].Split('"')[0]; }
temp = result.Split(new string[] { "availableProfiles\":[{\"id\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { uuid = temp[1].Split('"')[0]; }
return LoginResult.Success;
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse response = (HttpWebResponse)e.Response;
if ((int)response.StatusCode == 403)
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
{
string result = sr.ReadToEnd();
if (result.Contains("UserMigratedException"))
{
return LoginResult.AccountMigrated;
}
else return LoginResult.WrongPassword;
}
}
else if ((int)response.StatusCode == 503)
{
return LoginResult.ServiceUnavailable;
}
else
{
Console.ForegroundColor = ConsoleColor.DarkGray;
ConsoleIO.WriteLine("Got error code from server: " + (int)response.StatusCode);
Console.ForegroundColor = ConsoleColor.Gray;
return LoginResult.OtherError;
}
}
else if (e.Status == WebExceptionStatus.SendFailure)
{
return LoginResult.SSLError;
}
else return LoginResult.OtherError;
}
}
#endregion
#region Session checking when joining a server in online mode
/// <summary>
/// Check session using the Yggdrasil authentication scheme. Allow to join an online-mode server
/// </summary>
/// <param name="user">Username</param>
/// <param name="accesstoken">Session ID</param>
/// <param name="serverhash">Server ID</param>
/// <returns>TRUE if session was successfully checked</returns>
public static bool SessionCheck(string uuid, string accesstoken, string serverhash)
{
try
{
WebClient wClient = new WebClient();
wClient.Headers.Add("Content-Type: application/json");
string json_request = "{\"accessToken\":\"" + accesstoken + "\",\"selectedProfile\":\"" + uuid + "\",\"serverId\":\"" + serverhash + "\"}";
return (wClient.UploadString("https://sessionserver.mojang.com/session/minecraft/join", json_request) == "");
}
catch (WebException) { return false; }
}
#endregion
TcpClient c = new TcpClient();
Crypto.IAesStream s;
public bool HasBeenKicked { get { return connectionlost; } }
bool connectionlost = false;
bool encrypted = false;
int protocolversion;
public MinecraftCom()
{
foreach (ChatBot bot in scripts_on_hold) { bots.Add(bot); }
scripts_on_hold.Clear();
}
public bool Update()
{
for (int i = 0; i < bots.Count; i++) { bots[i].Update(); }
if (c.Client == null || !c.Connected) { return false; }
int id = 0, size = 0;
try
{
while (c.Client.Available > 0)
{
size = readNextVarInt(); //Packet size
id = readNextVarInt(); //Packet ID
switch (id)
{
case 0x00:
byte[] keepalive = new byte[4] { 0, 0, 0, 0 };
Receive(keepalive, 0, 4, SocketFlags.None);
byte[] keepalive_packet = concatBytes(getVarInt(0x00), keepalive);
byte[] keepalive_tosend = concatBytes(getVarInt(keepalive_packet.Length), keepalive_packet);
Send(keepalive_tosend);
break;
case 0x02:
string message = readNextString();
//printstring("§8" + message, false); //Debug : Show the RAW JSON data
message = ChatParser.ParseText(message);
printstring(message, false);
for (int i = 0; i < bots.Count; i++) { bots[i].GetText(message); } break;
case 0x3A:
int autocomplete_count = readNextVarInt();
string tab_list = "";
for (int i = 0; i < autocomplete_count; i++)
{
autocomplete_result = readNextString();
if (autocomplete_result != "")
tab_list = tab_list + autocomplete_result + " ";
}
autocomplete_received = true;
tab_list = tab_list.Trim();
if (tab_list.Length > 0)
printstring("§8" + tab_list, false);
break;
case 0x40: string reason = ChatParser.ParseText(readNextString());
ConsoleIO.WriteLine("Disconnected by Server :");
printstring(reason, true);
connectionlost = true;
for (int i = 0; i < bots.Count; i++)
bots[i].OnDisconnect(ChatBot.DisconnectReason.InGameKick, reason);
return false;
default:
readData(size - getVarInt(id).Length); //Skip packet
break;
}
}
}
catch (SocketException) { return false; }
return true;
}
public void DebugDump()
{
byte[] cache = new byte[128000];
Receive(cache, 0, 128000, SocketFlags.None);
string dump = BitConverter.ToString(cache);
System.IO.File.WriteAllText("debug.txt", dump);
System.Diagnostics.Process.Start("debug.txt");
}
public bool OnConnectionLost(ChatBot.DisconnectReason reason, string reason_message)
{
if (!connectionlost)
{
connectionlost = true;
for (int i = 0; i < bots.Count; i++)
{
if (bots[i].OnDisconnect(reason, reason_message))
{
return true; //The client is about to restart
}
}
}
return false;
}
private void readData(int offset)
{
if (offset > 0)
{
try
{
byte[] cache = new byte[offset];
Receive(cache, 0, offset, SocketFlags.None);
}
catch (OutOfMemoryException) { }
}
}
private string readNextString()
{
int length = readNextVarInt();
if (length > 0)
{
byte[] cache = new byte[length];
Receive(cache, 0, length, SocketFlags.None);
return Encoding.UTF8.GetString(cache);
}
else return "";
}
private byte[] readNextByteArray()
{
byte[] tmp = new byte[2];
Receive(tmp, 0, 2, SocketFlags.None);
Array.Reverse(tmp);
short len = BitConverter.ToInt16(tmp, 0);
byte[] data = new byte[len];
Receive(data, 0, len, SocketFlags.None);
return data;
}
private int readNextVarInt()
{
int i = 0;
int j = 0;
int k = 0;
byte[] tmp = new byte[1];
while (true)
{
Receive(tmp, 0, 1, SocketFlags.None);
k = tmp[0];
i |= (k & 0x7F) << j++ * 7;
if (j > 5) throw new OverflowException("VarInt too big");
if ((k & 0x80) != 128) break;
}
return i;
}
public static byte[] getPaddingPacket()
{
//Will generate a 15-bytes long padding packet
byte[] id = getVarInt(0x17); //Plugin Message
byte[] channel_name = Encoding.UTF8.GetBytes("MCC|Pad");
byte[] channel_name_len = getVarInt(channel_name.Length);
byte[] data = new byte[] { 0x00, 0x00, 0x00 };
byte[] data_len = BitConverter.GetBytes((short)data.Length); Array.Reverse(data_len);
byte[] packet_data = concatBytes(id, channel_name_len, channel_name, data_len, data);
byte[] packet_length = getVarInt(packet_data.Length);
return concatBytes(packet_length, packet_data);
}
private static byte[] getVarInt(int paramInt)
{
List<byte> bytes = new List<byte>();
while ((paramInt & -128) != 0)
{
bytes.Add((byte)(paramInt & 127 | 128));
paramInt = (int)(((uint)paramInt) >> 7);
}
bytes.Add((byte)paramInt);
return bytes.ToArray();
}
private static byte[] concatBytes(params byte[][] bytes)
{
List<byte> result = new List<byte>();
foreach (byte[] array in bytes)
result.AddRange(array);
return result.ToArray();
}
private static int atoi(string str)
{
return int.Parse(new string(str.Trim().TakeWhile(char.IsDigit).ToArray()));
}
private static void setcolor(char c)
{
switch (c)
{
case '0': Console.ForegroundColor = ConsoleColor.Gray; break; //Should be Black but Black is non-readable on a black background
case '1': Console.ForegroundColor = ConsoleColor.DarkBlue; break;
case '2': Console.ForegroundColor = ConsoleColor.DarkGreen; break;
case '3': Console.ForegroundColor = ConsoleColor.DarkCyan; break;
case '4': Console.ForegroundColor = ConsoleColor.DarkRed; break;
case '5': Console.ForegroundColor = ConsoleColor.DarkMagenta; break;
case '6': Console.ForegroundColor = ConsoleColor.DarkYellow; break;
case '7': Console.ForegroundColor = ConsoleColor.Gray; break;
case '8': Console.ForegroundColor = ConsoleColor.DarkGray; break;
case '9': Console.ForegroundColor = ConsoleColor.Blue; break;
case 'a': Console.ForegroundColor = ConsoleColor.Green; break;
case 'b': Console.ForegroundColor = ConsoleColor.Cyan; break;
case 'c': Console.ForegroundColor = ConsoleColor.Red; break;
case 'd': Console.ForegroundColor = ConsoleColor.Magenta; break;
case 'e': Console.ForegroundColor = ConsoleColor.Yellow; break;
case 'f': Console.ForegroundColor = ConsoleColor.White; break;
case 'r': Console.ForegroundColor = ConsoleColor.White; break;
}
}
private static void printstring(string str, bool acceptnewlines)
{
if (!String.IsNullOrEmpty(str))
{
if (Settings.chatTimeStamps)
{
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
ConsoleIO.Write(hour.ToString("00") + ':' + minute.ToString("00") + ':' + second.ToString("00") + ' ');
}
if (!acceptnewlines) { str = str.Replace('\n', ' '); }
if (ConsoleIO.basicIO) { ConsoleIO.WriteLine(str); return; }
string[] subs = str.Split(new char[] { '§' });
if (subs[0].Length > 0) { ConsoleIO.Write(subs[0]); }
for (int i = 1; i < subs.Length; i++)
{
if (subs[i].Length > 0)
{
setcolor(subs[i][0]);
if (subs[i].Length > 1)
{
ConsoleIO.Write(subs[i].Substring(1, subs[i].Length - 1));
}
}
}
ConsoleIO.Write('\n');
}
Console.ForegroundColor = ConsoleColor.Gray;
}
private bool autocomplete_received = false;
private string autocomplete_result = "";
public string AutoComplete(string behindcursor)
{
if (String.IsNullOrEmpty(behindcursor))
return "";
byte[] packet_id = getVarInt(0x14);
byte[] tocomplete_val = Encoding.UTF8.GetBytes(behindcursor);
byte[] tocomplete_len = getVarInt(tocomplete_val.Length);
byte[] tabcomplete_packet = concatBytes(packet_id, tocomplete_len, tocomplete_val);
byte[] tabcomplete_packet_tosend = concatBytes(getVarInt(tabcomplete_packet.Length), tabcomplete_packet);
autocomplete_received = false;
autocomplete_result = behindcursor;
Send(tabcomplete_packet_tosend);
int wait_left = 50; //do not wait more than 5 seconds (50 * 100 ms)
while (wait_left > 0 && !autocomplete_received) { System.Threading.Thread.Sleep(100); wait_left--; }
return autocomplete_result;
}
public void setVersion(int ver) { protocolversion = ver; }
public void setClient(TcpClient n) { c = n; }
private void setEncryptedClient(Crypto.IAesStream n) { s = n; encrypted = true; }
private void Receive(byte[] buffer, int start, int offset, SocketFlags f)
{
int read = 0;
while (read < offset)
{
if (encrypted)
{
read += s.Read(buffer, start + read, offset - read);
}
else read += c.Client.Receive(buffer, start + read, offset - read, f);
}
}
private void Send(byte[] buffer)
{
if (encrypted)
{
s.Write(buffer, 0, buffer.Length);
}
else c.Client.Send(buffer);
}
public static bool GetServerInfo(string serverIP, ref int protocolversion, ref string version)
{
try
{
string host; int port;
string[] sip = serverIP.Split(':');
host = sip[0];
if (sip.Length == 1)
{
port = 25565;
}
else
{
try
{
port = Convert.ToInt32(sip[1]);
}
catch (FormatException) { port = 25565; }
}
TcpClient tcp = new TcpClient(host, port);
tcp.ReceiveBufferSize = 1024 * 1024;
byte[] packet_id = getVarInt(0);
byte[] protocol_version = getVarInt(4);
byte[] server_adress_val = Encoding.UTF8.GetBytes(host);
byte[] server_adress_len = getVarInt(server_adress_val.Length);
byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port);
byte[] next_state = getVarInt(1);
byte[] packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state);
byte[] tosend = concatBytes(getVarInt(packet.Length), packet);
tcp.Client.Send(tosend, SocketFlags.None);
byte[] status_request = getVarInt(0);
byte[] request_packet = concatBytes(getVarInt(status_request.Length), status_request);
tcp.Client.Send(request_packet, SocketFlags.None);
MinecraftCom ComTmp = new MinecraftCom();
ComTmp.setClient(tcp);
if (ComTmp.readNextVarInt() > 0) //Read Response length
{
if (ComTmp.readNextVarInt() == 0x00) //Read Packet ID
{
string result = ComTmp.readNextString(); //Get the Json data
if (result[0] == '{' && result.Contains("protocol\":") && result.Contains("name\":\""))
{
string[] tmp_ver = result.Split(new string[] { "protocol\":" }, StringSplitOptions.None);
string[] tmp_name = result.Split(new string[] { "name\":\"" }, StringSplitOptions.None);
if (tmp_ver.Length >= 2 && tmp_name.Length >= 2)
{
protocolversion = atoi(tmp_ver[1]);
version = tmp_name[1].Split('"')[0];
if (result.Contains("modinfo\":"))
{
//Server is running Forge (which is not supported)
version = "Forge " + version;
protocolversion = 0;
}
Console.ForegroundColor = ConsoleColor.DarkGray;
//Console.WriteLine(result); //Debug: show the full Json string
Console.WriteLine("Server version : " + version + " (protocol v" + protocolversion + ").");
Console.ForegroundColor = ConsoleColor.Gray;
return true;
}
}
}
}
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Unexpected answer from the server (is that a MC 1.7+ server ?)");
Console.ForegroundColor = ConsoleColor.Gray;
return false;
}
catch
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("An error occured while attempting to connect to this IP.");
Console.ForegroundColor = ConsoleColor.Gray;
return false;
}
}
public bool Login(string username, string uuid, string sessionID, string host, int port)
{
byte[] packet_id = getVarInt(0);
byte[] protocol_version = getVarInt(protocolversion);
byte[] server_adress_val = Encoding.UTF8.GetBytes(host);
byte[] server_adress_len = getVarInt(server_adress_val.Length);
byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port);
byte[] next_state = getVarInt(2);
byte[] handshake_packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state);
byte[] handshake_packet_tosend = concatBytes(getVarInt(handshake_packet.Length), handshake_packet);
Send(handshake_packet_tosend);
byte[] username_val = Encoding.UTF8.GetBytes(username);
byte[] username_len = getVarInt(username_val.Length);
byte[] login_packet = concatBytes(packet_id, username_len, username_val);
byte[] login_packet_tosend = concatBytes(getVarInt(login_packet.Length), login_packet);
Send(login_packet_tosend);
readNextVarInt(); //Packet size
int pid = readNextVarInt(); //Packet ID
if (pid == 0x00) //Login rejected
{
Console.WriteLine("Login rejected by Server :");
printstring(ChatParser.ParseText(readNextString()), true);
return false;
}
else if (pid == 0x01) //Encryption request
{
string serverID = readNextString();
byte[] Serverkey = readNextByteArray();
byte[] token = readNextByteArray();
return StartEncryption(uuid, sessionID, token, serverID, Serverkey);
}
else if (pid == 0x02) //Login successfull
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Server is in offline mode.");
Console.ForegroundColor = ConsoleColor.Gray;
return true; //No need to check session or start encryption
}
else return false;
}
public bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, byte[] serverKey)
{
System.Security.Cryptography.RSACryptoServiceProvider RSAService = Crypto.DecodeRSAPublicKey(serverKey);
byte[] secretKey = Crypto.GenerateAESPrivateKey();
Console.ForegroundColor = ConsoleColor.DarkGray;
ConsoleIO.WriteLine("Crypto keys & hash generated.");
Console.ForegroundColor = ConsoleColor.Gray;
if (serverIDhash != "-")
{
Console.WriteLine("Checking Session...");
if (!SessionCheck(uuid, sessionID, Crypto.getServerHash(serverIDhash, serverKey, secretKey)))
{
return false;
}
}
//Encrypt the data
byte[] key_enc = RSAService.Encrypt(secretKey, false);
byte[] token_enc = RSAService.Encrypt(token, false);
byte[] key_len = BitConverter.GetBytes((short)key_enc.Length); Array.Reverse(key_len);
byte[] token_len = BitConverter.GetBytes((short)token_enc.Length); Array.Reverse(token_len);
//Encryption Response packet
byte[] packet_id = getVarInt(0x01);
byte[] encryption_response = concatBytes(packet_id, key_len, key_enc, token_len, token_enc);
byte[] encryption_response_tosend = concatBytes(getVarInt(encryption_response.Length), encryption_response);
Send(encryption_response_tosend);
//Start client-side encryption
Crypto.IAesStream encrypted;
if (Program.isUsingMono) { encrypted = new Crypto.MonoAesStream(c.GetStream(), secretKey); }
else encrypted = new Crypto.AesStream(c.GetStream(), secretKey);
setEncryptedClient(encrypted);
//Get the next packet
readNextVarInt(); //Skip Packet size (not needed)
return (readNextVarInt() == 0x02); //Packet ID. 0x02 = Login Success
}
public bool SendChatMessage(string message)
{
if (String.IsNullOrEmpty(message))
return true;
try
{
byte[] packet_id = getVarInt(0x01);
byte[] message_val = Encoding.UTF8.GetBytes(message);
byte[] message_len = getVarInt(message_val.Length);
byte[] message_packet = concatBytes(packet_id, message_len, message_val);
byte[] message_packet_tosend = concatBytes(getVarInt(message_packet.Length), message_packet);
Send(message_packet_tosend);
return true;
}
catch (SocketException) { return false; }
}
public bool SendRespawnPacket()
{
try
{
byte[] packet_id = getVarInt(0x16);
byte[] action_id = new byte[] { 0 };
byte[] respawn_packet = concatBytes(getVarInt(packet_id.Length + 1), packet_id, action_id);
Send(respawn_packet);
return true;
}
catch (SocketException) { return false; }
}
public void Disconnect(string message)
{
if (message == null)
message = "";
message.Replace("\"", "\\\"");
message = "\"" + message + "\"";
try
{
byte[] packet_id = getVarInt(0x40);
byte[] message_val = Encoding.UTF8.GetBytes(message);
byte[] message_len = getVarInt(message_val.Length);
byte[] disconnect_packet = concatBytes(packet_id, message_len, message_val);
byte[] disconnect_packet_tosend = concatBytes(getVarInt(disconnect_packet.Length), disconnect_packet);
Send(disconnect_packet_tosend);
}
catch (SocketException) { }
catch (System.IO.IOException) { }
catch (NullReferenceException) { }
catch (ObjectDisposedException) { }
foreach (ChatBot bot in bots)
if (bot is Bots.Script)
scripts_on_hold.Add((Bots.Script)bot);
}
private List<ChatBot> bots = new List<ChatBot>();
private static List<Bots.Script> scripts_on_hold = new List<Bots.Script>();
public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); Settings.SingleCommand = ""; }
public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); }
public void BotClear() { bots.Clear(); }
}
}

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using MinecraftClient.Protocol;
namespace MinecraftClient namespace MinecraftClient
{ {
@ -15,7 +16,7 @@ namespace MinecraftClient
{ {
private static McTcpClient Client; private static McTcpClient Client;
public static string[] startupargs; public static string[] startupargs;
public const string Version = "1.7.3"; public const string Version = "1.8.0-Indev";
/// <summary> /// <summary>
/// The main entry point of Minecraft Console Client /// The main entry point of Minecraft Console Client
@ -23,7 +24,7 @@ namespace MinecraftClient
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("Console Client for MC 1.7.2 to 1.7.9 - v" + Version + " - By ORelio & Contributors"); Console.WriteLine("Console Client for MC 1.4.6 to 1.7.9 - v" + Version + " - By ORelio & Contributors");
//Basic Input/Output ? //Basic Input/Output ?
if (args.Length >= 1 && args[args.Length - 1] == "BasicIO") if (args.Length >= 1 && args[args.Length - 1] == "BasicIO")
@ -104,26 +105,24 @@ namespace MinecraftClient
private static void InitializeClient() private static void InitializeClient()
{ {
ProtocolHandler.LoginResult result;
MinecraftCom.LoginResult result;
Settings.Username = Settings.Login; Settings.Username = Settings.Login;
string sessionID = ""; string sessionID = "";
string UUID = ""; string UUID = "";
if (Settings.Password == "-") if (Settings.Password == "-")
{ {
Console.ForegroundColor = ConsoleColor.DarkGray; ConsoleIO.WriteLineFormatted("§8You chose to run in offline mode.", false);
Console.WriteLine("You chose to run in offline mode."); result = ProtocolHandler.LoginResult.Success;
Console.ForegroundColor = ConsoleColor.Gray;
result = MinecraftCom.LoginResult.Success;
sessionID = "0"; sessionID = "0";
} }
else else
{ {
Console.WriteLine("Connecting to Minecraft.net..."); Console.WriteLine("Connecting to Minecraft.net...");
result = MinecraftCom.GetLogin(ref Settings.Username, Settings.Password, ref sessionID, ref UUID); result = ProtocolHandler.GetLogin(ref Settings.Username, Settings.Password, ref sessionID, ref UUID);
} }
if (result == MinecraftCom.LoginResult.Success)
if (result == ProtocolHandler.LoginResult.Success)
{ {
if (Settings.ConsoleTitle != "") if (Settings.ConsoleTitle != "")
{ {
@ -140,39 +139,18 @@ namespace MinecraftClient
//Get server version //Get server version
Console.WriteLine("Retrieving Server Info..."); Console.WriteLine("Retrieving Server Info...");
int protocolversion = 0; string version = ""; int protocolversion = 0; string version = "";
if (MinecraftCom.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version)) if (ProtocolHandler.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version))
{ {
//Supported protocol version ? try
int[] supportedVersions = { 4, 5 };
if (Array.IndexOf(supportedVersions, protocolversion) > -1)
{ {
//Load translations (Minecraft 1.6+)
ChatParser.InitTranslations();
//Will handle the connection for this client
Console.WriteLine("Version is supported.");
MinecraftCom handler = new MinecraftCom();
ConsoleIO.SetAutoCompleteEngine(handler);
handler.setVersion(protocolversion);
//Load & initialize bots if needed
if (Settings.AntiAFK_Enabled) { handler.BotLoad(new Bots.AntiAFK(Settings.AntiAFK_Delay)); }
if (Settings.Hangman_Enabled) { handler.BotLoad(new Bots.Pendu(Settings.Hangman_English)); }
if (Settings.Alerts_Enabled) { handler.BotLoad(new Bots.Alerts()); }
if (Settings.ChatLog_Enabled) { handler.BotLoad(new Bots.ChatLog(Settings.ChatLog_File.Replace("%username%", Settings.Username), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
if (Settings.PlayerLog_Enabled) { handler.BotLoad(new Bots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.PlayerLog_File.Replace("%username%", Settings.Username))); }
if (Settings.AutoRelog_Enabled) { handler.BotLoad(new Bots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); }
if (Settings.ScriptScheduler_Enabled) { handler.BotLoad(new Bots.ScriptScheduler(Settings.ScriptScheduler_TasksFile.Replace("%username%", Settings.Username))); }
if (Settings.RemoteCtrl_Enabled) { handler.BotLoad(new Bots.RemoteControl()); }
//Start the main TCP client //Start the main TCP client
if (Settings.SingleCommand != "") if (Settings.SingleCommand != "")
{ {
Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, handler, Settings.SingleCommand); Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, protocolversion, Settings.SingleCommand);
} }
else Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, handler); else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, Settings.ServerIP);
} }
else catch (NotSupportedException)
{ {
Console.WriteLine("Cannot connect to the server : This version is not supported !"); Console.WriteLine("Cannot connect to the server : This version is not supported !");
ReadLineReconnect(); ReadLineReconnect();
@ -183,7 +161,7 @@ namespace MinecraftClient
Console.WriteLine("Failed to ping this IP."); Console.WriteLine("Failed to ping this IP.");
if (Settings.AutoRelog_Enabled) if (Settings.AutoRelog_Enabled)
{ {
Bots.AutoRelog bot = new Bots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries); ChatBots.AutoRelog bot = new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries);
if (!bot.OnDisconnect(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP.")) { ReadLineReconnect(); } if (!bot.OnDisconnect(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP.")) { ReadLineReconnect(); }
} }
else ReadLineReconnect(); else ReadLineReconnect();
@ -195,19 +173,17 @@ namespace MinecraftClient
Console.Write("Connection failed : "); Console.Write("Connection failed : ");
switch (result) switch (result)
{ {
case MinecraftCom.LoginResult.AccountMigrated: Console.WriteLine("Account migrated, use e-mail as username."); break; case ProtocolHandler.LoginResult.AccountMigrated: Console.WriteLine("Account migrated, use e-mail as username."); break;
case MinecraftCom.LoginResult.ServiceUnavailable: Console.WriteLine("Login servers are unavailable. Please try again later."); break; case ProtocolHandler.LoginResult.ServiceUnavailable: Console.WriteLine("Login servers are unavailable. Please try again later."); break;
case MinecraftCom.LoginResult.WrongPassword: Console.WriteLine("Incorrect password."); break; case ProtocolHandler.LoginResult.WrongPassword: Console.WriteLine("Incorrect password."); break;
case MinecraftCom.LoginResult.NotPremium: Console.WriteLine("User not premium."); break; case ProtocolHandler.LoginResult.NotPremium: Console.WriteLine("User not premium."); break;
case MinecraftCom.LoginResult.OtherError: Console.WriteLine("Network error."); break; case ProtocolHandler.LoginResult.OtherError: Console.WriteLine("Network error."); break;
case MinecraftCom.LoginResult.SSLError: Console.WriteLine("SSL Error."); case ProtocolHandler.LoginResult.SSLError: Console.WriteLine("SSL Error.");
if (isUsingMono) if (isUsingMono)
{ {
Console.ForegroundColor = ConsoleColor.DarkGray; ConsoleIO.WriteLineFormatted("§8It appears that you are using Mono to run this program."
Console.WriteLine("It appears that you are using Mono to run this program."
+ '\n' + "The first time, you have to import HTTPS certificates using:" + '\n' + "The first time, you have to import HTTPS certificates using:"
+ '\n' + "mozroots --import --ask-remove"); + '\n' + "mozroots --import --ask-remove", true);
Console.ForegroundColor = ConsoleColor.Gray;
return; return;
} }
break; break;

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace MinecraftClient namespace MinecraftClient.Protocol.Handlers
{ {
/// <summary> /// <summary>
/// This class parses JSON chat data from MC 1.6+ and returns the appropriate string to be printed. /// This class parses JSON chat data from MC 1.6+ and returns the appropriate string to be printed.
@ -127,9 +127,7 @@ namespace MinecraftClient
&& System.IO.File.Exists(Settings.TranslationsFile_FromMCDir)) && System.IO.File.Exists(Settings.TranslationsFile_FromMCDir))
{ {
Language_File = Settings.TranslationsFile_FromMCDir; Language_File = Settings.TranslationsFile_FromMCDir;
Console.ForegroundColor = ConsoleColor.DarkGray; ConsoleIO.WriteLineFormatted("§8Defaulting to en_GB.lang from your Minecraft directory.", false);
ConsoleIO.WriteLine("Defaulting to en_GB.lang from your Minecraft directory.");
Console.ForegroundColor = ConsoleColor.Gray;
} }
//Load the external dictionnary of translation rules or display an error message //Load the external dictionnary of translation rules or display an error message
@ -148,16 +146,12 @@ namespace MinecraftClient
} }
} }
Console.ForegroundColor = ConsoleColor.DarkGray; ConsoleIO.WriteLineFormatted("§8Translations file loaded.", false);
ConsoleIO.WriteLine("Translations file loaded.");
Console.ForegroundColor = ConsoleColor.Gray;
} }
else //No external dictionnary found. else //No external dictionnary found.
{ {
Console.ForegroundColor = ConsoleColor.DarkGray; ConsoleIO.WriteLineFormatted("§8Translations file not found: \"" + Language_File + "\""
ConsoleIO.WriteLine("Translations file not found: \"" + Language_File + "\"" + "\nSome messages won't be properly printed without this file.", true);
+ "\nSome messages won't be properly printed without this file.");
Console.ForegroundColor = ConsoleColor.Gray;
} }
} }

View file

@ -0,0 +1,684 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using MinecraftClient.Crypto;
using MinecraftClient.Proxy;
namespace MinecraftClient.Protocol.Handlers
{
/// <summary>
/// Implementation for Minecraft 1.4.X, 1.5.X, 1.6.X Protocols
/// </summary>
class Protocol16Handler : IMinecraftCom
{
IMinecraftComHandler handler;
private bool autocomplete_received = false;
private string autocomplete_result = "";
private bool encrypted = false;
private int protocolversion;
private Thread netRead;
Crypto.IAesStream s;
TcpClient c;
public Protocol16Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler)
{
ConsoleIO.SetAutoCompleteEngine(this);
if (protocolversion >= 72)
ChatParser.InitTranslations();
this.c = Client;
this.protocolversion = ProtocolVersion;
this.handler = Handler;
}
private Protocol16Handler(TcpClient Client)
{
this.c = Client;
}
private void Updater()
{
try
{
do { Thread.Sleep(100); }
while (Update());
}
catch (System.IO.IOException) { }
catch (SocketException) { }
catch (ObjectDisposedException) { }
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
}
private bool Update()
{
bool connection_ok = true;
while (c.Client.Available > 0 && connection_ok)
{
byte id = readNextByte();
connection_ok = processPacket(id);
}
return connection_ok;
}
private bool processPacket(byte id)
{
int nbr = 0;
switch (id)
{
case 0x00: byte[] keepalive = new byte[5] { 0, 0, 0, 0, 0 };
Receive(keepalive, 1, 4, SocketFlags.None);
Send(keepalive); break;
case 0x01: readData(4); readNextString(); readData(5); break;
case 0x02: readData(1); readNextString(); readNextString(); readData(4); break;
case 0x03:
string message = readNextString();
if (protocolversion >= 72) { message = ChatParser.ParseText(message); }
handler.OnTextReceived(message); break;
case 0x04: readData(16); break;
case 0x05: readData(6); readNextItemSlot(); break;
case 0x06: readData(12); break;
case 0x07: readData(9); break;
case 0x08: if (protocolversion >= 72) { readData(10); } else readData(8); break;
case 0x09: readData(8); readNextString(); break;
case 0x0A: readData(1); break;
case 0x0B: readData(33); break;
case 0x0C: readData(9); break;
case 0x0D: readData(41); break;
case 0x0E: readData(11); break;
case 0x0F: readData(10); readNextItemSlot(); readData(3); break;
case 0x10: readData(2); break;
case 0x11: readData(14); break;
case 0x12: readData(5); break;
case 0x13: if (protocolversion >= 72) { readData(9); } else readData(5); break;
case 0x14: readData(4); readNextString(); readData(16); readNextEntityMetaData(); break;
case 0x16: readData(8); break;
case 0x17: readData(19); readNextObjectData(); break;
case 0x18: readData(26); readNextEntityMetaData(); break;
case 0x19: readData(4); readNextString(); readData(16); break;
case 0x1A: readData(18); break;
case 0x1B: if (protocolversion >= 72) { readData(10); } break;
case 0x1C: readData(10); break;
case 0x1D: nbr = (int)readNextByte(); readData(nbr * 4); break;
case 0x1E: readData(4); break;
case 0x1F: readData(7); break;
case 0x20: readData(6); break;
case 0x21: readData(9); break;
case 0x22: readData(18); break;
case 0x23: readData(5); break;
case 0x26: readData(5); break;
case 0x27: if (protocolversion >= 72) { readData(9); } else readData(8); break;
case 0x28: readData(4); readNextEntityMetaData(); break;
case 0x29: readData(8); break;
case 0x2A: readData(5); break;
case 0x2B: readData(8); break;
case 0x2C: if (protocolversion >= 72) { readNextEntityProperties(protocolversion); } break;
case 0x33: readData(13); nbr = readNextInt(); readData(nbr); break;
case 0x34: readData(10); nbr = readNextInt(); readData(nbr); break;
case 0x35: readData(12); break;
case 0x36: readData(14); break;
case 0x37: readData(17); break;
case 0x38: readNextChunkBulkData(); break;
case 0x3C: readData(28); nbr = readNextInt(); readData(3 * nbr); readData(12); break;
case 0x3D: readData(18); break;
case 0x3E: readNextString(); readData(17); break;
case 0x3F: if (protocolversion > 51) { readNextString(); readData(32); } break;
case 0x46: readData(2); break;
case 0x47: readData(17); break;
case 0x64: readNextWindowData(protocolversion); break;
case 0x65: readData(1); break;
case 0x66: readData(7); readNextItemSlot(); break;
case 0x67: readData(3); readNextItemSlot(); break;
case 0x68: readData(1); for (nbr = readNextShort(); nbr > 0; nbr--) { readNextItemSlot(); } break;
case 0x69: readData(5); break;
case 0x6A: readData(4); break;
case 0x6B: readData(2); readNextItemSlot(); break;
case 0x6C: readData(2); break;
case 0x82: readData(10); readNextString(); readNextString(); readNextString(); readNextString(); break;
case 0x83: readData(4); nbr = readNextShort(); readData(nbr); break;
case 0x84: readData(11); nbr = readNextShort(); if (nbr > 0) { readData(nbr); } break;
case 0x85: if (protocolversion >= 74) { readData(13); } break;
case 0xC8:
if (readNextInt() == 2022) { handler.OnTextReceived("You are dead. Type /reco to respawn & reconnect."); }
if (protocolversion >= 72) { readData(4); } else readData(1);
break;
case 0xC9: readNextString(); readData(3); break;
case 0xCA: if (protocolversion >= 72) { readData(9); } else readData(3); break;
case 0xCB: autocomplete_result = readNextString(); autocomplete_received = true; break;
case 0xCC: readNextString(); readData(4); break;
case 0xCD: readData(1); break;
case 0xCE: if (protocolversion > 51) { readNextString(); readNextString(); readData(1); } break;
case 0xCF: if (protocolversion > 51) { readNextString(); readData(1); readNextString(); } readData(4); break;
case 0xD0: if (protocolversion > 51) { readData(1); readNextString(); } break;
case 0xD1: if (protocolversion > 51) { readNextTeamData(); } break;
case 0xFA: readNextString(); nbr = readNextShort(); readData(nbr); break;
case 0xFF: string reason = readNextString();
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, reason); break;
default: return false; //unknown packet!
}
return true; //packet has been successfully skipped
}
private void StartUpdating()
{
netRead = new Thread(new ThreadStart(Updater));
netRead.Name = "ProtocolPacketHandler";
netRead.Start();
}
public void Dispose()
{
try
{
if (netRead != null)
{
netRead.Abort();
c.Close();
}
}
catch { }
}
private void readData(int offset)
{
if (offset > 0)
{
try
{
byte[] cache = new byte[offset];
Receive(cache, 0, offset, SocketFlags.None);
}
catch (OutOfMemoryException) { }
}
}
private string readNextString()
{
ushort length = (ushort)readNextShort();
if (length > 0)
{
byte[] cache = new byte[length * 2];
Receive(cache, 0, length * 2, SocketFlags.None);
string result = Encoding.BigEndianUnicode.GetString(cache);
return result;
}
else return "";
}
private byte[] readNextByteArray()
{
short len = readNextShort();
byte[] data = new byte[len];
Receive(data, 0, len, SocketFlags.None);
return data;
}
private short readNextShort()
{
byte[] tmp = new byte[2];
Receive(tmp, 0, 2, SocketFlags.None);
Array.Reverse(tmp);
return BitConverter.ToInt16(tmp, 0);
}
private int readNextInt()
{
byte[] tmp = new byte[4];
Receive(tmp, 0, 4, SocketFlags.None);
Array.Reverse(tmp);
return BitConverter.ToInt32(tmp, 0);
}
private byte readNextByte()
{
byte[] result = new byte[1];
Receive(result, 0, 1, SocketFlags.None);
return result[0];
}
private void readNextItemSlot()
{
short itemid = readNextShort();
//If slot not empty (item ID != -1)
if (itemid != -1)
{
readData(1); //Item count
readData(2); //Item damage
short length = readNextShort();
//If length of optional NBT data > 0, read it
if (length > 0) { readData(length); }
}
}
private void readNextEntityMetaData()
{
do
{
byte[] id = new byte[1];
Receive(id, 0, 1, SocketFlags.None);
if (id[0] == 0x7F) { break; }
int index = id[0] & 0x1F;
int type = id[0] >> 5;
switch (type)
{
case 0: readData(1); break; //Byte
case 1: readData(2); break; //Short
case 2: readData(4); break; //Int
case 3: readData(4); break; //Float
case 4: readNextString(); break; //String
case 5: readNextItemSlot(); break; //Slot
case 6: readData(12); break; //Vector (3 Int)
}
} while (true);
}
private void readNextObjectData()
{
int id = readNextInt();
if (id != 0) { readData(6); }
}
private void readNextTeamData()
{
readNextString(); //Internal Name
byte mode = readNextByte();
if (mode == 0 || mode == 2)
{
readNextString(); //Display Name
readNextString(); //Prefix
readNextString(); //Suffix
readData(1); //Friendly Fire
}
if (mode == 0 || mode == 3 || mode == 4)
{
short count = readNextShort();
for (int i = 0; i < count; i++)
{
readNextString(); //Players
}
}
}
private void readNextEntityProperties(int protocolversion)
{
if (protocolversion >= 72)
{
if (protocolversion >= 74)
{
//Minecraft 1.6.2
readNextInt(); //Entity ID
int count = readNextInt();
for (int i = 0; i < count; i++)
{
readNextString(); //Property name
readData(8); //Property value (Double)
short othercount = readNextShort();
readData(25 * othercount);
}
}
else
{
//Minecraft 1.6.0 / 1.6.1
readNextInt(); //Entity ID
int count = readNextInt();
for (int i = 0; i < count; i++)
{
readNextString(); //Property name
readData(8); //Property value (Double)
}
}
}
}
private void readNextWindowData(int protocolversion)
{
readData(1);
byte windowtype = readNextByte();
readNextString();
readData(1);
if (protocolversion > 51)
{
readData(1);
if (protocolversion >= 72 && windowtype == 0xb)
{
readNextInt();
}
}
}
private void readNextChunkBulkData()
{
short chunkcount = readNextShort();
int datalen = readNextInt();
readData(1);
readData(datalen);
readData(12 * (chunkcount));
}
private void Receive(byte[] buffer, int start, int offset, SocketFlags f)
{
int read = 0;
while (read < offset)
{
if (encrypted)
{
read += s.Read(buffer, start + read, offset - read);
}
else read += c.Client.Receive(buffer, start + read, offset - read, f);
}
}
private void Send(byte[] buffer)
{
if (encrypted)
{
s.Write(buffer, 0, buffer.Length);
}
else c.Client.Send(buffer);
}
private bool Handshake(string uuid, string username, string sessionID, string host, int port)
{
//array
byte[] data = new byte[10 + (username.Length + host.Length) * 2];
//packet id
data[0] = (byte)2;
//Protocol Version
data[1] = (byte)protocolversion;
//short len
byte[] sh = BitConverter.GetBytes((short)username.Length);
Array.Reverse(sh);
sh.CopyTo(data, 2);
//username
byte[] bname = Encoding.BigEndianUnicode.GetBytes(username);
bname.CopyTo(data, 4);
//short len
sh = BitConverter.GetBytes((short)host.Length);
Array.Reverse(sh);
sh.CopyTo(data, 4 + (username.Length * 2));
//host
byte[] bhost = Encoding.BigEndianUnicode.GetBytes(host);
bhost.CopyTo(data, 6 + (username.Length * 2));
//port
sh = BitConverter.GetBytes(port);
Array.Reverse(sh);
sh.CopyTo(data, 6 + (username.Length * 2) + (host.Length * 2));
Send(data);
byte[] pid = new byte[1];
Receive(pid, 0, 1, SocketFlags.None);
while (pid[0] == 0xFA) //Skip some early plugin messages
{
processPacket(pid[0]);
Receive(pid, 0, 1, SocketFlags.None);
}
if (pid[0] == 0xFD)
{
string serverID = readNextString();
byte[] PublicServerkey = readNextByteArray();
byte[] token = readNextByteArray();
if (serverID == "-")
{
ConsoleIO.WriteLineFormatted("§8Server is in offline mode.", false);
return true; //No need to check session or start encryption
}
else
{
ConsoleIO.WriteLineFormatted("§8Handshake successful. (Server ID: " + serverID + ')', false);
return StartEncryption(uuid, username, sessionID, token, serverID, PublicServerkey);
}
}
else return false;
}
private bool StartEncryption(string uuid, string username, string sessionID, byte[] token, string serverIDhash, byte[] serverKey)
{
System.Security.Cryptography.RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverKey);
byte[] secretKey = CryptoHandler.GenerateAESPrivateKey();
ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated.", false);
if (serverIDhash != "-")
{
Console.WriteLine("Checking Session...");
if (!ProtocolHandler.SessionCheck(uuid, sessionID, CryptoHandler.getServerHash(serverIDhash, serverKey, secretKey)))
{
return false;
}
}
//Encrypt the data
byte[] key_enc = RSAService.Encrypt(secretKey, false);
byte[] token_enc = RSAService.Encrypt(token, false);
byte[] keylen = BitConverter.GetBytes((short)key_enc.Length);
byte[] tokenlen = BitConverter.GetBytes((short)token_enc.Length);
Array.Reverse(keylen);
Array.Reverse(tokenlen);
//Building the packet
byte[] data = new byte[5 + (short)key_enc.Length + (short)token_enc.Length];
data[0] = 0xFC;
keylen.CopyTo(data, 1);
key_enc.CopyTo(data, 3);
tokenlen.CopyTo(data, 3 + (short)key_enc.Length);
token_enc.CopyTo(data, 5 + (short)key_enc.Length);
//Send it back
Send(data);
//Getting the next packet
byte[] pid = new byte[1];
Receive(pid, 0, 1, SocketFlags.None);
if (pid[0] == 0xFC)
{
readData(4);
s = CryptoHandler.getAesStream(c.GetStream(), secretKey, this);
encrypted = true;
return true;
}
else return false;
}
public bool Login()
{
if (Handshake(handler.getUserUUID(), handler.getUsername(), handler.getSessionID(), handler.getServerHost(), handler.getServerPort()))
{
Send(new byte[] { 0xCD, 0 });
try
{
byte[] pid = new byte[1];
try
{
if (c.Connected)
{
Receive(pid, 0, 1, SocketFlags.None);
while (pid[0] >= 0xC0 && pid[0] != 0xFF) //Skip some early packets or plugin messages
{
processPacket(pid[0]);
Receive(pid, 0, 1, SocketFlags.None);
}
if (pid[0] == (byte)1)
{
readData(4); readNextString(); readData(5);
StartUpdating();
return true; //The Server accepted the request
}
else if (pid[0] == (byte)0xFF)
{
string reason = readNextString();
handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, reason);
return false;
}
}
}
catch
{
//Connection failed
return false;
}
}
catch
{
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
return false;
}
return false; //Login was unsuccessful (received a kick...)
}
else return false;
}
public void Disconnect()
{
const string message = "disconnect.quitting";
try
{
byte[] reason = new byte[3 + (message.Length * 2)];
reason[0] = (byte)0xff;
byte[] msglen;
msglen = BitConverter.GetBytes((short)message.Length);
Array.Reverse(msglen);
msglen.CopyTo(reason, 1);
if (message.Length > 0)
{
byte[] msg;
msg = Encoding.BigEndianUnicode.GetBytes(message);
msg.CopyTo(reason, 3);
}
Send(reason);
}
catch (SocketException) { }
catch (System.IO.IOException) { }
}
public bool SendChatMessage(string message)
{
if (String.IsNullOrEmpty(message))
return true;
try
{
byte[] chat = new byte[3 + (message.Length * 2)];
chat[0] = (byte)3;
byte[] msglen;
msglen = BitConverter.GetBytes((short)message.Length);
Array.Reverse(msglen);
msglen.CopyTo(chat, 1);
byte[] msg;
msg = Encoding.BigEndianUnicode.GetBytes(message);
msg.CopyTo(chat, 3);
Send(chat);
return true;
}
catch (SocketException) { return false; }
}
public bool SendRespawnPacket()
{
try
{
Send(new byte[] { 0xCD, 1 });
return true;
}
catch (SocketException) { return false; }
}
public string AutoComplete(string BehindCursor)
{
if (String.IsNullOrEmpty(BehindCursor))
return "";
byte[] autocomplete = new byte[3 + (BehindCursor.Length * 2)];
autocomplete[0] = 0xCB;
byte[] msglen = BitConverter.GetBytes((short)BehindCursor.Length);
Array.Reverse(msglen); msglen.CopyTo(autocomplete, 1);
byte[] msg = Encoding.BigEndianUnicode.GetBytes(BehindCursor);
msg.CopyTo(autocomplete, 3);
autocomplete_received = false;
autocomplete_result = BehindCursor;
Send(autocomplete);
int wait_left = 50; //do not wait more than 5 seconds (50 * 100 ms)
while (wait_left > 0 && !autocomplete_received) { System.Threading.Thread.Sleep(100); wait_left--; }
string[] results = autocomplete_result.Split((char)0x00);
return results[0];
}
private static byte[] concatBytes(params byte[][] bytes)
{
List<byte> result = new List<byte>();
foreach (byte[] array in bytes)
result.AddRange(array);
return result.ToArray();
}
public byte[] getPaddingPacket()
{
//Will generate a 15-bytes long padding packet
byte[] id = new byte[1] { 0xFA }; //Plugin Message
byte[] channel_name = Encoding.BigEndianUnicode.GetBytes("MCC|");
byte[] channel_name_len = BitConverter.GetBytes((short)channel_name.Length); Array.Reverse(channel_name_len);
byte[] data = new byte[] { 0x00, 0x00 };
byte[] data_len = BitConverter.GetBytes((short)data.Length); Array.Reverse(data_len);
byte[] packet_data = concatBytes(id, channel_name_len, channel_name, data_len, data);
return packet_data;
}
public static bool doPing(string host, int port, ref int protocolversion, ref string version)
{
try
{
TcpClient tcp = ProxyHandler.newTcpClient(host, port);
byte[] ping = new byte[2] { 0xfe, 0x01 };
tcp.Client.Send(ping, SocketFlags.None);
tcp.Client.Receive(ping, 0, 1, SocketFlags.None);
if (ping[0] == 0xff)
{
Protocol16Handler ComTmp = new Protocol16Handler(tcp);
string result = ComTmp.readNextString();
if (result.Length > 2 && result[0] == '§' && result[1] == '1')
{
string[] tmp = result.Split((char)0x00);
protocolversion = (byte)Int16.Parse(tmp[1]);
version = tmp[2];
if (protocolversion == 127) //MC 1.7.2+
return false;
}
else
{
protocolversion = (byte)39;
version = "B1.8.1 - 1.3.2";
}
ConsoleIO.WriteLineFormatted("§8Server version : MC " + version + " (protocol v" + protocolversion + ").", false);
return true;
}
else return false;
}
catch { return false; }
}
}
}

View file

@ -0,0 +1,550 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using MinecraftClient.Crypto;
using MinecraftClient.Proxy;
namespace MinecraftClient.Protocol.Handlers
{
/// <summary>
/// Implementation for Minecraft 1.7.X Protocol
/// </summary>
class Protocol17Handler : IMinecraftCom
{
IMinecraftComHandler handler;
private bool autocomplete_received = false;
private string autocomplete_result = "";
private bool encrypted = false;
private int protocolversion;
private Thread netRead;
Crypto.IAesStream s;
TcpClient c;
public Protocol17Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler)
{
ConsoleIO.SetAutoCompleteEngine(this);
ChatParser.InitTranslations();
this.c = Client;
this.protocolversion = ProtocolVersion;
this.handler = Handler;
}
private Protocol17Handler(TcpClient Client)
{
this.c = Client;
}
/// <summary>
/// Separate thread. Network reading loop.
/// </summary>
private void Updater()
{
try
{
do { Thread.Sleep(100); }
while (Update());
}
catch (System.IO.IOException) { }
catch (SocketException) { }
catch (ObjectDisposedException) { }
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
}
/// <summary>
/// Read and data from the network. Should be called on a separate thread.
/// </summary>
/// <returns></returns>
private bool Update()
{
handler.OnUpdate();
if (c.Client == null || !c.Connected) { return false; }
int id = 0, size = 0;
try
{
while (c.Client.Available > 0)
{
size = readNextVarInt(); //Packet size
id = readNextVarInt(); //Packet ID
switch (id)
{
case 0x00:
byte[] keepalive = new byte[4] { 0, 0, 0, 0 };
Receive(keepalive, 0, 4, SocketFlags.None);
byte[] keepalive_packet = concatBytes(getVarInt(0x00), keepalive);
byte[] keepalive_tosend = concatBytes(getVarInt(keepalive_packet.Length), keepalive_packet);
Send(keepalive_tosend);
break;
case 0x02:
handler.OnTextReceived(ChatParser.ParseText(readNextString()));
break;
case 0x3A:
int autocomplete_count = readNextVarInt();
string tab_list = "";
for (int i = 0; i < autocomplete_count; i++)
{
autocomplete_result = readNextString();
if (autocomplete_result != "")
tab_list = tab_list + autocomplete_result + " ";
}
autocomplete_received = true;
tab_list = tab_list.Trim();
if (tab_list.Length > 0)
ConsoleIO.WriteLineFormatted("§8" + tab_list, false);
break;
case 0x40:
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString()));
return false;
default:
readData(size - getVarInt(id).Length); //Skip packet
break;
}
}
}
catch (SocketException) { return false; }
return true;
}
/// <summary>
/// Start the updating thread. Should be called after login success.
/// </summary>
private void StartUpdating()
{
netRead = new Thread(new ThreadStart(Updater));
netRead.Name = "ProtocolPacketHandler";
netRead.Start();
}
/// <summary>
/// Disconnect from the server, cancel network reading.
/// </summary>
public void Dispose()
{
try
{
if (netRead != null)
{
netRead.Abort();
c.Close();
}
}
catch { }
}
/// <summary>
/// Read some data and discard the result
/// </summary>
/// <param name="offset">Amount of bytes to read</param>
private void readData(int offset)
{
if (offset > 0)
{
try
{
byte[] cache = new byte[offset];
Receive(cache, 0, offset, SocketFlags.None);
}
catch (OutOfMemoryException) { }
}
}
/// <summary>
/// Read a string from the network
/// </summary>
/// <returns>The string</returns>
private string readNextString()
{
int length = readNextVarInt();
if (length > 0)
{
byte[] cache = new byte[length];
Receive(cache, 0, length, SocketFlags.None);
return Encoding.UTF8.GetString(cache);
}
else return "";
}
/// <summary>
/// Read a byte array from the network
/// </summary>
/// <returns>The byte array</returns>
private byte[] readNextByteArray()
{
byte[] tmp = new byte[2];
Receive(tmp, 0, 2, SocketFlags.None);
Array.Reverse(tmp);
short len = BitConverter.ToInt16(tmp, 0);
byte[] data = new byte[len];
Receive(data, 0, len, SocketFlags.None);
return data;
}
/// <summary>
/// Read an integer from the network
/// </summary>
/// <returns>The integer</returns>
private int readNextVarInt()
{
int i = 0;
int j = 0;
int k = 0;
byte[] tmp = new byte[1];
while (true)
{
Receive(tmp, 0, 1, SocketFlags.None);
k = tmp[0];
i |= (k & 0x7F) << j++ * 7;
if (j > 5) throw new OverflowException("VarInt too big");
if ((k & 0x80) != 128) break;
}
return i;
}
/// <summary>
/// Build an integer for sending over the network
/// </summary>
/// <param name="paramInt">Integer to encode</param>
/// <returns>Byte array for this integer</returns>
private static byte[] getVarInt(int paramInt)
{
List<byte> bytes = new List<byte>();
while ((paramInt & -128) != 0)
{
bytes.Add((byte)(paramInt & 127 | 128));
paramInt = (int)(((uint)paramInt) >> 7);
}
bytes.Add((byte)paramInt);
return bytes.ToArray();
}
/// <summary>
/// Easily append several byte arrays
/// </summary>
/// <param name="bytes">Bytes to append</param>
/// <returns>Array containing all the data</returns>
private static byte[] concatBytes(params byte[][] bytes)
{
List<byte> result = new List<byte>();
foreach (byte[] array in bytes)
result.AddRange(array);
return result.ToArray();
}
/// <summary>
/// C-like atoi function for parsing an int from string
/// </summary>
/// <param name="str">String to parse</param>
/// <returns>Int parsed</returns>
private static int atoi(string str)
{
return int.Parse(new string(str.Trim().TakeWhile(char.IsDigit).ToArray()));
}
/// <summary>
/// Network reading method. Read bytes from the socket or encrypted socket.
/// </summary>
private void Receive(byte[] buffer, int start, int offset, SocketFlags f)
{
int read = 0;
while (read < offset)
{
if (encrypted)
{
read += s.Read(buffer, start + read, offset - read);
}
else read += c.Client.Receive(buffer, start + read, offset - read, f);
}
}
/// <summary>
/// Network sending method. Send bytes using the socket or encrypted socket.
/// </summary>
/// <param name="buffer"></param>
private void Send(byte[] buffer)
{
if (encrypted)
{
s.Write(buffer, 0, buffer.Length);
}
else c.Client.Send(buffer);
}
/// <summary>
/// Do the Minecraft login.
/// </summary>
/// <returns>True if login successful</returns>
public bool Login()
{
byte[] packet_id = getVarInt(0);
byte[] protocol_version = getVarInt(protocolversion);
byte[] server_adress_val = Encoding.UTF8.GetBytes(handler.getServerHost());
byte[] server_adress_len = getVarInt(server_adress_val.Length);
byte[] server_port = BitConverter.GetBytes((ushort)handler.getServerPort()); Array.Reverse(server_port);
byte[] next_state = getVarInt(2);
byte[] handshake_packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state);
byte[] handshake_packet_tosend = concatBytes(getVarInt(handshake_packet.Length), handshake_packet);
Send(handshake_packet_tosend);
byte[] username_val = Encoding.UTF8.GetBytes(handler.getUsername());
byte[] username_len = getVarInt(username_val.Length);
byte[] login_packet = concatBytes(packet_id, username_len, username_val);
byte[] login_packet_tosend = concatBytes(getVarInt(login_packet.Length), login_packet);
Send(login_packet_tosend);
readNextVarInt(); //Packet size
int pid = readNextVarInt(); //Packet ID
if (pid == 0x00) //Login rejected
{
handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(readNextString()));
return false;
}
else if (pid == 0x01) //Encryption request
{
string serverID = readNextString();
byte[] Serverkey = readNextByteArray();
byte[] token = readNextByteArray();
return StartEncryption(handler.getUserUUID(), handler.getSessionID(), token, serverID, Serverkey);
}
else if (pid == 0x02) //Login successful
{
ConsoleIO.WriteLineFormatted("§8Server is in offline mode.", false);
StartUpdating();
return true; //No need to check session or start encryption
}
else return false;
}
/// <summary>
/// Start network encryption. Automatically called by Login() if the server requests encryption.
/// </summary>
/// <returns>True if encryption was successful</returns>
private bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, byte[] serverKey)
{
System.Security.Cryptography.RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverKey);
byte[] secretKey = CryptoHandler.GenerateAESPrivateKey();
ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated.", false);
if (serverIDhash != "-")
{
Console.WriteLine("Checking Session...");
if (!ProtocolHandler.SessionCheck(uuid, sessionID, CryptoHandler.getServerHash(serverIDhash, serverKey, secretKey)))
{
handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, "Failed to check session.");
return false;
}
}
//Encrypt the data
byte[] key_enc = RSAService.Encrypt(secretKey, false);
byte[] token_enc = RSAService.Encrypt(token, false);
byte[] key_len = BitConverter.GetBytes((short)key_enc.Length); Array.Reverse(key_len);
byte[] token_len = BitConverter.GetBytes((short)token_enc.Length); Array.Reverse(token_len);
//Encryption Response packet
byte[] packet_id = getVarInt(0x01);
byte[] encryption_response = concatBytes(packet_id, key_len, key_enc, token_len, token_enc);
byte[] encryption_response_tosend = concatBytes(getVarInt(encryption_response.Length), encryption_response);
Send(encryption_response_tosend);
//Start client-side encryption
s = CryptoHandler.getAesStream(c.GetStream(), secretKey, this);
encrypted = true;
//Read and skip the next packet
int received_packet_size = readNextVarInt();
int received_packet_id = readNextVarInt();
bool encryption_success = (received_packet_id == 0x02);
if (received_packet_id == 0) { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(readNextString())); }
else readData(received_packet_size - getVarInt(received_packet_id).Length);
if (encryption_success) { StartUpdating(); }
return encryption_success;
}
/// <summary>
/// Useless padding packet for solving Mono issue.
/// </summary>
/// <returns>The padding packet</returns>
public byte[] getPaddingPacket()
{
//Will generate a 15-bytes long padding packet
byte[] id = getVarInt(0x17); //Plugin Message
byte[] channel_name = Encoding.UTF8.GetBytes("MCC|Pad");
byte[] channel_name_len = getVarInt(channel_name.Length);
byte[] data = new byte[] { 0x00, 0x00, 0x00 };
byte[] data_len = BitConverter.GetBytes((short)data.Length); Array.Reverse(data_len);
byte[] packet_data = concatBytes(id, channel_name_len, channel_name, data_len, data);
byte[] packet_length = getVarInt(packet_data.Length);
return concatBytes(packet_length, packet_data);
}
/// <summary>
/// Send a chat message to the server
/// </summary>
/// <param name="message">Message</param>
/// <returns>True if properly sent</returns>
public bool SendChatMessage(string message)
{
if (String.IsNullOrEmpty(message))
return true;
try
{
byte[] packet_id = getVarInt(0x01);
byte[] message_val = Encoding.UTF8.GetBytes(message);
byte[] message_len = getVarInt(message_val.Length);
byte[] message_packet = concatBytes(packet_id, message_len, message_val);
byte[] message_packet_tosend = concatBytes(getVarInt(message_packet.Length), message_packet);
Send(message_packet_tosend);
return true;
}
catch (SocketException) { return false; }
}
/// <summary>
/// Send a respawn packet to the server
/// </summary>
/// <param name="message">Message</param>
/// <returns>True if properly sent</returns>
public bool SendRespawnPacket()
{
try
{
byte[] packet_id = getVarInt(0x16);
byte[] action_id = new byte[] { 0 };
byte[] respawn_packet = concatBytes(getVarInt(packet_id.Length + 1), packet_id, action_id);
Send(respawn_packet);
return true;
}
catch (SocketException) { return false; }
}
/// <summary>
/// Disconnect from the server
/// </summary>
/// <param name="message">Optional disconnect reason</param>
public void Disconnect()
{
try
{
byte[] packet_id = getVarInt(0x40);
byte[] message_val = Encoding.UTF8.GetBytes("\"disconnect.quitting\"");
byte[] message_len = getVarInt(message_val.Length);
byte[] disconnect_packet = concatBytes(packet_id, message_len, message_val);
byte[] disconnect_packet_tosend = concatBytes(getVarInt(disconnect_packet.Length), disconnect_packet);
Send(disconnect_packet_tosend);
}
catch (SocketException) { }
catch (System.IO.IOException) { }
catch (NullReferenceException) { }
catch (ObjectDisposedException) { }
}
/// <summary>
/// Autocomplete text while typing username or command
/// </summary>
/// <param name="BehindCursor">Text behind cursor</param>
/// <returns>Completed text</returns>
public string AutoComplete(string BehindCursor)
{
if (String.IsNullOrEmpty(BehindCursor))
return "";
byte[] packet_id = getVarInt(0x14);
byte[] tocomplete_val = Encoding.UTF8.GetBytes(BehindCursor);
byte[] tocomplete_len = getVarInt(tocomplete_val.Length);
byte[] tabcomplete_packet = concatBytes(packet_id, tocomplete_len, tocomplete_val);
byte[] tabcomplete_packet_tosend = concatBytes(getVarInt(tabcomplete_packet.Length), tabcomplete_packet);
autocomplete_received = false;
autocomplete_result = BehindCursor;
Send(tabcomplete_packet_tosend);
int wait_left = 50; //do not wait more than 5 seconds (50 * 100 ms)
while (wait_left > 0 && !autocomplete_received) { System.Threading.Thread.Sleep(100); wait_left--; }
return autocomplete_result;
}
/// <summary>
/// Ping a Minecraft server to get information about the server
/// </summary>
/// <returns>True if ping was successful</returns>
public static bool doPing(string host, int port, ref int protocolversion, ref string version)
{
TcpClient tcp = ProxyHandler.newTcpClient(host, port);
tcp.ReceiveBufferSize = 1024 * 1024;
byte[] packet_id = getVarInt(0);
byte[] protocol_version = getVarInt(4);
byte[] server_adress_val = Encoding.UTF8.GetBytes(host);
byte[] server_adress_len = getVarInt(server_adress_val.Length);
byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port);
byte[] next_state = getVarInt(1);
byte[] packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state);
byte[] tosend = concatBytes(getVarInt(packet.Length), packet);
tcp.Client.Send(tosend, SocketFlags.None);
byte[] status_request = getVarInt(0);
byte[] request_packet = concatBytes(getVarInt(status_request.Length), status_request);
tcp.Client.Send(request_packet, SocketFlags.None);
Protocol17Handler ComTmp = new Protocol17Handler(tcp);
if (ComTmp.readNextVarInt() > 0) //Read Response length
{
if (ComTmp.readNextVarInt() == 0x00) //Read Packet ID
{
string result = ComTmp.readNextString(); //Get the Json data
if (result[0] == '{' && result.Contains("protocol\":") && result.Contains("name\":\""))
{
string[] tmp_ver = result.Split(new string[] { "protocol\":" }, StringSplitOptions.None);
string[] tmp_name = result.Split(new string[] { "name\":\"" }, StringSplitOptions.None);
if (tmp_ver.Length >= 2 && tmp_name.Length >= 2)
{
protocolversion = atoi(tmp_ver[1]);
version = tmp_name[1].Split('"')[0];
if (result.Contains("modinfo\":"))
{
//Server is running Forge (which is not supported)
version = "Forge " + version;
protocolversion = 0;
}
ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + ").", false);
return true;
}
}
}
}
return false;
}
}
}

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MinecraftClient.Crypto;
namespace MinecraftClient.Protocol
{
/// <summary>
/// Interface for the Minecraft protocol handler.
/// A protocol handler is used to communicate with the Minecraft server.
/// This interface allows to abstract from the underlying minecraft version in other parts of the program.
/// The protocol handler will take care of parsing and building the appropriate network packets.
/// </summary>
public interface IMinecraftCom : IDisposable, IAutoComplete, IPaddingProvider
{
/// <summary>
/// Start the login procedure once connected to the server
/// </summary>
/// <returns>True if login was successful</returns>
bool Login();
/// <summary>
/// Disconnect from the server
/// </summary>
/// <param name="message">Reason</param>
void Disconnect();
/// <summary>
/// Send a chat message or command to the server
/// </summary>
/// <param name="message">Text to send</param>
/// <returns>True if successfully sent</returns>
bool SendChatMessage(string message);
/// <summary>
/// Allow to respawn after death
/// </summary>
/// <returns>True if packet successfully sent</returns>
bool SendRespawnPacket();
}
}

View file

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Protocol
{
/// <summary>
/// Interface for the MinecraftCom Handler.
/// It defines some callbacks that the MinecraftCom handler must have.
/// It allows the protocol handler to abstract from the other parts of the program.
/// </summary>
public interface IMinecraftComHandler
{
/* The MinecraftCom Hanler must
* provide these getters */
int getServerPort();
string getServerHost();
string getUsername();
string getUserUUID();
string getSessionID();
/// <summary>
/// This method is called when the protocol handler receives a chat message
/// </summary>
void OnTextReceived(string text);
/// <summary>
/// This method is called when the connection has been lost
/// </summary>
void OnConnectionLost(ChatBot.DisconnectReason reason, string message);
/// <summary>
/// Called ~10 times per second (10 ticks per second)
/// Useful for updating bots in other parts of the program
/// </summary>
void OnUpdate();
}
}

View file

@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MinecraftClient.Protocol.Handlers;
using MinecraftClient.Proxy;
using System.Net.Sockets;
using System.Net.Security;
namespace MinecraftClient.Protocol
{
/// <summary>
/// Handle login, session, server ping and provide a protocol handler for interacting with a minecraft server.
/// </summary>
public static class ProtocolHandler
{
/// <summary>
/// Retrieve information about a Minecraft server
/// </summary>
/// <param name="serverIP">Server IP to ping</param>
/// <param name="protocolversion">Will contain protocol version, if ping successful</param>
/// <param name="version">Will contain minecraft version, if ping successful</param>
/// <returns>TRUE if ping was successful</returns>
public static bool GetServerInfo(string serverIP, ref int protocolversion, ref string version)
{
try
{
string host; int port;
string[] sip = serverIP.Split(':');
host = sip[0];
if (sip.Length == 1)
{
port = 25565;
}
else
{
try
{
port = Convert.ToInt32(sip[1]);
}
catch (FormatException) { port = 25565; }
}
if (Protocol16Handler.doPing(host, port, ref protocolversion, ref version))
{
return true;
}
else if (Protocol17Handler.doPing(host, port, ref protocolversion, ref version))
{
return true;
}
else
{
ConsoleIO.WriteLineFormatted("§8Unexpected answer from the server (is that a Minecraft server ?)", false);
return false;
}
}
catch
{
ConsoleIO.WriteLineFormatted("§8An error occured while attempting to connect to this IP.", false);
return false;
}
}
/// <summary>
/// Get a protocol handler for the specified Minecraft version
/// </summary>
/// <param name="Client">Tcp Client connected to the server</param>
/// <param name="ProtocolVersion">Protocol version to handle</param>
/// <param name="Handler">Handler with the appropriate callbacks</param>
/// <returns></returns>
public static IMinecraftCom getProtocolHandler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler)
{
int[] supportedVersions_Protocol16 = { 51, 60, 61, 72, 73, 74, 78 };
if (Array.IndexOf(supportedVersions_Protocol16, ProtocolVersion) > -1)
return new Protocol16Handler(Client, ProtocolVersion, Handler);
int[] supportedVersions_Protocol17 = { 4, 5 };
if (Array.IndexOf(supportedVersions_Protocol17, ProtocolVersion) > -1)
return new Protocol17Handler(Client, ProtocolVersion, Handler);
throw new NotSupportedException("The protocol version '" + ProtocolVersion + "' is not supported.");
}
public enum LoginResult { OtherError, ServiceUnavailable, SSLError, Success, WrongPassword, AccountMigrated, NotPremium };
/// <summary>
/// Allows to login to a premium Minecraft account using the Yggdrasil authentication scheme.
/// </summary>
/// <param name="user">Login</param>
/// <param name="pass">Password</param>
/// <param name="accesstoken">Will contain the access token returned by Minecraft.net, if the login is successful</param>
/// <param name="uuid">Will contain the player's UUID, needed for multiplayer</param>
/// <returns>Returns the status of the login (Success, Failure, etc.)</returns>
public static LoginResult GetLogin(ref string user, string pass, ref string accesstoken, ref string uuid)
{
try
{
string result = "";
string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + user + "\", \"password\": \"" + pass + "\" }";
int code = doHTTPSPost("authserver.mojang.com", "/authenticate", json_request, ref result);
if (code == 200)
{
if (result.Contains("availableProfiles\":[]}"))
{
return LoginResult.NotPremium;
}
else
{
string[] temp = result.Split(new string[] { "accessToken\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { accesstoken = temp[1].Split('"')[0]; }
temp = result.Split(new string[] { "name\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { user = temp[1].Split('"')[0]; }
temp = result.Split(new string[] { "availableProfiles\":[{\"id\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { uuid = temp[1].Split('"')[0]; }
return LoginResult.Success;
}
}
else if (code == 403)
{
if (result.Contains("UserMigratedException"))
{
return LoginResult.AccountMigrated;
}
else return LoginResult.WrongPassword;
}
else if (code == 503)
{
return LoginResult.ServiceUnavailable;
}
else
{
ConsoleIO.WriteLineFormatted("§8Got error code from server: " + code, false);
return LoginResult.OtherError;
}
}
catch (System.Security.Authentication.AuthenticationException)
{
return LoginResult.SSLError;
}/*
catch
{
return LoginResult.OtherError;
}*/
}
/// <summary>
/// Check session using Mojang's Yggdrasil authentication scheme. Allow to join an online-mode server
/// </summary>
/// <param name="user">Username</param>
/// <param name="accesstoken">Session ID</param>
/// <param name="serverhash">Server ID</param>
/// <returns>TRUE if session was successfully checked</returns>
public static bool SessionCheck(string uuid, string accesstoken, string serverhash)
{
try
{
string result = "";
string json_request = "{\"accessToken\":\"" + accesstoken + "\",\"selectedProfile\":\"" + uuid + "\",\"serverId\":\"" + serverhash + "\"}";
int code = doHTTPSPost("sessionserver.mojang.com", "/session/minecraft/join", json_request, ref result);
return (result == "");
}
catch { return false; }
}
/// <summary>
/// Manual HTTPS request since we must directly use a TcpClient because of the proxy.
/// This method connects to the server, enables SSL, do the request and read the response.
/// </summary>
/// <param name="host">Host to connect to</param>
/// <param name="endpoint">Endpoint for making the request</param>
/// <param name="request">Request payload</param>
/// <param name="result">Request result</param>
/// <returns>HTTP Status code</returns>
private static int doHTTPSPost(string host, string endpoint, string request, ref string result)
{
TcpClient client = ProxyHandler.newTcpClient(host, 443);
SslStream stream = new SslStream(client.GetStream());
stream.AuthenticateAsClient(host);
List<String> http_request = new List<string>();
http_request.Add("POST " + endpoint + " HTTP/1.1");
http_request.Add("Host: " + host);
http_request.Add("User-Agent: MCC/" + Program.Version);
http_request.Add("Content-Type: application/json");
http_request.Add("Content-Length: " + Encoding.ASCII.GetBytes(request).Length);
http_request.Add("Connection: close");
http_request.Add("");
http_request.Add(request);
stream.Write(Encoding.ASCII.GetBytes(String.Join("\r\n", http_request.ToArray())));
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string raw_result = sr.ReadToEnd();
if (raw_result.StartsWith("HTTP/1.1"))
{
result = raw_result.Substring(raw_result.IndexOf("\r\n\r\n") + 4);
return Settings.str2int(raw_result.Split(' ')[1]);
}
else return 520; //Web server is returning an unknown error
}
}
}

View file

@ -0,0 +1,60 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Net.Sockets;
using System.ComponentModel;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// Event arguments class for the EncryptAsyncCompleted event.
/// </summary>
public class CreateConnectionAsyncCompletedEventArgs : AsyncCompletedEventArgs
{
private TcpClient _proxyConnection;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="error">Exception information generated by the event.</param>
/// <param name="cancelled">Cancelled event flag. This flag is set to true if the event was cancelled.</param>
/// <param name="proxyConnection">Proxy Connection. The initialized and open TcpClient proxy connection.</param>
public CreateConnectionAsyncCompletedEventArgs(Exception error, bool cancelled, TcpClient proxyConnection)
: base(error, cancelled, null)
{
_proxyConnection = proxyConnection;
}
/// <summary>
/// The proxy connection.
/// </summary>
public TcpClient ProxyConnection
{
get { return _proxyConnection; }
}
}
}

View file

@ -0,0 +1,77 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Runtime.Serialization;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// This exception is thrown when a general, unexpected proxy error.
/// </summary>
[Serializable()]
public class ProxyException : Exception
{
/// <summary>
/// Constructor.
/// </summary>
public ProxyException()
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="message">Exception message text.</param>
public ProxyException(string message)
: base(message)
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="message">Exception message text.</param>
/// <param name="innerException">The inner exception object.</param>
public ProxyException(string message, Exception innerException)
:
base(message, innerException)
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="info">Serialization information.</param>
/// <param name="context">Stream context information.</param>
protected ProxyException(SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
}
}

View file

@ -0,0 +1,527 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Globalization;
using System.ComponentModel;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// HTTP connection proxy class. This class implements the HTTP standard proxy protocol.
/// <para>
/// You can use this class to set up a connection to an HTTP proxy server. Calling the
/// CreateConnection() method initiates the proxy connection and returns a standard
/// System.Net.Socks.TcpClient object that can be used as normal. The proxy plumbing
/// is all handled for you.
/// </para>
/// <code>
///
/// </code>
/// </summary>
public class HttpProxyClient : IProxyClient
{
private string _proxyHost;
private int _proxyPort;
private string _proxyUsername;
private string _proxyPassword;
private HttpResponseCodes _respCode;
private string _respText;
private TcpClient _tcpClient;
private TcpClient _tcpClientCached;
private const int HTTP_PROXY_DEFAULT_PORT = 8080;
private const string HTTP_PROXY_CONNECT_CMD = "CONNECT {0}:{1} HTTP/1.0\r\nHOST {0}:{1}\r\n\r\n";
private const string HTTP_PROXY_AUTHENTICATE_CMD = "CONNECT {0}:{1} HTTP/1.0\r\nHOST {0}:{1}\r\nProxy-Authorization: Basic {2}\r\n\r\n";
private const int WAIT_FOR_DATA_INTERVAL = 50; // 50 ms
private const int WAIT_FOR_DATA_TIMEOUT = 15000; // 15 seconds
private const string PROXY_NAME = "HTTP";
private enum HttpResponseCodes
{
None = 0,
Continue = 100,
SwitchingProtocols = 101,
OK = 200,
Created = 201,
Accepted = 202,
NonAuthoritiveInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultipleChoices = 300,
MovedPermanetly = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
UserProxy = 305,
TemporaryRedirect = 307,
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticantionRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
PreconditionFailed = 411,
RequestEntityTooLarge = 413,
RequestURITooLong = 414,
UnsupportedMediaType = 415,
RequestedRangeNotSatisfied = 416,
ExpectationFailed = 417,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HTTPVersionNotSupported = 505
}
/// <summary>
/// Constructor.
/// </summary>
public HttpProxyClient() { }
/// <summary>
/// Creates a HTTP proxy client object using the supplied TcpClient object connection.
/// </summary>
/// <param name="tcpClient">A TcpClient connection object.</param>
public HttpProxyClient(TcpClient tcpClient)
{
if (tcpClient == null)
throw new ArgumentNullException("tcpClient");
_tcpClientCached = tcpClient;
}
/// <summary>
/// Constructor. The default HTTP proxy port 8080 is used.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy.</param>
public HttpProxyClient(string proxyHost)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
_proxyHost = proxyHost;
_proxyPort = HTTP_PROXY_DEFAULT_PORT;
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port number to connect to the proxy server.</param>
/// <param name="proxyUsername">Username for the proxy server.</param>
/// <param name="proxyPassword">Password for the proxy server.</param>
public HttpProxyClient(string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (String.IsNullOrEmpty(proxyUsername))
throw new ArgumentNullException("proxyUsername");
if (proxyPassword == null)
throw new ArgumentNullException("proxyPassword");
if (proxyPort <= 0 || proxyPort > 65535)
throw new ArgumentOutOfRangeException("proxyPort", "port must be greater than zero and less than 65535");
_proxyHost = proxyHost;
_proxyPort = proxyPort;
_proxyUsername = proxyUsername;
_proxyPassword = proxyPassword;
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port number for the proxy server.</param>
public HttpProxyClient(string proxyHost, int proxyPort)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (proxyPort <= 0 || proxyPort > 65535)
throw new ArgumentOutOfRangeException("proxyPort", "port must be greater than zero and less than 65535");
_proxyHost = proxyHost;
_proxyPort = proxyPort;
}
/// <summary>
/// Gets or sets host name or IP address of the proxy server.
/// </summary>
public string ProxyHost
{
get { return _proxyHost; }
set { _proxyHost = value; }
}
/// <summary>
/// Gets or sets port number for the proxy server.
/// </summary>
public int ProxyPort
{
get { return _proxyPort; }
set { _proxyPort = value; }
}
/// <summary>
/// Gets String representing the name of the proxy.
/// </summary>
/// <remarks>This property will always return the value 'HTTP'</remarks>
public string ProxyName
{
get { return PROXY_NAME; }
}
/// <summary>
/// Gets or sets the TcpClient object.
/// This property can be set prior to executing CreateConnection to use an existing TcpClient connection.
/// </summary>
public TcpClient TcpClient
{
get { return _tcpClientCached; }
set { _tcpClientCached = value; }
}
/// <summary>
/// Creates a remote TCP connection through a proxy server to the destination host on the destination port.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <returns>
/// Returns an open TcpClient object that can be used normally to communicate
/// with the destination server
/// </returns>
/// <remarks>
/// This method creates a connection to the proxy server and instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
public TcpClient CreateConnection(string destinationHost, int destinationPort)
{
try
{
// if we have no cached tcpip connection then create one
if (_tcpClientCached == null)
{
if (String.IsNullOrEmpty(_proxyHost))
throw new ProxyException("ProxyHost property must contain a value.");
if (_proxyPort <= 0 || _proxyPort > 65535)
throw new ProxyException("ProxyPort value must be greater than zero and less than 65535");
// create new tcp client object to the proxy server
_tcpClient = new TcpClient();
// attempt to open the connection
_tcpClient.Connect(_proxyHost, _proxyPort);
}
else
{
_tcpClient = _tcpClientCached;
}
// send connection command to proxy host for the specified destination host and port
SendConnectionCommand(destinationHost, destinationPort);
// remove the private reference to the tcp client so the proxy object does not keep it
// return the open proxied tcp client object to the caller for normal use
TcpClient rtn = _tcpClient;
_tcpClient = null;
return rtn;
}
catch (SocketException ex)
{
throw new ProxyException(String.Format(CultureInfo.InvariantCulture, "Connection to proxy host {0} on port {1} failed.", Utils.GetHost(_tcpClient), Utils.GetPort(_tcpClient)), ex);
}
}
private void SendConnectionCommand(string host, int port)
{
NetworkStream stream = _tcpClient.GetStream();
string connectCmd = CreateCommandString(host, port);
byte[] request = ASCIIEncoding.ASCII.GetBytes(connectCmd);
// send the connect request
stream.Write(request, 0, request.Length);
// wait for the proxy server to respond
WaitForData(stream);
// PROXY SERVER RESPONSE
// =======================================================================
//HTTP/1.0 200 Connection Established<CR><LF>
//[.... other HTTP header lines ending with <CR><LF>..
//ignore all of them]
//<CR><LF> // Last Empty Line
// create an byte response array
byte[] response = new byte[_tcpClient.ReceiveBufferSize];
StringBuilder sbuilder = new StringBuilder();
int bytes = 0;
long total = 0;
do
{
bytes = stream.Read(response, 0, _tcpClient.ReceiveBufferSize);
total += bytes;
sbuilder.Append(System.Text.ASCIIEncoding.UTF8.GetString(response, 0, bytes));
} while (stream.DataAvailable);
ParseResponse(sbuilder.ToString());
// evaluate the reply code for an error condition
if (_respCode != HttpResponseCodes.OK)
HandleProxyCommandError(host, port);
}
private string CreateCommandString(string host, int port)
{
string connectCmd;
if (!string.IsNullOrEmpty(_proxyUsername))
{
// gets the user/pass into base64 encoded string in the form of [username]:[password]
string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", _proxyUsername, _proxyPassword)));
// PROXY SERVER REQUEST
// =======================================================================
//CONNECT starksoft.com:443 HTTP/1.0<CR><LF>
//HOST starksoft.com:443<CR><LF>
//Proxy-Authorization: username:password<CR><LF>
// NOTE: username:password string will be base64 encoded as one
// concatenated string
//[... other HTTP header lines ending with <CR><LF> if required]>
//<CR><LF> // Last Empty Line
connectCmd = String.Format(CultureInfo.InvariantCulture, HTTP_PROXY_AUTHENTICATE_CMD, host, port.ToString(CultureInfo.InvariantCulture), auth);
}
else
{
// PROXY SERVER REQUEST
// =======================================================================
//CONNECT starksoft.com:443 HTTP/1.0 <CR><LF>
//HOST starksoft.com:443<CR><LF>
//[... other HTTP header lines ending with <CR><LF> if required]>
//<CR><LF> // Last Empty Line
connectCmd = String.Format(CultureInfo.InvariantCulture, HTTP_PROXY_CONNECT_CMD + "\r\n", host, port.ToString(CultureInfo.InvariantCulture));
}
return connectCmd;
}
private void HandleProxyCommandError(string host, int port)
{
string msg;
switch (_respCode)
{
case HttpResponseCodes.None:
msg = String.Format(CultureInfo.InvariantCulture, "Proxy destination {0} on port {1} failed to return a recognized HTTP response code. Server response: {2}", Utils.GetHost(_tcpClient), Utils.GetPort(_tcpClient), _respText);
break;
case HttpResponseCodes.BadGateway:
//HTTP/1.1 502 Proxy Error (The specified Secure Sockets Layer (SSL) port is not allowed. ISA Server is not configured to allow SSL requests from this port. Most Web browsers use port 443 for SSL requests.)
msg = String.Format(CultureInfo.InvariantCulture, "Proxy destination {0} on port {1} responded with a 502 code - Bad Gateway. If you are connecting to a Microsoft ISA destination please refer to knowledge based article Q283284 for more information. Server response: {2}", Utils.GetHost(_tcpClient), Utils.GetPort(_tcpClient), _respText);
break;
default:
msg = String.Format(CultureInfo.InvariantCulture, "Proxy destination {0} on port {1} responded with a {2} code - {3}", Utils.GetHost(_tcpClient), Utils.GetPort(_tcpClient), ((int)_respCode).ToString(CultureInfo.InvariantCulture), _respText);
break;
}
// throw a new application exception
throw new ProxyException(msg);
}
private void WaitForData(NetworkStream stream)
{
int sleepTime = 0;
while (!stream.DataAvailable)
{
Thread.Sleep(WAIT_FOR_DATA_INTERVAL);
sleepTime += WAIT_FOR_DATA_INTERVAL;
if (sleepTime > WAIT_FOR_DATA_TIMEOUT)
throw new ProxyException(String.Format("A timeout while waiting for the proxy server at {0} on port {1} to respond.", Utils.GetHost(_tcpClient), Utils.GetPort(_tcpClient) ));
}
}
private void ParseResponse(string response)
{
string[] data = null;
// get rid of the LF character if it exists and then split the string on all CR
data = response.Replace('\n', ' ').Split('\r');
ParseCodeAndText(data[0]);
}
private void ParseCodeAndText(string line)
{
int begin = 0;
int end = 0;
string val = null;
if (line.IndexOf("HTTP") == -1)
throw new ProxyException(String.Format("No HTTP response received from proxy destination. Server response: {0}.", line));
begin = line.IndexOf(" ") + 1;
end = line.IndexOf(" ", begin);
val = line.Substring(begin, end - begin);
Int32 code = 0;
if (!Int32.TryParse(val, out code))
throw new ProxyException(String.Format("An invalid response code was received from proxy destination. Server response: {0}.", line));
_respCode = (HttpResponseCodes)code;
_respText = line.Substring(end + 1).Trim();
}
#region "Async Methods"
private BackgroundWorker _asyncWorker;
private Exception _asyncException;
bool _asyncCancelled;
/// <summary>
/// Gets a value indicating whether an asynchronous operation is running.
/// </summary>
/// <remarks>Returns true if an asynchronous operation is running; otherwise, false.
/// </remarks>
public bool IsBusy
{
get { return _asyncWorker == null ? false : _asyncWorker.IsBusy; }
}
/// <summary>
/// Gets a value indicating whether an asynchronous operation is cancelled.
/// </summary>
/// <remarks>Returns true if an asynchronous operation is cancelled; otherwise, false.
/// </remarks>
public bool IsAsyncCancelled
{
get { return _asyncCancelled; }
}
/// <summary>
/// Cancels any asychronous operation that is currently active.
/// </summary>
public void CancelAsync()
{
if (_asyncWorker != null && !_asyncWorker.CancellationPending && _asyncWorker.IsBusy)
{
_asyncCancelled = true;
_asyncWorker.CancelAsync();
}
}
private void CreateAsyncWorker()
{
if (_asyncWorker != null)
_asyncWorker.Dispose();
_asyncException = null;
_asyncWorker = null;
_asyncCancelled = false;
_asyncWorker = new BackgroundWorker();
}
/// <summary>
/// Event handler for CreateConnectionAsync method completed.
/// </summary>
public event EventHandler<CreateConnectionAsyncCompletedEventArgs> CreateConnectionAsyncCompleted;
/// <summary>
/// Asynchronously creates a remote TCP connection through a proxy server to the destination host on the destination port.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <returns>
/// Returns an open TcpClient object that can be used normally to communicate
/// with the destination server
/// </returns>
/// <remarks>
/// This method creates a connection to the proxy server and instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
public void CreateConnectionAsync(string destinationHost, int destinationPort)
{
if (_asyncWorker != null && _asyncWorker.IsBusy)
throw new InvalidOperationException("The HttpProxy object is already busy executing another asynchronous operation. You can only execute one asychronous method at a time.");
CreateAsyncWorker();
_asyncWorker.WorkerSupportsCancellation = true;
_asyncWorker.DoWork += new DoWorkEventHandler(CreateConnectionAsync_DoWork);
_asyncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CreateConnectionAsync_RunWorkerCompleted);
Object[] args = new Object[2];
args[0] = destinationHost;
args[1] = destinationPort;
_asyncWorker.RunWorkerAsync(args);
}
private void CreateConnectionAsync_DoWork(object sender, DoWorkEventArgs e)
{
try
{
Object[] args = (Object[])e.Argument;
e.Result = CreateConnection((string)args[0], (int)args[1]);
}
catch (Exception ex)
{
_asyncException = ex;
}
}
private void CreateConnectionAsync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (CreateConnectionAsyncCompleted != null)
CreateConnectionAsyncCompleted(this, new CreateConnectionAsyncCompletedEventArgs(_asyncException, _asyncCancelled, (TcpClient)e.Result));
}
#endregion
}
}

View file

@ -0,0 +1,97 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// Proxy client interface. This is the interface that all proxy clients must implement.
/// </summary>
public interface IProxyClient
{
/// <summary>
/// Event handler for CreateConnectionAsync method completed.
/// </summary>
event EventHandler<CreateConnectionAsyncCompletedEventArgs> CreateConnectionAsyncCompleted;
/// <summary>
/// Gets or sets proxy host name or IP address.
/// </summary>
string ProxyHost { get; set; }
/// <summary>
/// Gets or sets proxy port number.
/// </summary>
int ProxyPort { get; set; }
/// <summary>
/// Gets String representing the name of the proxy.
/// </summary>
string ProxyName { get; }
/// <summary>
/// Gets or set the TcpClient object if one was specified in the constructor.
/// </summary>
TcpClient TcpClient { get; set; }
/// <summary>
/// Creates a remote TCP connection through a proxy server to the destination host on the destination port.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <returns>
/// Returns an open TcpClient object that can be used normally to communicate
/// with the destination server
/// </returns>
/// <remarks>
/// This method creates a connection to the proxy server and instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
TcpClient CreateConnection(string destinationHost, int destinationPort);
/// <summary>
/// Asynchronously creates a remote TCP connection through a proxy server to the destination host on the destination port.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <returns>
/// Returns an open TcpClient object that can be used normally to communicate
/// with the destination server
/// </returns>
/// <remarks>
/// This method creates a connection to the proxy server and instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
void CreateConnectionAsync(string destinationHost, int destinationPort);
}
}

View file

@ -0,0 +1,208 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// The type of proxy.
/// </summary>
public enum ProxyType
{
/// <summary>
/// No Proxy specified. Note this option will cause an exception to be thrown if used to create a proxy object by the factory.
/// </summary>
None,
/// <summary>
/// HTTP Proxy
/// </summary>
Http,
/// <summary>
/// SOCKS v4 Proxy
/// </summary>
Socks4,
/// <summary>
/// SOCKS v4a Proxy
/// </summary>
Socks4a,
/// <summary>
/// SOCKS v5 Proxy
/// </summary>
Socks5
}
/// <summary>
/// Factory class for creating new proxy client objects.
/// </summary>
/// <remarks>
/// <code>
/// // create an instance of the client proxy factory
/// ProxyClientFactory factory = new ProxyClientFactory();
///
/// // use the proxy client factory to generically specify the type of proxy to create
/// // the proxy factory method CreateProxyClient returns an IProxyClient object
/// IProxyClient proxy = factory.CreateProxyClient(ProxyType.Http, "localhost", 6588);
///
/// // create a connection through the proxy to www.starksoft.com over port 80
/// System.Net.Sockets.TcpClient tcpClient = proxy.CreateConnection("www.starksoft.com", 80);
/// </code>
/// </remarks>
public class ProxyClientFactory
{
/// <summary>
/// Factory method for creating new proxy client objects.
/// </summary>
/// <param name="type">The type of proxy client to create.</param>
/// <returns>Proxy client object.</returns>
public IProxyClient CreateProxyClient(ProxyType type)
{
if (type == ProxyType.None)
throw new ArgumentOutOfRangeException("type");
switch (type)
{
case ProxyType.Http:
return new HttpProxyClient();
case ProxyType.Socks4:
return new Socks4ProxyClient();
case ProxyType.Socks4a:
return new Socks4aProxyClient();
case ProxyType.Socks5:
return new Socks5ProxyClient();
default:
throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
}
}
/// <summary>
/// Factory method for creating new proxy client objects using an existing TcpClient connection object.
/// </summary>
/// <param name="type">The type of proxy client to create.</param>
/// <param name="tcpClient">Open TcpClient object.</param>
/// <returns>Proxy client object.</returns>
public IProxyClient CreateProxyClient(ProxyType type, TcpClient tcpClient)
{
if (type == ProxyType.None)
throw new ArgumentOutOfRangeException("type");
switch (type)
{
case ProxyType.Http:
return new HttpProxyClient(tcpClient);
case ProxyType.Socks4:
return new Socks4ProxyClient(tcpClient);
case ProxyType.Socks4a:
return new Socks4aProxyClient(tcpClient);
case ProxyType.Socks5:
return new Socks5ProxyClient(tcpClient);
default:
throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
}
}
/// <summary>
/// Factory method for creating new proxy client objects.
/// </summary>
/// <param name="type">The type of proxy client to create.</param>
/// <param name="proxyHost">The proxy host or IP address.</param>
/// <param name="proxyPort">The proxy port number.</param>
/// <returns>Proxy client object.</returns>
public IProxyClient CreateProxyClient(ProxyType type, string proxyHost, int proxyPort)
{
if (type == ProxyType.None)
throw new ArgumentOutOfRangeException("type");
switch (type)
{
case ProxyType.Http:
return new HttpProxyClient(proxyHost, proxyPort);
case ProxyType.Socks4:
return new Socks4ProxyClient(proxyHost, proxyPort);
case ProxyType.Socks4a:
return new Socks4aProxyClient(proxyHost, proxyPort);
case ProxyType.Socks5:
return new Socks5ProxyClient(proxyHost, proxyPort);
default:
throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
}
}
/// <summary>
/// Factory method for creating new proxy client objects.
/// </summary>
/// <param name="type">The type of proxy client to create.</param>
/// <param name="proxyHost">The proxy host or IP address.</param>
/// <param name="proxyPort">The proxy port number.</param>
/// <param name="proxyUsername">The proxy username. This parameter is only used by Http, Socks4 and Socks5 proxy objects.</param>
/// <param name="proxyPassword">The proxy user password. This parameter is only used Http, Socks5 proxy objects.</param>
/// <returns>Proxy client object.</returns>
public IProxyClient CreateProxyClient(ProxyType type, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
{
if (type == ProxyType.None)
throw new ArgumentOutOfRangeException("type");
switch (type)
{
case ProxyType.Http:
return new HttpProxyClient(proxyHost, proxyPort, proxyUsername, proxyPassword);
case ProxyType.Socks4:
return new Socks4ProxyClient(proxyHost, proxyPort, proxyUsername);
case ProxyType.Socks4a:
return new Socks4aProxyClient(proxyHost, proxyPort, proxyUsername);
case ProxyType.Socks5:
return new Socks5ProxyClient(proxyHost, proxyPort, proxyUsername, proxyPassword);
default:
throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
}
}
/// <summary>
/// Factory method for creating new proxy client objects.
/// </summary>
/// <param name="type">The type of proxy client to create.</param>
/// <param name="tcpClient">Open TcpClient object.</param>
/// <param name="proxyHost">The proxy host or IP address.</param>
/// <param name="proxyPort">The proxy port number.</param>
/// <param name="proxyUsername">The proxy username. This parameter is only used by Http, Socks4 and Socks5 proxy objects.</param>
/// <param name="proxyPassword">The proxy user password. This parameter is only used Http, Socks5 proxy objects.</param>
/// <returns>Proxy client object.</returns>
public IProxyClient CreateProxyClient(ProxyType type, TcpClient tcpClient, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
{
IProxyClient c = CreateProxyClient(type, proxyHost, proxyPort, proxyUsername, proxyPassword);
c.TcpClient = tcpClient;
return c;
}
}
}

View file

@ -0,0 +1,585 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.IO;
using System.Threading;
using System.ComponentModel;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// Socks4 connection proxy class. This class implements the Socks4 standard proxy protocol.
/// </summary>
/// <remarks>
/// This class implements the Socks4 proxy protocol standard for TCP communciations.
/// </remarks>
public class Socks4ProxyClient : IProxyClient
{
private const int WAIT_FOR_DATA_INTERVAL = 50; // 50 ms
private const int WAIT_FOR_DATA_TIMEOUT = 15000; // 15 seconds
private const string PROXY_NAME = "SOCKS4";
private TcpClient _tcpClient;
private TcpClient _tcpClientCached;
private string _proxyHost;
private int _proxyPort;
private string _proxyUserId;
/// <summary>
/// Default Socks4 proxy port.
/// </summary>
internal const int SOCKS_PROXY_DEFAULT_PORT = 1080;
/// <summary>
/// Socks4 version number.
/// </summary>
internal const byte SOCKS4_VERSION_NUMBER = 4;
/// <summary>
/// Socks4 connection command value.
/// </summary>
internal const byte SOCKS4_CMD_CONNECT = 0x01;
/// <summary>
/// Socks4 bind command value.
/// </summary>
internal const byte SOCKS4_CMD_BIND = 0x02;
/// <summary>
/// Socks4 reply request grant response value.
/// </summary>
internal const byte SOCKS4_CMD_REPLY_REQUEST_GRANTED = 90;
/// <summary>
/// Socks4 reply request rejected or failed response value.
/// </summary>
internal const byte SOCKS4_CMD_REPLY_REQUEST_REJECTED_OR_FAILED = 91;
/// <summary>
/// Socks4 reply request rejected becauase the proxy server can not connect to the IDENTD server value.
/// </summary>
internal const byte SOCKS4_CMD_REPLY_REQUEST_REJECTED_CANNOT_CONNECT_TO_IDENTD = 92;
/// <summary>
/// Socks4 reply request rejected because of a different IDENTD server.
/// </summary>
internal const byte SOCKS4_CMD_REPLY_REQUEST_REJECTED_DIFFERENT_IDENTD = 93;
/// <summary>
/// Create a Socks4 proxy client object. The default proxy port 1080 is used.
/// </summary>
public Socks4ProxyClient() { }
/// <summary>
/// Creates a Socks4 proxy client object using the supplied TcpClient object connection.
/// </summary>
/// <param name="tcpClient">A TcpClient connection object.</param>
public Socks4ProxyClient(TcpClient tcpClient)
{
if (tcpClient == null)
throw new ArgumentNullException("tcpClient");
_tcpClientCached = tcpClient;
}
/// <summary>
/// Create a Socks4 proxy client object. The default proxy port 1080 is used.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyUserId">Proxy user identification information.</param>
public Socks4ProxyClient(string proxyHost, string proxyUserId)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (proxyUserId == null)
throw new ArgumentNullException("proxyUserId");
_proxyHost = proxyHost;
_proxyPort = SOCKS_PROXY_DEFAULT_PORT;
_proxyUserId = proxyUserId;
}
/// <summary>
/// Create a Socks4 proxy client object.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port used to connect to proxy server.</param>
/// <param name="proxyUserId">Proxy user identification information.</param>
public Socks4ProxyClient(string proxyHost, int proxyPort, string proxyUserId)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (proxyPort <= 0 || proxyPort > 65535)
throw new ArgumentOutOfRangeException("proxyPort", "port must be greater than zero and less than 65535");
if (proxyUserId == null)
throw new ArgumentNullException("proxyUserId");
_proxyHost = proxyHost;
_proxyPort = proxyPort;
_proxyUserId = proxyUserId;
}
/// <summary>
/// Create a Socks4 proxy client object. The default proxy port 1080 is used.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
public Socks4ProxyClient(string proxyHost)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
_proxyHost = proxyHost;
_proxyPort = SOCKS_PROXY_DEFAULT_PORT;
}
/// <summary>
/// Create a Socks4 proxy client object.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port used to connect to proxy server.</param>
public Socks4ProxyClient(string proxyHost, int proxyPort)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (proxyPort <= 0 || proxyPort > 65535)
throw new ArgumentOutOfRangeException("proxyPort", "port must be greater than zero and less than 65535");
_proxyHost = proxyHost;
_proxyPort = proxyPort;
}
/// <summary>
/// Gets or sets host name or IP address of the proxy server.
/// </summary>
public string ProxyHost
{
get { return _proxyHost; }
set { _proxyHost = value; }
}
/// <summary>
/// Gets or sets port used to connect to proxy server.
/// </summary>
public int ProxyPort
{
get { return _proxyPort; }
set { _proxyPort = value; }
}
/// <summary>
/// Gets String representing the name of the proxy.
/// </summary>
/// <remarks>This property will always return the value 'SOCKS4'</remarks>
virtual public string ProxyName
{
get { return PROXY_NAME; }
}
/// <summary>
/// Gets or sets proxy user identification information.
/// </summary>
public string ProxyUserId
{
get { return _proxyUserId; }
set { _proxyUserId = value; }
}
/// <summary>
/// Gets or sets the TcpClient object.
/// This property can be set prior to executing CreateConnection to use an existing TcpClient connection.
/// </summary>
public TcpClient TcpClient
{
get { return _tcpClientCached; }
set { _tcpClientCached = value; }
}
/// <summary>
/// Creates a TCP connection to the destination host through the proxy server
/// host.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address of the destination server.</param>
/// <param name="destinationPort">Port number to connect to on the destination server.</param>
/// <returns>
/// Returns an open TcpClient object that can be used normally to communicate
/// with the destination server
/// </returns>
/// <remarks>
/// This method creates a connection to the proxy server and instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
public TcpClient CreateConnection(string destinationHost, int destinationPort)
{
if (String.IsNullOrEmpty(destinationHost))
throw new ArgumentNullException("destinationHost");
if (destinationPort <= 0 || destinationPort > 65535)
throw new ArgumentOutOfRangeException("destinationPort", "port must be greater than zero and less than 65535");
try
{
// if we have no cached tcpip connection then create one
if (_tcpClientCached == null)
{
if (String.IsNullOrEmpty(_proxyHost))
throw new ProxyException("ProxyHost property must contain a value.");
if (_proxyPort <= 0 || _proxyPort > 65535)
throw new ProxyException("ProxyPort value must be greater than zero and less than 65535");
// create new tcp client object to the proxy server
_tcpClient = new TcpClient();
// attempt to open the connection
_tcpClient.Connect(_proxyHost, _proxyPort);
}
else
{
_tcpClient = _tcpClientCached;
}
// send connection command to proxy host for the specified destination host and port
SendCommand(_tcpClient.GetStream(), SOCKS4_CMD_CONNECT, destinationHost, destinationPort, _proxyUserId);
// remove the private reference to the tcp client so the proxy object does not keep it
// return the open proxied tcp client object to the caller for normal use
TcpClient rtn = _tcpClient;
_tcpClient = null;
return rtn;
}
catch (Exception ex)
{
throw new ProxyException(String.Format(CultureInfo.InvariantCulture, "Connection to proxy host {0} on port {1} failed.", Utils.GetHost(_tcpClient), Utils.GetPort(_tcpClient)), ex);
}
}
/// <summary>
/// Sends a command to the proxy server.
/// </summary>
/// <param name="proxy">Proxy server data stream.</param>
/// <param name="command">Proxy byte command to execute.</param>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Destination port number</param>
/// <param name="userId">IDENTD user ID value.</param>
internal virtual void SendCommand(NetworkStream proxy, byte command, string destinationHost, int destinationPort, string userId)
{
// PROXY SERVER REQUEST
// The client connects to the SOCKS server and sends a CONNECT request when
// it wants to establish a connection to an application server. The client
// includes in the request packet the IP address and the port number of the
// destination host, and userid, in the following format.
//
// +----+----+----+----+----+----+----+----+----+----+....+----+
// | VN | CD | DSTPORT | DSTIP | USERID |NULL|
// +----+----+----+----+----+----+----+----+----+----+....+----+
// # of bytes: 1 1 2 4 variable 1
//
// VN is the SOCKS protocol version number and should be 4. CD is the
// SOCKS command code and should be 1 for CONNECT request. NULL is a byte
// of all zero bits.
// userId needs to be a zero length string so that the GetBytes method
// works properly
if (userId == null)
userId = "";
byte[] destIp = GetIPAddressBytes(destinationHost);
byte[] destPort = GetDestinationPortBytes(destinationPort);
byte[] userIdBytes = ASCIIEncoding.ASCII.GetBytes(userId);
byte[] request = new byte[9 + userIdBytes.Length];
// set the bits on the request byte array
request[0] = SOCKS4_VERSION_NUMBER;
request[1] = command;
destPort.CopyTo(request, 2);
destIp.CopyTo(request, 4);
userIdBytes.CopyTo(request, 8);
request[8 + userIdBytes.Length] = 0x00; // null (byte with all zeros) terminator for userId
// send the connect request
proxy.Write(request, 0, request.Length);
// wait for the proxy server to respond
WaitForData(proxy);
// PROXY SERVER RESPONSE
// The SOCKS server checks to see whether such a request should be granted
// based on any combination of source IP address, destination IP address,
// destination port number, the userid, and information it may obtain by
// consulting IDENT, cf. RFC 1413. If the request is granted, the SOCKS
// server makes a connection to the specified port of the destination host.
// A reply packet is sent to the client when this connection is established,
// or when the request is rejected or the operation fails.
//
// +----+----+----+----+----+----+----+----+
// | VN | CD | DSTPORT | DSTIP |
// +----+----+----+----+----+----+----+----+
// # of bytes: 1 1 2 4
//
// VN is the version of the reply code and should be 0. CD is the result
// code with one of the following values:
//
// 90: request granted
// 91: request rejected or failed
// 92: request rejected becuase SOCKS server cannot connect to
// identd on the client
// 93: request rejected because the client program and identd
// report different user-ids
//
// The remaining fields are ignored.
//
// The SOCKS server closes its connection immediately after notifying
// the client of a failed or rejected request. For a successful request,
// the SOCKS server gets ready to relay traffic on both directions. This
// enables the client to do I/O on its connection as if it were directly
// connected to the application server.
// create an 8 byte response array
byte[] response = new byte[8];
// read the resonse from the network stream
proxy.Read(response, 0, 8);
// evaluate the reply code for an error condition
if (response[1] != SOCKS4_CMD_REPLY_REQUEST_GRANTED)
HandleProxyCommandError(response, destinationHost, destinationPort);
}
/// <summary>
/// Translate the host name or IP address to a byte array.
/// </summary>
/// <param name="destinationHost">Host name or IP address.</param>
/// <returns>Byte array representing IP address in bytes.</returns>
internal byte[] GetIPAddressBytes(string destinationHost)
{
IPAddress ipAddr = null;
// if the address doesn't parse then try to resolve with dns
if (!IPAddress.TryParse(destinationHost, out ipAddr))
{
try
{
ipAddr = Dns.GetHostEntry(destinationHost).AddressList[0];
}
catch (Exception ex)
{
throw new ProxyException(String.Format(CultureInfo.InvariantCulture, "A error occurred while attempting to DNS resolve the host name {0}.", destinationHost), ex);
}
}
// return address bytes
return ipAddr.GetAddressBytes();
}
/// <summary>
/// Translate the destination port value to a byte array.
/// </summary>
/// <param name="value">Destination port.</param>
/// <returns>Byte array representing an 16 bit port number as two bytes.</returns>
internal byte[] GetDestinationPortBytes(int value)
{
byte[] array = new byte[2];
array[0] = Convert.ToByte(value / 256);
array[1] = Convert.ToByte(value % 256);
return array;
}
/// <summary>
/// Receive a byte array from the proxy server and determine and handle and errors that may have occurred.
/// </summary>
/// <param name="response">Proxy server command response as a byte array.</param>
/// <param name="destinationHost">Destination host.</param>
/// <param name="destinationPort">Destination port number.</param>
internal void HandleProxyCommandError(byte[] response, string destinationHost, int destinationPort)
{
if (response == null)
throw new ArgumentNullException("response");
// extract the reply code
byte replyCode = response[1];
// extract the ip v4 address (4 bytes)
byte[] ipBytes = new byte[4];
for (int i = 0; i < 4; i++)
ipBytes[i] = response[i + 4];
// convert the ip address to an IPAddress object
IPAddress ipAddr = new IPAddress(ipBytes);
// extract the port number big endian (2 bytes)
byte[] portBytes = new byte[2];
portBytes[0] = response[3];
portBytes[1] = response[2];
Int16 port = BitConverter.ToInt16(portBytes, 0);
// translate the reply code error number to human readable text
string proxyErrorText;
switch (replyCode)
{
case SOCKS4_CMD_REPLY_REQUEST_REJECTED_OR_FAILED:
proxyErrorText = "connection request was rejected or failed";
break;
case SOCKS4_CMD_REPLY_REQUEST_REJECTED_CANNOT_CONNECT_TO_IDENTD:
proxyErrorText = "connection request was rejected because SOCKS destination cannot connect to identd on the client";
break;
case SOCKS4_CMD_REPLY_REQUEST_REJECTED_DIFFERENT_IDENTD:
proxyErrorText = "connection request rejected because the client program and identd report different user-ids";
break;
default:
proxyErrorText = String.Format(CultureInfo.InvariantCulture, "proxy client received an unknown reply with the code value '{0}' from the proxy destination", replyCode.ToString(CultureInfo.InvariantCulture));
break;
}
// build the exeception message string
string exceptionMsg = String.Format(CultureInfo.InvariantCulture, "The {0} concerning destination host {1} port number {2}. The destination reported the host as {3} port {4}.", proxyErrorText, destinationHost, destinationPort, ipAddr.ToString(), port.ToString(CultureInfo.InvariantCulture));
// throw a new application exception
throw new ProxyException(exceptionMsg);
}
internal void WaitForData(NetworkStream stream)
{
int sleepTime = 0;
while (!stream.DataAvailable)
{
Thread.Sleep(WAIT_FOR_DATA_INTERVAL);
sleepTime += WAIT_FOR_DATA_INTERVAL;
if (sleepTime > WAIT_FOR_DATA_TIMEOUT)
throw new ProxyException("A timeout while waiting for the proxy destination to respond.");
}
}
#region "Async Methods"
private BackgroundWorker _asyncWorker;
private Exception _asyncException;
bool _asyncCancelled;
/// <summary>
/// Gets a value indicating whether an asynchronous operation is running.
/// </summary>
/// <remarks>Returns true if an asynchronous operation is running; otherwise, false.
/// </remarks>
public bool IsBusy
{
get { return _asyncWorker == null ? false : _asyncWorker.IsBusy; }
}
/// <summary>
/// Gets a value indicating whether an asynchronous operation is cancelled.
/// </summary>
/// <remarks>Returns true if an asynchronous operation is cancelled; otherwise, false.
/// </remarks>
public bool IsAsyncCancelled
{
get { return _asyncCancelled; }
}
/// <summary>
/// Cancels any asychronous operation that is currently active.
/// </summary>
public void CancelAsync()
{
if (_asyncWorker != null && !_asyncWorker.CancellationPending && _asyncWorker.IsBusy)
{
_asyncCancelled = true;
_asyncWorker.CancelAsync();
}
}
private void CreateAsyncWorker()
{
if (_asyncWorker != null)
_asyncWorker.Dispose();
_asyncException = null;
_asyncWorker = null;
_asyncCancelled = false;
_asyncWorker = new BackgroundWorker();
}
/// <summary>
/// Event handler for CreateConnectionAsync method completed.
/// </summary>
public event EventHandler<CreateConnectionAsyncCompletedEventArgs> CreateConnectionAsyncCompleted;
/// <summary>
/// Asynchronously creates a remote TCP connection through a proxy server to the destination host on the destination port
/// using the supplied open TcpClient object with an open connection to proxy server.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <returns>
/// Returns TcpClient object that can be used normally to communicate
/// with the destination server.
/// </returns>
/// <remarks>
/// This instructs the proxy server to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
public void CreateConnectionAsync(string destinationHost, int destinationPort)
{
if (_asyncWorker != null && _asyncWorker.IsBusy)
throw new InvalidOperationException("The Socks4/4a object is already busy executing another asynchronous operation. You can only execute one asychronous method at a time.");
CreateAsyncWorker();
_asyncWorker.WorkerSupportsCancellation = true;
_asyncWorker.DoWork += new DoWorkEventHandler(CreateConnectionAsync_DoWork);
_asyncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CreateConnectionAsync_RunWorkerCompleted);
Object[] args = new Object[2];
args[0] = destinationHost;
args[1] = destinationPort;
_asyncWorker.RunWorkerAsync(args);
}
private void CreateConnectionAsync_DoWork(object sender, DoWorkEventArgs e)
{
try
{
Object[] args = (Object[])e.Argument;
e.Result = CreateConnection((string)args[0], (int)args[1]);
}
catch (Exception ex)
{
_asyncException = ex;
}
}
private void CreateConnectionAsync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (CreateConnectionAsyncCompleted != null)
CreateConnectionAsyncCompleted(this, new CreateConnectionAsyncCompletedEventArgs(_asyncException, _asyncCancelled, (TcpClient)e.Result));
}
#endregion
}
}

View file

@ -0,0 +1,230 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// Socks4a connection proxy class. This class implements the Socks4a standard proxy protocol
/// which is an extension of the Socks4 protocol
/// </summary>
/// <remarks>
/// In Socks version 4A if the client cannot resolve the destination host's domain name
/// to find its IP address the server will attempt to resolve it.
/// </remarks>
public class Socks4aProxyClient : Socks4ProxyClient
{
private const string PROXY_NAME = "SOCKS4a";
/// <summary>
/// Default constructor.
/// </summary>
public Socks4aProxyClient()
: base()
{ }
/// <summary>
/// Creates a Socks4 proxy client object using the supplied TcpClient object connection.
/// </summary>
/// <param name="tcpClient">An open TcpClient object with an established connection.</param>
public Socks4aProxyClient(TcpClient tcpClient)
: base(tcpClient)
{ }
/// <summary>
/// Create a Socks4a proxy client object. The default proxy port 1080 is used.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyUserId">Proxy user identification information for an IDENTD server.</param>
public Socks4aProxyClient(string proxyHost, string proxyUserId)
: base(proxyHost, proxyUserId)
{ }
/// <summary>
/// Create a Socks4a proxy client object.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port used to connect to proxy server.</param>
/// <param name="proxyUserId">Proxy user identification information.</param>
public Socks4aProxyClient(string proxyHost, int proxyPort, string proxyUserId)
: base(proxyHost, proxyPort, proxyUserId)
{ }
/// <summary>
/// Create a Socks4 proxy client object. The default proxy port 1080 is used.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
public Socks4aProxyClient(string proxyHost) : base(proxyHost)
{ }
/// <summary>
/// Create a Socks4a proxy client object.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port used to connect to proxy server.</param>
public Socks4aProxyClient(string proxyHost, int proxyPort)
: base(proxyHost, proxyPort)
{ }
/// <summary>
/// Gets String representing the name of the proxy.
/// </summary>
/// <remarks>This property will always return the value 'SOCKS4a'</remarks>
public override string ProxyName
{
get { return PROXY_NAME; }
}
/// <summary>
/// Sends a command to the proxy server.
/// </summary>
/// <param name="proxy">Proxy server data stream.</param>
/// <param name="command">Proxy byte command to execute.</param>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Destination port number</param>
/// <param name="userId">IDENTD user ID value.</param>
/// <remarks>
/// This method override the SendCommand message in the Sock4ProxyClient object. The override adds support for the
/// Socks4a extensions which allow the proxy client to optionally command the proxy server to resolve the
/// destination host IP address.
/// </remarks>
internal override void SendCommand(NetworkStream proxy, byte command, string destinationHost, int destinationPort, string userId)
{
// PROXY SERVER REQUEST
//Please read SOCKS4.protocol first for an description of the version 4
//protocol. This extension is intended to allow the use of SOCKS on hosts
//which are not capable of resolving all domain names.
//
//In version 4, the client sends the following packet to the SOCKS server
//to request a CONNECT or a BIND operation:
//
// +----+----+----+----+----+----+----+----+----+----+....+----+
// | VN | CD | DSTPORT | DSTIP | USERID |NULL|
// +----+----+----+----+----+----+----+----+----+----+....+----+
// # of bytes: 1 1 2 4 variable 1
//
//VN is the SOCKS protocol version number and should be 4. CD is the
//SOCKS command code and should be 1 for CONNECT or 2 for BIND. NULL
//is a byte of all zero bits.
//
//For version 4A, if the client cannot resolve the destination host's
//domain name to find its IP address, it should set the first three bytes
//of DSTIP to NULL and the last byte to a non-zero value. (This corresponds
//to IP address 0.0.0.x, with x nonzero. As decreed by IANA -- The
//Internet Assigned Numbers Authority -- such an address is inadmissible
//as a destination IP address and thus should never occur if the client
//can resolve the domain name.) Following the NULL byte terminating
//USERID, the client must sends the destination domain name and termiantes
//it with another NULL byte. This is used for both CONNECT and BIND requests.
//
//A server using protocol 4A must check the DSTIP in the request packet.
//If it represent address 0.0.0.x with nonzero x, the server must read
//in the domain name that the client sends in the packet. The server
//should resolve the domain name and make connection to the destination
//host if it can.
//
//SOCKSified sockd may pass domain names that it cannot resolve to
//the next-hop SOCKS server.
// userId needs to be a zero length string so that the GetBytes method
// works properly
if (userId == null)
userId = "";
byte[] destIp = {0,0,0,1}; // build the invalid ip address as specified in the 4a protocol
byte[] destPort = GetDestinationPortBytes(destinationPort);
byte[] userIdBytes = ASCIIEncoding.ASCII.GetBytes(userId);
byte[] hostBytes = ASCIIEncoding.ASCII.GetBytes(destinationHost);
byte[] request = new byte[10 + userIdBytes.Length + hostBytes.Length];
// set the bits on the request byte array
request[0] = SOCKS4_VERSION_NUMBER;
request[1] = command;
destPort.CopyTo(request, 2);
destIp.CopyTo(request, 4);
userIdBytes.CopyTo(request, 8); // copy the userid to the request byte array
request[8 + userIdBytes.Length] = 0x00; // null (byte with all zeros) terminator for userId
hostBytes.CopyTo(request, 9 + userIdBytes.Length); // copy the host name to the request byte array
request[9 + userIdBytes.Length + hostBytes.Length] = 0x00; // null (byte with all zeros) terminator for userId
// send the connect request
proxy.Write(request, 0, request.Length);
// wait for the proxy server to send a response
base.WaitForData(proxy);
// PROXY SERVER RESPONSE
// The SOCKS server checks to see whether such a request should be granted
// based on any combination of source IP address, destination IP address,
// destination port number, the userid, and information it may obtain by
// consulting IDENT, cf. RFC 1413. If the request is granted, the SOCKS
// server makes a connection to the specified port of the destination host.
// A reply packet is sent to the client when this connection is established,
// or when the request is rejected or the operation fails.
//
// +----+----+----+----+----+----+----+----+
// | VN | CD | DSTPORT | DSTIP |
// +----+----+----+----+----+----+----+----+
// # of bytes: 1 1 2 4
//
// VN is the version of the reply code and should be 0. CD is the result
// code with one of the following values:
//
// 90: request granted
// 91: request rejected or failed
// 92: request rejected becuase SOCKS server cannot connect to
// identd on the client
// 93: request rejected because the client program and identd
// report different user-ids
//
// The remaining fields are ignored.
//
// The SOCKS server closes its connection immediately after notifying
// the client of a failed or rejected request. For a successful request,
// the SOCKS server gets ready to relay traffic on both directions. This
// enables the client to do I/O on its connection as if it were directly
// connected to the application server.
// create an 8 byte response array
byte[] response = new byte[8];
// read the resonse from the network stream
proxy.Read(response, 0, 8);
// evaluate the reply code for an error condition
if (response[1] != SOCKS4_CMD_REPLY_REQUEST_GRANTED)
HandleProxyCommandError(response, destinationHost, destinationPort);
}
}
}

View file

@ -0,0 +1,765 @@
/*
* Authors: Benton Stark
*
* Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.ComponentModel;
namespace Starksoft.Net.Proxy
{
/// <summary>
/// Socks5 connection proxy class. This class implements the Socks5 standard proxy protocol.
/// </summary>
/// <remarks>
/// This implementation supports TCP proxy connections with a Socks v5 server.
/// </remarks>
public class Socks5ProxyClient : IProxyClient
{
private string _proxyHost;
private int _proxyPort;
private string _proxyUserName;
private string _proxyPassword;
private SocksAuthentication _proxyAuthMethod;
private TcpClient _tcpClient;
private TcpClient _tcpClientCached;
private const string PROXY_NAME = "SOCKS5";
private const int SOCKS5_DEFAULT_PORT = 1080;
private const byte SOCKS5_VERSION_NUMBER = 5;
private const byte SOCKS5_RESERVED = 0x00;
private const byte SOCKS5_AUTH_NUMBER_OF_AUTH_METHODS_SUPPORTED = 2;
private const byte SOCKS5_AUTH_METHOD_NO_AUTHENTICATION_REQUIRED = 0x00;
private const byte SOCKS5_AUTH_METHOD_GSSAPI = 0x01;
private const byte SOCKS5_AUTH_METHOD_USERNAME_PASSWORD = 0x02;
private const byte SOCKS5_AUTH_METHOD_IANA_ASSIGNED_RANGE_BEGIN = 0x03;
private const byte SOCKS5_AUTH_METHOD_IANA_ASSIGNED_RANGE_END = 0x7f;
private const byte SOCKS5_AUTH_METHOD_RESERVED_RANGE_BEGIN = 0x80;
private const byte SOCKS5_AUTH_METHOD_RESERVED_RANGE_END = 0xfe;
private const byte SOCKS5_AUTH_METHOD_REPLY_NO_ACCEPTABLE_METHODS = 0xff;
private const byte SOCKS5_CMD_CONNECT = 0x01;
private const byte SOCKS5_CMD_BIND = 0x02;
private const byte SOCKS5_CMD_UDP_ASSOCIATE = 0x03;
private const byte SOCKS5_CMD_REPLY_SUCCEEDED = 0x00;
private const byte SOCKS5_CMD_REPLY_GENERAL_SOCKS_SERVER_FAILURE = 0x01;
private const byte SOCKS5_CMD_REPLY_CONNECTION_NOT_ALLOWED_BY_RULESET = 0x02;
private const byte SOCKS5_CMD_REPLY_NETWORK_UNREACHABLE = 0x03;
private const byte SOCKS5_CMD_REPLY_HOST_UNREACHABLE = 0x04;
private const byte SOCKS5_CMD_REPLY_CONNECTION_REFUSED = 0x05;
private const byte SOCKS5_CMD_REPLY_TTL_EXPIRED = 0x06;
private const byte SOCKS5_CMD_REPLY_COMMAND_NOT_SUPPORTED = 0x07;
private const byte SOCKS5_CMD_REPLY_ADDRESS_TYPE_NOT_SUPPORTED = 0x08;
private const byte SOCKS5_ADDRTYPE_IPV4 = 0x01;
private const byte SOCKS5_ADDRTYPE_DOMAIN_NAME = 0x03;
private const byte SOCKS5_ADDRTYPE_IPV6 = 0x04;
/// <summary>
/// Authentication itemType.
/// </summary>
private enum SocksAuthentication
{
/// <summary>
/// No authentication used.
/// </summary>
None,
/// <summary>
/// Username and password authentication.
/// </summary>
UsernamePassword
}
/// <summary>
/// Create a Socks5 proxy client object.
/// </summary>
public Socks5ProxyClient() { }
/// <summary>
/// Creates a Socks5 proxy client object using the supplied TcpClient object connection.
/// </summary>
/// <param name="tcpClient">A TcpClient connection object.</param>
public Socks5ProxyClient(TcpClient tcpClient)
{
if (tcpClient == null)
throw new ArgumentNullException("tcpClient");
_tcpClientCached = tcpClient;
}
/// <summary>
/// Create a Socks5 proxy client object. The default proxy port 1080 is used.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
public Socks5ProxyClient(string proxyHost)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
_proxyHost = proxyHost;
_proxyPort = SOCKS5_DEFAULT_PORT;
}
/// <summary>
/// Create a Socks5 proxy client object.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port used to connect to proxy server.</param>
public Socks5ProxyClient(string proxyHost, int proxyPort)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (proxyPort <= 0 || proxyPort > 65535)
throw new ArgumentOutOfRangeException("proxyPort", "port must be greater than zero and less than 65535");
_proxyHost = proxyHost;
_proxyPort = proxyPort;
}
/// <summary>
/// Create a Socks5 proxy client object. The default proxy port 1080 is used.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyUserName">Proxy authentication user name.</param>
/// <param name="proxyPassword">Proxy authentication password.</param>
public Socks5ProxyClient(string proxyHost, string proxyUserName, string proxyPassword)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (proxyUserName == null)
throw new ArgumentNullException("proxyUserName");
if (proxyPassword == null)
throw new ArgumentNullException("proxyPassword");
_proxyHost = proxyHost;
_proxyPort = SOCKS5_DEFAULT_PORT;
_proxyUserName = proxyUserName;
_proxyPassword = proxyPassword;
}
/// <summary>
/// Create a Socks5 proxy client object.
/// </summary>
/// <param name="proxyHost">Host name or IP address of the proxy server.</param>
/// <param name="proxyPort">Port used to connect to proxy server.</param>
/// <param name="proxyUserName">Proxy authentication user name.</param>
/// <param name="proxyPassword">Proxy authentication password.</param>
public Socks5ProxyClient(string proxyHost, int proxyPort, string proxyUserName, string proxyPassword)
{
if (String.IsNullOrEmpty(proxyHost))
throw new ArgumentNullException("proxyHost");
if (proxyPort <= 0 || proxyPort > 65535)
throw new ArgumentOutOfRangeException("proxyPort", "port must be greater than zero and less than 65535");
if (proxyUserName == null)
throw new ArgumentNullException("proxyUserName");
if (proxyPassword == null)
throw new ArgumentNullException("proxyPassword");
_proxyHost = proxyHost;
_proxyPort = proxyPort;
_proxyUserName = proxyUserName;
_proxyPassword = proxyPassword;
}
/// <summary>
/// Gets or sets host name or IP address of the proxy server.
/// </summary>
public string ProxyHost
{
get { return _proxyHost; }
set { _proxyHost = value; }
}
/// <summary>
/// Gets or sets port used to connect to proxy server.
/// </summary>
public int ProxyPort
{
get { return _proxyPort; }
set { _proxyPort = value; }
}
/// <summary>
/// Gets String representing the name of the proxy.
/// </summary>
/// <remarks>This property will always return the value 'SOCKS5'</remarks>
public string ProxyName
{
get { return PROXY_NAME; }
}
/// <summary>
/// Gets or sets proxy authentication user name.
/// </summary>
public string ProxyUserName
{
get { return _proxyUserName; }
set { _proxyUserName = value; }
}
/// <summary>
/// Gets or sets proxy authentication password.
/// </summary>
public string ProxyPassword
{
get { return _proxyPassword; }
set { _proxyPassword = value; }
}
/// <summary>
/// Gets or sets the TcpClient object.
/// This property can be set prior to executing CreateConnection to use an existing TcpClient connection.
/// </summary>
public TcpClient TcpClient
{
get { return _tcpClientCached; }
set { _tcpClientCached = value; }
}
/// <summary>
/// Creates a remote TCP connection through a proxy server to the destination host on the destination port.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address of the destination server.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <returns>
/// Returns an open TcpClient object that can be used normally to communicate
/// with the destination server
/// </returns>
/// <remarks>
/// This method creates a connection to the proxy server and instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
public TcpClient CreateConnection(string destinationHost, int destinationPort)
{
if (String.IsNullOrEmpty(destinationHost))
throw new ArgumentNullException("destinationHost");
if (destinationPort <= 0 || destinationPort > 65535)
throw new ArgumentOutOfRangeException("destinationPort", "port must be greater than zero and less than 65535");
try
{
// if we have no cached tcpip connection then create one
if (_tcpClientCached == null)
{
if (String.IsNullOrEmpty(_proxyHost))
throw new ProxyException("ProxyHost property must contain a value.");
if (_proxyPort <= 0 || _proxyPort > 65535)
throw new ProxyException("ProxyPort value must be greater than zero and less than 65535");
// create new tcp client object to the proxy server
_tcpClient = new TcpClient();
// attempt to open the connection
_tcpClient.Connect(_proxyHost, _proxyPort);
}
else
{
_tcpClient = _tcpClientCached;
}
// determine which authentication method the client would like to use
DetermineClientAuthMethod();
// negotiate which authentication methods are supported / accepted by the server
NegotiateServerAuthMethod();
// send a connect command to the proxy server for destination host and port
SendCommand(SOCKS5_CMD_CONNECT, destinationHost, destinationPort);
// remove the private reference to the tcp client so the proxy object does not keep it
// return the open proxied tcp client object to the caller for normal use
TcpClient rtn = _tcpClient;
_tcpClient = null;
return rtn;
}
catch (Exception ex)
{
throw new ProxyException(String.Format(CultureInfo.InvariantCulture, "Connection to proxy host {0} on port {1} failed.", Utils.GetHost(_tcpClient), Utils.GetPort(_tcpClient)), ex);
}
}
private void DetermineClientAuthMethod()
{
// set the authentication itemType used based on values inputed by the user
if (_proxyUserName != null && _proxyPassword != null)
_proxyAuthMethod = SocksAuthentication.UsernamePassword;
else
_proxyAuthMethod = SocksAuthentication.None;
}
private void NegotiateServerAuthMethod()
{
// get a reference to the network stream
NetworkStream stream = _tcpClient.GetStream();
// SERVER AUTHENTICATION REQUEST
// The client connects to the server, and sends a version
// identifier/method selection message:
//
// +----+----------+----------+
// |VER | NMETHODS | METHODS |
// +----+----------+----------+
// | 1 | 1 | 1 to 255 |
// +----+----------+----------+
byte[] authRequest = new byte[4];
authRequest[0] = SOCKS5_VERSION_NUMBER;
authRequest[1] = SOCKS5_AUTH_NUMBER_OF_AUTH_METHODS_SUPPORTED;
authRequest[2] = SOCKS5_AUTH_METHOD_NO_AUTHENTICATION_REQUIRED;
authRequest[3] = SOCKS5_AUTH_METHOD_USERNAME_PASSWORD;
// send the request to the server specifying authentication types supported by the client.
stream.Write(authRequest, 0, authRequest.Length);
// SERVER AUTHENTICATION RESPONSE
// The server selects from one of the methods given in METHODS, and
// sends a METHOD selection message:
//
// +----+--------+
// |VER | METHOD |
// +----+--------+
// | 1 | 1 |
// +----+--------+
//
// If the selected METHOD is X'FF', none of the methods listed by the
// client are acceptable, and the client MUST close the connection.
//
// The values currently defined for METHOD are:
// * X'00' NO AUTHENTICATION REQUIRED
// * X'01' GSSAPI
// * X'02' USERNAME/PASSWORD
// * X'03' to X'7F' IANA ASSIGNED
// * X'80' to X'FE' RESERVED FOR PRIVATE METHODS
// * X'FF' NO ACCEPTABLE METHODS
// receive the server response
byte[] response = new byte[2];
stream.Read(response, 0, response.Length);
// the first byte contains the socks version number (e.g. 5)
// the second byte contains the auth method acceptable to the proxy server
byte acceptedAuthMethod = response[1];
// if the server does not accept any of our supported authenication methods then throw an error
if (acceptedAuthMethod == SOCKS5_AUTH_METHOD_REPLY_NO_ACCEPTABLE_METHODS)
{
_tcpClient.Close();
throw new ProxyException("The proxy destination does not accept the supported proxy client authentication methods.");
}
// if the server accepts a username and password authentication and none is provided by the user then throw an error
if (acceptedAuthMethod == SOCKS5_AUTH_METHOD_USERNAME_PASSWORD && _proxyAuthMethod == SocksAuthentication.None)
{
_tcpClient.Close();
throw new ProxyException("The proxy destination requires a username and password for authentication.");
}
if (acceptedAuthMethod == SOCKS5_AUTH_METHOD_USERNAME_PASSWORD)
{
// USERNAME / PASSWORD SERVER REQUEST
// Once the SOCKS V5 server has started, and the client has selected the
// Username/Password Authentication protocol, the Username/Password
// subnegotiation begins. This begins with the client producing a
// Username/Password request:
//
// +----+------+----------+------+----------+
// |VER | ULEN | UNAME | PLEN | PASSWD |
// +----+------+----------+------+----------+
// | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
// +----+------+----------+------+----------+
// create a data structure (binary array) containing credentials
// to send to the proxy server which consists of clear username and password data
byte[] credentials = new byte[_proxyUserName.Length + _proxyPassword.Length + 3];
// for SOCKS5 username/password authentication the VER field must be set to 0x01
// http://en.wikipedia.org/wiki/SOCKS
// field 1: version number, 1 byte (must be 0x01)"
credentials[0] = 0x01;
credentials[1] = (byte)_proxyUserName.Length;
Array.Copy(ASCIIEncoding.ASCII.GetBytes(_proxyUserName), 0, credentials, 2, _proxyUserName.Length);
credentials[_proxyUserName.Length + 2] = (byte)_proxyPassword.Length;
Array.Copy(ASCIIEncoding.ASCII.GetBytes(_proxyPassword), 0, credentials, _proxyUserName.Length + 3, _proxyPassword.Length);
// USERNAME / PASSWORD SERVER RESPONSE
// The server verifies the supplied UNAME and PASSWD, and sends the
// following response:
//
// +----+--------+
// |VER | STATUS |
// +----+--------+
// | 1 | 1 |
// +----+--------+
//
// A STATUS field of X'00' indicates success. If the server returns a
// `failure' (STATUS value other than X'00') status, it MUST close the
// connection.
// transmit credentials to the proxy server
stream.Write(credentials, 0, credentials.Length);
// read the response from the proxy server
byte[] crResponse = new byte[2];
stream.Read(crResponse, 0, crResponse.Length);
// check to see if the proxy server accepted the credentials
if (crResponse[1] != 0)
{
_tcpClient.Close();
throw new ProxyException("Proxy authentification failure! The proxy server has reported that the userid and/or password is not valid.");
}
}
}
private byte GetDestAddressType(string host)
{
IPAddress ipAddr = null;
bool result = IPAddress.TryParse(host, out ipAddr);
if (!result)
return SOCKS5_ADDRTYPE_DOMAIN_NAME;
switch (ipAddr.AddressFamily)
{
case AddressFamily.InterNetwork:
return SOCKS5_ADDRTYPE_IPV4;
case AddressFamily.InterNetworkV6:
return SOCKS5_ADDRTYPE_IPV6;
default:
throw new ProxyException(String.Format(CultureInfo.InvariantCulture, "The host addess {0} of type '{1}' is not a supported address type. The supported types are InterNetwork and InterNetworkV6.", host, Enum.GetName(typeof(AddressFamily), ipAddr.AddressFamily)));
}
}
private byte[] GetDestAddressBytes(byte addressType, string host)
{
switch (addressType)
{
case SOCKS5_ADDRTYPE_IPV4:
case SOCKS5_ADDRTYPE_IPV6:
return IPAddress.Parse(host).GetAddressBytes();
case SOCKS5_ADDRTYPE_DOMAIN_NAME:
// create a byte array to hold the host name bytes plus one byte to store the length
byte[] bytes = new byte[host.Length + 1];
// if the address field contains a fully-qualified domain name. The first
// octet of the address field contains the number of octets of name that
// follow, there is no terminating NUL octet.
bytes[0] = Convert.ToByte(host.Length);
Encoding.ASCII.GetBytes(host).CopyTo(bytes, 1);
return bytes;
default:
return null;
}
}
private byte[] GetDestPortBytes(int value)
{
byte[] array = new byte[2];
array[0] = Convert.ToByte(value / 256);
array[1] = Convert.ToByte(value % 256);
return array;
}
private void SendCommand(byte command, string destinationHost, int destinationPort)
{
NetworkStream stream = _tcpClient.GetStream();
byte addressType = GetDestAddressType(destinationHost);
byte[] destAddr = GetDestAddressBytes(addressType, destinationHost);
byte[] destPort = GetDestPortBytes(destinationPort);
// The connection request is made up of 6 bytes plus the
// length of the variable address byte array
//
// +----+-----+-------+------+----------+----------+
// |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
// +----+-----+-------+------+----------+----------+
// | 1 | 1 | X'00' | 1 | Variable | 2 |
// +----+-----+-------+------+----------+----------+
//
// * VER protocol version: X'05'
// * CMD
// * CONNECT X'01'
// * BIND X'02'
// * UDP ASSOCIATE X'03'
// * RSV RESERVED
// * ATYP address itemType of following address
// * IP V4 address: X'01'
// * DOMAINNAME: X'03'
// * IP V6 address: X'04'
// * DST.ADDR desired destination address
// * DST.PORT desired destination port in network octet order
byte[] request = new byte[4 + destAddr.Length + 2];
request[0] = SOCKS5_VERSION_NUMBER;
request[1] = command;
request[2] = SOCKS5_RESERVED;
request[3] = addressType;
destAddr.CopyTo(request, 4);
destPort.CopyTo(request, 4 + destAddr.Length);
// send connect request.
stream.Write(request, 0, request.Length);
// PROXY SERVER RESPONSE
// +----+-----+-------+------+----------+----------+
// |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
// +----+-----+-------+------+----------+----------+
// | 1 | 1 | X'00' | 1 | Variable | 2 |
// +----+-----+-------+------+----------+----------+
//
// * VER protocol version: X'05'
// * REP Reply field:
// * X'00' succeeded
// * X'01' general SOCKS server failure
// * X'02' connection not allowed by ruleset
// * X'03' Network unreachable
// * X'04' Host unreachable
// * X'05' Connection refused
// * X'06' TTL expired
// * X'07' Command not supported
// * X'08' Address itemType not supported
// * X'09' to X'FF' unassigned
//* RSV RESERVED
//* ATYP address itemType of following address
byte[] response = new byte[255];
// read proxy server response
stream.Read(response, 0, response.Length);
byte replyCode = response[1];
// evaluate the reply code for an error condition
if (replyCode != SOCKS5_CMD_REPLY_SUCCEEDED)
HandleProxyCommandError(response, destinationHost, destinationPort );
}
private void HandleProxyCommandError(byte[] response, string destinationHost, int destinationPort)
{
string proxyErrorText;
byte replyCode = response[1];
byte addrType = response[3];
string addr = "";
Int16 port = 0;
switch (addrType)
{
case SOCKS5_ADDRTYPE_DOMAIN_NAME:
int addrLen = Convert.ToInt32(response[4]);
byte[] addrBytes = new byte[addrLen];
for (int i = 0; i < addrLen; i++)
addrBytes[i] = response[i + 5];
addr = System.Text.ASCIIEncoding.ASCII.GetString(addrBytes);
byte[] portBytesDomain = new byte[2];
portBytesDomain[0] = response[6 + addrLen];
portBytesDomain[1] = response[5 + addrLen];
port = BitConverter.ToInt16(portBytesDomain, 0);
break;
case SOCKS5_ADDRTYPE_IPV4:
byte[] ipv4Bytes = new byte[4];
for (int i = 0; i < 4; i++)
ipv4Bytes[i] = response[i + 4];
IPAddress ipv4 = new IPAddress(ipv4Bytes);
addr = ipv4.ToString();
byte[] portBytesIpv4 = new byte[2];
portBytesIpv4[0] = response[9];
portBytesIpv4[1] = response[8];
port = BitConverter.ToInt16(portBytesIpv4, 0);
break;
case SOCKS5_ADDRTYPE_IPV6:
byte[] ipv6Bytes = new byte[16];
for (int i = 0; i < 16; i++)
ipv6Bytes[i] = response[i + 4];
IPAddress ipv6 = new IPAddress(ipv6Bytes);
addr = ipv6.ToString();
byte[] portBytesIpv6 = new byte[2];
portBytesIpv6[0] = response[21];
portBytesIpv6[1] = response[20];
port = BitConverter.ToInt16(portBytesIpv6, 0);
break;
}
switch (replyCode)
{
case SOCKS5_CMD_REPLY_GENERAL_SOCKS_SERVER_FAILURE:
proxyErrorText = "a general socks destination failure occurred";
break;
case SOCKS5_CMD_REPLY_CONNECTION_NOT_ALLOWED_BY_RULESET:
proxyErrorText = "the connection is not allowed by proxy destination rule set";
break;
case SOCKS5_CMD_REPLY_NETWORK_UNREACHABLE:
proxyErrorText = "the network was unreachable";
break;
case SOCKS5_CMD_REPLY_HOST_UNREACHABLE:
proxyErrorText = "the host was unreachable";
break;
case SOCKS5_CMD_REPLY_CONNECTION_REFUSED:
proxyErrorText = "the connection was refused by the remote network";
break;
case SOCKS5_CMD_REPLY_TTL_EXPIRED:
proxyErrorText = "the time to live (TTL) has expired";
break;
case SOCKS5_CMD_REPLY_COMMAND_NOT_SUPPORTED:
proxyErrorText = "the command issued by the proxy client is not supported by the proxy destination";
break;
case SOCKS5_CMD_REPLY_ADDRESS_TYPE_NOT_SUPPORTED:
proxyErrorText = "the address type specified is not supported";
break;
default:
proxyErrorText = String.Format(CultureInfo.InvariantCulture, "that an unknown reply with the code value '{0}' was received by the destination", replyCode.ToString(CultureInfo.InvariantCulture));
break;
}
string exceptionMsg = String.Format(CultureInfo.InvariantCulture, "The {0} concerning destination host {1} port number {2}. The destination reported the host as {3} port {4}.", proxyErrorText, destinationHost, destinationPort, addr, port.ToString(CultureInfo.InvariantCulture));
throw new ProxyException(exceptionMsg);
}
#region "Async Methods"
private BackgroundWorker _asyncWorker;
private Exception _asyncException;
bool _asyncCancelled;
/// <summary>
/// Gets a value indicating whether an asynchronous operation is running.
/// </summary>
/// <remarks>Returns true if an asynchronous operation is running; otherwise, false.
/// </remarks>
public bool IsBusy
{
get { return _asyncWorker == null ? false : _asyncWorker.IsBusy; }
}
/// <summary>
/// Gets a value indicating whether an asynchronous operation is cancelled.
/// </summary>
/// <remarks>Returns true if an asynchronous operation is cancelled; otherwise, false.
/// </remarks>
public bool IsAsyncCancelled
{
get { return _asyncCancelled; }
}
/// <summary>
/// Cancels any asychronous operation that is currently active.
/// </summary>
public void CancelAsync()
{
if (_asyncWorker != null && !_asyncWorker.CancellationPending && _asyncWorker.IsBusy)
{
_asyncCancelled = true;
_asyncWorker.CancelAsync();
}
}
private void CreateAsyncWorker()
{
if (_asyncWorker != null)
_asyncWorker.Dispose();
_asyncException = null;
_asyncWorker = null;
_asyncCancelled = false;
_asyncWorker = new BackgroundWorker();
}
/// <summary>
/// Event handler for CreateConnectionAsync method completed.
/// </summary>
public event EventHandler<CreateConnectionAsyncCompletedEventArgs> CreateConnectionAsyncCompleted;
/// <summary>
/// Asynchronously creates a remote TCP connection through a proxy server to the destination host on the destination port.
/// </summary>
/// <param name="destinationHost">Destination host name or IP address.</param>
/// <param name="destinationPort">Port number to connect to on the destination host.</param>
/// <returns>
/// Returns TcpClient object that can be used normally to communicate
/// with the destination server.
/// </returns>
/// <remarks>
/// This method instructs the proxy server
/// to make a pass through connection to the specified destination host on the specified
/// port.
/// </remarks>
public void CreateConnectionAsync(string destinationHost, int destinationPort)
{
if (_asyncWorker != null && _asyncWorker.IsBusy)
throw new InvalidOperationException("The Socks4 object is already busy executing another asynchronous operation. You can only execute one asychronous method at a time.");
CreateAsyncWorker();
_asyncWorker.WorkerSupportsCancellation = true;
_asyncWorker.DoWork += new DoWorkEventHandler(CreateConnectionAsync_DoWork);
_asyncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CreateConnectionAsync_RunWorkerCompleted);
Object[] args = new Object[2];
args[0] = destinationHost;
args[1] = destinationPort;
_asyncWorker.RunWorkerAsync(args);
}
private void CreateConnectionAsync_DoWork(object sender, DoWorkEventArgs e)
{
try
{
Object[] args = (Object[])e.Argument;
e.Result = CreateConnection((string)args[0], (int)args[1]);
}
catch (Exception ex)
{
_asyncException = ex;
}
}
private void CreateConnectionAsync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (CreateConnectionAsyncCompleted != null)
CreateConnectionAsyncCompleted(this, new CreateConnectionAsyncCompletedEventArgs(_asyncException, _asyncCancelled, (TcpClient)e.Result));
}
#endregion
}
}

View file

@ -0,0 +1,43 @@
using System;
using System.Text;
using System.Globalization;
using System.Net.Sockets;
namespace Starksoft.Net.Proxy
{
internal static class Utils
{
internal static string GetHost(TcpClient client)
{
if (client == null)
throw new ArgumentNullException("client");
string host = "";
try
{
host = ((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
}
catch
{ };
return host;
}
internal static string GetPort(TcpClient client)
{
if (client == null)
throw new ArgumentNullException("client");
string port = "";
try
{
port = ((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Port.ToString(CultureInfo.InvariantCulture);
}
catch
{ };
return port;
}
}
}

View file

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using Starksoft.Net.Proxy;
namespace MinecraftClient.Proxy
{
/// <summary>
/// Automatically handle proxies according to the app Settings.
/// Note: Underlying proxy handling is taken from Starksoft, LLC's Biko Library.
/// This library is open source and provided under the MIT license. More info at biko.codeplex.com.
/// </summary>
public static class ProxyHandler
{
public enum Type { HTTP, SOCKS4, SOCKS4a, SOCKS5 };
private static ProxyClientFactory factory = new ProxyClientFactory();
private static IProxyClient proxy;
private static bool proxy_ok = false;
/// <summary>
/// Create a regular TcpClient or a proxied TcpClient according to the app Settings.
/// </summary>
public static TcpClient newTcpClient(string host, int port)
{
try
{
if (Settings.ProxyEnabled)
{
ProxyType innerProxytype = ProxyType.Http;
switch (Settings.proxyType)
{
case Type.HTTP: innerProxytype = ProxyType.Http; break;
case Type.SOCKS4: innerProxytype = ProxyType.Socks4; break;
case Type.SOCKS4a: innerProxytype = ProxyType.Socks4a; break;
case Type.SOCKS5: innerProxytype = ProxyType.Socks5; break;
}
if (Settings.ProxyUsername != "" && Settings.ProxyPassword != "")
{
proxy = factory.CreateProxyClient(innerProxytype, Settings.ProxyHost, Settings.ProxyPort, Settings.ProxyUsername, Settings.ProxyPassword);
}
else proxy = factory.CreateProxyClient(innerProxytype, Settings.ProxyHost, Settings.ProxyPort);
if (!proxy_ok)
{
ConsoleIO.WriteLineFormatted("§8Connected to proxy " + Settings.ProxyHost + ':' + Settings.ProxyPort, false);
proxy_ok = true;
}
return proxy.CreateConnection(host, port);
}
else return new TcpClient(host, port);
}
catch (ProxyException e)
{
ConsoleIO.WriteLineFormatted("§8" + e.Message, false);
proxy = null;
return null;
}
}
}
}

View file

@ -23,6 +23,14 @@ namespace MinecraftClient
public static string SingleCommand = ""; public static string SingleCommand = "";
public static string ConsoleTitle = ""; public static string ConsoleTitle = "";
//Proxy Settings
public static bool ProxyEnabled = false;
public static string ProxyHost = "";
public static int ProxyPort = 0;
public static Proxy.ProxyHandler.Type proxyType = Proxy.ProxyHandler.Type.HTTP;
public static string ProxyUsername = "";
public static string ProxyPassword = "";
//Other Settings //Other Settings
public static string TranslationsFile_FromMCDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\.minecraft\assets\objects\9e\9e2fdc43fc1c7024ff5922b998fadb2971a64ee0"; //MC 1.7.4 en_GB.lang public static string TranslationsFile_FromMCDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\.minecraft\assets\objects\9e\9e2fdc43fc1c7024ff5922b998fadb2971a64ee0"; //MC 1.7.4 en_GB.lang
public static string TranslationsFile_Website_Index = "https://s3.amazonaws.com/Minecraft.Download/indexes/1.7.4.json"; public static string TranslationsFile_Website_Index = "https://s3.amazonaws.com/Minecraft.Download/indexes/1.7.4.json";
@ -52,7 +60,7 @@ namespace MinecraftClient
public static bool ChatLog_Enabled = false; public static bool ChatLog_Enabled = false;
public static bool ChatLog_DateTime = true; public static bool ChatLog_DateTime = true;
public static string ChatLog_File = "chatlog.txt"; public static string ChatLog_File = "chatlog.txt";
public static Bots.ChatLog.MessageFilter ChatLog_Filter = Bots.ChatLog.MessageFilter.AllMessages; public static ChatBots.ChatLog.MessageFilter ChatLog_Filter = ChatBots.ChatLog.MessageFilter.AllMessages;
//PlayerListLog Settings //PlayerListLog Settings
public static bool PlayerLog_Enabled = false; public static bool PlayerLog_Enabled = false;
@ -72,7 +80,7 @@ namespace MinecraftClient
//Remote Control //Remote Control
public static bool RemoteCtrl_Enabled = false; public static bool RemoteCtrl_Enabled = false;
private enum ParseMode { Default, Main, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl }; private enum ParseMode { Default, Main, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl };
/// <summary> /// <summary>
/// Load settings from the give INI file /// Load settings from the give INI file
@ -104,6 +112,7 @@ namespace MinecraftClient
case "main": pMode = ParseMode.Main; break; case "main": pMode = ParseMode.Main; break;
case "scriptscheduler": pMode = ParseMode.ScriptScheduler; break; case "scriptscheduler": pMode = ParseMode.ScriptScheduler; break;
case "remotecontrol": pMode = ParseMode.RemoteControl; break; case "remotecontrol": pMode = ParseMode.RemoteControl; break;
case "proxy": pMode = ParseMode.Proxy; break;
default: pMode = ParseMode.Default; break; default: pMode = ParseMode.Default; break;
} }
} }
@ -167,7 +176,7 @@ namespace MinecraftClient
{ {
case "enabled": ChatLog_Enabled = str2bool(argValue); break; case "enabled": ChatLog_Enabled = str2bool(argValue); break;
case "timestamps": ChatLog_DateTime = str2bool(argValue); break; case "timestamps": ChatLog_DateTime = str2bool(argValue); break;
case "filter": ChatLog_Filter = Bots.ChatLog.str2filter(argValue); break; case "filter": ChatLog_Filter = ChatBots.ChatLog.str2filter(argValue); break;
case "logfile": ChatLog_File = argValue; break; case "logfile": ChatLog_File = argValue; break;
} }
break; break;
@ -196,6 +205,24 @@ namespace MinecraftClient
case "enabled": RemoteCtrl_Enabled = str2bool(argValue); break; case "enabled": RemoteCtrl_Enabled = str2bool(argValue); break;
} }
break; break;
case ParseMode.Proxy:
switch (argName.ToLower())
{
case "enabled": ProxyEnabled = str2bool(argValue); break;
case "type":
argValue = argValue.ToLower();
if (argValue == "http") { proxyType = Proxy.ProxyHandler.Type.HTTP; }
else if (argValue == "socks4") { proxyType = Proxy.ProxyHandler.Type.SOCKS4; }
else if (argValue == "socks4a"){ proxyType = Proxy.ProxyHandler.Type.SOCKS4a;}
else if (argValue == "socks5") { proxyType = Proxy.ProxyHandler.Type.SOCKS5; }
break;
case "host": ProxyHost = argValue; break;
case "port": ProxyPort = str2int(argValue); break;
case "username": ProxyUsername = argValue; break;
case "password": ProxyPassword = argValue; break;
}
break;
} }
} }
} }
@ -222,7 +249,9 @@ namespace MinecraftClient
+ "#leave blank to prompt user on startup\r\n" + "#leave blank to prompt user on startup\r\n"
+ "#Use \"-\" as password for offline mode\r\n" + "#Use \"-\" as password for offline mode\r\n"
+ "\r\n" + "\r\n"
+ "login=\r\npassword=\r\nserverip=\r\n" + "login=\r\n"
+ "password=\r\n"
+ "serverip=\r\n"
+ "\r\n" + "\r\n"
+ "#Advanced settings\r\n" + "#Advanced settings\r\n"
+ "\r\n" + "\r\n"
@ -231,6 +260,14 @@ namespace MinecraftClient
+ "consoletitle=%username% - Minecraft Console Client\r\n" + "consoletitle=%username% - Minecraft Console Client\r\n"
+ "timestamps=false\r\n" + "timestamps=false\r\n"
+ "\r\n" + "\r\n"
+ "[Proxy]\r\n"
+ "enabled=false\r\n"
+ "type=HTTP #Supported types: HTTP, SOCKS4, SOCKS4a, SOCKS5\r\n"
+ "host=0.0.0.0\r\n"
+ "port=8080\r\n"
+ "username=\r\n"
+ "password=\r\n"
+ "\r\n"
+ "#Bot Settings\r\n" + "#Bot Settings\r\n"
+ "\r\n" + "\r\n"
+ "[Alerts]\r\n" + "[Alerts]\r\n"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 161 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 MiB