forked from xiaokangwang/AndroidLibV2ray
-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
122 lines (106 loc) · 2.52 KB
/
context.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package libv2ray
import (
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/xiaokangwang/AndroidLibV2Ray/CoreI"
)
func NewLib2rayContext() *V2RayContext {
return &V2RayContext{Status: new(CoreI.Status)}
}
type V2RayContext struct {
configureFile string
Callbacks V2RayContextCallbacks
Status *CoreI.Status
}
const configureFile = "ConfigureFile"
func (vc *V2RayContext) CheckConfigureFile() bool {
//Check if file exist
return exists(vc.configureFile)
}
func (vc *V2RayContext) SetPackageName(PackageName string) {
//Check if file exist
vc.Status.PackageName = PackageName
}
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func (vc *V2RayContext) ListConfigureFileDir() *StringArrayList {
none := func() *StringArrayList {
var retsg []string
retsg = append(retsg, "..")
return &StringArrayList{list: retsg}
}
if vc.GetConfigureFile() == "" {
return none()
}
dir := path.Dir(vc.configureFile)
dfd, err := os.Open(dir)
if err != nil {
return none()
}
d, err := dfd.Readdirnames(128)
if err != nil {
return none()
}
d = append(d, "..")
for di := range d {
d[di] = path.Dir(vc.GetConfigureFile()) + "/" + d[di]
}
return &StringArrayList{list: d}
}
func (vc *V2RayContext) GetBriefDesc(pathn string) string {
_, ret := path.Split(pathn)
return ret
}
func (vc *V2RayContext) AssignConfigureFile(cf string) {
if strings.HasSuffix(cf, "..") {
vc.Callbacks.OnRefreshNeeded()
vc.Callbacks.OnFileSelectTriggerd()
return
}
log.Print(cf)
vc.configureFile = cf
vc.WriteProp(configureFile, cf)
vc.Callbacks.OnRefreshNeeded()
}
func (vc *V2RayContext) GetConfigureFile() string {
if vc.configureFile == "" {
vc.configureFile, _ = vc.ReadProp(configureFile)
}
return vc.configureFile
}
type V2RayContextCallbacks interface {
OnRefreshNeeded()
OnFileSelectTriggerd()
}
func (vc *V2RayContext) ReadProp(name string) (string, error) {
os.MkdirAll(vc.Status.GetDataDir()+"config", 0700)
fd, err := os.Open(vc.Status.GetDataDir() + "config/" + name)
if err != nil {
return "", err
}
content, err := ioutil.ReadAll(fd)
if err != nil {
return "", err
}
fd.Close()
return string(content), nil
}
func (vc *V2RayContext) ReadPropD(name string) string {
ctx, _ := vc.ReadProp(name)
return ctx
}
func (vc *V2RayContext) WriteProp(name string, cont string) error {
os.MkdirAll(vc.Status.GetDataDir()+"config", 0700)
return ioutil.WriteFile(vc.Status.GetDataDir()+"config/"+name, []byte(cont), 0600)
}