Nix-Config/modules/k3s/images/rsshfs-client.nix
2026-06-29 11:45:03 -05:00

167 lines
No EOL
4 KiB
Nix

{ pkgs, ... }:
let
rsshfsCmd = pkgs.writeShellScriptBin "rsshfs" ''
# CONFIG OPTIONS
Host="$1"
LFolder="$2"
RFolder="$3"
SSHCMD="ssh -o \"StrictHostKeyChecking=no,UserKnownHostsFile=/dev/nul\" -q -S /tmp/sshfs.sock"
TmuxName="rsshfs"
# SETUP
# Args
# 1: channel
# 2: command
ftmux() {
tmux wait-for -L "$1"
tmux send-keys -t "$TmuxName" "$2; tmux wait-for -S $1" C-m
tmux wait-for "$1" &
local wait_pid=$!
wait "$wait_pid" 2>/dev/null
tmux wait-for -U "$1"
}
pause() {
echo "[ Waiting for user input to end ]"
ftmux "pause" "read -n1 -rsp $'[ Press any key to continue ]\n'"
}
cleanup() {
trap "" SIGTERM SIGINT
tmux send-keys -t "$TmuxName" C-m
while {
$SSHCMD null "df | grep -q \"$RFolder\"" ||
$SSHCMD null "[ -d \"$RFolder\" ]";
}; do
echo "[ Trying to unmount $RFolder ]"
$SSHCMD null "umount $RFolder >/dev/null 2>&1"
$SSHCMD null "rmdir $RFolder >/dev/null 2>&1"
if {
$SSHCMD null "df | grep -q \"$RFolder\"" ||
$SSHCMD null "[ -d $RFolder ]"
}; then
echo "[ Failed to unmount $RFolder, sending you in soldier ]"
ftmux "unmount" "$SSHCMD null"
fi
done
echo "[ Unmounted $RFolder successfully! ]"
echo "[ Exiting ssh connection ]"
while [[ -e /tmp/sshfs.sock ]]; do
$SSHCMD -O exit null
if [[ -e /tmp/sshfs.sock ]]; then
echo "[ Failed to remove the ssh socket, interactive mode activate]"
ftmux "sshsock" "$SHELL"
fi
done
echo "[ Exited ssh connection ]"
tmux kill-session -t "$TmuxName"
exit 0
}
trap cleanup SIGTERM SIGINT
tmux new-session -d -s "$TmuxName" /bin/bash
# MAIN SCRIPT
echo "[ Creating ssh connection socket ]"
$SSHCMD -MfNx dev@$host
echo "[ Opening ssh reverse proxy ]"
PORT=$($SSHCMD -O forward -R 0:localhost:2222 null)
echo "[ Reverse port opened on $PORT ]"
echo "[ Pushing local $LFolder to the remote $RFolder ]"
$SSHCMD null "if [[ ! -d $RFolder ]]; then mkdir -p $RFolder; fi"
$SSHCMD null "sshfs -o \"StrictHostKeyChecking=no,UserKnownHostsFile=/dev/null\" -p $PORT localhost:$LFolder $RFolder"
pause
cleanup
echo "[ End of Line ]"
'';
rsshfsEntrypoint = pkgs.writeShellScriptBin "entrypoint" ''
if [ ! -f /tmp/ssh_host_ed25519_key ]; then
/bin/ssh-keygen -t ed25519 -f /tmp/ssh_host_ed25519_key -N ""
fi
echo "Launching OpenSSH Server (Unauthenticated Access Mode)..."
/bin/sshd -f /etc/ssh/sshd_config
sleep 1
eval "exec $@"
'';
sshdConfig = pkgs.writeTextDir "etc/ssh/sshd_config" ''
Port 22
ListenAddress 0.0.0.0
PidFile /tmp/sshd.pid
# Turn off auth
PermitEmptyPasswords yes
AuthenticationMethods none
# Turn off backup auth
UsePAM no
KbdInteractiveAuthentication no
PasswordAuthentication no
PubkeyAuthentication no
# Only allow dev account
PermitRootLogin no
AllowUsers dev
AllowTcpForwarding yes
X11Forwarding no
HostKey /tmp/ssh_host_ed25519_key
'';
in
pkgs.dockerTools.buildLayeredImage {
name = "rsshfs-client";
tag = "latest";
contents = [
pkgs.bashInteractive
pkgs.coreutils
pkgs.openssh
pkgs.tmux
sshdConfig
rsshfsCmd
rsshfsEntrypoint
];
fakeRootCommands = ''
mkdir -p ./etc
mkdir -p ./tmp
cat <<EOF > ./etc/passwd
root:x:0:0:root:/root:/bin/sh
dev:x:1000:1000:dev:/home/dev:/bin/bash
EOF
cat <<EOF > ./etc/group
root:x:0:
dev:x:1000:
EOF
cat <<EOF > ./etc/shadow
root:*:19000:0:99999:7:::
dev::19000:0:99999:7:::
EOF
mkdir -p ./var/run/sshd
chmod 755 ./var/run/sshd
mkdir -p ./home/dev/.ssh
chown -R 1000:1000 ./home/dev
'';
config = {
Entrypoint = [ "/bin/entrypoint" ];
Cmd = [ "/bin/rsshfs" "rsshfs" "/" "/containers/tmp" ];
Env = [
"PATH=/bin"
];
};
}