pub struct HostsBuilder { /* private fields */ }Expand description
HostsBuilder manages a section of /etc/hosts file that contains a list of IP to hostname
mappings. A hosts file can have multiple sections that are distinguished by tag names.
§Examples
use hostsfile::{HostsBuilder, Result};
let mut hosts = HostsBuilder::new("dns");
hosts.add_hostname("8.8.8.8".parse().unwrap(), "google-dns1");
hosts.add_hostname("8.8.4.4".parse().unwrap(), "google-dns2");
hosts.write_to("/tmp/hosts")?;/tmp/hosts will have a section:
# DO NOT EDIT dns BEGIN
8.8.8.8 google-dns1
8.8.4.4 google-dns2
# DO NOT EDIT dns ENDAnother run of HostsBuilder with the same tag name overrides the section.
use hostsfile::{HostsBuilder, Result};
let mut hosts = HostsBuilder::new("dns");
hosts.add_hostnames("1.1.1.1".parse().unwrap(), &["cloudflare-dns", "apnic-dns"]);
hosts.write_to("/tmp/hosts")?;/tmp/hosts will have a section:
# DO NOT EDIT dns BEGIN
1.1.1.1 cloudflare-dns apnic-dns
# DO NOT EDIT dns ENDOn Windows the host file format is slightly different in this case:
# DO NOT EDIT dns BEGIN
1.1.1.1 cloudflare-dns
1.1.1.1 apnic-dns
# DO NOT EDIT dns ENDImplementations§
Source§impl HostsBuilder
impl HostsBuilder
Sourcepub fn new<S: Into<String>>(tag: S) -> Self
pub fn new<S: Into<String>>(tag: S) -> Self
Creates a new HostsBuilder with the given tag name. It corresponds to a section in the
hosts file containing a list of IP to hostname mappings.
Sourcepub fn add_hostname<S: ToString>(&mut self, ip: IpAddr, hostname: S)
pub fn add_hostname<S: ToString>(&mut self, ip: IpAddr, hostname: S)
Adds a mapping of ip to hostname. If there hostnames associated with the IP already,
the hostname will be appended to the list.
Sourcepub fn add_hostnames<I: IntoIterator<Item = impl ToString>>(
&mut self,
ip: IpAddr,
hostnames: I,
)
pub fn add_hostnames<I: IntoIterator<Item = impl ToString>>( &mut self, ip: IpAddr, hostnames: I, )
Adds a mapping of ip to a list of hostnames. If there hostnames associated with the IP
already, the new hostnames will be appended to the list.
Sourcepub fn write(&self) -> Result<bool>
pub fn write(&self) -> Result<bool>
Inserts a new section to the system’s default hosts file. If there is a section with the same tag name already, it will be replaced with the new list instead. Returns true if the hosts file has changed.
Sourcepub fn default_path() -> Result<PathBuf>
pub fn default_path() -> Result<PathBuf>
Returns the default hosts path based on the current OS.
pub fn get_temp_path(hosts_path: &Path) -> Result<PathBuf>
Sourcepub fn write_to<P: AsRef<Path>>(&self, hosts_path: P) -> Result<bool>
pub fn write_to<P: AsRef<Path>>(&self, hosts_path: P) -> Result<bool>
Inserts a new section to the specified hosts file. If there is a section with the same tag name already, it will be replaced with the new list instead.
hosts_path is the full path to write to, including the filename.
On Windows, the format of one hostname per line will be used, all other systems will use the same format as Unix and Unix-like systems (i.e. allow multiple hostnames per line).
Returns true if the hosts file has changed.