srctree

Gregory Mullen parent b00b9253 d3a4109c
add browser age when botdetection is enabled

inlinesplit
build.zig added: 47, removed: 16, total 31
@@ -7,6 +7,7 @@ pub fn build(b: *std.Build) !void {
const ver = version(b);
const options = b.addOptions();
options.addOption([]const u8, "version", ver);
options.addOption(bool, "botdetection", true);
 
const verse_lib = b.addModule("verse", .{
.root_source_file = b.path("src/verse.zig"),
@@ -14,14 +15,14 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
 
verse_lib.addOptions("build_options", options);
verse_lib.addOptions("verse_buildopts", options);
 
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/verse.zig"),
.target = target,
.optimize = optimize,
});
lib_unit_tests.root_module.addOptions("build_options", options);
lib_unit_tests.root_module.addOptions("verse_buildopts", options);
 
var compiler = Compiler.init(b);
 
 
src/bot-detection.zig added: 47, removed: 16, total 31
@@ -9,12 +9,24 @@ const BotDetection = @This();
pub fn init() BotDetection {}
 
pub const Browsers = struct {
const Date = i64;
const VerDate = struct { u16, Date };
const browser_count = @typeInfo(UA.Browser.Name).@"enum".fields.len;
pub const Versions: [browser_count][]const Date = .{
&Chrome.Version.Dates,
&.{},
&.{},
&.{},
&.{},
&.{},
&.{},
&.{},
};
pub const Chrome = struct {
pub const Version = enum(u16) {
_,
};
pub const Versions = struct {
const pairs = [_]struct { u16, f64 }{
 
pub const VerDates = [_]VerDate{
.{ 0, 1227513600 }, .{ 1, 1228982400 }, .{ 2, 1243148400 }, .{ 3, 1255330800 },
.{ 4, 1264406400 }, .{ 5, 1274425200 }, .{ 6, 1283410800 }, .{ 7, 1287644400 },
.{ 8, 1291276800 }, .{ 9, 1296720000 }, .{ 10, 1299571200 }, .{ 11, 1303887600 },
@@ -51,10 +63,10 @@ pub const Browsers = struct {
.{ 128, 1723618800 }, .{ 129, 1726038000 }, .{ 130, 1728457200 }, .{ 131, 1730880000 },
.{ 132, 1736323200 }, .{ 133, 1738137600 }, .{ 134, 1740556800 },
};
pub const release_date: [pairs.len]i64 = brk: {
var list: [pairs.len]i64 = @splat(0);
pub const Dates: [VerDates.len]Date = brk: {
var list: [VerDates.len]Date = @splat(0);
 
for (pairs) |line| {
for (VerDates) |line| {
std.debug.assert(list[line[0]] == 0);
list[line[0]] = line[1];
}
@@ -66,10 +78,11 @@ pub const Browsers = struct {
 
test Browsers {
_ = &Browsers.Chrome.Version;
try std.testing.expectEqual(Browsers.Chrome.Versions.release_date[93], 1630393200);
try std.testing.expectEqual(Browsers.Chrome.Version.Dates[93], 1630393200);
//for (0.., Browsers.Chrome.Versions.release) |i, date| {
// std.debug.print("{} on {}\n", .{ i, date });
//}
}
 
const std = @import("std");
const UA = @import("user-agent.zig");
 
src/frame.zig added: 47, removed: 16, total 31
@@ -398,5 +398,5 @@ const Websocket = @import("websocket.zig");
const Error = @import("errors.zig").Error;
const NetworkError = @import("errors.zig").NetworkError;
 
const build_options = @import("build_options");
const build_version = build_options.version;
const verse_buildopts = @import("verse_buildopts");
const build_version = verse_buildopts.version;
 
src/user-agent.zig added: 47, removed: 16, total 31
@@ -140,6 +140,20 @@ pub const Browser = struct {
version: u32,
version_string: []const u8 = "",
 
pub fn age(b: Browser) !i64 {
if (comptime !verse_buildopts.botdetection) @compileError("Bot Detection is currently disabled");
const versions = BotDetection.Browsers.Versions[@intFromEnum(b.name)];
if (b.version >= versions.len) return error.UnknownVersion;
return std.time.timestamp() - versions[b.version];
}
 
test age {
if (!verse_buildopts.botdetection) return error.SkipZigTest;
const browser = Browser{ .name = .chrome, .version = 134 };
try std.testing.expect(try browser.age() < 86400 * 3650); // breaks in 10 years, good luck future me!
try std.testing.expect(try browser.age() > 3148551);
}
 
pub const Name = enum {
chrome,
edge,
@@ -150,6 +164,8 @@ pub const Browser = struct {
brave,
ladybird,
};
 
test Name {}
};
 
pub const Script = enum {
@@ -168,12 +184,13 @@ pub fn init(ua_str: []const u8) UserAgent {
const Request = @import("request.zig");
const BotDetection = @import("bot-detection.zig");
 
test "ua" {
test UserAgent {
std.testing.refAllDecls(@This());
_ = &BotDetection;
}
 
const std = @import("std");
const verse_buildopts = @import("verse_buildopts");
const startsWith = std.mem.startsWith;
const endsWith = std.mem.endsWith;
const indexOf = std.mem.indexOf;