generate gfwlist.txt (#215)

* generate gfwlist.pac
This commit is contained in:
Kslr
2020-09-20 16:24:17 +08:00
committed by GitHub
parent 5c36ca16c1
commit 9515303ce4
2 changed files with 68 additions and 2 deletions

42
main.go
View File

@@ -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)