Skip to content

Instantly share code, notes, and snippets.

@dgl
Created July 24, 2026 01:51
Show Gist options
  • Select an option

  • Save dgl/d69b784e97ca07bace419de846211b8b to your computer and use it in GitHub Desktop.

Select an option

Save dgl/d69b784e97ca07bace419de846211b8b to your computer and use it in GitHub Desktop.
xc
#!/bin/sh
# xc.gd uploader; works on OpenBSD base system and other systems with curl+perl
# or nc available.
#
# Usage:
# xc [file [alternative filename]]
#
# SPDX-License-Identifier: 0BSD
if type curl >/dev/null; then
curl --no-progress-meter --netrc-optional -${DEBUG:+v}T "${1:--}" \
https://xc.gd/"$(perl -E \
'say shift =~ s/([^\w.])/sprintf "%%%02x", ord $1/egr' "$2")"
else
tls=-c
port=443
# Grep the usage output to figure out if TLS (-c) is supported,
# fallback to HTTP if not.
if ! nc 2>&1 | grep -q -- '-[0-9A-Za-z]*c'; then
echo "Your netcat does not support TLS, falling back to plain HTTP" >&2
echo "(Consider installing curl)" >&2
tls=
port=80
fi
file="$(mktemp)"
cat "${1:--}" > "$file"
size=$(stat -f '%z' "$file")
{
printf 'PUT /%s HTTP/1.0\r\nContent-type: text/plain\r\nHost: xc.gd\r\n' \
"$(basename "${2-$1}" | sed -E 's/[^A-Z0-9a-z._-]//g')"
printf 'Connection: close\r\nContent-Length: %d\r\n\r\n' "$size"
cat $file
} | nc $tls xc.gd $port | awk 'i == 1 { print }; /^\r?$/ { i=1 }'
rm -f "$file"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment