Skip to content

Instantly share code, notes, and snippets.

@cmpunches
Created April 29, 2015 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmpunches/9a8d0057b6a57c22a5d1 to your computer and use it in GitHub Desktop.
Save cmpunches/9a8d0057b6a57c22a5d1 to your computer and use it in GitHub Desktop.
Chris Punches / ProxyPunch
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace ProxyPunch
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
notifyln("UI Initialized.");
notifyln("Ready to bind a SOCKS IP:PORT to 127.0.0.1:PORT");
}
private void notify(string input)
{
// txtOutput.Text += input;
txtOutput.AppendText(input);
}
private void notifyln(string input)
{
txtOutput.AppendText(input + Environment.NewLine);
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
private void bindproxy(string strSocksIP, int socksPort, int localPort)
{
IPAddress SocksIP = IPAddress.Parse(strSocksIP);
// declare TcpListener object here to avoid scope issues outside of the try/catch/finally
TcpListener ListenSocket = null;
TcpClient client = null;
TcpClient rHost;
try
{
// Set the TcpListener/Server to listen to 127.0.0.1:localPort
IPAddress localAddress = IPAddress.Parse("127.0.0.1");
ListenSocket = new TcpListener(localAddress, localPort);
// Start listening for client requests.
ListenSocket.Start();
notifyln(string.Format("Starting listener on 127.0.0.1:{0}", localPort));
// Buffer for reading data
Byte[] bytes = new Byte[256];
// loop to wait for a connection
while (true)
{
notifyln("Waiting for a connection... ");
// Perform a blocking call to accept requests.
client = ListenSocket.AcceptTcpClient();
notifyln("Received local connection.");
notify("Connecting to proxy...");
// need to connect to the socks proxy to route the local connection to
IPAddress proxyendpoint = IPAddress.Parse(strSocksIP);
rHost = new TcpClient(strSocksIP, socksPort);
notifyln( String.Format("Connected to {0}:{1}.", strSocksIP, socksPort) );
// Get a stream object for reading and writing
NetworkStream localstream = client.GetStream();
// Get a stream object for reading and writing
NetworkStream remoteStream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = localstream.Read(bytes, 0, bytes.Length)) != 0)
{
notify( String.Format("{0}", System.Text.Encoding.ASCII.GetString(bytes) ) );
try
{
// Send back a response.
remoteStream.Write(bytes, 0, i);
}
catch
{
// connection's broken, move on
break;
}
}
bytes = null;
// client stream gets written to rHost stream and vice versa
while ((i = remoteStream.Read(bytes, 0, bytes.Length)) != 0)
{
notify(String.Format("{0}", System.Text.Encoding.ASCII.GetString(bytes)));
try
{
// Send back a response.
localstream.Write(bytes, 0, i);
}
catch
{
// connection's broken, move on
break;
}
}
// Shutdown and end connection
client.Close();
rHost.Close();
notifyln("No longer connected!");
}
}
catch (SocketException e)
{
notifyln(String.Format("SocketException: {0}", e));
}
finally
{
// Stop listening for new clients.
ListenSocket.Stop();
}
}
private void btnBind_Click(object sender, EventArgs e)
{
bindproxy(txtSocksIP.Text, Convert.ToInt32(txtSocksPort.Text), Convert.ToInt32(txtLocalPort.Text));
}
private void txtOutput_TextChanged(object sender, EventArgs e)
{
// scroll to bottom
txtOutput.SelectionStart = txtOutput.TextLength;
txtOutput.ScrollToCaret();
txtOutput.Refresh();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment