|
| 1 | +name: Update Iran IP list from v2fly/geoip |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 * */2 * *' |
| 6 | + workflow_dispatch: {} |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + update-geoip: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout repo |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + persist-credentials: true |
| 20 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 21 | + |
| 22 | + - name: Get latest v2fly/geoip release tag |
| 23 | + id: get_release |
| 24 | + run: | |
| 25 | + TAG=$(curl -s https://api.github.com/repos/v2fly/geoip/releases/latest | python3 -c "import sys,json; j=json.load(sys.stdin); print(j.get('tag_name',''))") |
| 26 | + if [ -z "$TAG" ]; then |
| 27 | + echo "tag=" >> $GITHUB_OUTPUT |
| 28 | + else |
| 29 | + echo "tag=$TAG" >> $GITHUB_OUTPUT |
| 30 | + fi |
| 31 | +
|
| 32 | + - name: Read last processed tag |
| 33 | + id: read_last |
| 34 | + run: | |
| 35 | + if [ -f .github/geoip_latest.txt ]; then |
| 36 | + LAST=$(cat .github/geoip_latest.txt | tr -d '\n') |
| 37 | + else |
| 38 | + LAST="" |
| 39 | + fi |
| 40 | + echo "last=$LAST" >> $GITHUB_OUTPUT |
| 41 | +
|
| 42 | + - name: Skip if release already processed |
| 43 | + if: ${{ steps.get_release.outputs.tag == steps.read_last.outputs.last }} |
| 44 | + run: | |
| 45 | + echo "No new release. Current latest tag is '${{ steps.get_release.outputs.tag }}'. Exiting." |
| 46 | + |
| 47 | + - name: Setup Go |
| 48 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 49 | + uses: actions/setup-go@v4 |
| 50 | + with: |
| 51 | + go-version: '1.21' # یا هر نسخهای که لازم داری |
| 52 | + |
| 53 | + - name: Ensure git installed (runner already has it, this is defensive) |
| 54 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 55 | + run: sudo apt-get update && sudo apt-get install -y git curl |
| 56 | + |
| 57 | + - name: Clone v2fly/geoip |
| 58 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 59 | + run: git clone https://github.com/v2fly/geoip.git |
| 60 | + |
| 61 | + - name: Create config.json in geoip repo |
| 62 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 63 | + run: | |
| 64 | + cat > geoip/config.json <<'JSON' |
| 65 | +{ |
| 66 | + "input": [ |
| 67 | + { |
| 68 | + "type": "v2rayGeoIPDat", |
| 69 | + "action": "add", |
| 70 | + "args": { |
| 71 | + "uri": "./geoip.dat" |
| 72 | + } |
| 73 | + } |
| 74 | + ], |
| 75 | + "output": [ |
| 76 | + { |
| 77 | + "type": "text", |
| 78 | + "action": "output", |
| 79 | + "args": { |
| 80 | + "outputDir": "./", |
| 81 | + "oneFilePerList": true, |
| 82 | + "wantedList": ["ir"], |
| 83 | + "onlyIPType": "ipv4" |
| 84 | + } |
| 85 | + } |
| 86 | + ] |
| 87 | +} |
| 88 | +JSON |
| 89 | + |
| 90 | + - name: Download latest geoip.dat |
| 91 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 92 | + run: | |
| 93 | + # direct download path for the asset (GitHub supports /latest/download/<name>) |
| 94 | + curl -L -o geoip/geoip.dat "https://github.com/v2fly/geoip/releases/latest/download/geoip.dat" |
| 95 | + ls -l geoip/geoip.dat |
| 96 | +
|
| 97 | + - name: Build dependencies and run generator |
| 98 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 99 | + working-directory: geoip |
| 100 | + run: | |
| 101 | + go mod download |
| 102 | + # run generator with config.json |
| 103 | + go run ./ -c config.json |
| 104 | +
|
| 105 | + - name: Prepare one-line IR list and update target file |
| 106 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 107 | + run: | |
| 108 | + # geoip generator should have produced ./ir.txt in geoip/ |
| 109 | + if [ ! -f geoip/ir.txt ]; then |
| 110 | + echo "geoip/ir.txt not found! Exiting with error." >&2 |
| 111 | + ls -la geoip |
| 112 | + exit 2 |
| 113 | + fi |
| 114 | + # convert newlines to spaces (keep it a single-line list) |
| 115 | + tr '\r\n' ' ' < geoip/ir.txt | sed 's/ */ /g' > /tmp/ir_one_line.txt |
| 116 | + # ensure trailing newline |
| 117 | + sed -n '1p' /tmp/ir_one_line.txt > /tmp/ir_one_line_with_nl.txt |
| 118 | + printf "\n" >> /tmp/ir_one_line_with_nl.txt |
| 119 | +
|
| 120 | + # create resources dir if missing (defensive) |
| 121 | + mkdir -p resources |
| 122 | + # move to repo file |
| 123 | + mv /tmp/ir_one_line_with_nl.txt resources/pbr-iplist-iran-v4 |
| 124 | + echo "Updated resources/pbr-iplist-iran-v4 with new data." |
| 125 | +
|
| 126 | + - name: Commit & push only if changed |
| 127 | + if: ${{ steps.get_release.outputs.tag != steps.read_last.outputs.last }} |
| 128 | + run: | |
| 129 | + git config user.name "github-actions[bot]" |
| 130 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 131 | +
|
| 132 | + # record new processed tag |
| 133 | + echo "${{ steps.get_release.outputs.tag }}" > .github/geoip_latest.txt |
| 134 | + git add resources/pbr-iplist-iran-v4 .github/geoip_latest.txt |
| 135 | +
|
| 136 | + # check if any changes staged |
| 137 | + if git diff --cached --quiet; then |
| 138 | + echo "No changes to commit." |
| 139 | + exit 0 |
| 140 | + fi |
| 141 | +
|
| 142 | + git commit -m "Update pbr-iplist-iran-v4 from v2fly/geoip release ${{ steps.get_release.outputs.tag }}" |
| 143 | + git push origin HEAD:main |
0 commit comments