-
Notifications
You must be signed in to change notification settings - Fork 60
/
nodep_wrapper.go
48 lines (42 loc) · 1.36 KB
/
nodep_wrapper.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
package libXray
import (
"encoding/base64"
"github.com/xtls/libxray/nodep"
)
type getFreePortsResponse struct {
Ports []int `json:"ports,omitempty"`
}
// Wrapper of nodep.GetFreePorts
// count means how many ports you need.
func GetFreePorts(count int) string {
var response nodep.CallResponse[*getFreePortsResponse]
ports, err := nodep.GetFreePorts(count)
if err != nil {
return response.EncodeToBase64(nil, err)
}
var res getFreePortsResponse
res.Ports = ports
return response.EncodeToBase64(&res, nil)
}
// Convert share text to XrayJson
// support XrayJson, v2rayN plain text, v2rayN base64 text, Clash yaml, Clash.Meta yaml
func ConvertShareLinksToXrayJson(base64Text string) string {
var response nodep.CallResponse[*nodep.XrayJson]
links, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64(nil, err)
}
xrayJson, err := nodep.ConvertShareLinksToXrayJson(string(links))
return response.EncodeToBase64(xrayJson, err)
}
// Convert XrayJson to share links.
// VMess will generate VMessAEAD link.
func ConvertXrayJsonToShareLinks(base64Text string) string {
var response nodep.CallResponse[string]
xray, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
links, err := nodep.ConvertXrayJsonToShareLinks(xray)
return response.EncodeToBase64(links, err)
}