check protocol

This commit is contained in:
Shawn Jones 2024-07-17 16:44:37 +08:00
parent 0e73e35e5c
commit af15a24d3e

View File

@ -3,27 +3,80 @@ const log = std.log;
const net = std.net;
const ArrayList = std.ArrayList;
pub fn handleClient(allocator: std.mem.Allocator, server: *net.Server) !void {
var full_message = ArrayList(u8).init(allocator);
defer full_message.deinit();
pub const Protocol = enum {
HTTP,
SSH,
// DNS,
// FTP,
// SMTP,
// IMAP,
// POP3,
Unknown,
};
const ProtocolMap = struct {
prefix: []const u8,
protocol: Protocol,
};
const ProtocolMappings = [_]ProtocolMap{
.{ .prefix = "HTTP/1.1", .protocol = .HTTP },
.{ .prefix = "GET", .protocol = Protocol.HTTP },
.{ .prefix = "HEAD", .protocol = Protocol.HTTP },
.{ .prefix = "POST", .protocol = Protocol.HTTP },
.{ .prefix = "PUT", .protocol = Protocol.HTTP },
.{ .prefix = "DELETE", .protocol = Protocol.HTTP },
.{ .prefix = "CONNECT", .protocol = Protocol.HTTP },
.{ .prefix = "OPTIONS", .protocol = Protocol.HTTP },
.{ .prefix = "TRACE", .protocol = Protocol.HTTP },
.{ .prefix = "PATCH", .protocol = Protocol.HTTP },
.{ .prefix = "SSH-", .protocol = Protocol.SSH },
};
/// ### check protocol
///
/// ### author
/// * Shawn Jones
fn detectApplicationLayerProtocol(data: []u8) Protocol {
for (ProtocolMappings) |mapping| {
if (std.mem.startsWith(u8, data, mapping.prefix)) {
return mapping.protocol;
}
}
return Protocol.Unknown;
}
pub fn handleClient(_: std.mem.Allocator, server: *net.Server) !void {
// var full_message = ArrayList(u8).init(allocator);
// defer full_message.deinit();
while (true) {
const client = try server.accept();
// log.info("has a client connected: {any}", .{client});
defer client.stream.close();
// [TODO 1] check protocol
var prefix: [10]u8 = undefined;
_ = try client.stream.read(&prefix);
log.info("prefix is {s}\n", .{prefix});
log.info("{?}\n", .{@TypeOf(prefix)});
const protocol: Protocol = detectApplicationLayerProtocol(&prefix);
log.info("the client using protocol is {?}\n", .{protocol});
if (protocol == Protocol.Unknown) {
log.err("Unsupport protocol!", .{});
continue;
}
// [TODO 2] forward data
// accept from client
const client = try server.accept();
log.info("has a client connected: {any}", .{client});
defer client.stream.close();
var message: [4]u8 = undefined;
var total_len: usize = 0;
r: while (true) {
const bytesRead = try client.stream.readAll(&message);
if (bytesRead == 0) break :r;
total_len += bytesRead;
std.debug.print("Received: {s}\n", .{message[0..bytesRead]});
try full_message.appendSlice(message[0..bytesRead]);
}
log.info("Full Message: {s}\n", .{full_message.items});
// var message: [4]u8 = undefined;
// var total_len: usize = 0;
// r: while (true) {
// const bytesRead = try client.stream.readAll(&message);
// if (bytesRead == 0) break :r;
// total_len += bytesRead;
// std.debug.print("Received: {s}\n", .{message[0..bytesRead]});
// try full_message.appendSlice(message[0..bytesRead]);
// }
// log.info("Full Message: {s}\n", .{full_message.items});
}
}