This commit is contained in:
2025-07-11 19:24:43 +07:00
parent 9d07df53d0
commit 825255c64a
10 changed files with 1813 additions and 50 deletions
+251 -18
View File
@@ -37,6 +37,24 @@ check_root() {
fi
}
# Получение IP адреса сервера
get_server_ip() {
# Попытка получить основной IP адрес
local ip=$(ip route get 8.8.8.8 | awk '{print $7; exit}' 2>/dev/null)
if [[ -z "$ip" ]]; then
# Альтернативный способ
ip=$(hostname -I | awk '{print $1}' 2>/dev/null)
fi
if [[ -z "$ip" ]]; then
print_warning "Не удалось определить IP адрес, используем 192.168.1.1"
ip="192.168.1.1"
fi
echo "$ip"
}
# Проверка версии Ubuntu
check_ubuntu_version() {
if [[ ! -f /etc/os-release ]]; then
@@ -110,9 +128,153 @@ copy_config_files() {
cp /etc/bird/bird6.conf /etc/bird/bird6.conf.backup.$(date +%Y%m%d_%H%M%S)
fi
# Копирование новых конфигураций
cp config/bird.conf /etc/bird/bird.conf
cp config/bird6.conf /etc/bird/bird6.conf
# Получение IP адреса сервера
local server_ip=$(get_server_ip)
print_info "Используется IP адрес: $server_ip"
# Создание правильной конфигурации BIRD IPv4
cat > /etc/bird/bird.conf << EOF
# BIRD Configuration for BGP Server
# Ubuntu 24.04
# Logging configuration
log "/var/log/bird/bird.log" all;
log stderr all;
# Router ID
router id $server_ip;
# Protocol configuration
protocol device {
scan time 10;
}
protocol direct {
disabled;
}
protocol kernel {
disabled;
}
table bgp_table;
protocol static {
table bgp_table;
}
protocol bgp {
local as 65001;
listen bgp port 179;
hold time 90;
keepalive time 30;
startup hold time 240;
# Сервер НЕ принимает маршруты от клиентов
import none;
# Сервер раздает ТОЛЬКО свои статические маршруты
export where source = RTS_STATIC;
# MD5 аутентификация (опционально)
# password "your_bgp_password";
table limit 100000;
# Фильтры: не принимаем ничего, раздаем всё
import filter {
reject;
};
export filter {
accept;
};
}
filter community_filter {
reject;
}
function add_community() {
# пусто
}
protocol pipe {
table master;
peer table bgp_table;
import all;
export none;
}
EOF
# Создание правильной конфигурации BIRD IPv6
cat > /etc/bird/bird6.conf << EOF
# BIRD IPv6 Configuration for BGP Server
# Ubuntu 24.04
# Logging configuration
log "/var/log/bird/bird6.log" all;
log stderr all;
# Router ID
router id $server_ip;
# Protocol configuration
protocol device {
scan time 10;
}
protocol direct {
disabled;
}
protocol kernel {
disabled;
}
table bgp_table;
protocol static {
table bgp_table;
}
protocol bgp {
local as 65001;
listen bgp port 179;
hold time 90;
keepalive time 30;
startup hold time 240;
# Сервер НЕ принимает маршруты от клиентов
import none;
# Сервер раздает ТОЛЬКО свои статические маршруты
export where source = RTS_STATIC;
# MD5 аутентификация (опционально)
# password "your_bgp_password";
table limit 100000;
# Фильтры: не принимаем ничего, раздаем всё
import filter {
reject;
};
export filter {
accept;
};
}
filter community_filter {
reject;
}
function add_community() {
# пусто
}
protocol pipe {
table master;
peer table bgp_table;
import all;
export none;
}
EOF
# Копирование скриптов
cp scripts/load_prefixes.py /opt/bgp-server/scripts/
@@ -124,7 +286,11 @@ copy_config_files() {
# Установка прав на выполнение для скриптов
chmod +x /opt/bgp-server/scripts/*.py
print_success "Файлы конфигурации скопированы"
# Установка правильных прав доступа к конфигурации
chown bird:bird /etc/bird/bird*.conf
chmod 644 /etc/bird/bird*.conf
print_success "Файлы конфигурации созданы и скопированы"
}
# Настройка firewall
@@ -152,15 +318,21 @@ configure_firewall() {
configure_services() {
print_info "Настройка systemd сервисов..."
# Включение автозапуска
# Включение автозапуска для bird (IPv4)
systemctl enable bird
systemctl enable bird6
# Проверка существования bird6 сервиса
if systemctl list-unit-files | grep -q bird6; then
systemctl enable bird6
print_info "BIRD6 сервис найден и включен"
else
print_warning "BIRD6 сервис не найден, будет использоваться только IPv4"
fi
# Запуск сервисов
systemctl start bird
systemctl start bird6
# Проверка статуса
# Проверка статуса bird
if systemctl is-active --quiet bird; then
print_success "BIRD (IPv4) запущен"
else
@@ -168,11 +340,18 @@ configure_services() {
systemctl status bird
fi
if systemctl is-active --quiet bird6; then
print_success "BIRD (IPv6) запущен"
# Запуск bird6 если существует
if systemctl list-unit-files | grep -q bird6; then
systemctl start bird6
if systemctl is-active --quiet bird6; then
print_success "BIRD (IPv6) запущен"
else
print_error "Ошибка запуска BIRD (IPv6)"
systemctl status bird6
fi
else
print_error "Ошибка запуска BIRD (IPv6)"
systemctl status bird6
print_warning "BIRD6 не запущен (сервис не найден)"
fi
}
@@ -185,18 +364,57 @@ check_configuration() {
print_success "Конфигурация BIRD (IPv4) корректна"
else
print_error "Ошибка в конфигурации BIRD (IPv4)"
exit 1
print_info "Попытка создания простой конфигурации..."
create_simple_config
if bird -p /etc/bird/bird.conf; then
print_success "Простая конфигурация BIRD (IPv4) создана"
else
print_error "Не удалось создать рабочую конфигурацию BIRD (IPv4)"
exit 1
fi
fi
# Проверка синтаксиса IPv6 конфигурации
if bird6 -p /etc/bird/bird6.conf; then
print_success "Конфигурация BIRD (IPv6) корректна"
if command -v bird6 &> /dev/null; then
if bird6 -p /etc/bird/bird6.conf; then
print_success "Конфигурация BIRD (IPv6) корректна"
else
print_warning "Ошибка в конфигурации BIRD (IPv6), будет использоваться только IPv4"
fi
else
print_error "Ошибка в конфигурации BIRD (IPv6)"
exit 1
print_warning "BIRD6 не установлен, пропускаем проверку IPv6"
fi
}
# Создание простой конфигурации
create_simple_config() {
local server_ip=$(get_server_ip)
print_info "Создание простой конфигурации BIRD с IP: $server_ip"
cat > /etc/bird/bird.conf << EOF
# Простая конфигурация BIRD для BGP сервера
# Ubuntu 24.04
log "/var/log/bird/bird.log" all;
log stderr all;
router id $server_ip;
protocol device { scan time 10; }
protocol direct { disabled; }
protocol kernel { disabled; }
protocol static {}
protocol bgp {
local as 65001;
listen bgp port 179;
import none;
export where source = RTS_STATIC;
import filter { reject; };
export filter { accept; };
}
EOF
chown bird:bird /etc/bird/bird.conf
chmod 644 /etc/bird/bird.conf
print_success "Простая конфигурация создана"
}
# Создание systemd таймера для автоматической загрузки префиксов
create_systemd_timer() {
print_info "Создание systemd таймера для загрузки префиксов..."
@@ -270,7 +488,13 @@ final_check() {
print_info "Статус сервисов:"
systemctl status bird --no-pager -l
echo ""
systemctl status bird6 --no-pager -l
# Проверка bird6 если существует
if systemctl list-unit-files | grep -q bird6; then
systemctl status bird6 --no-pager -l
else
print_warning "BIRD6 сервис не установлен"
fi
# Проверка прослушивания порта
if netstat -tlnp | grep :179; then
@@ -279,6 +503,15 @@ final_check() {
print_warning "BGP порт 179 не прослушивается"
fi
# Проверка конфигурации
echo ""
print_info "Проверка конфигурации:"
if birdc show protocols 2>/dev/null; then
print_success "BIRD отвечает на команды"
else
print_warning "BIRD не отвечает на команды (возможно, нужно время для запуска)"
fi
# Проверка firewall
echo ""
print_info "Статус firewall:"