Page 1 of 7 123 ... LastLast
Results 1 to 10 of 66

Thread: Socks V4

  1. #1
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Socks V4

    In my quest to learn C# I coded this Socks Proxy Server yesterday it complies with Socks Version 4 protocol. Now you can watch the data fly between your applications and its server. Youll need the microsoft .NET compiler version 1.1. to compile this code. After compiling you can setup IE to use the socks server running on ur machine.

    Code:
    /**
    *  @Author(s): Leoandru
    *  @Created: 04/23/2005 (m/d/y)
    *  @Last Modified: 04/23/2005  By: Leoandru
    *
    *
    *  This	file is part of a the free software.
    *  Redistribution and use in source and binary forms, with or without
    *  modification, are permitted provided that the following conditions
    *  are met:
    *
    *  1. Redistributions of source code must retain the above copyright
    *     notice, this list of conditions and the following disclaimer.
    *  2. Redistributions in binary form must reproduce the above copyright
    *     notice, this list of conditions and the following disclaimer in the
    *     documentation and/or other materials provided with the distribution.
    *
    *  THE SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS", WITHOUT WARRANTY OF ANY KIND,
    *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    *  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    *  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    *
    *  File Version 1.0
    *  Microsoft .Net Version 1.1
    */
    
    
    
    /// <remarks>
    ///  SharpSocks is a socks proxy server complying to socks version 4 and 5.
    /// </remarks>
    
    
    using System;
    using System.Net;
    using System.Net.Sockets;
    
    using System.Threading;
    using System.Text;
    
    namespace Codebox.SharpSocks
    {
    	
    	
    	/// <summary>
    	/// The SocksServer listen for socks connections determine the client's protocol
    	/// version and hands off the request accordinly.
    	/// </summary>
    	
    	public class SocksServer {
    		
    		
    		
    		#region variables
    		
    		protected TcpListener socksserv;    //Server Socket
    				
    		#endregion
    		
    		
    		
    		#region constructors
    		
    		/// <summary>
    		/// Create a socks server instance listening on port "port"
    		/// </summary>
    		/// <param name="args">The listening port of the socks server</param>
    		public SocksServer(Int32 port) : this( CreateEndPoint(port) ) {	}
    		
    		
    		/// <summary>
    		/// Create a socks server instance bound to the endpoint
    		/// <param name="ep">The end point to bind to</param>
    		/// </summary>
    		public SocksServer(IPEndPoint ep) {			
    			socksserv = new TcpListener(ep);
    		}
    		
    		
    		/// <summary>
    		/// Create a socks server binds to a specific local IPAddress and port number
    		/// <param name="addr">The IPAddress to bind to</param>
    		/// <param name="port">The port to bind to</param>
    		/// </summary>
    		public SocksServer(IPAddress addr, int port) {
    			socksserv = new TcpListener(addr, port);
    		}
    		
    		#endregion
    		
    		
    		
    		#region private methods
    		
    		/// <summary>
    		/// Create a IPEndPoint from the port.
    		/// <param name="port">The port to use for the endpoint</param>
    		/// </summary>
    		static private IPEndPoint CreateEndPoint(Int32 port) {
    			IPHostEntry local = Dns.GetHostByName(Dns.GetHostName());
    			return new IPEndPoint(local.AddressList[0], port);
    		}
    		
    		#endregion
    		
    				
    		
    		#region public methods		
    		
    		public void RunServer() {			
    			
    			socksserv.Start();
    			
    			Console.WriteLine("socks server started listening on: "+socksserv.LocalEndpoint.ToString());
    			
    			while(true) {				
    				try {
    					TcpClient c = socksserv.AcceptTcpClient(); //accept client connect
    					Console.WriteLine("Connection Accepted: Negotiating Connection...");
    					
    					NetworkStream ns = c.GetStream();
    					int version = ns.ReadByte();
    					Console.WriteLine("Version: {0}", version);
    					
    					if(version == 4) {
    						//version 4 protocol handler
    						Thread thread = new Thread(new ThreadStart(new SocksV4Handler(c).Start));
    						thread.Start();						
    					}
    					else if(version == 5) {
    						//version 5 protocol handler
    					}
    				}
    				catch(Exception e) { 
    					Console.WriteLine(e.StackTrace); 
    				}
    			}
    		}
    		
    		#endregion
    		
    		
    		
    		static public void Main(string[] args) {
    			new SocksServer(1080).RunServer();
    		}
    	}//SocksServer
    	
    	
    	
    	
    	
    	/// <summary>
    	/// Handles socks version 4 protocol
    	/// </summary>
    	
    	internal class SocksV4Handler {
    		
    		
    		#region protected members
    		
    		//The client connection
    		protected TcpClient client;
    		
    		#endregion
    		
    		
    		
    		#region constructors
    		
    		public SocksV4Handler(TcpClient c) {
    			if(c == null) {
    				throw new ArgumentNullException("tcpclient null");
    			}
    			client = c;
    		}
    		
    		#endregion
    		
    		
    		
    		#region public methods
    		
    		public void Start() {
    		/*
    			        +----+----+----+----+----+----+----+----+----+----+....+----+
    		            | VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
    		            +----+----+----+----+----+----+----+----+----+----+....+----+
            # of bytes:	   1    1      2              4           variable       1
            */
    			try {
    				NetworkStream ns = client.GetStream();
    				int cd = ns.ReadByte(); //the command
    				
    				byte[] p = new byte[2];
    				int r = ns.Read(p, 0, p.Length);
    				if(r != 2) { return; }				
    				int port = GetPort(p);
    				
    				byte[] ip = new byte[4];
    				r = ns.Read(ip, 0, ip.Length);
    				if(r != 4) { return; }				
    				
    				IPAddress address = new IPAddress( GetAddress(ip) );
    				Console.WriteLine("Connect To Address: "+address.ToString()+"Port: {0}",port);
    				
    				TcpClient app = new TcpClient();
    				app.Connect(address, port); //connect to the remote app				
    				Console.WriteLine("Successfuly connected");
    				
    				//read and discard the rest
    				while(ns.ReadByte() != 0) { }	
    				
    				//respond to the client
    				byte[] res = new byte[8];
    				
    				res[0] = 0;
    				res[1] = 90;
    				res[2] = p[0];
    				res[3] = p[1];
    				res[4] = ip[0];
    				res[5] = ip[1];
    				res[6] = ip[2];
    				res[7] = ip[3];
    				
    				ns.Write(res, 0, res.Length);
    				ns.Flush();
    				
    				TcpNetworkStreamTunnel t1 = new TcpNetworkStreamTunnel(app, client);
    				TcpNetworkStreamTunnel t2 = new TcpNetworkStreamTunnel(client, app);
    				
    				Thread thread1 = new Thread( new ThreadStart(t1.Start) );
    				Thread thread2 = new Thread( new ThreadStart(t2.Start) );
    				thread1.Start();
    				thread2.Start();				
    			}
    			catch(Exception e) {
            		Console.WriteLine( e.ToString() );
    			}
    		}
    		
    		
    		
    		
    		/// <summary>
    		/// Return the specified byte array as an integer.
    		/// </summary>
    		private int GetPort(byte[] ba) {
    			
    			int i = 0;
    			
    			foreach(byte b in ba) {
    				i = i << 8;
    				i |= (int)b;
    			}
    			return i;
    		}
    		
    		
    		
    		/// <summary>
    		/// Return the specified byte address as a long.
    		/// </summary>
    		private long GetAddress(byte[] ba) {
    			
    			long l = 0;
    			
    			for(int i = ba.Length - 1; i >= 0; --i) {
    				l = l << 8;
    				l |= (long)ba[i];
    			}
    			return l;
    		}
    		
    		#endregion
    		
    	}//SocksV4Handler
    	
    	
    	
    	
    	
    	internal sealed class TcpNetworkStreamTunnel {
    		
    		
    		//The stream to read from
    		private TcpClient input;
    		
    		//The stream to write to
    		private TcpClient output;
    		
    		private byte[] buffer;
    		
    		
    		
    		public TcpNetworkStreamTunnel(TcpClient input, TcpClient output) {
    			this.input  = input;
    			this.output = output;
    			buffer = new byte[49152];
    		}
    		
    		
    		
    		public void Start() {
    			
    			try {
    				NetworkStream ins  = input.GetStream();
    				NetworkStream outs = output.GetStream();
    				
    				while(true) {
    					int read = ins.Read(buffer, 0, buffer.Length);
    					string data = Encoding.ASCII.GetString(buffer, 0, read);
    					Console.Write(data);
    					if(read == 0) { break; }
    					outs.Write(buffer, 0, read);
    					outs.Flush();
    				}
    			}
    			catch(Exception) { }
    			finally {
    				try {
    					input.Close();
    					output.Close();
    				}
    				catch(Exception) { }
    			}			
    		}
    		
    	}//NetworkStreamTunnel
    }
    Here is a sample output:




    IF you here lots of beeping its the program printing alert characters. You computer won't explode .
    Last edited by leoandru; Apr 25, 2005 at 12:23 AM.

  2. #2
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    Big lame.. big O

    C# offends me, especially for networking code. One should always go back to basics

    for that and stick with C, not C++ and not C dull.

    Regards,
    Pogi Tuner.

  3. #3
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Sorry to offend you my friend but whats so lame about it?

  4. #4
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    I am not sure, I am just old school maybe.. everything new offends me

    C just has the fastest, most efficient, implementation of network code.

    Regards,
    Pogi Tuner.

  5. #5
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by pogi_2nr
    I am not sure, I am just old school maybe.. everything new offends me

    C just has the fastest, most efficient, implementation of network code.
    True but its not like C# and Java are lagging behind in speed. I love C too but im much more productive with these newer languages. I can focus on the problem rather than how to solve the problem.

  6. #6
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    New age propaganda. One should not abuse the excess in hardware resources by writing

    inefficient code. Lets all just spend more money and keep upgrading our servers!

    I am pretty sure you wouldnt like it if microsoft used one of those .NET languages to make

    the next version of windows.

  7. #7
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    I think parts of Longhorn will be developed in C#, I saw the article somewhere but can't find it. Also C will be completly phased out as a development language in Longhorn. Read More.
    Last edited by leoandru; Apr 25, 2005 at 12:15 PM.

  8. #8
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Here is another article:

    For the first time, Windows will be truly .NET. Windows Server 2003 (although codenamed as Windows .NET Server) did not fulfill the dream. All it managed to do in the end was carry .NET Frameworks v.1.1 by default. Longhorn on the other hand is coded from the ground up in .NET. This .NET is named WinFX. This presents new goals to programming. There will no longer be poor codes and hacks. When we create a WinForms application in the current environment, we do
    http://www.pcquest.com/content/searc...sp?artid=53896

  9. #9
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    Dim F As New Form()

    This is nothing but a wrapper to the underlying Windows API function CreateWindowsEx() with some default parameters. Not anymore in Longhorn, because CreateWindowsEx is no longer there. Instead, a traditional non .NET application will have to have a wrapper to make CreateWindowsEx call New Form()
    That is all userspace! I have not seen them mention anything about the implementation

    of the core OS functions, if they did those in C# that will just aid in me phasing MS out

    of my life.

  10. #10
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by pogi_2nr
    That is all userspace! I have not seen them mention anything about the implementation

    of the core OS functions, if they did those in C# that will just aid in me phasing MS out

    of my life.
    I doubt it very much that the core os would be done in .Net. The "kernel" would still need to be coded in C and ASM. But thats not my point. Everthing else that lies on top of that "kernel" will no longer be C/C++ based. So as developers we (well at least for Longhorn) will no longer need to medel in C/C++.

    I develop for both Windows and Linux and its hell to code a single project in C for both platforms . Trust me I have had numerous visits to the doctor when trying to do that. Why do u think companies take so long to release a Linux version of their software or vise versa?.

    Don't get me wrong im not lauding the fact that C/C++ will be phased out in Longhorn. But i tend to favor languages that give me cross platform development without much headaches.. Java, C#, Python just to name a few a much easier to use than C.
    When it comes to efficiency most ppl don't notice the difference of a few milli seconds. Only benchmarks can tell you that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •