/
env_setting_linux.go
143 lines (127 loc) · 4 KB
/
env_setting_linux.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// +build linux,!windows,!darwin,!js
package client
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/gen2brain/dlgs"
)
var RAW_PROFILE = ""
var (
GlobalStatus = false
)
func IfProxyStart() bool {
result := os.Getenv("http_proxy")
if result == "" {
if os.Getenv("HTTP_PROXY") != "" {
return true
}
} else {
return true
}
return false
}
func InitDesktop() {
KCPPATH := os.Args[0]
DEKPATH := filepath.Join(os.Getenv("HOME"), "Desktop")
if _, err := os.Stat(DEKPATH); err != nil {
DEKPATH = filepath.Join(os.Getenv("HOME"), "桌面")
}
DEKPATH_APP := filepath.Join(DEKPATH, "Kcpee.desktop")
APP_base := filepath.Join(os.Getenv("HOME"), ".local", "Kcpee")
if _, err := os.Stat(APP_base); err != nil {
os.MkdirAll(APP_base, os.ModePerm)
}
MAIN_EXE := filepath.Join(APP_base, "Kcpee")
if data, err := ioutil.ReadFile(KCPPATH); err != nil {
dlgs.Error("错误", err.Error())
} else {
if err := ioutil.WriteFile(MAIN_EXE, data, os.ModePerm); err != nil {
dlgs.Error("错误", err.Error())
}
}
if _, err := os.Stat(APP_base); err != nil {
os.MkdirAll(APP_base, os.ModePerm)
}
PICPATH := filepath.Join(APP_base, "icon.png")
dlgs.Info("Initing", "create Desktop app in "+PICPATH)
cmd := exec.Command("bash", "-c", "wget -c -t 5 'https://gitee.com/dark.H/Kcpee/raw/master/ICON@256x256.png' -O "+PICPATH)
if aa, err := cmd.Output(); err != nil {
dlgs.Info("Initing", string(aa)+err.Error())
}
INIT := `
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=sudo ` + MAIN_EXE + `
Name=Kcpee
Icon=` + PICPATH
if _, err := os.Stat(DEKPATH_APP); err != nil {
yes, _ := dlgs.Question("Question", "if Create Desktop App icon?", true)
if yes {
if err := ioutil.WriteFile(DEKPATH_APP, []byte(INIT), os.ModePerm); err != nil {
dlgs.Error("错误", err.Error())
}
}
}
}
func ProxySet(localAddr string) {
// DEKPATH := filepath.Join(os.Getenv("HOME"), "Desktop")
// if _, err := os.Stat(DEKPATH); err != nil {
// DEKPATH = filepath.Join(os.Getenv("HOME"), "桌面")
// }
// DEKPATH_APP := filepath.Join(DEKPATH, "Kcpee.desktop")
// if _, err := os.Stat(DEKPATH_APP); err != nil {
// dlgs.Info("Init ", DEKPATH_APP)
// InitDesktop()
// }
// if _, err := os.Stat("/etc/profile"); err != nil {
// return
// }
// RAWB, err := ioutil.ReadFile("/etc/profile")
// if err != nil {
// return
// }
// RAW := string(RAWB)
// if localAddr != "" {
// TMP := `
// ##### PROXY PROFILE START
// export http_proxy="` + localAddr + `"`
// TMP += `
// export https_proxy="` + localAddr + `
// ##### PROXY PROFILE END
// `
// if !strings.Contains(RAW, TMP) {
// RAW += TMP
// //err := ioutil.WriteFile("/etc/profile", []byte(RAW), os.ModePerm)
// _, err := exec.Command("bash", "-c", "sudo cat << EOF > /etc/profile\n"+RAW+"EOF\n").Output()
// if err != nil {
// dlgs.Error("Error Start Global Proxy", err.Error())
// }
// GlobalStatus = true
// dlgs.Info("Infomation", "try to start proxy: "+localAddr)
// }
// } else {
// if strings.Contains(RAW, "##### PROXY PROFILE START") {
// s := strings.SplitN(RAW, "##### PROXY PROFILE START", 2)
// PRE := s[0]
// AFT := strings.TrimSpace(strings.SplitN(s[1], "##### PROXY PROFILE END", 2)[1])
// RAW = PRE + "\n" + AFT
// // err := ioutil.WriteFile("/etc/profile", []byte(RAW), os.ModePerm)
// _, err := exec.Command("bash", "-c", "sudo cat << EOF > /etc/profile\n"+RAW+"EOF\n").Output()
// if err != nil {
// dlgs.Error("Error Start Global Proxy", err.Error())
// }
// //ioutil.WriteFile("/etc/profile", []byte(RAW), os.ModePerm)
// GlobalStatus = false
// dlgs.Info("Infomation", "try to stop proxy: ")
// }
// }
}
func KillKcpee() {
TMP := `sudo ps aux| grep Kcpee | egrep -v '(grep|egrep)' | awk '{ print $2}' | xargs kill -9 `
exec.Command("bash", "-c", TMP).Output()
}