How to detect a connection closure in Kestrel - c#

Once again, the Microsoft documentation leaves me wanting. I am trying to find the correct API where I can configure a callback to trap when a client closes their connection.
When I fire up a gRPC server, in the console I see all of the Kestrel configuration and startup log messages. When I fire up the gRPC client I can see in the server's console log messages indicating a connection has been made as follows:
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[39]
Connection id "0HMCEE5LHGSKR" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[1]
Connection id "0HMCEE5LHGSKR" started.
When I close the client by clicking the Close Window button (X), I see the following:
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[19]
Connection id "0HMCEE5LHGSKR" reset.
dbug: Microsoft.AspNetCore.Server.Kestrel.Http2[48]
Connection id "0HMCEE5LHGSKR" is closed. The last processed stream ID was 1.
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7]
Connection id "0HMCEE5LHGSKR" sending FIN because: "The client closed the connection."
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[2]
Connection id "0HMCEE5LHGSKR" stopped.
The option to use the ListenOptions.UseConnectionLogging(ListenOptions) extension method provides no callback option that I can find. Obviously, in the default middleware, the event is being captured, but I cannot find the path to that option. An examination of Microsoft.AspNetCore.Server.Kestrel namespace shows no way (that I can find) how to get to Microsoft.AspNetCore.Server.Kestrel.Connections or Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets data.
I am using Visual Studio 2022, .NET 6, C# 10 and gRPC. Here is my current Kestrel configuration:
// Configure Kestrel, the .NET Core web server.
var hostBuilder = webHostBuilder.ConfigureKestrel (kestrelServerOptions => {
kestrelServerOptions.ConfigureHttpsDefaults (httpsConnectionAdapterOptions => httpsConnectionAdapterOptions.SslProtocols = SslProtocols.Tls12);
// Read in the X.509 certificate file.
var certPath = Path.Combine (builder.Environment.ContentRootPath, "Certs", $"{environment}.pfx");
kestrelServerOptions.ConfigureEndpointDefaults (listenOptions => {
_ = listenOptions.UseHttps (certPath, password);
logger.Debug ($"Using {certPath} as the cert file.");
logger.Debug ("Configuring host to use HTTP/2 protocol.");
listenOptions.Protocols = HttpProtocols.Http2;
});
logger.Debug ("Reading config values for the server name and port.");
// Get the host name and port number to bind the service to.
var port = builder.Configuration.GetValue<int> ("AppSettings:OperationsServerPort");
var address = IPAddress.Parse ("0.0.0.0");
if (address != null) {
logger.Debug ($"Host will listen at https://{address}:{port}");
kestrelServerOptions.Listen (address, port);
} else {
logger.Error ("DNS address for service host cannot be determined! Exiting...");
Environment.Exit (-1);
}
});
Any clues, guidance, examples will be greatly appreciated!

Well, I may be late to the game in understanding all of this ASP.NET Core configuration, but to trap connections coming and going is tooooooo simple... Adding a middleware delegate to the listener is all it took...
var ipEndpoint = new IPEndPoint (address, port);
kestrelServerOptions.Listen (ipEndpoint, action => action.Use (async (context, next) => {
Console.WriteLine ($"INTERNAL ---------------- New Connection: {context.ConnectionId}");
await next.Invoke ();
Console.WriteLine ($"INTERNAL ---------------- Connection terminated: {context.ConnectionId}");
}));
This snippet modified my original post above by adding the middleware delegate. The required reading that got me beyond the logjam can be found here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0
I hope this helps somebody!!

Related

How to make GrpcChannel use HTTP/2 on MonoAndroid?

I am trying to make request to my server by MagicOnion protocol (it uses transport from gRPC, but deffrent serialization protocol, message pack instead of protobuf).
An simple test client app, working under net5.0 is executing code like this:
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var address = $"http://{ServerUrl.Host}:5002";
using var channel = GrpcChannel.ForAddress(address);
var myServiceClient = MagicOnionClient.Create<IMyService>(channel);
var result = await myServiceClient.GetMyData();
...and recieves response succesfully. But if I try to execute the same code on Android app, I am seeing this exception message on server logs:
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2ConnectionErrorException: HTTP/2 connection error (PROTOCOL_ERROR): Invalid HTTP/2 connection preface.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.ParsePreface(ReadOnlySequence`1& buffer, SequencePosition& consumed, SequencePosition& examined)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.TryReadPrefaceAsync()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.ProcessRequestsAsync[TContext](IHttpApplication`1 application)
With logs and traffic dump I can see that the client on .Net 5 uses HTTP/2, but on Android - HTTP/1.1. As I can see, this is the only deifference between requests.
So, how can I make Android (API30, monoandroid10.0) client use HTTP/2?
The resolution is to use another gRPCC implementation lib - Grpc.Core. It provides GrpcChannel class wich is compatible with MagicOnion.
In my case, the library didn't work immediately, throwing the error about libgrpc_csharp_ext loading trouble. To solve this, you also have to add pacakge Grpc.Core.Xamarin.
The usage example:
var channel = new Grpc.Core.Channel(ServerUrl.Host, 5002, Grpc.Core.ChannelCredentials.Insecure);
var myServiceClient = MagicOnionClient.Create<IMyService>(channel);
var result = await myServiceClient.GetMyData();

DotNetTor Cannot Make Requests

Recently I want to get a .json content from an onion site
(e.g. http://takedownmi4lfjhv.onion/root.json, this is an existing link)
So I installed the C# library DotNetTor (Nuget package is here)
I copy & pasted the DotNetTor QuickStart Example Project sample code and ran it with my C# project.
var requestUri = "http://icanhazip.com/";
// 1. Get real IP
using (var httpClient = new HttpClient())
{
var message = httpClient.GetAsync(requestUri).Result;
var content = message.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Your real IP: \t\t{content}");
}
// 2. Get TOR IP
using (var httpClient = new HttpClient(new SocksPortHandler("127.0.0.1", socksPort: 9050)))
{
var message = httpClient.GetAsync(requestUri).Result; // GOT ERROR HERE // GOT ERROR HERE
var content = message.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Your TOR IP: \t\t{content}");
// 3. Change TOR IP
var controlPortClient = new DotNetTor.ControlPort.Client("127.0.0.1", controlPort: 9051, password: "ILoveBitcoin21");
controlPortClient.ChangeCircuitAsync().Wait();
// 4. Get changed TOR IP
message = httpClient.GetAsync(requestUri).Result;
content = message.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Your other TOR IP: \t{content}");
}
However, at the line I marked // GOT ERROR HERE, it poped up an error
System.AggregateException
Inner Exception 1:
TorException: Failed to send the request
Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:9050
Why failed to send the request? How to resolve that?
The code above is simply a Socks Proxy handler and does not really contain anything about Tor.
new SocksPortHandler("127.0.0.1", socksPort: 9050)
See this link of code. This is nothing more than connecting via a SocksPortHandler 127.0.0.1:9050
But this port is not yet configured to Tor at all.
So before this, you should configure the port.
You can download the Tor Expert Bundle from here. This can help you configure that port for Tor.

Possible to have C# SSH client communicate with a NodeJS SSH Server?

I have a C# application that needs to run certain commands on a piece of hardware over SSH. The application is using SSH.Net to make the connection, send the command, and the read the result. I have this working if I connect to my local machine using OpenSSH. Finally, I wanted to go a step further and setup my own SSH server so I could simulate multiple hardware devices at one time (need to simulate having 50+ devices to SSH into).
For this I have setup a simple SSH server using nodejs and the ssh2 package. So far I have the client connected, authenticated (all connections are accepted for now), and I can see a session object being created. Although where I'm hitting a wall is with the execution of commands sent by the client. I noticed that ssh2 has an event for exec on the session object but this never seems to trigger (regardless of what i put in SSH.Net's ShellStream).
The C# client code that initiates the connection is the following (command is already defined that the command string to be executed):
using(SshClient client = new SshClient(hostname, port, username, password))
{
try
{
client.ErrorOccurred += Client_ErrorOccurred;
client.Connect();
ShellStream shellStream = client.CreateShellStream("xterm", Columns, Rows, Width, Height, BufferSize, terminalModes);
var initialPrompt = await ReadDataAsync(shellStream);
// The command I write to the stream will get executed on OpenSSH
// but not on the nodejs SSH server
shellStream.WriteLine(command);
var output = await ReadDataAsync(shellStream);
var results = $"Command: {command} \nResult: {output}";
client.Disconnect();
Console.WriteLine($"Prompt: {initialPrompt} \n{results}\n");
}
catch (Exception ex)
{
Console.WriteLine($"Exception during SSH connection: {ex.ToString()}");
}
}
The nodejs server code that set ups the ssh2 server is following:
new ssh2.Server({
hostKeys: [fs.readFileSync('host.key')]
}, function(client) {
console.log('Client connected!');
client.on('authentication', function(ctx) {
ctx.accept();
}).on('ready', function() {
console.log('Client authenticated!');
client.on('session', function(accept, reject) {
var session = accept();
// Code gets here but never triggers the exec
session.once('exec', function(accept, reject, info) {
console.log('Client wants to execute: ' + inspect(info.command));
var stream = accept();
stream.write('returned result\n');
stream.exit(0);
stream.end();
});
});
}).on('end', function() {
console.log('Client disconnected');
});
}).listen(port, '127.0.0.1', function() {
console.log('Listening on port ' + this.address().port);
});
I have seen various ssh2 client examples invoking a client.exec function but I was assuming that it did not matter that my client was not using the ssh2 node package. Is there something that I'm missing here?
The "exec" Node.js server session event is for "non-interactive (exec) command execution". By which they most likely mean SSH "exec" channel (which is intended for "non-interactive command execution").
To execute a command using "exec" SSH channel in SSH.NET, use SshClient.RunCommand.
On the contrary SshClient.CreateShellStream uses SSH "shell" channel, which is intended for implementing an interactive shell session.
For that, you need to handle "shell" Node.js server session event.

