check protocol
This commit is contained in:
parent
0e73e35e5c
commit
af15a24d3e
|
@ -3,27 +3,80 @@ const log = std.log;
|
||||||
const net = std.net;
|
const net = std.net;
|
||||||
const ArrayList = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
|
|
||||||
pub fn handleClient(allocator: std.mem.Allocator, server: *net.Server) !void {
|
pub const Protocol = enum {
|
||||||
var full_message = ArrayList(u8).init(allocator);
|
HTTP,
|
||||||
defer full_message.deinit();
|
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) {
|
while (true) {
|
||||||
|
const client = try server.accept();
|
||||||
|
// log.info("has a client connected: {any}", .{client});
|
||||||
|
defer client.stream.close();
|
||||||
// [TODO 1] check protocol
|
// [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
|
// [TODO 2] forward data
|
||||||
// accept from client
|
// accept from client
|
||||||
const client = try server.accept();
|
// var message: [4]u8 = undefined;
|
||||||
log.info("has a client connected: {any}", .{client});
|
// var total_len: usize = 0;
|
||||||
defer client.stream.close();
|
// r: while (true) {
|
||||||
|
// const bytesRead = try client.stream.readAll(&message);
|
||||||
var message: [4]u8 = undefined;
|
// if (bytesRead == 0) break :r;
|
||||||
var total_len: usize = 0;
|
// total_len += bytesRead;
|
||||||
r: while (true) {
|
// std.debug.print("Received: {s}\n", .{message[0..bytesRead]});
|
||||||
const bytesRead = try client.stream.readAll(&message);
|
// try full_message.appendSlice(message[0..bytesRead]);
|
||||||
if (bytesRead == 0) break :r;
|
// }
|
||||||
total_len += bytesRead;
|
// log.info("Full Message: {s}\n", .{full_message.items});
|
||||||
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});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user