1 #include "connection.h"
3 #include <nel/misc/ucstring.h>
4 #include <nel/misc/md5.h>
5 #include <nel/net/sock.h>
6 #include <nel/net/login_client.h>
8 #include "nel_launcher_dlg.h"
10 bool CNelLauncherConnection::connect()
12 std::string server
= ConfigFile
.getVar("StartupHost").asString(0);
14 if(m_Sock
.connected())
19 // add the default port if no port in the cfg
20 if(server
.find(':') == std::string::npos
)
22 m_Sock
.connect(NLNET::CInetAddress(server
));
23 if(!m_Sock
.connected())
25 nlwarning("Can't connect to web server '%s'", server
.c_str());
29 catch(NLMISC::Exception
&e
)
31 nlwarning("Can't connect to web server '%s': %s", server
.c_str(), e
.what());
39 if(m_Sock
.connected())
45 bool CNelLauncherConnection::send(const std::string
&url
)
47 nlassert(m_Sock
.connected());
49 std::string buffer
= "GET " + url
+ "\r\n";
50 uint32 size
= buffer
.size();
53 if(m_Sock
.send((uint8
*)buffer
.c_str(), size
, false) != NLNET::CSock::Ok
)
55 nlwarning ("Can't send data to the server");
62 bool CNelLauncherConnection::receive(std::string
&res
)
64 nlassert(m_Sock
.connected());
71 //if(VerboseLog) nlinfo("Receiving");
77 if(m_Sock
.receive((uint8
*)buf
, size
, false) == NLNET::CSock::Ok
)
79 //if(VerboseLog) nlinfo("Received OK %d bytes", size);
82 //nlinfo("block received '%s'", buf);
86 //if(VerboseLog) nlinfo("Received CLOSE %d bytes", size);
89 //nlwarning ("server connection closed");
94 // trim off whitespace.
95 res
= NLMISC::trim(res
);
96 nlinfo("all received '%s'", res
.c_str());
100 std::string
CNelLauncherConnection::checkLogin(const std::string
&login
, const std::string
&password
, const std::string
&clientApp
)
103 m_Login
= m_Password
= m_ClientApp
= "";
105 if(ConfigFile
.exists("UseDirectClient") && ConfigFile
.getVar("UseDirectClient").asBool())
107 ucstring pwd
= ucstring(password
);
108 NLMISC::CHashKeyMD5 hk
= NLMISC::getMD5((uint8
*)pwd
.c_str(), pwd
.size());
109 std::string cpwd
= hk
.toString();
110 nlinfo("The crypted password is %s", cpwd
.c_str());
111 std::string result
= NLNET::CLoginClient::authenticate(ConfigFile
.getVar("StartupHost").asString(), login
, cpwd
, clientApp
);
112 if (!result
.empty()) return result
;
113 for(uint i
= 0; i
< NLNET::CLoginClient::ShardList
.size(); ++i
)
115 nldebug("Shard '%u' '%s' '%u'", NLNET::CLoginClient::ShardList
[i
].Id
, NLNET::CLoginClient::ShardList
[i
].Name
.toString().c_str(), NLNET::CLoginClient::ShardList
[i
].NbPlayers
);
116 m_Shards
.push_back(CShard("1", true,
117 NLNET::CLoginClient::ShardList
[i
].Id
, NLNET::CLoginClient::ShardList
[i
].Name
.toString(), NLNET::CLoginClient::ShardList
[i
].NbPlayers
,
121 m_Password
= password
;
122 m_ClientApp
= clientApp
;
127 return "Can't connect (error code 1)";
129 //if(VerboseLog) nlinfo("Connected");
131 if(!send(ConfigFile
.getVar("StartupPage").asString()+"?login="+login
+"&password="+password
+"&clientApplication="+clientApp
))
132 return "Can't send (error code 2)";
134 //if(VerboseLog) nlinfo("Sent request login check");
139 return "Can't receive (error code 3)";
141 //if(VerboseLog) nlinfo("Received request login check");
144 return "Empty answer from server (error code 4)";
148 // server returns an error
149 nlwarning("server error: %s", res
.substr(2).c_str());
150 return res
.substr(2);
152 else if(res
[0] == '1')
154 // server returns ok, we have the list of shard
155 uint nbs
= atoi(res
.substr(2).c_str());
156 std::vector
<std::string
> lines
;
158 NLMISC::explode(res
, std::string("\n"), lines
, true);
162 //nlinfo ("Exploded, with nl, %d res", lines.size());
163 /* for (uint i = 0; i < lines.size(); i++)
165 nlinfo (" > '%s'", lines[i].c_str());
169 if(lines
.size() != nbs
+1)
171 nlwarning("bad shard lines number %d != %d", lines
.size(), nbs
+1);
172 nlwarning("'%s'", res
.c_str());
173 return "bad lines numbers (error code 5)";
176 for(uint i
= 1; i
< lines
.size(); i
++)
178 std::vector
<std::string
> res
;
179 NLMISC::explode(lines
[i
], std::string("|"), res
);
183 // nlinfo ("Exploded with '%s', %d res", "|", res.size());
184 /* for (uint i = 0; i < res.size(); i++)
186 nlinfo (" > '%s'", res[i].c_str());
192 nlwarning("bad | numbers %d != %d", res
.size(), 7);
193 nlwarning("'%s'", lines
[i
].c_str());
194 return "bad pipe numbers (error code 6)";
196 m_Shards
.push_back(CShard(res
[0], atoi(res
[1].c_str())>0, atoi(res
[2].c_str()), res
[3], atoi(res
[4].c_str()), res
[5], res
[6]));
201 // server returns ???
202 nlwarning("%s", res
.c_str());
207 m_Password
= password
;
208 m_ClientApp
= clientApp
;
213 std::string
CNelLauncherConnection::selectShard(uint32 shardId
, std::string
&cookie
, std::string
&addr
)
217 if(ConfigFile
.exists("UseDirectClient") && ConfigFile
.getVar("UseDirectClient").asBool())
218 return NLNET::CLoginClient::wantToConnectToShard(shardId
, addr
, cookie
);
220 if(!connect()) return "Can't connect (error code 7)";
222 if(m_Login
.empty()) return "Empty Login (error code 8)";
223 if(m_Password
.empty()) return "Empty Password (error code 9)";
224 if(m_ClientApp
.empty()) return "Empty Client Application (error code 10)";
226 if(!send(ConfigFile
.getVar("StartupPage").asString()+"?cmd=login&shardid="+NLMISC::toString(shardId
)+"&login="+m_Login
+"&password="+m_Password
+"&clientApplication="+m_ClientApp
))
227 return "Can't send (error code 11)";
232 return "Can't receive (error code 12)";
235 return "Empty result (error code 13)";
239 // server returns an error
240 nlwarning("server error: %s", res
.substr(2).c_str());
241 return res
.substr(2);
243 else if(res
[0] == '1')
245 // server returns ok, we have the access
247 std::vector
<std::string
> line
;
248 NLMISC::explode(res
, std::string(" "), line
, true);
252 nlwarning("bad launch lines number %d != %d", line
.size(), 2);
253 return "bad launch line number (error code 14)";
256 cookie
= line
[0].substr(2);
261 // server returns ???
262 nlwarning("%s", res
.c_str());