How to connect through the proxy with .Net/Mono TCP/UDP Socket? - c#

We are using C# Socket in Unity to connect to our server. Using proxy is crucial for our use case, unfortunately there is no direct support for proxy in Socket. We are using both TCP and UDP sockets at the same time to exchange data between our app and the server.
Based on this question I'm able to connect through the proxy with first TCP socket, but when I try to connect through the same proxy with second UDP socket, I receive this exception on line socket.Receive(receiveBuffer):
System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host.
ErrorCode = 10054
SocketErrorCode = ConnectionReset
Does it means that the first TCP ws closed because ther can be only one connection at the same time?
I'm using Fiddler to test this scenario. I do not see the second UDP connection in its log.
Is it possible to connect through single proxy with more than one socket from the same app (process)?
Is there some settings in Fiddler that should be changed? Am I missing something?
My simplified code:
class ConnectorExample
{
public class ProxyConfig
{
public string server; // http://127.0.0.1:8888
public string username;
public string password;
public Uri serverUri => new Uri(server);
}
void Connect(IPAddress ipAddress, int tcpPort, int udpPort, ProxyConfig proxyConfig)
{
IPEndPoint remoteTcpEndPoint = new IPEndPoint(ipAddress, tcpPort);
IPEndPoint remoteUdpEndPoint = new IPEndPoint(ipAddress, udpPort);
Socket tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket udpSocket = new Socket(tcpSocket.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
// TCP socket
ConnectThroughProxy(proxyConfig, tcpSocket, remoteTcpEndPoint);
// UDP Socket
// Bind UDP to a free port
udpSocket.Bind(new IPEndPoint(((IPEndPoint) tcpSocket.LocalEndPoint).Address, 0));
ConnectThroughProxy(proxyConfig, udpSocket, remoteUdpEndPoint);
}
// Returns true if connection through the proxy succeeded
private bool ConnectThroughProxy(ProxyConfig proxyConfig, Socket socket, IPEndPoint destination)
{
Uri proxyUri = proxyConfig.serverUri;
string username = proxyConfig.username;
string password = proxyConfig.password;
// Connect to the proxy
socket.Connect(proxyUri.Host, proxyUri.Port);
// Authentication
string auth = string.Empty;
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
auth = $"Proxy-Authorization: Basic {credentials}";
}
// Connect to the destination
// string connectString = $"CONNECT {destination.Address}:{destination.Port} HTTP/1.1{Environment.NewLine}{auth}{Environment.NewLine}{Environment.NewLine}";
string hostAddress = $"{destination.Address}:{destination.Port}";
string connectString = $"CONNECT {destination.Address}:{destination.Port} HTTP/1.1{Environment.NewLine}Host: {hostAddress}{Environment.NewLine}{auth}{Environment.NewLine}{Environment.NewLine}";
byte[] connectMessage = Encoding.UTF8.GetBytes(connectString);
socket.Send(connectMessage);
byte[] receiveBuffer = new byte[1024];
int received = socket.Receive(receiveBuffer); // Exception for second UDP socket
string response = ASCIIEncoding.ASCII.GetString(receiveBuffer, 0, received);
// Response content sample:
// HTTP/1.1 200 Connection Established
// TODO: Should we check for string: "HTTP/1.1 200" ? Checking just "200" seems to be quite weak check.
bool connected = response.Contains("200");
return connected;
}
}
EDIT:
Perhaps the correct question is: Is it possible to use proxy for UDP connections?

Related

c# HttpWebRequest through Org.Mentalis.Network.ProxySocket

I'm trying to make httpwebrequest through socks5 proxy, using mentalis library.
ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// set the proxy settings
s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
s.ProxyType = ProxyTypes.Socks5;
s.Connect("www.mentalis.org", 80);
HttpWebRequest reqTest = (HttpWebRequest)WebRequest.Create("http://www.mentalis.org");
So i create a socket manually and then connect it tor, then to the target. But now, i need to send a request to mentalis. But i can't find any way to set a socket to HttpWebRequest, is it even possible to suply manually connected socket o HttpWebRequest ? Or maybe a NetworkStream

C# socket communication between two servers

I have a service, which connects to the Socket Server (both are C#). The Service connects and successfully sends data to the Socket Server, but "freeze" on socket.Receive(receivedBytes); - line. This is happening when they are on different servers. When both (the Service and the Socket Server) are on the same machine - it works. In the Socket Server I opened the TCP port, inbound and outbound, on the service's server I opened the TCP port, inbound only. This is the code on the service side:
byte[] receivedBytes = new byte[1024];
IPHostEntry ipHost = Dns.GetHostEntry(socketHost);
IPAddress ipAddress = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, socketPort);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sender.Connect(ipEndPoint);
byte[] forwardMessage = Encoding.ASCII.GetBytes(msg);
sender.Send(forwardMessage);
int totalBytesReceived = sender.Receive(receivedBytes);
sender.Shutdown(SocketShutdown.Both);
sender.Close();
What I miss? Should I do any other security configurations?
Thank you!

Send packet from server to a specific client using TCP c#

