Cannot connect trough proxy in C# using websocket - c#

When I try to use proxy in my web socket sharp client it says "An invalid proxy authentication challenge is specified.", even if proxy credentials are correct, if I try to set that proxy as browser proxy it works fine.
What could be the problem?
My code:
static WebSocket socket;
socket = new WebSocket("ws://" + "example.com:8080", "guacamole");
socket.OnClose += OnClose;
socket.OnOpen += OnOpen;
socket.OnMessage += OnMessage;
socket.Compression = CompressionMethod.Deflate;
socket.SetProxy("http://" + "exampleproxy.com:31112", proxyUser, proxyPass);
socket.Connect();

Related

Grpc client on Net 6 doesn't connect with grpc server on Net 4.8

When client and server work on same pc they connect fine.
Next situation is server on real pc, client on VirtualBox. Both devices have Windows 10. Firewall is disabled on server. When client tries to connet to server exception occurs:
Grpc.Core.RpcException: 'Status(StatusCode="Internal", Detail="Error
starting gRPC call.
Client code:
public ThermocyclerClient()
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var httpClientHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
var httpClient = new HttpClient(httpClientHandler);
var channel = GrpcChannel.ForAddress("http://192.168.111.189:7001", new GrpcChannelOptions { HttpClient = httpClient });
client = new ThermocyclerRpcService.ThermocyclerRpcServiceClient(channel);
}
Server code:
server = new Server
{
Ports = { new ServerPort("localhost", 7001, ServerCredentials.Insecure) },
Services =
{
ExtractorRpcService.BindService(this.extractorService),
ThermocyclerRpcService.BindService(this.thermocyclerService)
}
};
Why client can't connect to server?
Real pc and VirtualBox ping each over successfully.
The correct answer was given by Marc Gravell in comments. Changing from localhost to computer name has helped. "0.0.0.0" in host name also works (thanks to zanseb).

C# WebSocket - Unable to connect to C# FleckWebSocket

I can't connect to my WebSocket C# Server with a C# Application with websocket.
I have a server with Fleck Websocket Server.
Before Windows update my app connected to my server without problem. (Not sure if Windows update is the problem) But this update make me update the TLS of the server to TLS 1.2 .
This is the code that allowed me to connect to the server (before the bug) :
using (var socket = new ClientWebSocket())
try
{
await socket.ConnectAsync(new Uri(string.Format("wss://myip:myport", ip, port)), CancellationToken.None);
webSocket = socket;
server = string.Format("{0}:{1}", ip, port);
SocketConnected = true;
OnSocketStateChanged?.Invoke(socket, new SocketStateChangeEventArgs() { Server = string.Format("{0}:{1}", ip, port) });
await Receive(socket);
}
catch (Exception ex)
{
SocketConnected = false;
OnSocketStateChanged?.Invoke(socket, new SocketStateChangeEventArgs() { Server = string.Format("{0}:{1}", ip, port) });
Console.WriteLine($"ERROR - {ex.Message}");
Thread.Sleep(1000);
}
} while (true);
I can still connect with a web application on my server and it works perfectly.
http://www.websocket.org/echo.html works too.
I tryied to change the connection method, so i have downloaded WebSocketSharp from nugget package for tryied another way to connect but it still doesn't work.
using (var ws = new WebSocket("wss://myip:myport"))
{
ws.OnMessage += Ws_OnMessage;
ws.OnOpen += Ws_OnOpen;
ws.OnClose += Ws_OnClose;
ws.OnError += Ws_OnError;
webSocket = ws;
ws.Connect();
}
My IP and Port are good, same values than the web Application.
I got no error in every case, but i'm still not connected, nothing on the server too.
My C# application works with .NET Framework 4.5.2.
No Firewall problem.
Thank you for your help and sorry for my bad english.

WebSocketSharp Exception: System.PlatformNotSupportedException: 'Operation is not supported on this platform.'

