Skip to content

Commit 219b755

Browse files
committed
Redesign Connectivity Settings and fixes for SSO.
ConnectivitySettings.SetupWebRequest(): * KeepAlive=on: for performance reasons. Web request acts as TCP. SingleSignOn: * Index tickets by sha1 for security reasons. * Check account and password when MSNTicket created. * Check account and password when Authenticate() or * Hit delete tick to prevent deleting most used tickets from cache. * Work around for SYNC CALL when calling RenewIfExpired. If we need the ticket absolutely (we have no ticket and need sync call) and an error occured, try again for soft connection errors.
1 parent aaf6b3b commit 219b755

16 files changed

+329
-194
lines changed

Example/ConversationForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ private void ConversationForm_Shown(object sender, EventArgs e)
945945

946946
if (remoteContact.UserTileURL != null && remoteContact.ClientType != IMAddressInfoType.WindowsLive) // For WLM display image, shouldn't overwrite with user tile.
947947
{
948-
HttpAsyncDataDownloader.BeginDownload(remoteContact.UserTileURL.AbsoluteUri, new EventHandler<ObjectEventArgs>(SetUserTileToPictureBox), _messenger.ConnectivitySettings.WebProxy);
948+
HttpAsyncDataDownloader.BeginDownload(remoteContact.UserTileURL.AbsoluteUri, new EventHandler<ObjectEventArgs>(SetUserTileToPictureBox), _messenger.ConnectivitySettings);
949949
}
950950
}
951951

Example/DotMSNClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ private void ShowNextNews()
465465

466466
HttpAsyncDataDownloader.BeginDownload(c.UserTileURL.AbsoluteUri + "?t=" + System.Web.HttpUtility.UrlEncode(Messenger.StorageTicket),
467467
new EventHandler<ObjectEventArgs>(SetUserTileToPictureBox),
468-
Messenger.ConnectivitySettings.WebProxy);
468+
Messenger.ConnectivitySettings);
469469
}
470470
else
471471
{

Example/ReverseAddedForm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private void ReverseAddedForm_Load(object sender, EventArgs e)
8989
pictureBox1.Image = Image.FromStream(new MemoryStream(oea.Object as byte[]));
9090
},
9191

92-
messenger.ConnectivitySettings.WebProxy);
92+
messenger.ConnectivitySettings);
9393
}
9494
},
9595
null);
@@ -159,7 +159,7 @@ void cont_CoreProfileUpdated(object sender, EventArgs e)
159159
pb.Image = Image.FromStream(new MemoryStream(oea.Object as byte[]));
160160
},
161161

162-
messenger.ConnectivitySettings.WebProxy);
162+
messenger.ConnectivitySettings);
163163

164164
}
165165
}

MSNPSharp/ConnectivitySettings.cs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ THE POSSIBILITY OF SUCH DAMAGE.
3333
using System;
3434
using System.Net;
3535
using System.Web;
36+
using System.Net.Sockets;
3637
using System.Net.Security;
3738
using System.Security.Cryptography.X509Certificates;
3839

