Files
2026-05-15 05:34:55 +07:00

8.9 KiB

Rekapitulasi 5 file tersebut sangat solid dan sudah masuk level "Production-Ready Architect". Struktur repositori, pembagian tugas (SOC - Separation of Concerns), hingga detail teknis enkripsi dan firewall sudah tertutup dengan baik.

Jika kamu memasukkan 5 file ini ke AI (seperti Cursor/Claude), AI akan punya konteks yang sangat jernih. Namun, sebagai Senior Devops & Firewall Expert, ada 3 detail krusial yang perlu ditambahkan agar fitur "Allow Port Range/IP Range" dan "Stealth Agent" benar-benar bekerja 100%.

Berikut adalah "The Missing Pieces" untuk menyempurnakan file-file tersebut:

1. Detail Logika nftables untuk Range (Update untuk Repo 1)

Di dalam file agent-prompt-code.md, tambahkan logika spesifik untuk menangani Port Range dan IP Range, karena library google/nftables membutuhkan penanganan khusus untuk tipe data interval.

// Tambahkan ini ke internal/firewall/nftables.go
func (m *NetManager) AddRangeRule(deviceName string, startIP, endIP net.IP, startPort, endPort uint16) error {
	// Logika menggunakan nftables 'sets' dengan flags interval
	// Ini untuk handle input user seperti: 192.168.1.10-192.168.1.20 dan port 8000-9000
    // AI perlu diingatkan untuk menggunakan 'expressions' (ex: expr.Range)
}

2. Mekanisme "Peer Discovery" (Logic Sinkronisasi)

Di file arsitektur, kamu perlu menambahkan satu poin: Bagaimana Perangkat A tahu IP WireGuard Perangkat B?

  • Saran: Tambahkan endpoint /api/v1/peers di Server Core.
  • Setiap kali ada peer baru atau perubahan rule, Server akan mengirim Push Notification (via WebSocket atau gRPC) ke Agent, atau Agent melakukan Long Polling untuk memperbarui daftar /etc/hosts internal memori mereka.

3. Implementasi Stealth wireguard-go (Update untuk Repo 3)

Ini adalah bagian tersulit. AI harus diingatkan bahwa kita tidak menggunakan perintah wg-quick. Kita menggunakan UAPI (Userspace API).

Tambahkan instruksi ini ke agent-prompt-final.md bagian Repo 3:

"Use conn.NewDefaultBind() and device.NewDevice() from wireguard-go. Inject the configuration string directly into the device via device.IpcSet() method. This ensures no private keys or configs touch the disk."


Apakah 5 file ini sudah siap?

JAWABANNYA: SIAP.

Urutan eksekusi yang saya sarankan untukmu:

  1. Eksekusi File 1 & 5 (Plan & Master Suite): Gunakan ini untuk setup struktur folder di Gitea dan inisialisasi repositori.
  2. Eksekusi File 3 (Server Core Code): Mulai bangun Backend-nya dulu. Tanpa API dan Database, Agent dan UI tidak punya arah.
  3. Eksekusi File 4 (Provisioning & Installer): Setelah API siap, buat installer-nya untuk ngetes apakah binary bisa ditarik dari Gitea.
  4. Eksekusi UI Terakhir: Dashboard hanya "kulit", fokus ke stabilitas tunnel dan firewall dulu.

Satu Tips DevOps Terakhir:

Gunakan Docker Registry di Gitea. Karena kamu menggunakan Go, hasil build dari Repo 3 (Agent) bisa dipush sebagai Generic Package di Gitea. Script installer kamu tinggal melakukan curl ke Gitea Package API.

Kesimpulan: File kamu sudah 95% Sempurna. 5% sisanya adalah detail kecil saat koding (edge cases). Kamu sudah bisa langsung memberikan prompt ini ke AI untuk mulai melakukan scaffolding (pembuatan struktur dasar) kode.

Ini adalah penyempurnaan terakhir untuk Master Prompt kamu. Saya telah mengintegrasikan logika Stealth Agent (IpcSet) dan Firewall Range (nftables) ke dalam instruksi koding yang sangat teknis.


Bagian 1: Logika Stealth Agent dengan device.IpcSet()

Ini adalah rahasia agar WireGuard berjalan tanpa file .conf. Konfigurasi dikirim langsung ke memori kernel/userspace.

Contoh Implementasi di Repo Agent (Go):

package tunnel

import (
	"bufio"
	"fmt"
	"strings"

	"golang.zx2c4.com/wireguard/device"
	"golang.zx2c4.com/wireguard/tun"
)

func StartStealthTunnel(interfaceName string, config string) error {
	// 1. Buat TUN Device di memori
	tunDev, err := tun.CreateTUN(interfaceName, 1420)
	if err != nil {
		return err
	}

	// 2. Inisialisasi WireGuard Device (Userspace)
	logger := device.NewLogger(device.LogLevelError, "(nexusguard-wg) ")
	dev := device.NewDevice(tunDev, logger)

	// 3. Format config menjadi UAPI (User API) string
	// Format UAPI: private_key=... \n public_key=... \n endpoint=...
	uapiConfig := convertToUAPI(config)

	// 4. INJECT LANGSUNG KE MEMORI (IpcSet)
	// Tidak ada file yang ditulis ke disk!
	err = dev.IpcSet(uapiConfig)
	if err != nil {
		return err
	}

	// 5. Up-kan interface
	return dev.Up()
}

