Sign inSign up

weerayuthmc/cloudflare-ddns

By weerayuthmc

•Updated 8 months ago

A Dynamic DNS service for Cloudflare, supporting multiple DNS providers and IP discovery services.

Image
Networking
0

699

weerayuthmc/cloudflare-ddns repository overview

⁠Cloudflare DDNS Service

A Dynamic DNS service for Cloudflare implemented with Clean Architecture and Adapter design pattern, supporting multiple DNS providers and IP discovery services.

⁠Features

  • Clean Architecture: Separated into domain, application, and infrastructure layers
  • Adapter Pattern: Easy to extend with new DNS providers and IP discovery services
  • Multiple IP Sources: Primary and backup IP discovery services for reliability
  • Docker Support: Containerized deployment with multi-platform support
  • CI/CD Pipeline: Automated build, test, and deployment with GitHub Actions
  • Graceful Shutdown: Proper signal handling for clean shutdowns

⁠Architecture

src/
├── domain/                    # Business logic and interfaces
│   ├── entities/             # Domain entities (IpRecord, DnsRecord)
│   ├── interfaces/           # Domain interfaces (IDnsProvider, IIpDiscoveryService)
│   └── services/             # Domain services (DdnsService)
├── infrastructure/           # External concerns
│   ├── adapters/            # Implementation of domain interfaces
│   │   ├── CloudflareDnsAdapter.ts
│   │   ├── MyIpDiscoveryAdapter.ts
│   │   └── MyIpComDiscoveryAdapter.ts
│   └── config/              # Configuration management
├── application/             # Application services
│   └── DdnsApplication.ts   # Main application orchestrator
└── index.ts                # Entry point

⁠Installation

docker run -d \
  --name cloudflare-ddns \
  -e CLOUDFLARE_API_TOKEN=your_token \
  -e CLOUDFLARE_ZONE_ID=your_zone_id \
  -e CLOUDFLARE_DNS_RECORD_ID=your_record_id \
  -e CLOUDFLARE_SUB_DOMAIN_NAME=your_subdomain \
  -e CLOUDFLARE_TIME_SLEEP=60000 \
  weerayuthmc/cloudflare-ddns:latest
# Clone the repository
git clone [email protected]:9mc/tools/cloudflare-ddns.git
cd cloudflare-ddns

# Copy and configure environment variables
cp .env .env.local
# Edit .env.local with your actual Cloudflare credentials

# Start the service with Docker Compose
docker compose up -d

# View logs
docker compose logs -f

# Stop the service
docker compose down
⁠Using Node.js
# Clone the repository
git clone [email protected]:9mc/tools/cloudflare-ddns.git
cd cloudflare-ddns/src

# Install dependencies
npm install

# Build the project
npm run build

# Set environment variables (see Configuration section)
# ...

# Start the service
npm start

⁠Configuration

Set the following environment variables:

VariableDescriptionRequired
CLOUDFLARE_API_TOKENCloudflare API token with DNS edit permissionsYes
CLOUDFLARE_ZONE_IDZone ID from Cloudflare dashboardYes
CLOUDFLARE_DNS_RECORD_IDDNS record ID to updateYes
CLOUDFLARE_SUB_DOMAIN_NAMESubdomain name (e.g., home.example.com)Yes
CLOUDFLARE_TIME_SLEEPCheck interval in milliseconds (default: 60000)No
⁠GitHub Actions Configuration

For GitHub Actions deployment, see GitHub Actions Setup Guide⁠ for detailed instructions on configuring secrets and variables.

⁠Extending the Service

⁠Adding a New DNS Provider
  1. Create a new adapter implementing IDnsProvider:
import { IDnsProvider } from '../../domain/interfaces/IDnsProvider';

export class NewDnsProviderAdapter implements IDnsProvider {
  async updateDnsRecord(record: DnsRecord): Promise<DnsUpdateResult> {
    // Implementation here
  }

  getName(): string {
    return 'New DNS Provider';
  }
}
  1. Update DdnsApplication to use the new provider.
⁠Adding a New IP Discovery Service
  1. Create a new adapter implementing IIpDiscoveryService:
import { IIpDiscoveryService } from '../../domain/interfaces/IIpDiscoveryService';

export class NewIpServiceAdapter implements IIpDiscoveryService {
  async getPublicIp(): Promise<string> {
    // Implementation here
  }

  getName(): string {
    return 'New IP Service';
  }
}
  1. Update DdnsApplication to use the new service.

⁠Development

# Install dependencies
npm install

# Start development with hot reload
npm run dev

# Build the project
npm run build

# Clean build artifacts
npm run clean

⁠CI/CD

The project includes a comprehensive CI/CD pipeline with:

  • Code Quality: TypeScript compilation and linting
  • Security: Dependency vulnerability scanning
  • Docker: Multi-platform image building and publishing
  • Automated Deployment: Automatic deployment to 9mcint server on main branch pushes
  • Automated Testing: Ready for test integration

The pipeline automatically deploys to the 9mcint server when changes are pushed to the main branch, ensuring the latest version is always running in production.

⁠Docker Build

# Build for development
docker build -t weerayuthmc/cloudflare-ddns:dev .

# Build for production with multi-platform support
docker buildx build --platform linux/amd64,linux/arm64 -t weerayuthmc/cloudflare-ddns:latest .

# Using Docker Compose for local development
docker compose build

# Using Docker Compose with custom environment file
docker compose --env-file .env.local up -d

⁠License

MIT License - see LICENSE file for details.

⁠Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes following the Clean Architecture principles
  4. Ensure all tests pass
  5. Submit a pull request

Tag summary

Content type

Image

Digest

sha256:20a4fd964…

Size

67.4 MB

Last updated

8 months ago

docker pull weerayuthmc/cloudflare-ddns