3940
namespace MSNPSharp
4041
{
4142
using MSNPSharp.Core;
43+
using MSNPSharp.Services;
4244

4345
public class MSNServiceCertificatePolicy : ICertificatePolicy
4446
{
@@ -289,7 +291,7 @@ public ConnectivitySettings(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint,
289291
/// <param name="proxyType">The proxy version, Socks4 or Socks5</param>
290292
/// <param name="webProxy">Webproxy to be used when accessing HTTP resources</param>
291293
public ConnectivitySettings(string host, int port, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, ProxyType proxyType, WebProxy webProxy)
292-
:this(string.Empty, 0, host, port, proxyHost, proxyPort, proxyUsername, proxyPassword, proxyType, webProxy)
294+
: this(string.Empty, 0, host, port, proxyHost, proxyPort, proxyUsername, proxyPassword, proxyType, webProxy)
293295
{
294296
}
295297

@@ -397,13 +399,13 @@ public int Port
397399
/// </summary>
398400
public string LocalHost
399401
{
400-
get
401-
{
402-
return localHost;
402+
get
403+
{
404+
return localHost;
403405
}
404-
set
405-
{
406-
localHost = value;
406+
set
407+
{
408+
localHost = value;
407409
}
408410
}
409411

@@ -412,13 +414,13 @@ public string LocalHost
412414
/// </summary>
413415
public int LocalPort
414416
{
415-
get
416-
{
417-
return localPort;
417+
get
418+
{
419+
return localPort;
418420
}
419-
set
420-
{
421-
localPort = value;
421+
set
422+
{
423+
localPort = value;
422424
}
423425
}
424426

@@ -536,5 +538,30 @@ public override string ToString()
536538
return "{Host=" + Host + ", Port=" + Port + "}";
537539
}
538540
#endregion
541+
542+
#region Public Methods
543+
544+
public void SetupWebRequest(HttpWebRequest webRequest)
545+
{
546+
// Set Keep-Alive for performance reasons.
547+
// Web request acts as TCP :)
548+
webRequest.KeepAlive = true;
549+
webRequest.ServicePoint.Expect100Continue = false;
550+
551+
// Web Proxy
552+
// Check for null and don't override system settings
553+
// webRequest.Proxy = null: overrides global settings
554+
if (webProxy != null)
555+
{
556+
webRequest.Proxy = webProxy;
557+
}
558+
559+
// Local bind address
560+
IPAddress bindAddress = String.IsNullOrEmpty(LocalHost) ? IPAddress.Any : IPAddress.Parse(LocalHost);
561+
webRequest.ServicePoint.BindIPEndPointDelegate =
562+
new BindIPEndPoint(new IPEndPointCallback(new IPEndPoint(bindAddress, LocalPort)).BindIPEndPointCallback);
563+
}
564+
565+
#endregion
539566
}
540567
};

MSNPSharp/Core/HttpSocketMessageProcessor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,11 @@ public override void Send(byte[] data, object userState)
309309

310310
isWebRequestInProcess = true;
311311
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GenerateURI());
312+
ConnectivitySettings.SetupWebRequest(request);
313+
312314
action = HttpPollAction.None;
313315

314-
request.Timeout = 5000;
316+
request.Timeout = 10000;
315317
request.Method = "POST";
316318
request.Accept = "*/*";
317319
request.AllowAutoRedirect = false;

MSNPSharp/IO/HttpAsyncDataDownloader.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ namespace MSNPSharp.IO
4545

4646
public class HttpAsyncDataDownloader
4747
{
48-
public static void BeginDownload(string URL, EventHandler<ObjectEventArgs> completeCallback, WebProxy proxy)
48+
public static void BeginDownload(string URL, EventHandler<ObjectEventArgs> completeCallback, ConnectivitySettings connectivitySettings)
4949
{
5050
Thread httpRequestThread = new Thread(new ParameterizedThreadStart(DoDownload));
51-
httpRequestThread.Start(new object[] { URL, completeCallback, proxy });
51+
httpRequestThread.Start(new object[] { URL, completeCallback, connectivitySettings });
5252
}
5353

5454
private static void DoDownload(object param)
@@ -57,22 +57,19 @@ private static void DoDownload(object param)
5757

5858
string usertileURL = paramlist[0].ToString();
5959
EventHandler<ObjectEventArgs> callBack = paramlist[1] as EventHandler<ObjectEventArgs>;
60-
WebProxy proxy = paramlist[2] as WebProxy;
60+
ConnectivitySettings connectivitySettings = paramlist[2] as ConnectivitySettings;
6161

6262
try
6363
{
6464
Uri uri = new Uri(usertileURL);
6565

6666
HttpWebRequest fwr = (HttpWebRequest)WebRequest.Create(uri);
67-
68-
// Don't override existing system wide proxy settings.
69-
if (proxy != null)
67+
if (connectivitySettings != null)
7068
{
71-
fwr.Proxy = proxy;
69+
connectivitySettings.SetupWebRequest(fwr);
7270
}
7371

7472
fwr.Timeout = 10000;
75-
7673
fwr.BeginGetResponse(delegate(IAsyncResult result)
7774
{
7875
try

MSNPSharp/LiveConnectAPI/LiveAtomAPILight.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,74 @@ THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131
#endregion
3232

33-
3433
using System;
3534
using System.Collections.Generic;
3635
using System.Text;
3736
using System.Xml;
3837
using System.Net;
39-
using MSNPSharp.LiveConnectAPI.Atom;
4038
using System.Xml.Serialization;
4139
using System.IO;
4240
using System.Threading;
4341

4442
namespace MSNPSharp.LiveConnectAPI
4543
{
44+
using MSNPSharp.LiveConnectAPI.Atom;
45+
4646
/// <summary>
4747
/// A wrapper of Windows Live API (http://api.live.net)
4848
/// </summary>
49-
public class LiveAtomAPILight
49+
internal static class LiveAtomAPILight
5050
{
5151
private const string liveAPIBaseURL = @"http://api.live.net";
5252
private const string appID = "1275182653";
5353

5454
internal static void UpdatePersonalStatusAsync(string newDisplayName, long ownerCID, string authToken,
55+
ConnectivitySettings connectivitySettings,
5556
EventHandler<AtomRequestSucceedEventArgs> onSucceed,
5657
EventHandler<ExceptionEventArgs> onError)
5758
{
5859
Thread workingThread = new Thread(new ParameterizedThreadStart(doUpdate));
59-
workingThread.Start(new object[] { newDisplayName, ownerCID, authToken, onSucceed, onError });
60+
workingThread.Start(new object[] { newDisplayName, ownerCID, authToken, connectivitySettings, onSucceed, onError });
6061
}
6162

6263
private static void doUpdate(object paramArray)
6364
{
6465
object[] parameters = paramArray as object[];
65-
entryType returnEntry = null;
6666
long ownerCID = 0;
6767
try
6868
{
6969
string newDisplayName = parameters[0].ToString();
7070
ownerCID = (long)parameters[1];
7171
string authToken = parameters[2].ToString();
72-
returnEntry = LiveAtomAPILight.UpdatePersonalStatus(newDisplayName, ownerCID, authToken);
72+
ConnectivitySettings connectivitySettings = (ConnectivitySettings)parameters[3];
73+
EventHandler<AtomRequestSucceedEventArgs> onSuccessHandler = parameters[4] as EventHandler<AtomRequestSucceedEventArgs>;
74+
75+
entryType returnEntry = LiveAtomAPILight.UpdatePersonalStatus(newDisplayName, ownerCID, authToken, connectivitySettings);
76+
77+
if (onSuccessHandler != null)
78+
onSuccessHandler(ownerCID, new AtomRequestSucceedEventArgs(returnEntry));
7379
}
7480
catch (Exception ex)
7581
{
76-
EventHandler<ExceptionEventArgs> onErrorHandler = parameters[4] as EventHandler<ExceptionEventArgs>;
82+
EventHandler<ExceptionEventArgs> onErrorHandler = parameters[5] as EventHandler<ExceptionEventArgs>;
83+
7784
if (onErrorHandler != null)
7885
onErrorHandler(ownerCID, new ExceptionEventArgs(ex));
7986
}
80-
81-
EventHandler<AtomRequestSucceedEventArgs> onSuccessHandler = parameters[3] as EventHandler<AtomRequestSucceedEventArgs>;
82-
if (onSuccessHandler != null)
83-
onSuccessHandler(ownerCID, new AtomRequestSucceedEventArgs(returnEntry));
8487
}
8588

86-
internal static entryType UpdatePersonalStatus(string newDisplayName, long ownerCID, string authToken)
89+
internal static entryType UpdatePersonalStatus(string newDisplayName, long ownerCID, string authToken, ConnectivitySettings connectivitySettings)
8790
{
8891
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(liveAPIBaseURL + "/Users({0})/Status".Replace("{0}", ownerCID.ToString()));
92+
if (connectivitySettings != null)
93+
{
94+
connectivitySettings.SetupWebRequest(request);
95+
}
8996
request.Method = "POST";
9097
request.Accept = @"application/atom+xml; type=entry";
9198
request.ContentType = @"application/atom+xml";
9299
request.Headers.Add(HttpRequestHeader.Authorization, "WLID1.0 " + authToken);
93100
request.Headers.Add("AppId", appID);
94-
request.ServicePoint.Expect100Continue = false;
95101

96102
entryType entry = new entryType();
97103

@@ -105,24 +111,19 @@ internal static entryType UpdatePersonalStatus(string newDisplayName, long owner
105111
MemoryStream memStream = new MemoryStream();
106112
ser.Serialize(memStream, entry);
107113
string xmlString = Encoding.UTF8.GetString(memStream.ToArray());
108-
request.ContentLength = Encoding.UTF8.GetByteCount(xmlString);
114+
int xmlLength = Encoding.UTF8.GetByteCount(xmlString);
109115

110-
request.GetRequestStream().Write(Encoding.UTF8.GetBytes(xmlString), 0, Encoding.UTF8.GetByteCount(xmlString));
116+
request.ContentLength = xmlLength;
117+
request.GetRequestStream().Write(Encoding.UTF8.GetBytes(xmlString), 0, xmlLength);
111118

112-
113-
entryType returnEntry = null;
114119
XmlReaderSettings readerSettings = new XmlReaderSettings();
115120
readerSettings.CloseInput = true;
116121

117-
XmlReader xmlReader = XmlReader.Create(request.GetResponse().GetResponseStream(), readerSettings);
118-
119-
120-
returnEntry = (entryType)ser.Deserialize(xmlReader);
121-
122-
123-
return returnEntry;
124-
122+
using (XmlReader xmlReader = XmlReader.Create(request.GetResponse().GetResponseStream(), readerSettings))
123+
{
124+
return (entryType)ser.Deserialize(xmlReader);
125+
}
125126
}
126127

127128
}
128-
}
129+
};

MSNPSharp/Services/ABServiceBindingWrapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ namespace MSNPSharp.Services
4343
[System.Web.Services.WebServiceBindingAttribute(Name = "ABServiceBinding", Namespace = "http://www.msn.com/webservices/AddressBook")]
4444
internal sealed class ABServiceBindingWrapper : ABServiceBinding
4545
{
46-
private IPEndPoint localEndPoint = null;
46+
private NSMessageHandler nsHandler;
4747

4848
public ABServiceBindingWrapper()
4949
: base()
5050
{
5151
}
5252

53-
public ABServiceBindingWrapper(IPEndPoint localEndPoint, NSMessageHandler nsHandler)
53+
public ABServiceBindingWrapper(NSMessageHandler nsHandler)
5454
: this()
5555
{
56-
this.localEndPoint = localEndPoint;
56+
this.nsHandler = nsHandler;
5757

5858
SingleSignOnManager.RenewIfExpired(nsHandler, SSOTicketType.Contact);
5959
}
@@ -64,7 +64,7 @@ protected override WebRequest GetWebRequest(Uri uri)
6464
HttpWebRequest httpRequest = request as HttpWebRequest;
6565
if (httpRequest != null)
6666
{
67-
httpRequest.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint((new IPEndPointCallback(localEndPoint)).BindIPEndPointCallback);
67+
nsHandler.ConnectivitySettings.SetupWebRequest(httpRequest);
6868
}
6969
return request;
7070
}

MSNPSharp/Services/DirectoryServiceBindingWrapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ namespace MSNPSharp.Services
4343
[System.Web.Services.WebServiceBindingAttribute(Name = "DirectoryServiceBinding", Namespace = "http://profile.live.com/")]
4444
internal sealed class DirectoryServiceWrapper : DirectoryService
4545
{
46-
private IPEndPoint localEndPoint = null;
46+
private NSMessageHandler nsHandler;
4747

4848
public DirectoryServiceWrapper()
4949
: base()
5050
{
5151
}
5252

53-
public DirectoryServiceWrapper(IPEndPoint localEndPoint, NSMessageHandler nsHandler)
53+
public DirectoryServiceWrapper(NSMessageHandler nsHandler)
5454
: this()
5555
{
56-
this.localEndPoint = localEndPoint;
56+
this.nsHandler = nsHandler;
5757

5858
SingleSignOnManager.RenewIfExpired(nsHandler, SSOTicketType.Directory);
5959
}
@@ -64,7 +64,7 @@ protected override WebRequest GetWebRequest(Uri uri)
6464
HttpWebRequest httpRequest = request as HttpWebRequest;
6565
if (httpRequest != null)
6666
{
67-
httpRequest.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint((new IPEndPointCallback(localEndPoint)).BindIPEndPointCallback);
67+
nsHandler.ConnectivitySettings.SetupWebRequest(httpRequest);
6868
}
6969
return request;
7070
}

0 commit comments

Comments
 (0)