Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: agsXMPP2010.csproj
- ===================================================================
- --- agsXMPP2010.csproj (revision 127)
- +++ agsXMPP2010.csproj (working copy)
- @@ -122,6 +122,7 @@
- <Compile Include="Idn\Stringprep.cs" />
- <Compile Include="Idn\StringprepException.cs" />
- <Compile Include="Net\BoshClientSocket.cs" />
- + <Compile Include="Net\Socks5Socket.cs" />
- <Compile Include="Properties\AssemblyInfo.cs">
- <SubType>Code</SubType>
- </Compile>
- Index: Net/BoshClientSocket.cs
- ===================================================================
- --- Net/BoshClientSocket.cs (revision 127)
- +++ 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);
- }
- }
- Index: Net/ClientSocket.cs
- ===================================================================
- --- Net/ClientSocket.cs (revision 127)
- +++ 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);
- }
- Index: Net/SocketConnectionType.cs
- ===================================================================
- --- Net/SocketConnectionType.cs (revision 127)
- +++ Net/SocketConnectionType.cs (working copy)
- @@ -43,6 +43,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,300 @@
- +п»ї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>
- + /// 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
- + };
- + /// <summary>
- + /// Provides sock5 error handling
- + /// </summary>
- + public class Socks5Exception : Exception {
- + 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(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(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(ErrorCode.GENERAL_FAILURE);
- + case 0x02:
- + throw new Socks5Exception(ErrorCode.CONNECTION_DIBALED);
- + case 0x03:
- + throw new Socks5Exception(ErrorCode.NETWORK_UNREACHABLE);
- + case 0x04:
- + throw new Socks5Exception(ErrorCode.HOST_UNREACHABLE);
- + case 0x05:
- + throw new Socks5Exception(ErrorCode.CONENCTION_FAILED);
- + case 0x06:
- + throw new Socks5Exception(ErrorCode.TTL_EXCEEDED);
- + case 0x07:
- + throw new Socks5Exception(ErrorCode.NOT_SUPPORTED);
- + case 0x08:
- + throw new 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 {
- + IPHostEntry ipHostInfo = System.Net.Dns.GetHostEntry(Address);
- + IPAddress ipAddress = null;
- + if (ipHostInfo.AddressList.Length > 0) {
- + ipAddress = ipHostInfo.AddressList[0];
- + } else {
- + 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) {
- + base.FireOnError(ex);
- + }
- + }
- +
- + new protected void EndConnect(IAsyncResult ar) {
- + // go do something
- + if (m_ConnectTimedOut) {
- + FireOnError(new 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 127)
- +++ XmppClientConnection.cs (working copy)
- @@ -742,8 +742,12 @@
- 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(ClientSocket)) {
- + ((ClientSocket)ClientSocket).SSL = m_UseSSL;
- + }
- #endif
- // this should start later
- //if (m_KeepAlive)
- Index: XmppConnection.cs
- ===================================================================
- --- XmppConnection.cs (revision 127)
- +++ XmppConnection.cs (working copy)
- @@ -328,12 +328,23 @@
- 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;
- + default:
- + FireOnError ( this, new Exception( "Can not determine connection type" ));
- + break;
- + }
- m_ClientSocket.OnConnect += new ObjectHandler(SocketOnConnect);
- m_ClientSocket.OnDisconnect += new ObjectHandler(SocketOnDisconnect);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement