Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: agsXMPP2010.csproj
- ===================================================================
- --- agsXMPP2010.csproj (revision 129)
- +++ agsXMPP2010.csproj (working copy)
- @@ -12,7 +12,8 @@
- <AssemblyKeyContainerName>
- </AssemblyKeyContainerName>
- <AssemblyName>agsXMPP</AssemblyName>
- - <AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
- + <AssemblyOriginatorKeyFile>
- + </AssemblyOriginatorKeyFile>
- <DefaultClientScript>JScript</DefaultClientScript>
- <DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout>
- <DefaultTargetSchema>IE50</DefaultTargetSchema>
- @@ -122,6 +123,8 @@
- <Compile Include="Idn\Stringprep.cs" />
- <Compile Include="Idn\StringprepException.cs" />
- <Compile Include="Net\BoshClientSocket.cs" />
- + <Compile Include="Net\ConnectClientSocket.cs" />
- + <Compile Include="Net\Socks5Socket.cs" />
- <Compile Include="Properties\AssemblyInfo.cs">
- <SubType>Code</SubType>
- </Compile>
- @@ -734,12 +737,8 @@
- <Compile Include="XmppConnection.cs">
- <SubType>Code</SubType>
- </Compile>
- - <Content Include="changelog.txt" />
- </ItemGroup>
- <ItemGroup>
- - <None Include="key.snk" />
- - </ItemGroup>
- - <ItemGroup>
- <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
- <Visible>False</Visible>
- <ProductName>.NET Framework Client Profile</ProductName>
- Index: agsXMPP2010.sln
- ===================================================================
- --- agsXMPP2010.sln (revision 0)
- +++ agsXMPP2010.sln (revision 0)
- @@ -0,0 +1,20 @@
- +
- +Microsoft Visual Studio Solution File, Format Version 11.00
- +# Visual C# Express 2010
- +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "agsXMPP2010", "agsXMPP2010.csproj", "{91EA0D0B-D3BA-497F-AFEB-2CAD59DEBA0E}"
- +EndProject
- +Global
- + GlobalSection(SolutionConfigurationPlatforms) = preSolution
- + Debug|Any CPU = Debug|Any CPU
- + Release|Any CPU = Release|Any CPU
- + EndGlobalSection
- + GlobalSection(ProjectConfigurationPlatforms) = postSolution
- + {91EA0D0B-D3BA-497F-AFEB-2CAD59DEBA0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- + {91EA0D0B-D3BA-497F-AFEB-2CAD59DEBA0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
- + {91EA0D0B-D3BA-497F-AFEB-2CAD59DEBA0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
- + {91EA0D0B-D3BA-497F-AFEB-2CAD59DEBA0E}.Release|Any CPU.Build.0 = Release|Any CPU
- + EndGlobalSection
- + GlobalSection(SolutionProperties) = preSolution
- + HideSolutionNode = FALSE
- + EndGlobalSection
- +EndGlobal
- Index: Net/BoshClientSocket.cs
- ===================================================================
- --- Net/BoshClientSocket.cs (revision 129)
- +++ Net/BoshClientSocket.cs (working copy)
- @@ -433,71 +433,71 @@
- catch (WebException ex)
- {
- FireOnError(ex);
- - Disconnect();
- }
- }
- private void OnGetSessionRequestResponse(IAsyncResult result)
- {
- - // grab the custom state object
- - WebRequestState state = (WebRequestState)result.AsyncState;
- - HttpWebRequest request = (HttpWebRequest)state.WebRequest;
- + try {
- + // grab the custom state object
- + WebRequestState state = (WebRequestState)result.AsyncState;
- + HttpWebRequest request = (HttpWebRequest)state.WebRequest;
- - //state.TimeOutTimer.Dispose();
- + //state.TimeOutTimer.Dispose();
- - // get the Response
- - HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(result);
- + // get the Response
- + HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(result);
- - // The server must always return a 200 response code,
- - // sending any session errors as specially-formatted identifiers.
- - if (resp.StatusCode != HttpStatusCode.OK)
- - {
- - //FireOnError(new PollSocketException("unexpected status code " + resp.StatusCode.ToString()));
- - return;
- - }
- + // The server must always return a 200 response code,
- + // sending any session errors as specially-formatted identifiers.
- + if (resp.StatusCode != HttpStatusCode.OK) {
- + //FireOnError(new PollSocketException("unexpected status code " + resp.StatusCode.ToString()));
- + return;
- + }
- - Stream rs = resp.GetResponseStream();
- + Stream rs = resp.GetResponseStream();
- - int readlen;
- - byte[] readbuf = new byte[1024];
- - MemoryStream ms = new MemoryStream();
- - while ((readlen = rs.Read(readbuf, 0, readbuf.Length)) > 0)
- - {
- - ms.Write(readbuf, 0, readlen);
- - }
- + int readlen;
- + byte[] readbuf = new byte[1024];
- + MemoryStream ms = new MemoryStream();
- + while ((readlen = rs.Read(readbuf, 0, readbuf.Length)) > 0) {
- + ms.Write(readbuf, 0, readlen);
- + }
- - byte[] recv = ms.ToArray();
- + byte[] recv = ms.ToArray();
- - if (recv.Length > 0)
- - {
- - string body = null;
- - string stanzas = null;
- + if (recv.Length > 0) {
- + string body = null;
- + string stanzas = null;
- - string res = Encoding.UTF8.GetString(recv, 0, recv.Length);
- + string res = Encoding.UTF8.GetString(recv, 0, recv.Length);
- - ParseResponse(res, ref body, ref stanzas);
- -
- - Document doc = new Document();
- - doc.LoadXml(body);
- - Body boshBody = doc.RootElement as Body;
- + ParseResponse(res, ref body, ref stanzas);
- - sid = boshBody.Sid;
- - polling = boshBody.Polling;
- - m_MaxPause = boshBody.MaxPause;
- + Document doc = new Document();
- + doc.LoadXml(body);
- + Body boshBody = doc.RootElement as Body;
- - byte[] bin = Encoding.UTF8.GetBytes(DummyStreamHeader + stanzas);
- -
- - base.FireOnReceive(bin, bin.Length);
- + sid = boshBody.Sid;
- + polling = boshBody.Polling;
- + m_MaxPause = boshBody.MaxPause;
- - // cleanup webrequest resources
- - ms.Close();
- - rs.Close();
- - resp.Close();
- + byte[] bin = Encoding.UTF8.GetBytes(DummyStreamHeader + stanzas);
- - activeRequests--;
- + base.FireOnReceive(bin, bin.Length);
- - if (activeRequests == 0)
- - StartWebRequest();
- + // cleanup webrequest resources
- + ms.Close();
- + rs.Close();
- + resp.Close();
- +
- + activeRequests--;
- +
- + if (activeRequests == 0)
- + StartWebRequest();
- + }
- + } catch (Exception ex) {
- + FireOnError(ex);
- }
- }
- @@ -714,7 +714,7 @@
- {
- IAsyncResult result = req.BeginGetRequestStream(new AsyncCallback(this.OnGetRequestStream), state);
- }
- - catch(Exception ex)
- + catch(Exception )
- {
- //Console.WriteLine(ex.Message);
- }
- @@ -769,7 +769,7 @@
- IAsyncResult result = requestStream.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(this.OnEndWrite), state);
- }
- }
- - catch (Exception ex)
- + catch (Exception )
- {
- //Console.WriteLine(ex.Message);
- activeRequests--;
- @@ -801,7 +801,7 @@
- result = req.BeginGetResponse(new AsyncCallback(this.OnGetResponse), state);
- }
- - catch (Exception ex)
- + catch (Exception )
- {
- //Console.WriteLine(ex.Message);
- }
- @@ -935,7 +935,7 @@
- StartWebRequest();
- }
- }
- - catch (Exception ex)
- + catch (Exception )
- {
- //Console.WriteLine("Error in OnGetResponse");
- //Console.WriteLine(ex.Message);
- Index: Net/ClientSocket.cs
- ===================================================================
- --- Net/ClientSocket.cs (revision 129)
- +++ Net/ClientSocket.cs (working copy)
- @@ -58,18 +58,18 @@
- /// </summary>
- public class ClientSocket : BaseSocket
- {
- - Socket _socket;
- + protected Socket _socket;
- #if SSL
- SslStream m_SSLStream;
- #endif
- - NetworkStream m_Stream;
- - Stream m_NetworkStream = null;
- + protected NetworkStream m_Stream;
- + protected Stream m_NetworkStream = null;
- - const int BUFFERSIZE = 1024;
- - private byte[] m_ReadBuffer = null;
- + protected const int BUFFERSIZE = 1024;
- + protected byte[] m_ReadBuffer = null;
- - private bool m_SSL = false;
- + protected bool m_SSL = false;
- private bool m_PendingSend = false;
- private Queue m_SendQueue = new Queue();
- @@ -77,9 +77,9 @@
- /// <summary>
- /// is compression used for this connection
- /// </summary>
- - private bool m_Compressed = false;
- + protected bool m_Compressed = false;
- - private bool m_ConnectTimedOut = false;
- + protected bool m_ConnectTimedOut = false;
- /// <summary>
- /// is used to compress data
- /// </summary>
- @@ -89,7 +89,7 @@
- /// </summary>
- private Inflater inflater = null;
- - private Timer connectTimeoutTimer;
- + protected Timer connectTimeoutTimer;
- #region << Constructor >>
- @@ -221,7 +221,7 @@
- }
- }
- - private void EndConnect(IAsyncResult ar)
- + protected void EndConnect(IAsyncResult ar)
- {
- if (m_ConnectTimedOut)
- {
- @@ -262,7 +262,7 @@
- /// Connect Timeout Timer Callback
- /// </summary>
- /// <param name="stateInfo"></param>
- - private void connectTimeoutTimerDelegate(Object stateInfo)
- + protected void connectTimeoutTimerDelegate(Object stateInfo)
- {
- connectTimeoutTimer.Dispose();
- m_ConnectTimedOut = true;
- @@ -284,7 +284,7 @@
- /// <summary>
- ///
- /// </summary>
- - private void InitSSL()
- + protected void InitSSL()
- {
- InitSSL(SslProtocols.Default);
- }
- @@ -293,7 +293,7 @@
- ///
- /// </summary>
- /// <param name="protocol"></param>
- - private void InitSSL(SslProtocols protocol)
- + protected void InitSSL(SslProtocols protocol)
- {
- m_SSLStream = new SslStream(
- m_Stream,
- @@ -533,7 +533,7 @@
- /// <summary>
- /// Read data from server.
- /// </summary>
- - private void Receive()
- + protected void Receive()
- {
- m_NetworkStream.BeginRead(m_ReadBuffer, 0, BUFFERSIZE, new AsyncCallback(EndReceive), null);
- }
- @@ -574,7 +574,7 @@
- //object already disposed, just exit
- return;
- }
- - catch (System.IO.IOException ex)
- + catch (System.IO.IOException)
- {
- //Console.WriteLine("\nSocket Exception: " + ex.Message);
- Disconnect();
- Index: Net/ConnectClientSocket.cs
- ===================================================================
- --- Net/ConnectClientSocket.cs (revision 0)
- +++ Net/ConnectClientSocket.cs (revision 0)
- @@ -0,0 +1,167 @@
- +using System;
- +using System.Net;
- +using System.Net.Sockets;
- +using System.Threading;
- +using System.IO;
- +using System.Text;
- +using System.Configuration;
- +using System.Collections;
- +using System.Diagnostics;
- +using System.Web;
- +
- +#if SSL
- +using System.Net.Security;
- +using System.Security.Authentication;
- +using System.Security.Cryptography.X509Certificates;
- +#endif
- +
- +namespace agsXMPP.Net {
- + public class ConnectClientException : Exception {
- + public enum ErrorCode {
- + HTTP_CONNECT,
- + CONNECTION_TIMEDOUT
- + };
- + private static string[] ErrorStrings = new string[] {
- + "Error creating HTTP tunnel",
- + "Conenction timed out"
- + };
- + public ErrorCode HTTPError;
- + public ConnectClientException(ErrorCode err)
- + : base(ErrorStrings[(int)err]) {
- + HTTPError = err;
- + }
- + }
- +
- + public class ConnectClientSocket : ClientSocket {
- + public ConnectClientSocket() {
- + }
- +
- + public override void Connect() {
- + // Socket is never compressed at startup
- + m_Compressed = false;
- + m_ReadBuffer = null;
- + m_ReadBuffer = new byte[BUFFERSIZE];
- +
- + try {
- + IPAddress ipAddress = null;
- + try {
- + IPHostEntry ipHostInfo = System.Net.Dns.GetHostEntry(Address);
- +
- + if (ipHostInfo.AddressList.Length > 0) {
- + ipAddress = ipHostInfo.AddressList[0];
- + } else {
- + ipAddress = IPAddress.Parse(Address);
- + }
- + } catch (Exception) {
- + // just try to assign as IP address
- + ipAddress = IPAddress.Parse(Address);
- + }
- + IPEndPoint endPoint = new IPEndPoint(ipAddress, Port);
- + // Timeout
- + // .NET supports no timeout for connect, and the default timeout is very high, so it could
- + // take very long to establish the connection with the default timeout. So we handle custom
- + // connect timeouts with a timer
- + m_ConnectTimedOut = false;
- + TimerCallback timerDelegate = new TimerCallback(connectTimeoutTimerDelegate);
- + connectTimeoutTimer = new Timer(timerDelegate, null, ConnectTimeout, ConnectTimeout);
- +#if !(CF || CF_2)
- + // IPV6 Support for .NET 2.0
- + if (Socket.OSSupportsIPv6 && (endPoint.AddressFamily == AddressFamily.InterNetworkV6)) {
- + _socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- + } else {
- + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- + }
- +#else
- + // CF, there is no IPV6 support yet
- + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- +#endif
- + _socket.BeginConnect(endPoint, new AsyncCallback(EndConnect), null);
- + } catch (Exception ex) {
- + FireOnError(ex);
- + }
- + }
- +
- + public static string EncodeUIDPWD(string uid, string pwd) {
- + string uidpwd = uid + ":" + pwd;
- + byte[] data = System.Text.UnicodeEncoding.UTF8.GetBytes(uidpwd);
- + StringBuilder sb = new StringBuilder();
- + sb.Append(System.Convert.ToBase64String(data));
- + return (sb.ToString());
- + }
- +
- + public string HTTPConnectAddr = "";
- + public int HTTPConnectPort = 80;
- + public string HTTPProxyUser = "";
- + public string HTTPProxyPass = "";
- +
- + private string ReadHTTPAnswer() {
- + byte[] buf = new byte [1] { 0 };
- + int crlfcnt = 0;
- + string answer = "";
- + while (true) {
- + if (_socket.Receive(buf, 1, SocketFlags.None) == 1) {
- + answer += Encoding.UTF8.GetString(buf);
- + if (buf[0] == '\n' || buf[0] == '\r') {
- + crlfcnt++;
- + if (crlfcnt == 4) {
- + break;
- + }
- + } else {
- + crlfcnt = 0;
- + }
- + } else {
- + break;
- + }
- + }
- + return answer;
- + }
- +
- + private void ConnectToHTTPProxy() {
- + // creating request
- + string proxyMsg = "CONNECT " + HTTPConnectAddr + ":" + HTTPConnectPort + " HTTP/1.0\n";
- + // adding some headers
- + proxyMsg += "Host: " + HTTPConnectAddr + "\n";
- + if (HTTPProxyUser.Length > 0) {
- + string EncodedData = EncodeUIDPWD(HTTPProxyUser, HTTPProxyPass);
- + proxyMsg += "Proxy-Authorization:Basic " + EncodedData + "\n";
- + }
- + proxyMsg += "\n";
- + // sending
- + byte[] buffer = Encoding.ASCII.GetBytes(proxyMsg);
- + _socket.Send(buffer, buffer.Length, SocketFlags.None);
- + // receiving answer and analizing
- + string data = ReadHTTPAnswer();
- + if (data.IndexOf("200") == -1) {
- + throw new ConnectClientException(ConnectClientException.ErrorCode.HTTP_CONNECT);
- + }
- + }
- +
- + new protected void EndConnect(IAsyncResult ar) {
- + // go do something
- + if (m_ConnectTimedOut) {
- + FireOnError(new ConnectClientException(ConnectClientException.ErrorCode.CONNECTION_TIMEDOUT));
- + } else {
- + try {
- + // stop the timeout timer
- + connectTimeoutTimer.Dispose();
- + // pass connection status with event
- + _socket.EndConnect(ar);
- + // try HTTP connect and auth
- + ConnectToHTTPProxy();
- + // assing network stream as usual
- + m_Stream = new NetworkStream(_socket, false);
- + m_NetworkStream = m_Stream;
- +#if SSL
- + if (m_SSL)
- + InitSSL();
- +#endif
- + FireOnConnect();
- + // Setup Receive Callback
- + Receive();
- + } catch (Exception ex) {
- + FireOnError(ex);
- + }
- + }
- + }
- + }
- +}
- Index: Net/SocketConnectionType.cs
- ===================================================================
- --- Net/SocketConnectionType.cs (revision 129)
- +++ Net/SocketConnectionType.cs (working copy)
- @@ -34,6 +34,11 @@
- /// </summary>
- Direct,
- #endif
- + /// <summary>
- + /// <para>HTTP tunneling using CONNECT method</para>
- + /// </summary>
- + HttpConnect,
- +
- /// <summary>
- /// A HTTP Polling Socket connection (JEP-0025)
- /// </summary>
- @@ -43,6 +48,11 @@
- /// <para>XEP-0124: Bidirectional-streams Over Synchronous HTTP (BOSH)</para>
- /// <para>http://www.xmpp.org/extensions/xep-0124.html</para>
- /// </summary>
- - Bosh
- + Bosh,
- +
- + /// <summary>
- + /// <para>SOCKS5 proxy</para>
- + /// </summary>
- + SOCKS5
- }
- }
- Index: Net/Socks5Socket.cs
- ===================================================================
- --- Net/Socks5Socket.cs (revision 0)
- +++ Net/Socks5Socket.cs (revision 0)
- @@ -0,0 +1,307 @@
- +using System;
- +using System.Net;
- +using System.Net.Sockets;
- +using System.Threading;
- +using System.IO;
- +using System.Text;
- +using System.Configuration;
- +using System.Collections;
- +using System.Diagnostics;
- +
- +#if SSL
- +using System.Net.Security;
- +using System.Security.Authentication;
- +using System.Security.Cryptography.X509Certificates;
- +#endif
- +
- +#if BCCRYPTO
- +using Org.BouncyCastle.Crypto.Tls;
- +#endif
- +
- +using agsXMPP.IO.Compression;
- +
- +using agsXMPP;
- +
- +namespace agsXMPP.Net {
- + /// <summary>
- + /// Provides sock5 error handling
- + /// </summary>
- + public class Socks5Exception : Exception {
- + /// <summary>
- + /// Socks5 error codes
- + /// </summary>
- + public enum ErrorCode {
- + NO_AUTH_METHODS,
- + BAD_AUTH,
- + GENERAL_FAILURE,
- + CONNECTION_DIBALED,
- + NETWORK_UNREACHABLE,
- + HOST_UNREACHABLE,
- + CONENCTION_FAILED,
- + TTL_EXCEEDED,
- + NOT_SUPPORTED,
- + ADDR_TYPE_NOT_SUPPORTED,
- + CONNECTION_TIMEDOUT
- + };
- +
- + private static string[] ErrorStrings = new string[] {
- + "None of the authentication method was accepted by proxy server",
- + "Bad Usernaem/Password",
- + "General server failure",
- + "Connection disabled by rule",
- + "Network unreachable",
- + "Host unreachable",
- + "Connection failed",
- + "TTL exceeded",
- + "Commandnot supported or protocol error",
- + "Address type not supported",
- + "Connection timed out"
- + };
- + public ErrorCode Socks5Error;
- + public Socks5Exception ( ErrorCode err ) : base (ErrorStrings[(int)err]) {
- + Socks5Error = err;
- + }
- + }
- + /// <summary>
- + /// Provides sock5 functionality to clients
- + /// </summary>
- + public class Socks5Socket : ClientSocket {
- + public string socks5host = null;
- + public string socks5user = "";
- + public string socks5pass = "";
- + public ushort socks5port = 1080;
- +
- + /// <summary>
- + /// Read and analize last server response
- + /// </summary>
- + private byte GetSocks5Response() {
- + byte[] response = new byte[257];
- + // version
- + _socket.Receive(response, 1, SocketFlags.None); // 1
- + if (response[0] == 0x05) {
- + // read response code
- + _socket.Receive(response, 1, SocketFlags.None); // 2
- + byte retcode = response[0];
- + // reserved
- + _socket.Receive(response, 1, SocketFlags.None);
- + // connect host data
- + _socket.Receive(response, 1, SocketFlags.None);
- + switch (response[0]) {
- + case 0x01:
- + // ipv4 address
- + _socket.Receive(response, 4, SocketFlags.None);
- + break;
- + case 0x04:
- + // ipv6 address
- + _socket.Receive(response, 16, SocketFlags.None);
- + break;
- + case 0x03:
- + // host len
- + _socket.Receive(response, 1, SocketFlags.None);
- + // host
- + _socket.Receive(response, response[0], SocketFlags.None);
- + break;
- + default:
- + // unknown value
- + retcode = 0x01;
- + break;
- + }
- + // port
- + _socket.Receive(response, 2, SocketFlags.None);
- + // return result
- + return retcode;
- + } else {
- + return 0x01;
- + }
- + }
- +
- + /// <summary>
- + /// Initialize byte array with 0x00
- + /// </summary>
- + private void Clear(ref byte[] arr) {
- + for (int a = 0; a < arr.Length; a++) {
- + arr[a] = 0x00;
- + }
- + }
- +
- + /// <summary>
- + /// Connect to socks5 proxy using specified settings
- + /// </summary>
- + private void ConnectToSocks5Proxy() {
- + byte[] request = new byte[257];
- + byte[] response = new byte[257];
- + byte[] rawBytes = null;
- + ushort nIndex;
- +
- + nIndex = 0;
- + // Version 5
- + request[nIndex++] = 0x05;
- + // One authentication methods are in packet...
- + request[nIndex++] = 0x01;
- + if (socks5user.Length != 0 || socks5pass.Length != 0) {
- + // username and password auth
- + request[nIndex++] = 0x02;
- + } else {
- + // no auth
- + request[nIndex++] = 0x00;
- + }
- + // Send the authentication negotiation request...
- + _socket.Send(request, nIndex, SocketFlags.None);
- +
- + // Receive 2 byte response...
- + Clear(ref response);
- + _socket.Receive(response, 2, SocketFlags.None);
- + if (response[1] == 0xFF) {
- + // No authentication method was accepted close the socket.
- + throw new Socks5Exception(Socks5Exception.ErrorCode.NO_AUTH_METHODS);
- + }
- +
- + // server selected Username/Password Authentication protocol
- + if (response[1] == 0x02) {
- + nIndex = 0;
- + // Version 5
- + request[nIndex++] = 0x05;
- + // add user name
- + request[nIndex++] = (byte)socks5user.Length;
- + rawBytes = Encoding.Default.GetBytes(socks5user);
- + rawBytes.CopyTo(request, nIndex);
- + nIndex += (ushort)rawBytes.Length;
- + // add password
- + request[nIndex++] = (byte)socks5pass.Length;
- + rawBytes = Encoding.Default.GetBytes(socks5pass);
- + rawBytes.CopyTo(request, nIndex);
- + nIndex += (ushort)rawBytes.Length;
- + // Send the Username/Password request
- + _socket.Send(request, nIndex, SocketFlags.None);
- + // Receive 2 byte response
- + Clear(ref response);
- + _socket.Receive(response, 2, SocketFlags.None);
- + if (response[1] != 0x00) {
- + throw new Socks5Exception(Socks5Exception.ErrorCode.BAD_AUTH);
- + }
- + }
- +
- + // Send connect request
- + nIndex = 0;
- + // version 5.
- + request[nIndex++] = 0x05;
- + // command = connect.
- + request[nIndex++] = 0x01;
- + // Reserve = must be 0x00
- + request[nIndex++] = 0x00;
- +
- + // Address is full-qualified domain name.
- + request[nIndex++] = 0x03;
- + // length of address.
- + request[nIndex++] = Convert.ToByte(socks5host.Length);
- + rawBytes = Encoding.Default.GetBytes(socks5host);
- + rawBytes.CopyTo(request, nIndex);
- + nIndex += (ushort)rawBytes.Length;
- +
- + // using big-edian byte order
- + byte[] portBytes = BitConverter.GetBytes(socks5port);
- + for (int i = portBytes.Length - 1; i >= 0; i--) {
- + request[nIndex++] = portBytes[i];
- + }
- +
- + // send connect request.
- + _socket.Send(request, nIndex, SocketFlags.None);
- + byte retcode = GetSocks5Response();
- + // Get variable length response
- + switch (retcode) {
- + case 0x00:
- + // connected
- + break;
- + case 0x01:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.GENERAL_FAILURE);
- + case 0x02:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.CONNECTION_DIBALED);
- + case 0x03:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.NETWORK_UNREACHABLE);
- + case 0x04:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.HOST_UNREACHABLE);
- + case 0x05:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.CONENCTION_FAILED);
- + case 0x06:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.TTL_EXCEEDED);
- + case 0x07:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.NOT_SUPPORTED);
- + case 0x08:
- + throw new Socks5Exception(Socks5Exception.ErrorCode.ADDR_TYPE_NOT_SUPPORTED);
- + }
- + }
- +
- + public override void Connect() {
- + // Socket is never compressed at startup
- + m_Compressed = false;
- + m_ReadBuffer = null;
- + m_ReadBuffer = new byte[BUFFERSIZE];
- +
- + try {
- + IPAddress ipAddress = null;
- + try {
- + IPHostEntry ipHostInfo = System.Net.Dns.GetHostEntry(Address);
- +
- + if (ipHostInfo.AddressList.Length > 0) {
- + ipAddress = ipHostInfo.AddressList[0];
- + } else {
- + ipAddress = IPAddress.Parse(Address);
- + }
- + } catch (Exception) {
- + // just try to assign as IP address
- + ipAddress = IPAddress.Parse(Address);
- + }
- + IPEndPoint endPoint = new IPEndPoint(ipAddress, Port);
- + // Timeout
- + // .NET supports no timeout for connect, and the default timeout is very high, so it could
- + // take very long to establish the connection with the default timeout. So we handle custom
- + // connect timeouts with a timer
- + m_ConnectTimedOut = false;
- + TimerCallback timerDelegate = new TimerCallback(connectTimeoutTimerDelegate);
- + connectTimeoutTimer = new Timer(timerDelegate, null, ConnectTimeout, ConnectTimeout);
- +#if !(CF || CF_2)
- + // IPV6 Support for .NET 2.0
- + if (Socket.OSSupportsIPv6 && (endPoint.AddressFamily == AddressFamily.InterNetworkV6)) {
- + _socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- + } else {
- + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- + }
- +#else
- + // CF, there is no IPV6 support yet
- + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- +#endif
- + _socket.BeginConnect(endPoint, new AsyncCallback(EndConnect), null);
- + } catch (Exception ex) {
- + FireOnError(ex);
- + }
- + }
- +
- + new protected void EndConnect(IAsyncResult ar) {
- + // go do something
- + if (m_ConnectTimedOut) {
- + FireOnError(new Socks5Exception(Socks5Exception.ErrorCode.CONNECTION_TIMEDOUT));
- + } else {
- + try {
- + // stop the timeout timer
- + connectTimeoutTimer.Dispose();
- + // pass connection status with event
- + _socket.EndConnect(ar);
- + // try socks5 auth
- + ConnectToSocks5Proxy();
- + // assing network stream as usual
- + m_Stream = new NetworkStream(_socket, false);
- + m_NetworkStream = m_Stream;
- +#if SSL
- + if (m_SSL)
- + InitSSL();
- +#endif
- + FireOnConnect();
- + // Setup Receive Callback
- + Receive();
- + } catch (Exception ex) {
- + FireOnError(ex);
- + }
- + }
- + }
- + }
- +}
- Index: XmppClientConnection.cs
- ===================================================================
- --- XmppClientConnection.cs (revision 129)
- +++ XmppClientConnection.cs (working copy)
- @@ -742,8 +742,15 @@
- StreamParser.Reset();
- #if SSL
- - if (ClientSocket.GetType() == typeof(ClientSocket))
- - ((ClientSocket) ClientSocket).SSL = m_UseSSL;
- + if (ClientSocket.GetType() == typeof(Socks5Socket)) {
- + ((Socks5Socket)ClientSocket).SSL = m_UseSSL;
- + }
- + if (ClientSocket.GetType() == typeof(ConnectClientSocket)) {
- + ((ConnectClientSocket)ClientSocket).SSL = m_UseSSL;
- + }
- + if (ClientSocket.GetType() == typeof(ClientSocket)) {
- + ((ClientSocket)ClientSocket).SSL = m_UseSSL;
- + }
- #endif
- // this should start later
- //if (m_KeepAlive)
- Index: XmppConnection.cs
- ===================================================================
- --- XmppConnection.cs (revision 129)
- +++ XmppConnection.cs (working copy)
- @@ -328,12 +328,26 @@
- m_ClientSocket = null;
- // Socket Stuff
- - if (m_SocketConnectionType == agsXMPP.Net.SocketConnectionType.HttpPolling)
- - m_ClientSocket= new PollClientSocket();
- - else if (m_SocketConnectionType == agsXMPP.Net.SocketConnectionType.Bosh)
- - m_ClientSocket = new BoshClientSocket(this);
- - else
- - m_ClientSocket = new ClientSocket();
- + switch (m_SocketConnectionType) {
- + case Net.SocketConnectionType.Direct:
- + m_ClientSocket = new ClientSocket();
- + break;
- + case Net.SocketConnectionType.HttpPolling:
- + m_ClientSocket= new PollClientSocket();
- + break;
- + case Net.SocketConnectionType.Bosh:
- + m_ClientSocket = new BoshClientSocket(this);
- + break;
- + case Net.SocketConnectionType.SOCKS5:
- + m_ClientSocket = new Socks5Socket();
- + break;
- + case Net.SocketConnectionType.HttpConnect:
- + m_ClientSocket = new ConnectClientSocket();
- + break;
- + default:
- + FireOnError ( this, new Exception( "Can not determine connection type" ));
- + break;
- + }
- m_ClientSocket.OnConnect += new ObjectHandler(SocketOnConnect);
- m_ClientSocket.OnDisconnect += new ObjectHandler(SocketOnDisconnect);
- @@ -360,6 +374,10 @@
- public void SocketDisconnect()
- {
- m_ClientSocket.Disconnect();
- + // this is special case
- + if (m_ConnectionState == agsXMPP.XmppConnectionState.Connecting) {
- + m_ConnectionState = agsXMPP.XmppConnectionState.Disconnected;
- + }
- }
- /// <summary>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement