2024-05-10 13:13:59 +05:00
|
|
|
const std = @import("std");
|
|
|
|
|
2024-05-10 14:23:57 +05:00
|
|
|
const ArgError = error{
|
|
|
|
NotEnoughArgs,
|
|
|
|
BadDomain,
|
|
|
|
};
|
|
|
|
|
2024-05-10 12:49:51 +05:00
|
|
|
pub const IPs = struct {
|
|
|
|
v4: [:0]const u8,
|
|
|
|
v6: [:0]const u8,
|
|
|
|
};
|
|
|
|
|
2024-05-10 14:23:57 +05:00
|
|
|
pub const Domain = struct {
|
|
|
|
name: [:0]const u8,
|
|
|
|
labels: u8,
|
|
|
|
};
|
|
|
|
|
2024-05-10 13:13:59 +05:00
|
|
|
pub fn check_perms() !void {
|
|
|
|
var f = try std.fs.openFileAbsolute("/etc/hosts", .{ .mode = .write_only });
|
|
|
|
f.close();
|
|
|
|
}
|
2024-05-10 12:49:51 +05:00
|
|
|
|
2024-05-10 14:23:57 +05:00
|
|
|
pub fn get_input() !Domain {
|
|
|
|
var args = std.process.args();
|
|
|
|
_ = args.next();
|
|
|
|
const domain = args.next() orelse return ArgError.NotEnoughArgs;
|
|
|
|
return check_domain(domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_domain(domain_str: [:0]const u8) !Domain {
|
|
|
|
var domain = Domain{
|
|
|
|
.name = domain_str,
|
|
|
|
.labels = 0,
|
|
|
|
};
|
|
|
|
var labels = std.mem.split(u8, domain_str, ".");
|
|
|
|
var last: []const u8 = "";
|
|
|
|
while (labels.next()) |segment| {
|
|
|
|
last = segment;
|
|
|
|
domain.labels += 1;
|
|
|
|
}
|
|
|
|
if (domain.labels <= 1 or !std.mem.eql(u8, last, "local")) {
|
|
|
|
return ArgError.BadDomain;
|
|
|
|
}
|
|
|
|
return domain;
|
2024-05-10 12:49:51 +05:00
|
|
|
}
|