forked from mmmray/xray-online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (63 loc) · 1.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"syscall/js"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/platform/filesystem"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/infra/conf"
json_reader "github.com/xtls/xray-core/infra/conf/json"
)
//go:embed assets/geoip.dat
var geoipRaw []byte
//go:embed assets/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("XrayGetVersion", js.FuncOf(func(this js.Value, args []js.Value) any {
return core.Version()
}))
js.Global().Set("XrayParseConfig", js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) < 1 {
fmt.Println("invalid number of args")
return nil
}
arg := args[0]
if arg.Type() != js.TypeString {
fmt.Println("the argument should be a string")
return nil
}
jsonConfig := &conf.Config{}
jsonReader := &json_reader.Reader{
Reader: bytes.NewReader([]byte(arg.String())),
}
decoder := json.NewDecoder(jsonReader)
if err := decoder.Decode(jsonConfig); err != nil {
return err.Error()
}
if _, err := jsonConfig.Build(); err != nil {
return err.Error()
}
return nil
}))
js.Global().Get("onWasmInitialized").Invoke()
// Prevent the program from exiting.
// Note: the exported func should be released if you don't need it any more,
// and let the program exit after then. To simplify this demo, this is
// omitted. See https://pkg.go.dev/syscall/js#Func.Release for more information.
select {}
}