C# client/server web proxy - c#

I want to implement a client/server web proxy like this:
I set the proxy in web browser as 127.0.0.1:8080. I want catch every tcp request and send it to a server and get the response of that request and send it back to client. for example my client request is https://www.google.com and client send it to server and server get it's page and send it back to client and client can see google.com in the browser. how can I implement this? Is there any open-source project like this?
var tcpListener = new TcpListener(IPAddress.Any, 8080);
tcpListener.Start();
Console.WriteLine("Listening...");
var result = (IAsyncResult)tcpListener.BeginAcceptTcpClient(RespCallback, tcpListener);
//...
//???

Related

How to use WebSocket in one server and access it in another PC in the same LAN?

When I use WebSocket server in C#, and access it in local PC, it works normally. But when I try to access with another PC, it doesn't work.
I'm using WebSocket library from Fleck.
Code in C# WebSocket Server:
_server = new Fleck.WebSocketServer("wss://127.0.0.1:8181");
_server.Certificate = new X509Certificate2(#"C:\test.com.pfx", "123");
Code in HTML/Javascript:
websocket = new WebSocket('wss://192.168.1.37:8181');
When I work with insecure WebSocket "ws://127.0.0.1:8181" when I try to access the local IP "ws://192.168.1.37:8181", it the Chrome Console sends the error:
The page at 'https://www.websocket.org/echo.html' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://192.168.1.37:8181/?encoding=text'. This request has been blocked; this endpoint must be available over WSS.
When I work with Secure WebSocket "wss://127.0.0.1:8181"
it sends the error:
WebSocket connection to 'wss://192.168.1.37:8181/?encoding=text' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
It only works when I type "wss://127.0.0.1:8181" in WebSocket client, but I want to work with the standard IP, to be accessed by other PC.
I found the solution, here the steps if someone needs:
You need to start the Fleck WebSocket server as bellow:
_server = new Fleck.WebSocketServer("wss://0.0.0.0:8181");
_server.Certificate = new X509Certificate2(#"C:\test.com.pfx", "123");
Then in the PC that you want to access though WebSocket client, you need to access first with the browser:
https://192.168.1.37:8181
Then you allow the access. (It'll let the Certification of an Unknown User)
After that you WebSocket Client will work.

How to disconnect SignalR form client side to achieve timeout funcitonality

I have client-side code that sends requests to server for some data. Sometime server don't respond and it take too long to establish connection. I want to set some timeout functionality on client-side so after that timeout, client will stop to try connecting server. In simple web request we can use timeout functionality. For example I am using the following code somewhere else and it is working fine for non-signalr request.
But when I send request through signalr, I can not set timeout to end this request and show user a message that data could not be fetched from server.
On this site someone suggested "Send a specific string to the client to force the disconnect" but that is not my scenario. I want to handle it on client side.
var refit = RestService.For<SomeApi>(new HttpClient
{
BaseAddress = new Uri(BASE_URL),
Timeout = TimeSpan.FromSeconds(30)
});

Connect TcpClient through Fiddler proxy

How can I use a Fiddler proxy with a TcpClient? The answer on this similar question did not work for me: How to use Proxy with TcpClient.ConnectAsync()?
var client = new Pop3Client();
var tcpClient = new TcpClient(hostname, port);
var sslStream = new SslStream(tcpClient.GetStream());
sslStream.AuthenticateAsClient(hostname);
client.Connect(sslStream);
After some discussion it turned out that the code to create a connection through a proxy which was referenced in the question actually worked, but
SSL decryption need to be off in Fiddler.
Otherwise Fiddler will not pass the original TLS handshake through but will create one between Fiddler and Server and another one between Client and Fiddler, where the last one has a certificate created by Fiddler. The client will usually not trust this certificate by default and thus fail the TLS handshake.
Moreover, Fiddler expects the traffic inside the TLS connection to be HTTP, i.e. the client sends a HTTP request and the server sends a HTTP response back. POP3 works differently by having both a different message syntax and by having the server start with sending and not the client.
It really has to be client.Connect(sslStream) as shown in the question and not something like client.Connect(tcpStream) as the OP had in its actual code. In the last case the client will just try to read the encrypted data from the connection and thus fail.

Multiple website server side Clients.All.receiveNotification challenge with SignalR

So I have two websites that I am using SignalR and I have the following in both my Global.asax files:
HubConfiguration hubConfig = new HubConfiguration();
hubConfig.EnableCrossDomain = true;
RouteTable.Routes.MapHubs(hubConfig);
I have a server side event side and I get the hub context to send a message to all listening clients:
var signalrContext = GlobalHost.ConnectionManager.GetHubContext<Notifications.OfferHub>();
signalrContext.Clients.All.receiveNotification("hello world");
The same event happens server side on both websites and I would like to broadcast this cross-domain to all listening clients. I am thinking this is not possible server side because I will not be able to get the HubContext for both websites change the hub.url server side.
Unless anyone has any other suggestions?
Well, it took me a minute to realize what do here. Thanks to David Fowler for his help and SignalR awesomeness! I ended up adding a ServiceReference in each website to a web service in the other website and calling a "Broadcast" WebMethod. The web service webmethod just gets the current SignalR context and broadcasts my message to all clients listening on the respective website.

Connect to a TCP server via TcpClient through proxy

My system is accessing internet via a proxy server
(IE proxy server address : myserver.mydomain.com , port: 80).
Im trying to send some data to a TCP server using class "System.Net.Sockets.TcpClient". But i am unable to connect. If I try using static IP internet, i am able to connect.
Is there any way to provide proxy address ? I am pretty sure the problem is the proxy, because i have tried from two systems which do not use proxy and it worked fine.
My application is a console application.
My Code is something like :
TcpClient tcpClient = new TcpClient("tcpserver.com", 3101);
string message = "message";
byte[] auditMessageStream;
auditMessageStream = Encoding.UTF8.GetBytes(message);
int i = tcpClient.Client.Send(auditMessageStream);
You can use this opensource lib to create a socket conection through a proxy.
Watch this too
You need HTTP tunneling unless your proxy is a SOCKS proxy.

Categories

Resources