|
| 1 | +package main |
| 2 | + |
| 3 | +import "C" |
| 4 | +import ( |
| 5 | + "github.com/amnezia-vpn/amnezia-libxray/nodep" |
| 6 | + "github.com/amnezia-vpn/amnezia-libxray/xray" |
| 7 | +) |
| 8 | + |
| 9 | +// Read geo data and cut the codes we need. |
| 10 | +// datDir means the dir which geo dat are in. |
| 11 | +// dstDir means the dir which new geo dat are in. |
| 12 | +// cutCodePath means geoCutCode json file path |
| 13 | +// |
| 14 | +// This function is used to reduce memory when init instance. |
| 15 | +// You can cut the country codes which rules and nameservers contain. |
| 16 | +// |
| 17 | +//export LibXrayCutGeoData |
| 18 | +func LibXrayCutGeoData(datDir, dstDir, cutCodePath *C.char) *C.char { |
| 19 | + err := xray.CutGeoData(C.GoString(datDir), C.GoString(dstDir), C.GoString(cutCodePath)) |
| 20 | + return C.CString(nodep.WrapError(err)) |
| 21 | +} |
| 22 | + |
| 23 | +// Read geo data and write all codes to text file. |
| 24 | +// datDir means the dir which geo dat are in. |
| 25 | +// name means the geo dat file name, like "geosite", "geoip" |
| 26 | +// geoType must be the value of geoType |
| 27 | +// |
| 28 | +//export LibXrayLoadGeoData |
| 29 | +func LibXrayLoadGeoData(datDir, name, geoType *C.char) *C.char { |
| 30 | + err := xray.LoadGeoData(C.GoString(datDir), C.GoString(name), C.GoString(geoType)) |
| 31 | + return C.CString(nodep.WrapError(err)) |
| 32 | +} |
| 33 | + |
| 34 | +// Ping Xray config and find the delay and country code of its outbound. |
| 35 | +// datDir means the dir which geosite.dat and geoip.dat are in. |
| 36 | +// configPath means the config.json file path. |
| 37 | +// timeout means how long the http request will be cancelled if no response, in units of seconds. |
| 38 | +// url means the website we use to test speed. "https://www.google.com" is a good choice for most cases. |
| 39 | +// proxy means the local http/socks5 proxy, like "socks5://[::1]:1080". |
| 40 | +// |
| 41 | +//export LibXrayPing |
| 42 | +func LibXrayPing(datDir, configPath *C.char, timeout int, url, proxy *C.char) *C.char { |
| 43 | + return C.CString(xray.Ping(C.GoString(datDir), C.GoString(configPath), timeout, C.GoString(url), C.GoString(proxy))) |
| 44 | +} |
| 45 | + |
| 46 | +// query system stats and outbound stats. |
| 47 | +// server means The API server address, like "127.0.0.1:8080". |
| 48 | +// dir means the dir which result json will be wrote to. |
| 49 | +// |
| 50 | +//export LibXrayQueryStats |
| 51 | +func LibXrayQueryStats(server, dir *C.char) *C.char { |
| 52 | + err := xray.QueryStats(C.GoString(server), C.GoString(dir)) |
| 53 | + return C.CString(nodep.WrapError(err)) |
| 54 | +} |
| 55 | + |
| 56 | +// convert text to uuid |
| 57 | +// |
| 58 | +//export LibXrayCustomUUID |
| 59 | +func LibXrayCustomUUID(text *C.char) *C.char { |
| 60 | + return C.CString(xray.CustomUUID(C.GoString(text))) |
| 61 | +} |
| 62 | + |
| 63 | +// Test Xray Config. |
| 64 | +// datDir means the dir which geosite.dat and geoip.dat are in. |
| 65 | +// configPath means the config.json file path. |
| 66 | +// |
| 67 | +//export LibXrayTestXray |
| 68 | +func LibXrayTestXray(datDir, configPath *C.char) *C.char { |
| 69 | + err := xray.TestXray(C.GoString(datDir), C.GoString(configPath)) |
| 70 | + return C.CString(nodep.WrapError(err)) |
| 71 | +} |
| 72 | + |
| 73 | +// Run Xray instance. |
| 74 | +// datDir means the dir which geosite.dat and geoip.dat are in. |
| 75 | +// configPath means the config.json file path. |
| 76 | +// maxMemory means the soft memory limit of golang, see SetMemoryLimit to find more information. |
| 77 | +// |
| 78 | +//export LibXrayRunXray |
| 79 | +func LibXrayRunXray(datDir, configPath *C.char, maxMemory int64) *C.char { |
| 80 | + err := xray.RunXray(C.GoString(datDir), C.GoString(configPath), maxMemory) |
| 81 | + return C.CString(nodep.WrapError(err)) |
| 82 | +} |
| 83 | + |
| 84 | +// Stop Xray instance. |
| 85 | +// |
| 86 | +//export LibXrayStopXray |
| 87 | +func LibXrayStopXray() *C.char { |
| 88 | + err := xray.StopXray() |
| 89 | + return C.CString(nodep.WrapError(err)) |
| 90 | +} |
| 91 | + |
| 92 | +// Xray's version |
| 93 | +// |
| 94 | +//export LibXrayXrayVersion |
| 95 | +func LibXrayXrayVersion() *C.char { |
| 96 | + return C.CString(xray.XrayVersion()) |
| 97 | +} |
0 commit comments