Tự động phát hiện IPv4 public và reconcile quyền truy cập vào:
- Google Cloud Firewall Rules
- Google Cloud SQL Authorized Networks
- AWS Security Groups
Chương trình chỉ thay đổi cloud khi IP của từng provider chưa ở trạng thái mong muốn. State được lưu riêng cho từng provider, vì vậy lỗi AWS không làm GCP chạy lại và ngược lại.
- Python 3.10 trở lên
- macOS hoặc Windows nếu sử dụng trình cài autostart
- Credentials GCP/AWS có quyền tối thiểu phù hợp
git clone https://github.com/lequyettien/ip-updater.git
cd ip-updater
python -m venv .venvKích hoạt virtual environment:
# macOS/Linux
source .venv/bin/activate
# Windows PowerShell
.\.venv\Scripts\Activate.ps1Cài package và tất cả provider:
python -m pip install -e ".[all]"Tạo cấu hình:
# macOS/Linux
cp config.json.example config.json
# Windows PowerShell
Copy-Item config.json.example config.jsonSau đó sửa config.json theo tài nguyên của bạn.
{
"gcp": {
"enabled": true,
"project_id": "your-project",
"credentials_file": "gcp-credentials.json",
"firewall_rules": ["allow-office-ssh"],
"sql_instances": ["production-db"]
},
"aws": {
"enabled": true,
"profile": "default",
"region": "ap-southeast-1",
"security_groups_ssh": [
{
"group_id": "sg-xxxxxxxx",
"description": "Office SSH"
}
],
"security_groups_mysql": [],
"ports_ssh": [
{
"protocol": "tcp",
"port": 22,
"description": "SSH"
}
],
"ports_mysql": []
},
"state_file": "ip_state.json",
"lock_file": ".ip_updater.lock"
}Đặt enabled: false cho provider không sử dụng. SDK của provider bị tắt không
cần được cài.
Không commit config.json, credentials, cache hoặc state. Các file này đã nằm
trong .gitignore.
ez-ip-updater
ez-ip-updater --dry-run
ez-ip-updater --force
ez-ip-updater --verbose
ez-ip-updater --custom-ip 203.0.113.10--dry-run vẫn đọc trạng thái cloud nhưng không ghi thay đổi và không cập nhật
state local.
Cài cho user hiện tại:
ez-ip-updater-installTrình cài đặt sẽ:
- macOS: tạo LaunchAgent.
- Windows: tạo task lúc đăng nhập và task mỗi 5 phút.
- Chạy ngay khi đăng nhập và thử lại mỗi 5 phút.
- Dùng process lock để các lần chạy không chồng lên nhau.
Xem trước mà không thay đổi hệ thống:
ez-ip-updater-install --dry-runGỡ:
ez-ip-updater-uninstallNếu di chuyển hoặc xoá virtual environment/thư mục dự án, hãy gỡ autostart và cài lại để scheduler nhận đường dẫn Python mới.
- Chỉ chấp nhận IPv4 hợp lệ.
- AWS thêm quyền cho IP mới trước khi xoá IP cũ.
- Reconcile idempotent: chạy lại không tạo rule trùng.
- State tách riêng
gcp_firewall,gcp_sqlvàaws. - File cache/state được ghi atomic.
- Process lock ngăn hai scheduler cùng sửa firewall.
- Log tự rotate: tối đa 5 MB, giữ ba bản backup.
GCP hỗ trợ service-account file trong cấu hình hoặc Application Default Credentials. AWS hỗ trợ profile trong cấu hình/CLI và credential chain mặc định của Boto3.
Chỉ cấp quyền cần thiết:
- GCP: đọc/cập nhật firewall và Cloud SQL instance được cấu hình.
- AWS:
ec2:AuthorizeSecurityGroupIngress,ec2:RevokeSecurityGroupIngressvàec2:DescribeSecurityGroups.
python -m pip install -e ".[all]" pytest
python -m pytestTest tập trung vào failure mode: IP không hợp lệ, lock cạnh tranh, state từng provider, add-before-remove của AWS và reconcile IP cũ khi IP mới đã tồn tại.
ez_ip_updater/
├── autostart.py
├── cli.py
├── config.py
├── ip_service.py
├── locking.py
├── logging_utils.py
├── orchestrator.py
├── state.py
└── providers/
├── aws.py
└── gcp.py