Compare commits

...

4 commits

Author SHA1 Message Date
6f28d1eccf
New post 2023-11-22 13:25:10 -05:00
ac5b3b6762
Added caveat 2023-11-22 12:59:09 -05:00
dec361641e
New post 2023-11-22 12:56:33 -05:00
772c2b05df
New post 2023-11-22 10:36:33 -05:00
3 changed files with 172 additions and 0 deletions

View file

@ -0,0 +1,101 @@
---
title: "Changing /etc/hosts based on network connection"
date: 2023-11-22T09:23:09-05:00
draft: false
tags: []
math: false
medium_enabled: false
---
I use my laptop at home, university, and public locations. The IP address I use to connect to a particular resource changes depending on if I'm within the network it's hosted on or a VPN. A common solution is to have a DNS server within the VPN that all clients use. This, however, has a few issues:
- If the client has multiple VPNs connected, only one DNS server can be set.
- There may be more latency using a DNS server within a VPN than using a default one provided by the ISP.
- You may not have permission to host the DNS server within the VPN network.
To address these set of issues, we'll go over how to change `/etc/hosts` on the local client machine depending on which network it's connected to.
In this setup, we'll have a default `/etc/hosts` file. I'll show how to then *swap* it with one for a particular connection. To do this, we need a way for a script to run when NetworkManager connects or disconnects from a network.
Luckily, `NetworkManager-dispatcher` handles this for us. To get a complete understanding on how to write scripts for this, reference
```bash
man 8 networkmanager-dispatcher
```
In essence, scripts within `/etc/NetworkManger/dispatcher.d/` get executed in alphabetical order with two arguments set and a lot of environmental variables.
What we'll care about in our scripts are:
- `$1` the first argument passed to the script is the interface
- `$2` the second argument refers to the *event* being triggered.
- Possible options include: pre-up, up, pre-down, down, vpn-up, vpn-pre-up, vpn-pre-down, vpn-down, hostname, dhcp4-change, dhcp6-change, connectivity-change, reapply.
- `$CONNECTION_UUID` refers to a particular connection profile in NetworkManager. This is so we know which `/etc/hosts` file to swap with which connection.
Doing some quick in dirty tests, I found the following events were triggered when connecting to a particular network:
` dhcp4-change -> up -> connectivity-change`
And, the following events were triggered when disconnecting from a particular network:
`connectivity-change -> down`
My first instinct was to use the `connectivity-change` event, however, the `CONNECTION_UUID` variable is not set for those. Instead we'll use the `up/down` events.
For our example, here's what our default `/etc/hosts/` file will look like:
```
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.3 home-server.brandonrozek.com
10.10.10.4 home-desktop.brandonrozek.com
```
When we're connected to my home network, we'll swap my `/etc/hosts/` to look like:
```
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.30 home-server.brandonrozek.com
192.168.0.40 home-desktop.brandonrozek.com
```
The following script we'll store at `/etc/NetworkManager/dispatcher.d/swap_home.sh` which will swap the `/etc/hosts` file with the one stored at `/etc/NetworkManager/hosts.home` when I connect to my home network.
```bash
#!/usr/bin/env bash
interface=$1
event=$2
if [[ $interface != "wlp0s20f3" ]] || [[ $event != "up" ]] then
exit 0
fi
# This environmental variable is set on UP/DOWN events
if [[ $CONNECTION_UUID != "901a1b68-e622-4be6-a61f-a8dc999212b3" ]] then
exit 0
fi
cp /etc/NetworkManager/hosts.home /etc/hosts
```
In this script, you might have to replace `wlp0s20f3` to reflect the interface that you're using for connecting to the network. Additionally, you'll have to replace the `CONNECTION_UUID` with the UUID of the connection you're trying to swap under. You can use `nmcli c` to show the UUIDs for each of your network connections.
Similarly, when we disconnect from the network, we'll need to set it back to our default `/etc/hosts` file.
```bash
#!/usr/bin/env bash
interface=$1
event=$2
if [[ $interface != "wlp0s20f3" ]] || [[ $event != "down" ]] then
exit 0
fi
cp /etc/NetworkManager/hosts.default /etc/hosts
```

View file

@ -0,0 +1,53 @@
---
title: "Mirror Public Repositories"
date: 2023-11-22T12:28:13-05:00
draft: false
tags: []
math: false
medium_enabled: false
---
Git is designed to be decentralized. However, many people treat it as a centralized solution by depending on services such as GitHub. What we've seen through the [youtube-dl debacle](https://www.zdnet.com/article/riaa-blitz-takes-down-18-github-projects-used-for-downloading-youtube-videos/) is that repositories that we depend on can be taken down.
This isn't to say that GitHub is evil and that we should move to Bitbucket, Gitlab, Source Hut, etc. But this is more of a commentary on what happens when we depend on one service to host our code. Git is designed to be decentralized, we should make use of that fact!
Also, alleged illegal activity is not the only reason repositories are taken down from the Internet. Sometimes, the [developer themselves](https://www.theregister.co.uk/2016/03/23/npm_left_pad_chaos/) decide to take it down.
As individuals, we can maintain mirrors of repositories we care about. That way if it ever gets removed from a particular service, we're not out of luck.
The simplest way to go about this is to `git clone` the repositories you care about, and regularly `pull` through a cron job or systemd timer. Through systemd maybe it'll look like this:
`/etc/systemd/system/refresh-hugo.service`
```ini
[Unit]
Description=Pull latest changes from Hugo
[Service]
Type=oneshot
WorkingDirectory=/home/brandonrozek/repo/hugo
ExecStart=/usr/bin/git pull
[Install]
WantedBy=multi-user.target
```
`/etc/systemd/system/refresh-hugo.timer`
```ini
[Unit]
Description=Pull latest changes from Hugo every week
Requires=refresh-hugo.service
[Timer]
Unit=refresh-hugo.service
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
```
Alternatively, you can use a self-hosted git server instance such as [Forgejo](https://forgejo.org/) to set up pull mirrors through the migration tool. As of the time of writing, this is what I currently do with repositories at https://git.brandonrozek.com/github
I do recommend only mirroring/pulling infrequently such as weekly. We don't want to induce unnecessary load on these centralized services.

View file

@ -0,0 +1,18 @@
---
title: "On Earbuds"
date: 2023-11-22T13:13:34-05:00
draft: false
tags: []
math: false
medium_enabled: false
---
In response to [Ricard Torres post](https://ricard.blog/other/my-first-noise-cancelling-earbuds/)
I also recently bought the Google Pixel buds (August 2023) and agree with you on how magical they are. Beforehand, I primarily used the [Sony WH-100XM4 headphones](https://www.sony.com/et/electronics/headband-headphones/wh-1000xm4) for a couple years. While I still believe that those are superior in terms of audio quality, the Google Pixel buds have it beat in terms of convenience. They allow me to have one earbud in to not only enjoy whichever podcast I'm listening to at the moment, but also be able to quickly hear my fiance when she wants to strike a conversation with me.
While the Google Pixel buds are my new default. I do believe there are situations when other solutions are appropriate.
- For video calls, I often use a cheap pair of 2.5mm wired earphones. This is mainly to not include latency introduced by bluetooth.
- When I'm in an extended noisy non-interactive situation like a airline flight. I find that the Sony WH-100XM4 headphones cancels out more noise which is needed for an enjoyable experience.