Proxy Server - modify header of request - c#

How do I develop an application that modifies all requests to server, listens to local ports, adds headers, and then transfers between client and server?

Grab a copy of mentalis: http://www.mentalis.org/soft/projects/proxy/
It supports SOCKS4/5 as well as HTTP proxy. You can take a look at the source and figure it out - its not that hard.
[EDIT - and if you're specifically looking at implementing SOCKS, you should read the protocol definition: http://www.openssh.org/txt/socks4.protocol. Theres really only one message you need to implement in a basic proxy (CONNECT) and all you do is parse the target IP/port, open a socket and start relaying data to the incoming socket that sent the request.]

Related

Serving a binary protocol in .Net. Use WCF? IIS?

I need to write a server that will handle a binary protocol with TLS. Nothing to do with HTTP or SOAP. But it needs to be able to accept incoming connections, with client certificates, maybe pooling, and provide responses. Ideally could also post messages back to the client asynchronously but not a requirement.
Can this be done with a special IIS plug in? or with WCF? Or is it best done by just listening on a raw socket?
Edit. To be clear, this is a binary protocol that is well defined by an industry standard. I do not want any software to interpret it, package things into objects etc. I just want to have a listener send bytes sent by a client to a class and then send them back.
I think Remoting can help you, you can make your own channel for example

Server communicate back to multiple different clients

So I got as an assignment to make a small chat function where multiple clients should be able to connect to a server and communicate with it, the server should then be able to choose whom it wants to communicate back with. (From a dropdown list or something).
What I've been able to do so far, with help from some tutorials, is that clients can connect to the server and communicate with it but nothing more. The server can't communicate back.
I'm very new to this and have limited knowledge in both C# and TCP/IP.
https://gist.github.com/4565988 <-Contains both code for client and server.
So I what I need help with is a way for the server to reply to different clients and for the client to recieve a message from the server.
Any help is appreciated!
Best Regards, Fredrik
With regards to a starting point, I would have a look at WCF Duplex Services. Duplex allow you to subscribe to the service and send updates out using two-way communication.
Essentially you can create the server as a WCF service with a couple of methods: getclients and sendMessage. From there, a client can then subscribe to the service and (while connected) get a list of other subscribers (which you provide to the UI) and then send a message back to the service (which will then use duplexing to send it to whomever it needs to).
As long as you're not married to using sockets, this would be a lot easier than creating a protocol and managing a list of connections. There are also examples of using WCF as a chat medium available on code project.
For TCP knowledge I reconn Barbara Heckler's vid where she shows a brief implementation of such kind of server. Unfortunately in Java, but nevertheless very useful.
I reconn minute 0 - 15 for the basics (UDP) and 15 - 40 for the TCP connection and why mutlithreading is need for TCP but not for UDP.
http://www.youtube.com/watch?v=5QzNHEcLp10
It's pretty simple, really. That TCP stream you successfully extract and use to read what the client(s) sent can also be written to in order to send back something, so all you have to do is move the connection and stream objects out to shared collections of some sort so that your server-side sending logic can get at it when it wants to send something. Similarly, in the client you would issue a read on the TCP stream to read what the server sent.

Synchronizing Client and Server interaction

So I am making this tcp program which simply sends and receives information between a client and a server.
My setup works as follows:
1)Server starts listening
2)Client sends "hello" command as well as a username/password
3)Server sends either "correctpass" or "wrongpass"
4)Client starts sending massive amounts of data in 50kb intervals
5)Server receives and stores this data as it comes
My question is: Is there something I should do to make sure that client doesn't send data when the server isn't listening? Forexample, should there be a command sent from server saying that it successfully got the data? I am just wondering this because I can't have the data come not in order.
I am receiving via tcp and I understand that TCP should send all the data to the server, but my problem is that the server might not be reading at the time that it is sent to it.
My other question is: Is TCP a good protocol for sending lots of small data (adding up to alot) through the internet? Is this how dropbox and other sync utilities communicate with their servers?
Edit:
I am currently using C# and networkstream to communicate
Thanks,
Rohit
First think that you need to do it's to read about data communications protocols and standarts thats already invented.
Includes OSI/ISO http://en.wikipedia.org/wiki/OSI_model
That help you to understand levels of tcp and udp, http, rest and etc.
Learn about technologies designed for interaction and communication like WCF.
But dont forget to play with your custom protocol it gives you experiences and representation how data comunications work and why and when use different protocols and technologies.
To work around data transfer collision you can use reqest/answer organization of communication.
But with WCF service you can do data transfer easyly. Without a lot of coding and misatkes.
Tcp is good to send data and be enshured from data coruption.
my problem is that the server might not be reading at the time that it
is sent to it.
The problem you are worrying about doesn't really exist. If the server doesn't have the connection open you will get a 'connection reset'. If the server isn't reading as fast as you are writing your writes will block in blocking mode, or return a retry indication in non-blocking mode.

How can I "bridge" VNC traffic from two clients connected to a C# proxy-type server?

I'm looking to develop an asynchronous C# TCP server which can act as a proxy between two client VNC connections, passing data between the two transparently.
I've already got some asynchronous client-server code set up where I can effectively communicate messages between the server & any connected clients, now I need a way to host a kind of proxy for VNC traffic.
Client A--------------Server--------------Client B
VNC traffic sent -> relayed through server -> VNC traffic received
And then any response from client B to simply flow back to the server, then transitively to client A.
If any more information is required for a proper answer, please do let me know.
Thanks!
I managed to get this answered on Server Fault...the key is to launch "socat" from your application and use it to "link" the connections together.
See: https://serverfault.com/questions/254855/socat-connect-connect-proxy-two-inbound-tcp-connections-to-expose-a-firewalled
For more information.
You say that you already can receive data on the server that is sent from the client - you just write exactly what you read from one client back out to the other connected client.
It seems like you've already done the hard work, and I'm confused as to what part you're not understanding on how to finish it up.

Creating a simple proxy forwarding server in C#

All I want to do is forward every request coming into my server and port, to the same server and a different port, and optionally add one header.
That is it. is there a really simple C# program I could write, that just takes bytes from here and pushes them to this other port, and same with the response, just throws it down to the client?
sTCPPipe by Luigi Auriemma is a great simple C++ TCP pipe implementation that does exactly what you need, but does not allow the addition of extra headers.
For a C# implementation that does HTTP header inspection and acts as a proxy and not just a simple tunnel, look at the Mentalis proxy project. You can easily modify it to direct all requests to one address instead of the address specified in the HTTP Host Header, but the source is delegate spaghetti.
Or you can write one yourself with a TcpListener that listens on say, port 8080, and after accepting a connection connects to another host (using a different socket) and relays all traffic between the two. If you don't use non-blocking sockets, you'll need to use a few threads to accomplish this.
If it's for commercial use, then the challenge with writing a proxy is to make sure it is reliable and can withstand all types of buffer overflow attacks.

Categories