#!/bin/bash set -euo pipefail umask 077 if [ "$(id -u)" != 0 ]; then echo 'Run as root, or use: curl -fsSL https://corelines.xyz/install/linux.sh | sudo bash' >&2 exit 1 fi . /etc/os-release if [ "${ID:-}" != debian ] || ! [[ "${VERSION_ID:-}" =~ ^(12|13)$ ]]; then echo 'This installer targets Debian 12/13 with systemd.' >&2 exit 1 fi if [ -e /opt/coreline ] || [ -e /var/lib/coreline/config.json ] || [ -e /etc/systemd/system/coreline.service ]; then echo 'Existing CoreLine installation found. Back up data and use coreline-update, not this fresh installer.' >&2 exit 1 fi echo 'CoreLine PBX: trusted private LAN only. No emergency calling. Web Admin uses port 8080.' echo 'This installs Debian packages and creates a coreline service account. No firewall rules are changed.' if [ -z "${CORELINE_ADVERTISE:-}" ]; then if [ ! -r /dev/tty ]; then echo 'Set CORELINE_ADVERTISE to the assigned private LAN IPv4.' >&2; exit 1; fi read -r -p 'PBX private LAN IPv4 address: ' CORELINE_ADVERTISE !Number.isInteger(n)||n<0||n>255)||!(a[0]===10||a[0]===192&&a[1]===168||a[0]===172&&a[1]>=16&&a[1]<=31)||!Object.values(os.networkInterfaces()).flat().some(n=>n?.address===ip))throw Error('Use a private IPv4 actually assigned to this machine; public VPS addresses are not accepted'); NODE stage=$(mktemp -d /tmp/coreline-install.XXXXXXXX) cleanup() { case "$stage" in /tmp/coreline-install.*) rm -rf -- "$stage" ;; esac; } trap cleanup EXIT curl --proto '=https' --tlsv1.2 --max-time 30 -fsS https://upgrade.corelines.xyz/stable.json -o "$stage/feed.json" cat > "$stage/public.pem" <<'PUBLICKEY' -----BEGIN PUBLIC KEY----- MCowBQYDK2VwAyEA2S5ettAjZ3bSe9I6U+pLNUx1RjM3bhsykFVG41Nxgzs= -----END PUBLIC KEY----- PUBLICKEY node - "$stage" <<'NODE' const fs=require('fs'),crypto=require('crypto'),path=require('path'),root=process.argv[2],e=JSON.parse(fs.readFileSync(path.join(root,'feed.json'))); if(typeof e.payload!=='string'||e.payload.length>24000||!crypto.verify(null,Buffer.from(e.payload,'base64'),fs.readFileSync(path.join(root,'public.pem')),Buffer.from(e.signature||'','base64')))throw Error('Invalid release signature'); const d=JSON.parse(Buffer.from(e.payload,'base64')),u=new URL(d.debian.url); if(!/^\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(d.version)||!Number.isFinite(Date.parse(d.expires))||Date.parse(d.expires)Date.now()+600000||u.protocol!=='https:'||u.hostname!=='upgrade.corelines.xyz'||u.port||u.username||u.password||!Number.isSafeInteger(d.debian.bytes)||d.debian.bytes<1||d.debian.bytes>150000000||!/^[a-f0-9]{64}$/.test(d.debian.sha256))throw Error('Invalid or expired release'); fs.writeFileSync(path.join(root,'release.json'),JSON.stringify(d));fs.writeFileSync(path.join(root,'url'),d.debian.url); NODE curl --proto '=https' --tlsv1.2 --max-time 120 --max-filesize 150000000 -fsS "$(<"$stage/url")" -o "$stage/runtime.tar.gz" node - "$stage" <<'NODE' const fs=require('fs'),crypto=require('crypto'),path=require('path'),r=process.argv[2],d=JSON.parse(fs.readFileSync(path.join(r,'release.json'))),b=fs.readFileSync(path.join(r,'runtime.tar.gz')); if(b.length!==d.debian.bytes||crypto.createHash('sha256').update(b).digest('hex')!==d.debian.sha256)throw Error('Release checksum mismatch'); NODE tar -xzf "$stage/runtime.tar.gz" -C "$stage" version=$(node -p 'JSON.parse(require("fs").readFileSync(process.argv[1])).version' "$stage/release.json") bundle="$stage/coreline-debian-$version" test -f "$bundle/runtime/server.js" id coreline >/dev/null 2>&1 || useradd --system --home-dir /var/lib/coreline --shell /usr/sbin/nologin coreline install -d -m 0755 /opt/coreline/releases /opt/coreline/tools mv "$bundle" "/opt/coreline/releases/$version" chown -R root:root "/opt/coreline/releases/$version" find "/opt/coreline/releases/$version" -type d -exec chmod 0755 {} + find "/opt/coreline/releases/$version" -type f -exec chmod 0644 {} + cp -a "/opt/coreline/releases/$version/tools/." /opt/coreline/tools/ ln -s "/opt/coreline/releases/$version/runtime" /opt/coreline/current install -d -o coreline -g coreline -m 0700 /var/lib/coreline for folder in admin-data logs voicemail-data prompts; do install -d -o coreline -g coreline -m 0700 "/var/lib/coreline/$folder"; done cp /opt/coreline/current/prompts/*.wav /var/lib/coreline/prompts/ node - <<'NODE' const fs=require('fs'),crypto=require('crypto'),ip=process.env.CORELINE_ADVERTISE; fs.writeFileSync('/var/lib/coreline/config.json',JSON.stringify({bind:ip,advertise:ip,adminPort:8080,sipPort:5060,sccpPort:2000,statusBind:'127.0.0.1',statusPort:8088,rtpMin:18000,rtpMax:18200,provisioning:{enabled:false,httpPorts:[84,6970],tftpPort:69},accounts:[{number:'1000',name:'CoreLine',password:crypto.randomBytes(24).toString('base64url')}]},null,2),{mode:0o600}); NODE chown -R coreline:coreline /var/lib/coreline install -m 0644 "/opt/coreline/releases/$version/coreline.service" /etc/systemd/system/coreline.service printf '%s\n' '#!/bin/sh' 'exec /usr/bin/node /opt/coreline/tools/update.cjs "$@"' > /usr/local/bin/coreline-update chmod 0755 /usr/local/bin/coreline-update systemctl daemon-reload systemctl enable --now coreline healthy=no for attempt in {1..30}; do if curl -fsS --max-time 2 http://127.0.0.1:8088/ >/dev/null; then healthy=yes; break; fi sleep 1 done if [ "$healthy" != yes ]; then echo "CoreLine did not become healthy; inspect: journalctl -u coreline -n 50" >&2; exit 1; fi echo "Installed CoreLine $version. Web Admin: http://$CORELINE_ADVERTISE:8080/admin" echo 'Initial login: sudo cat /var/lib/coreline/admin-data/initial-login.txt (wait a few seconds for first startup).' echo 'Logs: journalctl -u coreline -f. Update check: sudo coreline-update.'