From 9515303ce43f9712d53f9d7161f33b6b83d48155 Mon Sep 17 00:00:00 2001 From: Kslr Date: Sun, 20 Sep 2020 16:24:17 +0800 Subject: [PATCH] generate gfwlist.txt (#215) * generate gfwlist.pac --- .github/workflows/build.yml | 28 +++++++++++++++++++++++-- main.go | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a0f0a9bf..ffb3d81f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,13 +47,17 @@ jobs: xz -z -9 -k dlc.dat sha256sum dlc.dat.xz > dlc.dat.xz.sha256sum + - name: Generate gfwlist.txt sha256 hash + run: | + sha256sum gfwlist.txt > gfwlist.txt.sha256sum + - name: Git push assets to "release" branch run: | git init git config --local user.name "actions" git config --local user.email "action@github.com" git checkout -b release - git add *.txt dlc.dat dlc.dat.sha256sum dlc.dat.zip dlc.dat.zip.sha256sum dlc.dat.xz dlc.dat.xz.sha256sum + git add *.txt dlc.dat dlc.dat.sha256sum dlc.dat.zip dlc.dat.zip.sha256sum dlc.dat.xz dlc.dat.xz.sha256sum gfwlist.txt gfwlist.txt.sha256sum git commit -m "${{ env.RELEASE_NAME }}" git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}" git push -f -u origin release @@ -119,7 +123,7 @@ jobs: asset_name: dlc.dat.xz asset_content_type: application/octet-stream - - name: Upload dlc.dat sha256sum + - name: Upload dlc.dat.xz sha256sum uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -127,4 +131,24 @@ jobs: upload_url: ${{ steps.create_release.outputs.upload_url }} asset_path: ./dlc.dat.xz.sha256sum asset_name: dlc.dat.xz.sha256sum + asset_content_type: text/plain + + - name: Upload gfwlist.txt + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./gfwlist.txt + asset_name: gfwlist.txt + asset_content_type: text/plain + + - name: Upload gfwlist.txt sha256sum + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./gfwlist.txt.sha256sum + asset_name: gfwlist.txt.sha256sum asset_content_type: text/plain \ No newline at end of file diff --git a/main.go b/main.go index c8d41d85..51a256ed 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "encoding/base64" "errors" "flag" "fmt" @@ -107,6 +108,39 @@ func exportPlainTextList(list []string, refName string, pl *ParsedList) { } } +func exportGfwList(pl *ParsedList) error { + var entryBytes []byte + entryBytes = append(entryBytes, []byte("[AutoProxy 0.2.9]\n")...) + for _, entry := range pl.Entry { + switch entry.Type { + case "domain": + entryBytes = append(entryBytes, []byte("||"+entry.Value+"\n")...) + case "full": + entryBytes = append(entryBytes, []byte("|http://"+entry.Value+"\n")...) + entryBytes = append(entryBytes, []byte("|https://"+entry.Value+"\n")...) + case "keyword": + entryBytes = append(entryBytes, []byte(entry.Value+"\n")...) + case "regexp": + entryBytes = append(entryBytes, []byte("/"+entry.Value+"/\n")...) + default: + return errors.New("unknown domain type: " + entry.Type) + } + } + + f, err := os.OpenFile("gfwlist.txt", os.O_RDWR|os.O_CREATE, 0644) + if err != nil { + return err + } + encoder := base64.NewEncoder(base64.StdEncoding, f) + if _, err = encoder.Write(entryBytes); err != nil { + return err + } + if err = encoder.Close(); err != nil { + return err + } + return nil +} + func removeComment(line string) string { idx := strings.Index(line, "#") if idx == -1 { @@ -374,6 +408,14 @@ func main() { } } } + + // Export GfwList + if refName == "GEOLOCATION-!CN" { + if err := exportGfwList(pl); err != nil { + fmt.Println("Failed: ", err) + os.Exit(1) + } + } } protoBytes, err := proto.Marshal(protoList)