1#!/bin/bash
2set -e
3
4# ── setup ────────────────────────────────────────────────────
5RED='\033[0;31m'
6GREEN='\033[0;32m'
7YELLOW='\033[1;33m'
8BLUE='\033[0;34m'
9NC='\033[0m'
10
11cleanline() { printf '\033[1A\033[2K\r'; }
12ok() { echo -e "${GREEN}✓${NC} $1"; }
13info() { echo -e "${BLUE}→${NC} $1"; sleep 0.5 ; cleanline; }
14warn() { echo -e "${YELLOW}⚠${NC} $1"; }
15fail() { echo -e "${RED}✗${NC} $1"; exit 1; }
16
17echo ""
18echo -e "${GREEN}initiating dnsense..."
19echo ""
20# ──────────────────────────────────────────────────────
21
22info "checking go..."
23if ! command -v go &> /dev/null; then
24 fail "go not found. install from https://go.dev/dl"
25fi
26GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//')
27REQUIRED="1.25"
28if [ "$(printf '%s\n' "$REQUIRED" "$GO_VERSION" | sort -V | head -n1)" != "$REQUIRED" ]; then
29 fail "go $REQUIRED+ required, found $GO_VERSION"
30fi
31
32ok "go $GO_VERSION"
33
34info "checking ollama..."
35if ! command -v ollama &> /dev/null; then
36 warn "ollama not found. installing..."
37 curl -fsSL https://ollama.com/install.sh | sh
38 ok "ollama installed"
39fi
40
41if ! curl -sf http://localhost:11434/api/tags > /dev/null 2>&1; then
42 warn "ollama not running."
43 info "starting ollama service..."
44 ollama serve > /tmp/ollama.log 2>&1 &
45 OLLAMA_PID=$!
46 echo $OLLAMA_PID > /tmp/ollama.pid
47
48 ATTEMPTS=0
49 until curl -sf http://localhost:11434/api/tags > /dev/null 2>&1; do
50 sleep 1
51 ATTEMPTS=$((ATTEMPTS + 1))
52 if [ $ATTEMPTS -ge 30 ]; then
53 fail "ollama failed to start. check /tmp/ollama.log"
54 fi
55 done
56 ok "ollama started (pid $OLLAMA_PID)"
57else
58 ok "ollama already running"
59fi
60
61MODEL="llama3-chatqa:8b"
62info "checking model $MODEL..."
63if ! ollama list 2>/dev/null | grep -q "llama3-chatqa"; then
64 warn "model not found. pulling $MODEL (~5GB, once only)..."
65 ollama pull $MODEL
66 ok "model $MODEL ready"
67else
68 ok "model $MODEL is present"
69fi
70
71info "building dnsense..."
72if ! go build -ldflags="-s -w" -trimpath -o dnsense .; then
73 fail "build failed"
74fi
75ok "dnsense build successful ($(du -sh dnsense | cut -f1) binary)"
76
77PORT=${DNS_PORT:-8053}
78info "checking port $PORT..."
79if lsof -i UDP:$PORT > /dev/null 2>&1; then
80 fail "port $PORT already in use. set DNS_PORT=<other> to override"
81fi
82ok "port $PORT is free"
83
84echo ""
85echo -e "${GREEN}checks passed. launching dnsense${NC}"
86echo ""
87
88DNS_PORT=$PORT exec ./dnsense