Add subdomain constructor

This commit is contained in:
Muaz Ahmad 2024-05-13 00:28:18 +05:00
parent 93045d80f7
commit ec18ac2152
2 changed files with 13 additions and 1 deletions

View file

@ -62,14 +62,26 @@ fn append_new(tmp_hosts: std.fs.File, addr: std.net.Address) !void {
const writer = tmp_hosts.writer();
var buff = [_]u8{0x00} ** 50;
// write output
const header_len = hosts_header.*.len;
@memcpy(buff[0..header_len], hosts_header);
try writer.writeAll(buff[0..header_len]);
try writer.writeByte('\n');
var subdomains = try get_subdomains();
const domain = try util.getenv("TARGET_DOMAIN");
while (subdomains.next()) |subdomain| {
std.debug.print("{s}.{s}\n", .{ subdomain, domain });
}
// add extra final newline
try writer.writeByte('\n');
}
fn get_subdomains() !std.mem.SplitIterator(u8, .scalar) {
const subdomain_list = try util.getenv("SUBDOMAIN_LIST");
return std.mem.splitScalar(u8, std.mem.span(subdomain_list), ' ');
}
fn create_tmp_hosts(ip: util.IP) !void {
var old_hosts = try std.fs.openFileAbsoluteZ(try util.getenv("OLD_HOSTS_PATH"), .{ .mode = .read_only });
defer old_hosts.close();

View file

@ -64,7 +64,7 @@ fn check_domain(domain_str: [:0]const u8) !Domain {
.name = domain_str,
.labels = [_][]const u8{&[_]u8{}} ** 5,
};
var labels = std.mem.split(u8, domain_str, ".");
var labels = std.mem.splitScalar(u8, domain_str, '.');
var last: []const u8 = "";
var i: usize = 0;
while (labels.next()) |segment| {