diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e1ab762..8bc7cf7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -24,3 +24,10 @@ sast: stage: test include: - template: Auto-DevOps.gitlab-ci.yml + +python_tests: + stage: test + image: python:3.9-slim-buster + script: + - pip install pytest + - pytest test_policy_routing.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..7b12c81 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Policy Routing + +이 프로젝트는 정책 기반 라우팅을 구현하기 위한 Python 스크립트입니다. 이 스크립트는 특정 IP 주소에 대해 지정된 게이트웨이를 사용하여 패킷을 라우팅합니다. + +사전 조건으로는 `iproute2` 패키지가 설치되어 있어야 하며, 이 패키지는 Linux 시스템에서 네트워크 인터페이스와 라우팅 테이블을 관리하는 데 사용됩니다. +NIC 의 ip 설정이 미리 되어 있어야 합니다. + +## 기능 + +- 특정 IP 주소에 대해 지정된 게이트웨이를 사용하여 패킷 라우팅 +- 라우팅 테이블을 생성하고, 해당 테이블에 규칙을 추가하여 정책 기반 라우팅을 구현 +- 자동으로 NIC를 검색하고, 해당 NIC에 대한 라우팅 테이블을 설정 + +# 사용 방법 + +스크립트는 아래 명령어로 다운로드 받을 수 있습니다 + +```bash +wget -O policy_routing.py https://git.dmslab.xyz/dmslab/policy-routing/-/raw/main/policy_routing.py +# or +curl -o policy_routing.py https://git.dmslab.xyz/dmslab/policy-routing/-/raw/main/policy_routing.py +``` diff --git a/pbr-script-cloud-init.yaml b/pbr-script-cloud-init.yaml new file mode 100644 index 0000000..0f4ea94 --- /dev/null +++ b/pbr-script-cloud-init.yaml @@ -0,0 +1,45 @@ +#cloud-config + +write_files: + - path: /tmp/pbr-script-cloud-init.sh + permissions: '0755' + owner: root:root + content: | + #!/bin/bash + + # GitLab 스크립트 URL (공개 저장소 또는 접근 가능한 URL) + # 예시: GitLab Pages, Raw 파일 URL 등 + # private repository인 경우 인증 관련 부분을 추가해야 합니다. (아래 설명) + SCRIPT_URL="https://git.dmslab.xyz/dmslab/policy-routing/-/raw/main/policy_routing.py" + DEST_PATH="/opt/PBR/routing.py" + + # 스크립트 저장될 디렉토리 생성 (필요하다면) + mkdir -p $(dirname "${DEST_PATH}") + + echo "Downloading script from ${SCRIPT_URL}..." + # wget 또는 curl 사용 + # wget이 일반적으로 더 많이 사용됨 + if command -v wget &> /dev/null + then + wget -O "${DEST_PATH}" "${SCRIPT_URL}" + elif command -v curl &> /dev/null + then + curl -o "${DEST_PATH}" "${SCRIPT_URL}" + else + echo "Error: Neither wget nor curl found. Cannot download script." + exit 1 + fi + + if [ $? -eq 0 ]; then + echo "Script downloaded successfully to ${DEST_PATH}. Executing..." + chmod +x "${DEST_PATH}" # 실행 권한 부여 + "${DEST_PATH}" install # 스크립트 실행 + else + echo "Error: Failed to download script from ${SCRIPT_URL}." + exit 1 + fi + + echo "Script execution finished." + +runcmd: + - /tmp/pbr-script-cloud-init.sh \ No newline at end of file diff --git a/policy_routing.py b/policy_routing.py index 8ef4993..b8feab2 100644 --- a/policy_routing.py +++ b/policy_routing.py @@ -847,6 +847,7 @@ SUBSYSTEM=="net", ACTION=="move", RUN+="{SCRIPT_PATH} refresh" subprocess.run(["udevadm", "control", "--reload-rules"], check=True) subprocess.run(["systemctl", "daemon-reload"], check=True) subprocess.run(["systemctl", "enable", "policy-routing"], check=True) + subprocess.run(["systemctl", "restart", "policy-routing"], check=True) if not os.path.exists(CONFIG_FILE): with open(CONFIG_FILE, "w") as f: