Move general refactoring for constants

This commit is contained in:
Muaz Ahmad 2024-05-13 13:28:33 +05:00
parent 5b05de3ab3
commit f8c4545ea6
4 changed files with 28 additions and 19 deletions

7
src/consts.zig Normal file
View file

@ -0,0 +1,7 @@
pub const ENV_KEY_OLD_HOSTS_PATH = "OLD_HOSTS_PATH";
pub const ENV_KEY_TMP_HOSTS_PATH = "TMP_HOSTS_PATH";
pub const ENV_KEY_TARGET_DOMAIN = "TARGET_DOMAIN";
pub const ENV_KEY_SUBDOMAIN_LIST = "SUBDOMAIN_LIST";
pub const FILE_LINE_BUFF_SIZE = 100;
pub const MSG_BUFF_SIZE = 200;

View file

@ -3,7 +3,7 @@ const util = @import("util.zig");
const hosts_header = "# local-etc-hosts-updater"; const hosts_header = "# local-etc-hosts-updater";
const FILE_LINE_BUFF_SIZE = 100; const FILE_LINE_BUFF_SIZE = util.consts.FILE_LINE_BUFF_SIZE;
pub fn update_hosts(ip: util.IP, domain: util.Domain) !void { pub fn update_hosts(ip: util.IP, domain: util.Domain) !void {
try create_tmp_hosts(ip, domain); try create_tmp_hosts(ip, domain);
@ -22,7 +22,7 @@ inline fn check_purge(line: []u8, purging: *bool, skip_last_line: *bool) void {
} }
fn purge_existing(old_hosts: std.fs.File, tmp_hosts: std.fs.File) !void { fn purge_existing(old_hosts: std.fs.File, tmp_hosts: std.fs.File) !void {
var buff = [_]u8{0x00} ** FILE_LINE_BUFF_SIZE; var buff: [FILE_LINE_BUFF_SIZE]u8 = undefined;
var buff_stream = std.io.fixedBufferStream(&buff); var buff_stream = std.io.fixedBufferStream(&buff);
const buff_reader = buff_stream.reader(); const buff_reader = buff_stream.reader();
const buff_writer = buff_stream.writer(); const buff_writer = buff_stream.writer();
@ -63,9 +63,9 @@ fn purge_existing(old_hosts: std.fs.File, tmp_hosts: std.fs.File) !void {
fn write_subdomains_map(writer: std.fs.File.Writer, addr: std.net.Address, local_domain: util.Domain) !void { fn write_subdomains_map(writer: std.fs.File.Writer, addr: std.net.Address, local_domain: util.Domain) !void {
var subdomains = try get_subdomains(); var subdomains = try get_subdomains();
const domain = try util.getenv("TARGET_DOMAIN"); const domain = try util.getenv(util.consts.ENV_KEY_TARGET_DOMAIN);
var addr_buff = [_]u8{0x00} ** 50; var addr_buff: [50]u8 = undefined; // max is ~43
var addr_str = try std.fmt.bufPrint(&addr_buff, "{}", .{addr}); var addr_str = try std.fmt.bufPrint(&addr_buff, "{}", .{addr});
if (addr_str[0] == '[') { if (addr_str[0] == '[') {
addr_str = addr_str[1 .. addr_str.len - 3]; // [fd00::0000]:0 addr_str = addr_str[1 .. addr_str.len - 3]; // [fd00::0000]:0
@ -81,7 +81,7 @@ fn write_subdomains_map(writer: std.fs.File.Writer, addr: std.net.Address, local
fn append_new(tmp_hosts: std.fs.File, ip: util.IP, local_domain: util.Domain) !void { fn append_new(tmp_hosts: std.fs.File, ip: util.IP, local_domain: util.Domain) !void {
const writer = tmp_hosts.writer(); const writer = tmp_hosts.writer();
var buff = [_]u8{0x00} ** FILE_LINE_BUFF_SIZE; var buff: [FILE_LINE_BUFF_SIZE]u8 = undefined;
// write output // write output
const header_len = hosts_header.*.len; const header_len = hosts_header.*.len;
@ -101,14 +101,14 @@ fn append_new(tmp_hosts: std.fs.File, ip: util.IP, local_domain: util.Domain) !v
} }
fn get_subdomains() !std.mem.SplitIterator(u8, .scalar) { fn get_subdomains() !std.mem.SplitIterator(u8, .scalar) {
const subdomain_list = try util.getenv("SUBDOMAIN_LIST"); const subdomain_list = try util.getenv(util.consts.ENV_KEY_SUBDOMAIN_LIST);
return std.mem.splitScalar(u8, std.mem.span(subdomain_list), ' '); return std.mem.splitScalar(u8, std.mem.span(subdomain_list), ' ');
} }
fn create_tmp_hosts(ip: util.IP, domain: util.Domain) !void { fn create_tmp_hosts(ip: util.IP, domain: util.Domain) !void {
var old_hosts = try std.fs.Dir.openFileZ(util.cwd, try util.getenv("OLD_HOSTS_PATH"), .{ .mode = .read_only }); var old_hosts = try std.fs.Dir.openFileZ(util.cwd, try util.getenv(util.consts.ENV_KEY_OLD_HOSTS_PATH), .{ .mode = .read_only });
defer old_hosts.close(); defer old_hosts.close();
var tmp_hosts = try std.fs.Dir.createFileZ(util.cwd, try util.getenv("TMP_HOSTS_PATH"), .{ .truncate = true }); var tmp_hosts = try std.fs.Dir.createFileZ(util.cwd, try util.getenv(util.consts.ENV_KEY_TMP_HOSTS_PATH), .{ .truncate = true });
defer tmp_hosts.close(); defer tmp_hosts.close();
try purge_existing(old_hosts, tmp_hosts); try purge_existing(old_hosts, tmp_hosts);
@ -116,10 +116,10 @@ fn create_tmp_hosts(ip: util.IP, domain: util.Domain) !void {
} }
fn move_tmp_hosts() !void { fn move_tmp_hosts() !void {
var target_hosts = try std.fs.Dir.createFileZ(util.cwd, try util.getenv("OLD_HOSTS_PATH"), .{ .lock = .exclusive }); var target_hosts = try std.fs.Dir.createFileZ(util.cwd, try util.getenv(util.consts.ENV_KEY_OLD_HOSTS_PATH), .{ .lock = .exclusive });
defer target_hosts.close(); defer target_hosts.close();
const target_writer = target_hosts.writer(); const target_writer = target_hosts.writer();
var tmp_hosts = try std.fs.Dir.openFileZ(util.cwd, try util.getenv("TMP_HOSTS_PATH"), .{}); var tmp_hosts = try std.fs.Dir.openFileZ(util.cwd, try util.getenv(util.consts.ENV_KEY_TMP_HOSTS_PATH), .{});
defer tmp_hosts.close(); defer tmp_hosts.close();
const source_reader = tmp_hosts.reader(); const source_reader = tmp_hosts.reader();
@ -129,5 +129,5 @@ fn move_tmp_hosts() !void {
} }
}; };
try std.fs.Dir.deleteFileZ(util.cwd, try util.getenv("TMP_HOSTS_PATH")); try std.fs.Dir.deleteFileZ(util.cwd, try util.getenv(util.consts.ENV_KEY_TMP_HOSTS_PATH));
} }

View file

@ -30,7 +30,7 @@ pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP {
} }
const socket = c_int; const socket = c_int;
const MSG_BUFF_SIZE = 200; const MSG_BUFF_SIZE = util.consts.MSG_BUFF_SIZE;
fn get_mdns_socket(ip_info: util.IPInfo) !socket { fn get_mdns_socket(ip_info: util.IPInfo) !socket {
const sock = std.c.socket(switch (ip_info.version) { const sock = std.c.socket(switch (ip_info.version) {
@ -84,7 +84,7 @@ fn get_target_address(ip_info: util.IPInfo) !std.net.Address {
const target_addr: []const u8 = switch (ip_info.version) { const target_addr: []const u8 = switch (ip_info.version) {
util.IP_VER_ENUM.IPv4 => "224.0.0.251", util.IP_VER_ENUM.IPv4 => "224.0.0.251",
util.IP_VER_ENUM.IPv6 => blk: { util.IP_VER_ENUM.IPv6 => blk: {
var buf = [_]u8{0x00} ** MSG_BUFF_SIZE; var buf: [50]u8 = undefined;
var byte_buf = std.io.fixedBufferStream(&buf); var byte_buf = std.io.fixedBufferStream(&buf);
const writer = byte_buf.writer(); const writer = byte_buf.writer();
try std.fmt.format(writer, "ff02::fb%{s}", .{ip_info.interface.?}); try std.fmt.format(writer, "ff02::fb%{s}", .{ip_info.interface.?});
@ -101,7 +101,7 @@ fn send_query(domain: util.Domain, ip_info: util.IPInfo) !socket {
const addr = try get_target_address(ip_info); const addr = try get_target_address(ip_info);
var buff = [_]u8{0x00} ** MSG_BUFF_SIZE; var buff: [MSG_BUFF_SIZE]u8 = undefined;
const n = try construct_mdns_query(domain, ip_info, &buff); const n = try construct_mdns_query(domain, ip_info, &buff);
if (std.c.sendto(sock, &buff, n, std.c.MSG.DONTWAIT, &addr.any, addr.getOsSockLen()) == -1) { if (std.c.sendto(sock, &buff, n, std.c.MSG.DONTWAIT, &addr.any, addr.getOsSockLen()) == -1) {
@ -113,7 +113,7 @@ fn send_query(domain: util.Domain, ip_info: util.IPInfo) !socket {
fn receive_response(sock: socket, ip_info: util.IPInfo) !util.IP { fn receive_response(sock: socket, ip_info: util.IPInfo) !util.IP {
defer _ = std.c.close(sock); defer _ = std.c.close(sock);
var buff = [_]u8{0x00} ** MSG_BUFF_SIZE; var buff: [MSG_BUFF_SIZE]u8 = undefined;
const n: usize = blk: { const n: usize = blk: {
const ret = std.c.recv(sock, &buff, MSG_BUFF_SIZE, 0); const ret = std.c.recv(sock, &buff, MSG_BUFF_SIZE, 0);
if (ret < 0) { if (ret < 0) {
@ -173,13 +173,13 @@ fn parse_mdns_response(response: []u8, ip_info: util.IPInfo) !util.IP {
} }
if (ip_len == 4) { if (ip_len == 4) {
if (ip_bytes[0] == 192 and ip_bytes[1] == 168) { if (ip_bytes[0] == 192 and ip_bytes[1] == 168) {
var addr_buff = [_]u8{0x00} ** 4; var addr_buff: [4]u8 = undefined;
@memcpy(&addr_buff, ip_bytes); @memcpy(&addr_buff, ip_bytes);
addr = std.net.Address.initIp4(addr_buff, 0); addr = std.net.Address.initIp4(addr_buff, 0);
} }
} else if (ip_len == 16) { } else if (ip_len == 16) {
if (ip_bytes[0] == 0xfd) { if (ip_bytes[0] == 0xfd) {
var addr_buff = [_]u8{0x00} ** 16; var addr_buff: [16]u8 = undefined;
@memcpy(&addr_buff, ip_bytes); @memcpy(&addr_buff, ip_bytes);
addr = std.net.Address.initIp6(addr_buff, 0, 0, @intCast(std.c.if_nametoindex(ip_info.interface.?))); addr = std.net.Address.initIp6(addr_buff, 0, 0, @intCast(std.c.if_nametoindex(ip_info.interface.?)));
} }

View file

@ -1,5 +1,7 @@
const std = @import("std"); const std = @import("std");
pub const consts = @import("consts.zig");
const ArgError = error{ const ArgError = error{
NotEnoughArgs, NotEnoughArgs,
BadDomain, BadDomain,
@ -43,7 +45,7 @@ pub fn getenv(key: [*:0]const u8) ![*:0]const u8 {
} }
pub fn check_perms() !void { pub fn check_perms() !void {
var f = try std.fs.Dir.openFileZ(cwd, try getenv("OLD_HOSTS_PATH"), .{ .mode = .write_only }); var f = try std.fs.Dir.openFileZ(cwd, try getenv(consts.ENV_KEY_OLD_HOSTS_PATH), .{ .mode = .write_only });
f.close(); f.close();
} }
@ -76,7 +78,7 @@ pub fn get_input() !struct {
fn check_domain(domain_str: [:0]const u8) !Domain { fn check_domain(domain_str: [:0]const u8) !Domain {
var domain = Domain{ var domain = Domain{
.name = domain_str, .name = domain_str,
.labels = [_][]const u8{&[_]u8{}} ** 5, .labels = undefined,
}; };
var labels = std.mem.splitScalar(u8, domain_str, '.'); var labels = std.mem.splitScalar(u8, domain_str, '.');
var last: []const u8 = ""; var last: []const u8 = "";