forked from chungy/wordfeud-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.cs
150 lines (123 loc) · 5.4 KB
/
Session.cs
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
/*
* This file is part of wordfeud-csharp.
*
* Wordfeud-csharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Wordfeud-csharp is distributed in the hopes that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wordfeud-csharp. If not, see
* <http://www.gnu.org/licenses/>.
*/
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
namespace Wordfeud.Client
{
public class Session
{
public string UserID { get; private set; }
public string Username { get; private set; }
private readonly string _passhash;
protected string SessionID { get; private set; }
protected string Domain { get; private set; }
private static readonly Random _random = new Random();
public Session(string username, string password)
{
Username = username;
_passhash = Hash.Password(password);
Login();
}
public IList<Game> Games
{
get
{
JObject response = Request("/wf/user/games/");
if (response == null) { throw new WebException(); }
JToken games = response["content"]["games"];
//if (!games.Valid) { throw new WebException(); }
List<Game> gameList = new List<Game>();
foreach(JToken item in games)
{
if (item["id"] == null) { continue; }
gameList.Add(new Game(this, item["id"].Value<uint>()));
}
return gameList;
}
}
private void Login()
{
string data = "{";
data += "\"username\": \"" + Username + "\", ";
data += "\"password\": \"" + _passhash + "\"";
data += "}";
Domain = "http://game0" + _random.Next(0, 7) + ".wordfeud.com";
Tuple<string, string> login = Request(Domain + "/wf/user/login/", data, null);
JObject response = JObject.Parse(login.Item1);
SessionID = login.Item2;
if (response == null) { throw new WebException("Response was empty or invalid."); }
if(response["status"].Value<string>().Equals("error",StringComparison.InvariantCultureIgnoreCase))
{
//usually "wrong_password"
throw new WebException(response["content"]["type"].Value<string>());
}
UserID = response["content"]["id"].Value<string>();
if (UserID == null) { throw new WebException("UserID was null."); }
}
private static Tuple<string, string> Request(string address, string post, string sessionId)
{
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
request.Method = post != null ? "POST" : "GET";
request.Expect = null;
request.UserAgent = "User-Agent: WebFeudClient/1.2.11 (Android 2.3.3)";
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");
if (sessionId != null)
{
request.CookieContainer = new CookieContainer();
Cookie c = new Cookie("sessionid", sessionId) { Domain = ".wordfeud.com" };
request.CookieContainer.Add(c);
}
if (post != null)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(post);
request.ContentType = "application/json";
request.ContentLength = byte1.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);
newStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string cookieHeader = response.Headers["Set-Cookie"];
string newSessionId = null;
if (cookieHeader != null){newSessionId = cookieHeader.Split('=', ';')[1];}
Stream responseStream = response.GetResponseStream();
if (response.ContentEncoding.ToLower().Contains("gzip")){responseStream = new GZipStream(responseStream, CompressionMode.Decompress);}
if (responseStream == null){throw new WebException("Response was null.");}
StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
string Html = Reader.ReadToEnd();
response.Close();
responseStream.Close();
return new Tuple<string, string>(Html, newSessionId);
}
internal JObject Request(string address, string post)
{
return JObject.Parse(Request(Domain + address, post, SessionID).Item1);
}
internal JObject Request(string address)
{
return JObject.Parse(Request(Domain + address, "[]", SessionID).Item1);
}
}
}