1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#include "cAuthenticator.h"
#include "cBlockingTCPLink.h"
#include "cMCLogger.h"
#include "../iniFile/iniFile.h"
#ifndef _WIN32
#include <cstring>
#endif
#include <string>
#include <sstream>
extern void ReplaceString( std::string & a_HayStack, const std::string & a_Needle, const std::string & a_ReplaceWith );
cAuthenticator::cAuthenticator()
{
}
cAuthenticator::~cAuthenticator()
{
}
bool cAuthenticator::Authenticate( const char* a_PlayerName, const char* a_ServerID )
{
// Default values
std::string Server = "www.minecraft.net";
std::string Address = "/game/checkserver.jsp?user=%USERNAME%&serverId=%SERVERID%";
bool bAuthenticate = true;
// Read custom values from INI
cIniFile IniFile("settings.ini");
if( IniFile.ReadFile() )
{
std::string tServer = IniFile.GetValue("Authentication", "Server");
std::string tAddress = IniFile.GetValue("Authentication", "Address");
bAuthenticate = IniFile.GetValueB("Authentication", "Authenticate", true);
bool bSave = false;
if( tServer.length() == 0 )
{
IniFile.SetValue("Authentication", "Server", Server, true );
bSave = true;
}
else
Server = tServer;
if( tAddress.length() == 0 )
{
IniFile.SetValue("Authentication", "Address", Address, true );
bSave = true;
}
else
Address = tAddress;
if( bSave )
{
IniFile.SetValueB("Authentication", "Authenticate", bAuthenticate, true );
IniFile.WriteFile();
}
}
if( !bAuthenticate ) // If we don't want to authenticate.. just return true
{
return true;
}
ReplaceString( Address, "%USERNAME%", a_PlayerName );
ReplaceString( Address, "%SERVERID%", a_ServerID );
cBlockingTCPLink TCPLink;
if( TCPLink.Connect( Server.c_str(), 80 ) )
{
//TCPLink.SendMessage( std::string( "GET /game/checkserver.jsp?user=" + std::string(a_PlayerName) + "&serverId=" + std::string(a_ServerID) + " HTTP/1.0\r\n\r\n" ).c_str() );
TCPLink.SendMessage( std::string( "GET " + Address + " HTTP/1.0\r\n\r\n" ).c_str() );
//LOGINFO("Successfully connected to mc.net");
std::string Received = TCPLink.ReceiveData();
//LOGINFO("Received data: %s", Received.c_str() );
return ParseReceived( Received.c_str(), &TCPLink );
}
else
{
LOGERROR("Could not connect to %s to verify player name! (%s)", Server.c_str(), a_PlayerName );
return false;
}
}
bool cAuthenticator::ParseReceived( const char* a_Data, cBlockingTCPLink* a_TCPLink )
{
std::stringstream ss(a_Data);
std::string temp;
ss >> temp;
//LOGINFO("tmp: %s", temp.c_str() );
bool bRedirect = false;
bool bOK = false;
if( temp.compare("HTTP/1.1") == 0 || temp.compare("HTTP/1.0") == 0 )
{
int code;
ss >> code;
if( code == 302 )
{
// redirect blabla
LOGINFO("Need to redirect!");
bRedirect = true;
}
else if( code == 200 )
{
LOGINFO("Got 200 OK :D");
bOK = true;
}
}
else
return false;
if( bRedirect )
{
std::string Location;
// Search for "Location:"
bool bFoundLocation = false;
while( !bFoundLocation && ss.good() )
{
char c = 0;
while( c != '\n' )
{
ss.get( c );
}
std::string Name;
ss >> Name;
if( Name.compare("Location:") == 0 )
{
bFoundLocation = true;
ss >> Location;
}
}
if( !bFoundLocation )
{
LOGERROR("Could not find location");
return false;
}
Location = Location.substr( strlen("http://"), std::string::npos ); // Strip http://
std::string Server = Location.substr( 0, Location.find( "/" ) ); // Only leave server address
Location = Location.substr( Server.length(), std::string::npos );
//LOGINFO("Got location: (%s)", Location.c_str() );
//LOGINFO("Got server addr: (%s)", Server.c_str() );
a_TCPLink->CloseSocket();
if( a_TCPLink->Connect( Server.c_str(), 80 ) )
{
LOGINFO("Successfully connected to %s", Server.c_str() );
a_TCPLink->SendMessage( ( std::string("GET ") + Location + " HTTP/1.0\r\n\r\n").c_str() );
std::string Received = a_TCPLink->ReceiveData();
//LOGINFO("Received data: %s", Received.c_str() );
return ParseReceived( Received.c_str(), a_TCPLink );
}
else
{
LOGERROR("Could not connect to %s to verify player name!", Server.c_str() );
}
}
else if( bOK )
{
// Header says OK, so receive the rest.
// Go past header, double \n means end of headers
char c = 0;
while( ss.good() )
{
while( c != '\n' )
{
ss.get( c );
}
ss.get( c );
if( c == '\n' || c == '\r' || ss.peek() == '\r' || ss.peek() == '\n' )
break;
}
if( !ss.good() ) return false;
std::string Result;
ss >> Result;
LOGINFO("Got result: %s", Result.c_str() );
if( Result.compare("YES") == 0 )
{
LOGINFO("Result was \"YES\", so player is authenticated!");
return true;
}
else
{
LOGINFO("Result was \"%s\", so player is NOT authenticated!", Result.c_str() );
return false;
}
}
return false;
}
|