headscale-tailscale-replace.../create-preauth-key.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

168 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
# 팜큐(FARMQ) Pre-auth Key 생성 스크립트
# 사용법: ./create-preauth-key.sh [사용자명] [유효기간(시간)]
set -e
# 기본 설정
DEFAULT_USER="myuser"
DEFAULT_EXPIRY="24h" # 24시간
# 사용법 출력
usage() {
echo "사용법: $0 [사용자명] [유효기간]"
echo ""
echo "예시:"
echo " $0 # myuser 사용자, 24시간 유효"
echo " $0 pharmacy1 # pharmacy1 사용자, 24시간 유효"
echo " $0 pharmacy1 7d # pharmacy1 사용자, 7일 유효"
echo " $0 pharmacy1 1h # pharmacy1 사용자, 1시간 유효"
echo ""
echo "유효기간 형식: 1h, 24h, 7d, 30d 등"
}
# 색상 출력 함수
print_status() {
echo -e "\n🔧 $1"
}
print_success() {
echo -e "\n✅ $1"
}
print_error() {
echo -e "\n❌ $1"
}
print_info() {
echo -e "\n📋 $1"
}
# 사용자 존재 확인
check_user_exists() {
local username=$1
print_status "사용자 '$username' 확인 중..."
if docker exec headscale headscale users list | grep -q "$username"; then
print_info "사용자 '$username'이 존재합니다."
return 0
else
print_error "사용자 '$username'이 존재하지 않습니다."
print_info "사용자 생성 중..."
if docker exec headscale headscale users create "$username"; then
print_success "사용자 '$username'이 생성되었습니다."
else
print_error "사용자 생성에 실패했습니다."
exit 1
fi
fi
}
# 사용자 ID 가져오기
get_user_id() {
local username=$1
local user_id=$(docker exec headscale headscale users list | grep "$username" | awk '{print $1}')
if [[ -n "$user_id" ]]; then
echo $user_id
else
print_error "사용자 ID를 찾을 수 없습니다."
exit 1
fi
}
# Pre-auth key 생성
create_preauth_key() {
local username=$1
local expiry=$2
print_status "Pre-auth key 생성 중..."
print_info "사용자: $username"
print_info "유효기간: $expiry"
local user_id=$(get_user_id "$username")
print_info "사용자 ID: $user_id"
# Pre-auth key 생성 (재사용 가능, 임시 아님)
local preauth_output=$(docker exec headscale headscale preauthkeys create \
-u "$user_id" \
--expiration "$expiry" \
--reusable)
if [[ $? -eq 0 ]]; then
# Key 값 추출
local preauth_key=$(echo "$preauth_output" | grep -o '[a-f0-9]\{48\}')
if [[ -n "$preauth_key" ]]; then
print_success "Pre-auth key가 생성되었습니다!"
print_info "Key: $preauth_key"
# 클라이언트 등록 스크립트에 추가할 명령어 출력
echo ""
echo "=========================================="
echo "📋 클라이언트에서 사용할 명령어:"
echo "=========================================="
echo ""
echo "Linux/macOS:"
echo "sudo tailscale up \\"
echo " --login-server=\"https://head.0bin.in\" \\"
echo " --authkey=\"$preauth_key\" \\"
echo " --accept-routes \\"
echo " --accept-dns=false"
echo ""
echo "=========================================="
echo "📋 등록 스크립트 업데이트:"
echo "=========================================="
echo ""
echo "register-client.sh 파일의 PREAUTH_KEY 값을 다음으로 업데이트하세요:"
echo "PREAUTH_KEY=\"$preauth_key\""
echo ""
return 0
fi
fi
print_error "Pre-auth key 생성에 실패했습니다."
exit 1
}
# 기존 Pre-auth key 목록 표시
list_existing_keys() {
local username=$1
local user_id=$(get_user_id "$username")
print_info "기존 Pre-auth key 목록 (사용자: $username):"
docker exec headscale headscale preauthkeys list -u "$user_id"
}
# 메인 함수
main() {
local username="${1:-$DEFAULT_USER}"
local expiry="${2:-$DEFAULT_EXPIRY}"
echo "=========================================="
echo " 🔑 팜큐(FARMQ) Pre-auth Key 생성"
echo "=========================================="
# 도움말 요청 확인
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
usage
exit 0
fi
check_user_exists "$username"
list_existing_keys "$username"
create_preauth_key "$username" "$expiry"
echo ""
print_success "완료!"
print_info "이 key는 $expiry 동안 유효하며, 여러 번 사용할 수 있습니다."
}
# 스크립트 실행
main "$@"