Grapevine Remote Connection Bad Request - Invalid Hostname

I set up a rest service with the grapevine, plus I'm having trouble accessing remotely even with the firewall turned off.
Are you only accepting connections through localhost or 127.0.0.1, when I try to access the IP of the machine or remotely gives this error
Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.
using (var server = new RestServer())
{
server.Port = "9999";
server.LogToConsole().Start();
Console.ReadLine();
server.Stop();
}
Edit: Please refer to the (updated) documentation, specifically the page On Using HttpListener
The current default value is localhost. You can change the directly using the Host property:
server.Host = "*";
Use "*" to indicate that the HttpListener accepts requests sent to the port if the requested URI does not match any other prefix. Similarly, to specify that the HttpListener accepts all requests sent to a port, replace the host element with the "+" character.
So, for Grapevine 4, you could write your code as follows:
using (var server = new RestServer{Port = "9999", Host = "*"})
{
server.LogToConsole().Start();
Console.ReadLine();
server.Stop();
}

Nodejs Express + Socket.io + client on C#

i just don't understand why simple socket.io part don't work.
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
app.use(function(req, res, next) {
console.log("INIT");
console.log(req.headers['user-agent']);
console.log(req.ip);
next();
});
app.use('/', routes);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
This is my cliend side code at C#. So when my nodejs server is online i don't get any errors from C#, so it's connecting, but i don't see it at node console.
And this must work, i get this example here - https://www.youtube.com/watch?v=nwV3MS6pryY
using System;
using System.Net.Sockets;
namespace TCPSocketConsole
{
class Program
{
static TcpClient mySocket = new TcpClient();
static void Main(string[] args)
{
mySocket.Connect("127.0.0.1", 3000);
Console.ReadLine();
}
}
}
So when i connect to http://localhost:3000 i don't get "a user connected" at my console.
You are listening for a socket.io connection on your server, but your client is just make a plain TCP connection. The two protocols on each end must be the same. socket.io is not a plain TCP connection.
You can either listen for a plain TCP connection on your node.js server (and thus invent your own protocol) or you can get a class for a socket.io connection in your C# client so your C# client can actually speak to a socket.io server properly.
socket.io is based on webSocket and webSocket has a whole protocol for establishing the initial connection (it starts with an HTTP connection that is then upgraded to a webSocket connection) and then both webSocket and socket.io have their own framing for how data is sent. Both ends of the connection must speak the same protocol.
In the socket.io docs you have an example of the client and server side. It seems your are not connecting from client side.

Categories

Resources