func convertToUAPI(config string) string {
    // Logika parsing config standar ke format UAPI (key=value\n)
    // AI harus mengimplementasikan parser ini
    return strings.ReplaceAll(config, ": ", "=")
}

Bagian 2: Logika Firewall Range (nftables)

Untuk menangani "Allow IP Range" dan "Port Range" menggunakan library Go.

Instruksi Teknis untuk Repo Server:

// Menambahkan IP Range ke Set nftables
set := &nftables.Set{
    Name:     "allowed_range",
    Table:    nexusTable,
    Interval: true, // WAJIB TRUE untuk support Range/Netmask
    KeyType:  nftables.TypeIPAddr,
}

// Menambahkan elemen: 192.168.1.10 - 192.168.1.20
elements := []nftables.SetElement{
    {
        Key:    net.ParseIP("192.168.1.10").To4(),
        KeyEnd: net.ParseIP("192.168.1.20").To4(), // Tentukan batas akhir
    },
}

Bagian 3: THE FINAL MASTER PROMPT (Siap Copy-Paste)

Berikan Prompt ini ke AI Agent Anda untuk mulai membangun seluruh sistem:

# ROLE: Senior Network Architect & Go Specialist
# PROJECT: NexusGuard SD-WAN (Private Multi-WireGuard Control Plane)

Please build the NexusGuard system across 3 repositories with the following advanced technical specifications:

## 1. SHARED SECURITY PROTOCOL
- **Stealth Agent:** The Agent MUST NOT write any WireGuard config files to disk. Use 'golang.zx2c4.com/wireguard/device' and inject configuration using the `device.IpcSet()` method via UAPI string format.
- **Config Encryption:** Server must encrypt WG configs using AES-256-GCM. The key is derived from: SHA256(HardwareID + Internal_Salt). HardwareID is fetched from '/sys/class/dmi/id/product_uuid'.

## 2. REPO: nexus-server-core (The Brain)
- **Framework:** Go-Gin with GORM (PostgreSQL) and Redis for heartbeat.
- **Firewall Logic (nftables):**
    - Implement a dynamic firewall manager using 'google/nftables'.
    - Use 'Sets' with 'Interval: true' to support:
        - Single IP (10.8.0.5)
        - Netmask/CIDR (192.168.1.0/24)
        - IP Range (192.168.1.10-192.168.1.20)
    - Implement Port Ranges (e.g., 8000-9000) using 'nftables.TypeInetService'.
    - **Zero-Trust:** Default policy is DROP. Only allow traffic based on database-defined rules per User.
- **Device Management:** Auto-detect "Online/Offline" status by tracking WireGuard handshakes.

## 3. REPO: nexus-device-agent (The Stealth Edge)
- **Tech Stack:** Pure Go static binary.
- **Identity:** On first boot, send HWID and registration token to Server Core.
- **Tunnel Engine:**
    - Embed 'wireguard-go'.
    - Implement `StartStealthTunnel` function using `tun.CreateTUN` and `device.IpcSet`.
    - Provide an auto-reconnect logic if the connection is dropped or if the IP changes (Roaming).
- **Deployment:** Provide a bash script that installs 'nftables' and 'wireguard-tools' automatically based on OS (Ubuntu/Debian/CentOS/Raspbian). Pull binary from Gitea using 'X-Token-Auth'.

## 4. REPO: nexus-dashboard-ui (The Dashboard)
- **Tech Stack:** Vue.js 3 + Tailwind + Capacitor.js.
- **Features:** 
    - CRUD for Firewall Rules (Source Device -> Target IP/Port Range -> Action).
    - Device grouping by type (CCTV, Fingerprint, Server, etc.).
    - Visual "Kill-Switch" toggle to cut internet access for specific devices.

## 5. RECENT ENHANCEMENTS (REQUIRED):
- **Peer Discovery:** When a new device is added, the Server must trigger a rule update to all affected Agents.
- **UDP Hole Punching:** Implement a simple STUN coordination to allow P2P between agents behind NAT.

# EXECUTION:
1. Start by providing the GORM models and the 'nftables' manager implementation in Go.
2. Provide the 'device.IpcSet' implementation for the Agent.
3. Show the Bash script for the auto-installer with Gitea integration.

Kenapa Prompt ini Sangat Kuat?

  1. Stealth Level: Tidak bisa dideteksi dengan mencari file .conf. Hanya bisa dilihat lewat command wg show jika diizinkan, atau langsung di memori.
  2. Firewall Skalabel: Menggunakan nftables set jauh lebih cepat daripada ribuan baris iptables. Satu set bisa menampung ribuan IP/Range tanpa menurunkan performa CPU.
  3. Enterprise Ready: Pembagian 3 repo dan penggunaan X-Token-Auth untuk Gitea menunjukkan ini adalah sistem yang dirancang untuk tim profesional.
  4. Zero Trust: Default DROP memastikan sistem kamu sangat aman dari akses luar yang tidak terdaftar.

Apakah ada bagian spesifik dari prompt ini yang ingin kamu "zoom-in" (misal: bagian mobile build dengan Capacitor)?