Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Chat : Page
- {
- public TcpClient client = new TcpClient();
- NetworkStream stream;
- private SslStream ssl;
- private string AppId { get; set; }
- public string AppSecret { get; set; }
- public string AppUrl { get; set; }
- public string UserId { get; set; }
- public string AccessToken { get; set; }
- private string _error = string.Empty;//global error string for watch debugging in VS.
- public const string FbServer = "chat.facebook.com";
- private const string STREAM_XML = "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:client\" to=\"chat.facebook.com\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">";
- private const string AUTH_XML = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-FACEBOOK-PLATFORM'></auth>";
- private const string CLOSE_XML = "</stream:stream>";
- private const string RESOURCE_XML = "<iq type=\"set\" id=\"3\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><resource>fb_xmpp_script</resource></bind></iq>";
- private const string SESSION_XML = "<iq type=\"set\" id=\"4\" to=\"chat.facebook.com\"><session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/></iq>";
- private const string START_TLS = "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
- protected void Page_Load(object sender, EventArgs e)
- this.AppId = "000000082000090";//TODO get from appsettings.
- //AddAdditionalPermissions("xmpp_login");//TODO handle xmpp_login persmission
- this.AppSecret = "d370c1bfec9be6d9accbdf0117f2c495"; //TODO Get appsecret from appsetting.
- this.AppUrl = "https://fbd.anteckna.nu";
- SetUserNameAndAuthToken();
- Connect(FbServer);
- // initiates auth process (using X-FACEBOOK_PLATFORM)
- InitiateAuthProcess(STREAM_XML);
- // starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
- StartTlsConnection(START_TLS);
- // gets decoded challenge from server
- var decoded = GetDecodedChallenge(AUTH_XML);
- // creates the response and signature
- string response = CreateResponse(decoded);
- //send response to server
- SendResponseToServer(response);
- SendMessage("test");
- // finishes auth process
- FinishAuthProcess();
- // we made it!
- string streamresponseEnd = SendWihSsl(CLOSE_XML);
- }
- public void Connect(string server)
- {
- try
- {
- client.Connect(server, 5222);
- stream = client.GetStream();
- }
- catch (ArgumentNullException e)
- {
- Console.WriteLine("ArgumentNullException: {0}", e);
- }
- catch (SocketException e)
- {
- Console.WriteLine("SocketException: {0}", e);
- }
- }
- private void InitiateAuthProcess(string streamXml)
- {
- string initiation = Send(streamXml);
- if (!initiation.Contains("X-FACEBOOK-PLATFORM"))
- _error = initiation; //TODO handle error
- }
- //Facebook answer
- //<?xml version='1.0' ?><stream:stream from='chat.facebook.com' id='1' version='1.0' //xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en'><stream:features><starttls //xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>X-//FACEBOOK-PLATFORM</mechanism><mechanism>PLAIN</mechanism></mechanisms></stream:features>
- public string Send(string dataToSend)
- {
- Byte[] data = Encoding.UTF8.GetBytes(dataToSend);
- stream.Write(data, 0, data.Length);
- data = new Byte[2048];
- Int32 bytes = stream.Read(data, 0, data.Length);
- string responseData = Encoding.UTF8.GetString(data, 0, bytes);
- return responseData;
- }
- private void StartTlsConnection(string startTls)
- {
- string tlsResult = Send(startTls);
- if (!tlsResult.ToLower().Contains("proceed"))
- _error = tlsResult; //TODO handle error
- ssl = new SslStream(client.GetStream(), false,
- new RemoteCertificateValidationCallback(ValidateServerCertificate),
- null);
- try
- {
- ssl.AuthenticateAsClient(FbServer, new X509CertificateCollection(), SslProtocols.Tls, false);
- }
- catch (AuthenticationException ex)
- {
- Console.WriteLine("Exception: {0}", ex.Message);
- if (ex.InnerException != null)
- {
- Console.WriteLine("Inner exception: {0}", ex.InnerException.Message);
- }
- Console.WriteLine("Authentication failed - closing the connection.");
- client.Close();
- return;
- }
- string initiationSsl = SendWihSsl(STREAM_XML);
- if (!initiationSsl.Contains("X-FACEBOOK-PLATFORM"))
- _error = initiationSsl;//TODO handle error
- }
- //Response from facebook in tlsResult is:
- //<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
- //Response from Facebook in initiationSsl is :
- //<?xml version='1.0' ?><stream:stream from='chat.facebook.com' id='1' version='1.0' //xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en'><stream:features><mechanisms //xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>X-FACEBOOK-PLATFORM</mechanism>
- //<mechanism>PLAIN</mechanism></mechanisms></stream:features>
- public string SendWihSsl(string dataToSend)
- {
- Byte[] data = System.Text.Encoding.UTF8.GetBytes(dataToSend);
- ssl.Write(data, 0, data.Length);
- ssl.Flush();
- data = new Byte[2048];
- int myBytesRead = 0;
- StringBuilder myResponseAsSB = new StringBuilder();
- do
- {
- myBytesRead = ssl.Read(data, 0, data.Length);
- myResponseAsSB.Append(System.Text.Encoding.UTF8.GetString(data, 0, myBytesRead));
- if (myResponseAsSB.ToString().IndexOf("</") != -1)
- {
- break;
- }
- } while (myBytesRead != 0);
- return myResponseAsSB.ToString();
- }
- //reeturn version=1&method=auth.xmpp_login&nonce=234A8DD1234AB6ABSD1234ABC1AB123
- private string GetDecodedChallenge(string authXml)
- {
- string sslResponse = SendWihSsl(authXml);
- int start = sslResponse.IndexOf("'>") + 2;
- int end = sslResponse.IndexOf("</");
- string challenge = sslResponse.Substring(start, end - start);
- string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(challenge));
- return decoded;
- }
- private void SendResponseToServer(string response)
- {
- string xml = String.Format("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">{0}</response>", response);
- string response2 = SendWihSsl2(xml);
- if (!response2.ToLower().Contains("success"))
- _error = response2;
- }
- //After 100seconds I get : <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
Add Comment
Please, Sign In to add comment