I am currently writing a client-server application. The client sends a UDP broadcast message out trying to locate a server, the server sends a UDP broadcast message out identifying its location.
When a client receives a message identifying the servers location it attempts to connect to the server using socket.Connect(remoteEndPoint).
The server listens for these TCP requests on a listening socket and accepts the requests using listensocket.accept(). The clients details get stored in array (including their IP and port number)
The client sends a message to the server with information about the username and password the user entered.
The server checks the database for the username and password and depending on the result sends back a TCP message (this is where it breaks) to the client who sent the request to check the U&P using the listening socket. I am trying to use the below code to send the TCP message but it will not work as i get an error.
TCPSocket.SendTo(message, clientsArray[i].theirSocket.RemoteEndPoint);
I'm not sure about what method you are using.
But in C# there are 2 common classes that can use as server : TcpClient & Socket
in TcpClient
...
//Start Server
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start();
//Accept Client
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
String data = "Message From Server";
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
//Send to Client
stream.Write(msg, 0, msg.Length);
...
and in Socket using TCP
...
//Start Server
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress hostIP = (Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];
IPEndPoint ep = new IPEndPoint(hostIP, port);
listenSocket.Bind(ep);
listenSocket.Listen(backlog);
//Accept Client
Socket handler = listener.Accept();
String data = "Message From Server";
byte[] msg = Encoding.ASCII.GetBytes(data);
//Send to Client
handler.Send(msg);
...
and in Socket using UDP
You shouldn't use this on your server since you are using TCP
...
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);
Socket s = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
String data = "Message From Server";
byte[] msg = Encoding.ASCII.GetBytes(data);
//Send to Client by UDP
s.SendTo(msg, endPoint);
...
Normally, when you get a connection from a client, you'll get a TcpClient. That class has a method GetStream(). If you like to send some data to that client, simply write your desired data to that stream.

UdpClient.Receive(...) doesn't receive any data

I have a socket server (written on C++) which receives requests from clients and sends responses back. So, my test client application (C#) is very simple:
try {
UdpClient udpClient = new UdpClient(10241);
// Connect
udpClient.Connect(server_ip_address, 10240);
// prepare the packet to send
iWritableBuff wbuff = new iWritableBuff();
[ ... ]
// Send
udpClient.Send(wbuff.GetBuff(), (int)wbuff.Written());
// Receive a response
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
Console.WriteLine("We've got some data from the server!!! " + receiveBytes.Length + " bytes.");
udpClient.Close();
} catch (Exception e) {
Console.WriteLine("ERROR: " + e.ToString());
}
I run both applications on the same computer. Server receives a request to port 10240 from the client port 10241 and sends a response back to client port 10241, but client never receive it. So, I'm sure that server sends packet back, because everything works perfectly with C++ client. It means that I'm, doing something wrong on my C# client. Any idea?
Thanks!
P.S.> Just test it with Berkley Socket C# client:
try {
// Create socket
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Bind
IPEndPoint myEP = new IPEndPoint(IPAddress.Any, 0);
s.Bind(myEP);
// prepare the packet to send
iWritableBuff wbuff = new iWritableBuff();
[ ... ]
// Send it
IPEndPoint sEP = new IPEndPoint(IPAddress.Parse(server_ip_address), 10240);
int res = s.SendTo(wbuff.GetBuff(), (int)wbuff.Written(), 0, sEP);
// Receive the response
byte[] receiveBytes = new Byte[1024];
EndPoint recEP = new IPEndPoint(IPAddress.Any, 0);
res = s.ReceiveFrom(receiveBytes, ref recEP);
Console.WriteLine("We've got some data from the server!!! " + res + " bytes.");
} catch (Exception e) {
Console.WriteLine("ERROR: " + e.ToString());
}
And it works perfect! What's is wrong with UdpSocket?
Not sure if this applies to UDPClients that don't broadcast packets but simply send one to a specified address, but I ran into a similar roadblock.
I had a UDPClient that broadcasted a udp packet for discovery of some custom machines on our network. When I tried to receive the message that the servers would simply echo back, it would not receive the information and would timeout. Turns out that if you use a UDPClient that broadcasts a message out, it WILL NOT be able to receive messages back. It's not documented anywhere, except for one forum topic on msdn that I luckily came across.
The solution was to send the message, immediately close the socket, open a NEW UDPClient on the same port, then use this new UDPClient to receive the echoed back UDP packet. Very annoying.
Give that a shot and see if it works. It definitely works for sending out a broadcasted packet.

Get Http Response

I using Socket to get http response from www.google.com.vn
TcpClient c = new TcpClient();
IPAddress ip = IPAddress.Parse("74.125.128.94"); // www.google.com.vn
IPEndPoint remoteEP = new IPEndPoint(ip, 80);
c.Connect(remoteEP);
StreamReader sr = new StreamReader(c.GetStream());
String s = sr.ReadToEnd();
Console.WriteLine(s);
I do not get any results out. What's problem?
You're not actually making a request - you're just connecting to the port.
Either write an HTTP request to the socket, or (preferrably) use WebRequest or WebClient so that you don't end up implementing an HTTP client yourself...

Categories

Resources