-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
178 lines (169 loc) · 4.66 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"os"
"strings"
"github.com/y805939188/dcommand"
mySvc "easy-proxy/binary_service"
"easy-proxy/certificate"
myCommand "easy-proxy/command"
myNet "easy-proxy/net"
"easy-proxy/tools"
)
func initFiles() error {
easyProxyRootPath, err := tools.GetEasyRootPath()
if err != nil {
return fmt.Errorf("获取程序根路径失败")
}
if !tools.FileIsExisted(easyProxyRootPath) {
err := tools.CreateDir(easyProxyRootPath)
if err != nil {
return fmt.Errorf("创建程序根路径失败")
}
tmpCaPath, err := tools.GetTmpCaPath()
if err != nil {
return fmt.Errorf("获取程序临时证书存放目录失败")
}
err = tools.CreateDir(tmpCaPath)
if err != nil {
return fmt.Errorf("创建程序证书临时存储目录失败")
}
}
scriptPath, err := tools.GetUpdateSystemCaScriptPath()
if err != nil {
return err
}
if !tools.FileIsExisted(scriptPath) {
err = tools.CreateFile(
scriptPath,
[]byte(certificate.UpdateCaCertificatesBashScriptContent),
0766,
)
if err != nil {
return err
}
}
servicePath, err := tools.GetTmpLocalServicePath()
if err != nil {
return err
}
// 拷贝启动本地代理服务器的二进制到临时目录下
// svcRootPath 是这个二进制文件的父级目录
svcRootPath, err := tools.GetTmpLocalServiceRootPath()
if err != nil {
return err
}
if !tools.FileIsExisted(servicePath) {
err = mySvc.RestoreAssets(svcRootPath, "service")
if err != nil {
return err
}
}
proxyInfoPath, err := tools.GetTmpProxyInfoPath()
if err != nil {
return err
}
if !tools.FileIsExisted(proxyInfoPath) {
err := tools.CreateDir(proxyInfoPath)
if err != nil {
return fmt.Errorf("创建 proxy info 目录失败")
}
}
return nil
}
func main() {
err := initFiles()
if err != nil {
fmt.Println("初始化项目路径失败, err: ", err.Error())
return
}
cmd := &dcommand.DCommand{}
cmd.Command("easy-proxy").
Operator("set").
Flag(&dcommand.FlagInfo{Name: "source", Short: "s"}).
Flag(&dcommand.FlagInfo{Name: "target", Short: "t"}).
Operator("del").
Flag(&dcommand.FlagInfo{Name: "id", Short: "id"}).
Operator("fresh").
Operator("list").
Handler(func(command string, fc *dcommand.DCommand) error {
_cmd := fc.GetCommandIfExist(command)
currentOperator := ""
for _, operator := range _cmd.Operators {
if operator.Passed {
if currentOperator != "" {
return fmt.Errorf(fmt.Sprintf("无法同时设置 operator: %s & %s", currentOperator, operator.Name))
}
currentOperator = operator.Name
}
}
if currentOperator == "" {
return fmt.Errorf("easy-proxy 命令需要至少一个 operator: set | del | fresh")
}
cmd := fc.GetCommandIfExist(command)
op := fc.GetOperatorIfExistByCommand(currentOperator, cmd)
switch currentOperator {
case "set":
sourceFlag := fc.GetFlagIfExistInOperatorByOperator("source", true, op)
targetFlag := fc.GetFlagIfExistInOperatorByOperator("target", true, op)
if !sourceFlag.Passed || !targetFlag.Passed {
return fmt.Errorf(fmt.Sprintf("%s 指令一定需要 %s & %s 参数", currentOperator, "-s", "-t"))
}
if len(sourceFlag.Params) != 1 || len(targetFlag.Params) != 1 {
return fmt.Errorf(fmt.Sprintf("-s & -t 参数需要 1 个参数"))
}
sourceHost, err := myNet.IsValidAddr(sourceFlag.Params[0])
if err != nil {
return err
}
if strings.HasPrefix(targetFlag.Params[0], "https://") {
return fmt.Errorf("暂不支持把请求代理到 https 服务")
}
if !strings.HasPrefix(targetFlag.Params[0], "http://") {
targetFlag.Params[0] = "http://" + targetFlag.Params[0]
}
targetHost, err := myNet.IsValidAddr(targetFlag.Params[0])
if err != nil {
return err
}
if targetHost.IsDomain {
return fmt.Errorf("target 暂不支持域名")
}
err = myCommand.ExecSetCommand(sourceHost, targetHost)
if err != nil {
return err
}
case "del":
idFlag := fc.GetFlagIfExistInOperatorByOperator("id", true, op)
if !idFlag.Passed {
return fmt.Errorf("del 指令一定需要一个 -id 作为参数")
}
if len(idFlag.Params) == 0 {
return fmt.Errorf("-id 缺少参数")
}
err = myCommand.ExecDelCommand(idFlag.Params...)
if err != nil {
return err
}
case "fresh":
err := myCommand.ExecFreshCommand()
if err != nil {
return err
}
case "list":
err := myCommand.ExecListCommand()
if err != nil {
return err
}
break
default:
return fmt.Errorf("无效的操作符")
}
return nil
})
testCmd := "easy-proxy " + strings.Join(os.Args[1:], " ")
err = cmd.ExecuteStr(testCmd)
if err != nil {
fmt.Println(err.Error())
}
}