Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine & exclude rules with @cn attribute from gfwlist.txt #236

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

"github.com/golang/protobuf/proto"
"v2ray.com/core/app/router"
Expand Down Expand Up @@ -98,7 +99,7 @@ func (l *ParsedList) toProto() (*router.GeoSite, error) {

func exportPlainTextList(list []string, refName string, pl *ParsedList) {
for _, listName := range list {
if strings.ToUpper(refName) == strings.ToUpper(listName) {
if strings.EqualFold(refName, listName) {
if err := pl.toPlainText(strings.ToLower(refName)); err != nil {
fmt.Println("Failed: ", err)
continue
Expand All @@ -108,10 +109,31 @@ func exportPlainTextList(list []string, refName string, pl *ParsedList) {
}
}

func exportGfwList(pl *ParsedList) error {
func exportGFWList(pl *ParsedList) error {
var entryBytes []byte
timeString := fmt.Sprintf("! Last Modified: %s\n", time.Now())
entryBytes = append(entryBytes, []byte("[AutoProxy 0.2.9]\n")...)
entryBytes = append(entryBytes, []byte(timeString)...)
entryBytes = append(entryBytes, []byte("! Expires: 24h\n")...)
entryBytes = append(entryBytes, []byte("! HomePage: https://github.com/v2fly/domain-list-community\n")...)
entryBytes = append(entryBytes, []byte("! GitHub URL: https://raw.githubusercontent.com/v2fly/domain-list-community/release/gfwlist.txt\n")...)
entryBytes = append(entryBytes, []byte("! jsdelivr URL: https://cdn.jsdelivr.net/gh/v2fly/domain-list-community@release/gfwlist.txt\n")...)

for _, entry := range pl.Entry {
exclude := false
if attrs := entry.Attrs; len(attrs) > 0 {
// exclude rules that have '@cn' attribute
for _, attr := range attrs {
if strings.EqualFold(attr.GetKey(), "cn") {
exclude = true
break
}
}
}
if exclude {
fmt.Printf("Exclude '%s' from gfwlist.txt because it has '@cn' attribute\n", entry.Value)
continue
}
switch entry.Type {
case "domain":
entryBytes = append(entryBytes, []byte("||"+entry.Value+"\n")...)
Expand All @@ -135,6 +157,7 @@ func exportGfwList(pl *ParsedList) error {
if _, err = encoder.Write(entryBytes); err != nil {
return err
}
fmt.Println("gfwlist.txt has been generated successfully in current directory.")
if err = encoder.Close(); err != nil {
return err
}
Expand Down Expand Up @@ -411,7 +434,7 @@ func main() {

// Export GfwList
if refName == "GEOLOCATION-!CN" {
if err := exportGfwList(pl); err != nil {
if err := exportGFWList(pl); err != nil {
fmt.Println("Failed: ", err)
os.Exit(1)
}
Expand Down