From 9e26f894c2aefcc9065d89fe519e101b21eb570d Mon Sep 17 00:00:00 2001 From: mmmray <142015632+mmmray@users.noreply.github.com> Date: Sun, 8 Sep 2024 12:57:19 +0200 Subject: [PATCH] add geosite.dat support --- .gitignore | 1 + Makefile | 10 ++++++++-- main.go | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index cfd8b49..33eb35c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.wasm +*.dat wasm_exec.js diff --git a/Makefile b/Makefile index 20401b7..baf75b8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,12 @@ -build: +geoip.dat: + wget https://github.com/v2fly/geoip/releases/latest/download/geoip.dat + +geosite.dat: + wget https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat -O geosite.dat + +build: geoip.dat geosite.dat cp "$$(go env GOROOT)/misc/wasm/wasm_exec.js" . - GOARCH=wasm GOOS=js go build -o main.wasm + GOARCH=wasm GOOS=js go build -o main.wasm main.go serve: python3 -mhttp.server diff --git a/main.go b/main.go index d4caefc..9d70372 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,38 @@ package main import ( "fmt" + "strings" "syscall/js" "encoding/json" "bytes" + "io" + _ "embed" + "io/ioutil" "github.com/xtls/xray-core/infra/conf" + "github.com/xtls/xray-core/common/platform/filesystem" + "github.com/xtls/xray-core/common/errors" json_reader "github.com/xtls/xray-core/infra/conf/json" ) +//go:embed geoip.dat +var geoipRaw []byte +//go:embed geosite.dat +var geositeRaw []byte + func main() { + filesystem.NewFileReader = func(path string) (io.ReadCloser, error) { + if strings.HasSuffix(path, "geoip.dat") { + return ioutil.NopCloser(bytes.NewReader(geoipRaw)), nil + } + + if strings.HasSuffix(path, "geosite.dat") { + return ioutil.NopCloser(bytes.NewReader(geositeRaw)), nil + } + + return nil, errors.New(path + " cannot be opened in the browser") + } + js.Global().Set("XrayParseConfig", js.FuncOf(func(this js.Value, args []js.Value) any { if len(args) < 1 { fmt.Println("invalid number of args")