I am wokring on a C# application that communicates with a node.js server via a secure websocket. There is also a javascript client that connects to the same websocket hosted on the node server. The javascript client connects successfully connects to the node server. However the c# application throws the error described in the title.
I used the SimpleWebSocketClient extension for chrome to connect to my socket. This works fine.
I also tried websocket4net for the C# application. This doesn't work either.
I used the wss://echo.websocket.org websocket with my c# application and it works fine. However I have to set _socket.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.tls12;
The Node Server
const app = require('express')()
const fs = require("fs"); // file system core module
var https = require("https").createServer({
cert: fs.readFileSync(__dirname + "/certs/cert.pem"),
key: fs.readFileSync(__dirname + "/certs/key.pem")
}, app)
const express = require("express"); // web framework external module
// Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
app.use(express.static(__dirname + "/static/"));
const ws = require('ws')
const wss = new ws.Server({
server: https
});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send('reply from server : ' + message)
});
ws.send('something');
});
// Listen on port 8080
https.listen(8080, () => {
console.log('listening on https://localhost:8080')
})
And here is the C# Code
_socket = new WebSocket("wss://127.0.0.1:8080");
_id = Guid.NewGuid().ToString();
_socket.OnOpen += (sender, e) =>
{
Console.WriteLine("WebSocket Connection is now Open with ID: " + _id);
};
_socket.OnMessage += (sender, e) =>
{
Console.WriteLine("On Message");
Console.WriteLine(e.Data);
};
_socket.Connect();
I'm confused why my C# application doesn't work. It should be able to build a connection just like with wss://echo.websocket.org

Cannot connect to SSL TCP socket using TLS in node.js

I'm making a api service which connects to a SSL TCP server to retrieve data. I first made this api in C# and it works. Later I ported it to node.js to take the advantages of node.js. However I couldn't connect to the server.
Target :
Host : prodtw.lol.garenanow.com
Port : 2099
Code in C# which can connect to the server and works perfectly:
TcpClient client;
try
{
client = new TcpClient("prodtw.lol.garenanow.com", 2099);
Console.WriteLine("Connected to server.");
}
catch
{
Console.WriteLine("Error");
return;
}
SslStream sslStream = new SslStream(client.GetStream(), false, AcceptAllCertificates); //AcceptAllCertificate does nothing, just return true
var ar = sslStream.BeginAuthenticateAsClient("prodtw.lol.garenanow.com", null, null);
using (ar.AsyncWaitHandle)
{
if (ar.AsyncWaitHandle.WaitOne(-1))
sslStream.EndAuthenticateAsClient(ar);
}
// Connected, write something
byte[] handshakePacket = new byte[1537];
sslStream.Write(handshakePacket);
Code in node.js, which couldn't connect to the SSL TCP server :
var tls = require('tls');
console.log('Connecting');
var stream = tls.connect(2099, "prodtw.lol.garenanow.com", function() {
console.log('Successfully connected!'); //Never print this message.
});
After running the above node.js code, it never shows "Successfully connected". It only shows "Connecting" and stucks at tls.connect...

Connecting to Websocket server using C# client instead of Javascript

I was using Alchemy websockets for both my client and server but ran into a problem with corrupted/dropped messsages. So I'm trying out another server side implementation. I implemented the server using Fleck, and when I send messages using javascript, the server receives all the messages, solving my previous problem.
However, I need to be able to send messages to the websocket server from a C# client also. Since Fleck does not have a client side implementation in C#, I thought I'd stick with Alchemy. I left the client-side code unchanged so I thought it should just connect to the server as before, however, no messages are being received (though they are being sent according to the debugger).
Here is my server side implementation (Fleck):
private void OnStartWebSocketServer()
{
var server = new WebSocketServer("ws://localhost:11005");
server.Start(socket =>
{
socket.OnOpen = () => Console.WriteLine("Open!");
socket.OnClose = () => Console.WriteLine("Close!");
socket.OnMessage = message => OnReceive(message);
});
}
private static void OnReceive(String message)
{
UpdateUserLocation(message);
}
Here is my client side implementation (Alchemy):
class WSclient
{
WebSocketClient aClient;
public WSclient(String host, String port)
{
aClient = new WebSocketClient("ws://" + host + ":" + 11005 + "/chat")
{
OnReceive = OnReceive,
OnSend = OnSend,
OnConnect = OnConnected,
OnConnected = OnConnect,
OnDisconnect = OnDisconnect
};
aClient.Connect();
}
...
public void Send(String data)
{
aClient.Send(data);
}
I thought it might have something to do with the fact that the Alchemy client requires a channel at the end of the connection string '/chat'. However leaving it blank, or just the '/' gives an error.

Categories

Resources