headscale-tailscale-replace.../register-client.sh
시골약사 53c1f45e02 🚀 Add complete client registration system for FARMQ Headscale
## New Features:
- **register-client.sh**: Automated client registration script
  - Auto-detects OS (Ubuntu/CentOS/macOS)
  - Installs Tailscale automatically
  - Registers to https://head.0bin.in with pre-auth key
  - Verifies connection and displays status

- **create-preauth-key.sh**: Pre-auth key management script
  - Creates users and pre-auth keys with custom expiration
  - Supports reusable keys for multiple devices
  - Provides ready-to-use registration commands
  - Example: `./create-preauth-key.sh pharmacy1 7d`

- **CLIENT_SETUP_GUIDE.md**: Complete installation guide
  - Automated and manual installation instructions
  - Cross-platform support (Linux/macOS/Windows/Mobile)
  - Troubleshooting section
  - Key management for admins

## Pharmacy Page Fix:
- Fix machine count display in pharmacy management page
- Update get_all_pharmacies_with_stats() to use actual Headscale Node data
- Show correct online/offline machine counts per pharmacy
- Fixed: "0대" → "2대 online" for proper machine statistics

## Key Benefits:
- **One-line registration**: `sudo ./register-client.sh`
- **Pre-auth keys work once, connect forever** - answers user's question
- **Reusable keys** for multiple devices per pharmacy
- **Cross-platform** support for all major operating systems

Current active keys:
- myuser: fc4f2dc55ee00c5352823d156129b9ce2df4db02f1d76a21
- pharmacy1: 5c15b41ea8b135dbed42455ad1a9a0cf0352b100defd241c (7d validity)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-09 18:23:04 +09:00

162 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# 팜큐(FARMQ) Headscale 클라이언트 등록 스크립트
# 사용법: ./register-client.sh
set -e
# 설정
HEADSCALE_SERVER="https://head.0bin.in"
PREAUTH_KEY="fc4f2dc55ee00c5352823d156129b9ce2df4db02f1d76a21"
# 색상 출력 함수
print_status() {
echo -e "\n🔧 $1"
}
print_success() {
echo -e "\n✅ $1"
}
print_error() {
echo -e "\n❌ $1"
}
print_info() {
echo -e "\n📋 $1"
}
# 운영체제 감지
detect_os() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if command -v apt &> /dev/null; then
OS="ubuntu"
elif command -v yum &> /dev/null; then
OS="centos"
else
OS="linux"
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
OS="windows"
else
OS="unknown"
fi
echo $OS
}
# Tailscale 설치 확인 및 설치
install_tailscale() {
OS=$(detect_os)
if command -v tailscale &> /dev/null; then
print_info "Tailscale이 이미 설치되어 있습니다."
return 0
fi
print_status "Tailscale 설치 중..."
case $OS in
"ubuntu")
curl -fsSL https://tailscale.com/install.sh | sh
;;
"centos")
curl -fsSL https://tailscale.com/install.sh | sh
;;
"macos")
echo "macOS용 Tailscale을 다운로드합니다."
echo "다음 URL에서 수동으로 설치하세요: https://tailscale.com/download/mac"
exit 1
;;
"windows")
echo "Windows용 Tailscale을 다운로드합니다."
echo "다음 URL에서 수동으로 설치하세요: https://tailscale.com/download/windows"
exit 1
;;
*)
print_error "지원되지 않는 운영체제입니다: $OSTYPE"
exit 1
;;
esac
}
# 기존 Tailscale 연결 해제
disconnect_existing() {
if tailscale status --json &> /dev/null; then
local current_status=$(tailscale status --json 2>/dev/null || echo "{}")
if echo "$current_status" | grep -q '"BackendState":"Running"'; then
print_status "기존 Tailscale 연결을 해제합니다..."
sudo tailscale logout || true
fi
fi
}
# Headscale에 등록
register_to_headscale() {
print_status "팜큐 Headscale 서버에 등록 중..."
print_info "서버: $HEADSCALE_SERVER"
# Tailscale을 Headscale 서버로 설정하고 등록
sudo tailscale up \
--login-server="$HEADSCALE_SERVER" \
--authkey="$PREAUTH_KEY" \
--accept-routes \
--accept-dns=false
}
# 연결 상태 확인
check_connection() {
print_status "연결 상태 확인 중..."
# 잠시 대기
sleep 3
# 상태 확인
if tailscale status &> /dev/null; then
local tailscale_ip=$(tailscale ip -4 2>/dev/null || echo "")
if [[ -n "$tailscale_ip" ]]; then
print_success "성공적으로 연결되었습니다!"
print_info "할당된 IP: $tailscale_ip"
print_info "네트워크 상태:"
tailscale status
return 0
fi
fi
print_error "연결에 실패했습니다."
print_info "수동으로 상태를 확인해보세요: tailscale status"
return 1
}
# 메인 함수
main() {
echo "=========================================="
echo " 🏥 팜큐(FARMQ) Headscale 클라이언트 등록"
echo "=========================================="
# 루트 권한 확인
if [[ $EUID -ne 0 ]] && ! sudo -n true 2>/dev/null; then
print_error "이 스크립트는 sudo 권한이 필요합니다."
exit 1
fi
# 단계별 실행
install_tailscale
disconnect_existing
register_to_headscale
if check_connection; then
print_success "🎉 등록 완료!"
print_info "이제 팜큐 네트워크에 연결되었습니다."
print_info "문제가 있으면 관리자에게 문의하세요."
else
print_error "등록 과정에서 문제가 발생했습니다."
exit 1
fi
}
# 스크립트 실행
main "$@"