forked from rnhws-Team/PhoenixBuilder-Achieved
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (176 loc) · 4.98 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"phoenixbuilder/fastbuilder/core"
"phoenixbuilder/fastbuilder/args"
I18n "phoenixbuilder/fastbuilder/i18n"
script_bridge "phoenixbuilder/fastbuilder/script_engine/bridge"
"phoenixbuilder/fastbuilder/utils"
"strings"
"syscall"
"github.com/pterm/pterm"
"golang.org/x/term"
"phoenixbuilder/fastbuilder/readline"
_ "phoenixbuilder/io"
_ "phoenixbuilder/plantform_specific/fix_timer"
)
func main() {
args.ParseArgs()
if len(args.PackScripts()) != 0 {
os.Exit(script_bridge.MakePackage(args.PackScripts(), args.PackScriptsOut()))
}
pterm.Error.Prefix = pterm.Prefix{
Text: "ERROR",
Style: pterm.NewStyle(pterm.BgBlack, pterm.FgRed),
}
I18n.Init()
pterm.DefaultBox.Println(pterm.LightCyan("https://github.com/RainyHallways/PhoenixBuilder"))
pterm.Println(pterm.Yellow("PhoenixBuilder Dev" + args.GetFBVersion()))
pterm.Println(pterm.Red("注意: 此版本为第三方预览版本,请勿用于实际生产用途!!!"))
// iSH.app specific, for foreground ability
if _, err := os.Stat("/dev/location"); err == nil {
// Call location service
pterm.Println(pterm.Yellow(I18n.T(I18n.Notice_iSH_Location_Service)))
cmd := exec.Command("ash", "-c", "cat /dev/location > /dev/null &")
err := cmd.Start()
if err != nil {
fmt.Println(err)
}
}
if !args.NoReadline() {
readline.InitReadline()
}
if I18n.ShouldDisplaySpecial() {
fmt.Printf("%s", I18n.T(I18n.Special_Startup))
}
defer core.Fatal()
if args.DebugMode() {
init_and_run_debug_client()
return
}
if !args.ShouldDisableHashCheck() {
fmt.Printf(I18n.T(I18n.Notice_CheckUpdate))
hasUpdate, latestVersion := utils.CheckUpdate(args.GetFBVersion())
fmt.Printf(I18n.T(I18n.Notice_OK))
if hasUpdate {
fmt.Printf(I18n.T(I18n.Notice_UpdateAvailable), latestVersion)
fmt.Printf(I18n.T(I18n.Notice_UpdateNotice))
// To ensure user won't ignore it directly, can be suppressed by command line argument.
os.Exit(0)
}
}
if !args.SpecifiedToken() {
token := loadTokenPath()
if _, err := os.Stat(token); os.IsNotExist(err) {
fbusername, err := getInputUserName()
if err != nil {
panic(err)
}
fbuntrim := fmt.Sprintf("%s", strings.TrimSuffix(fbusername, "\n"))
fbun := strings.TrimRight(fbuntrim, "\r\n")
fmt.Printf(I18n.T(I18n.EnterPasswordForFBUC))
fbpassword, err := term.ReadPassword(int(syscall.Stdin))
fmt.Printf("\n")
tokenstruct := &map[string]interface{}{
"encrypt_token": true,
"username": fbun,
"password": string(fbpassword),
}
token, err := json.Marshal(tokenstruct)
if err != nil {
fmt.Println(I18n.T(I18n.FBUC_Token_ErrOnGen))
fmt.Println(err)
return
}
runInteractiveClient(string(token))
} else {
token, err := readToken(token)
if err != nil {
fmt.Println(err)
return
}
runInteractiveClient(token)
}
} else {
runInteractiveClient(args.CustomTokenContent())
}
}
func runInteractiveClient(token string) {
var code, serverPasswd string
var err error
if !args.SpecifiedServer() {
code, serverPasswd, err = getRentalServerCode()
} else {
code = args.ServerCode()
serverPasswd = args.ServerPassword()
}
if err != nil {
fmt.Println(err)
return
}
env:=core.InitRealEnvironment(token, code, serverPasswd)
ptoken_succ:=core.ProcessTokenDefault(env)
//init_and_run_client(token, code, serverPasswd)
if !ptoken_succ {
panic("Failed to load token")
}
core.InitClient(env)
go core.EnterReadlineThread(env,nil)
defer core.DestroyClient(env)
core.EnterWorkerThread(env,nil)
}
func init_and_run_debug_client() {
env := core.InitDebugEnvironment()
core.InitClient(env)
go core.EnterReadlineThread(env,nil)
defer core.DestroyClient(env)
core.EnterWorkerThread(env,nil)
}
func getInput() (string, error) {
reader := bufio.NewReader(os.Stdin)
inp, err := reader.ReadString('\n')
inpl := strings.TrimRight(inp, "\r\n")
return inpl, err
}
func getInputUserName() (string, error) {
reader := bufio.NewReader(os.Stdin)
pterm.Printf(I18n.T(I18n.Enter_FBUC_Username))
fbusername, err := reader.ReadString('\n')
return fbusername, err
}
func getRentalServerCode() (string, string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(I18n.T(I18n.Enter_Rental_Server_Code))
code, err := reader.ReadString('\n')
if err != nil {
return "", "", err
}
fmt.Printf(I18n.T(I18n.Enter_Rental_Server_Password))
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
fmt.Printf("\n")
return strings.TrimRight(code, "\r\n"), string(bytePassword), err
}
func readToken(path string) (string, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(content), nil
}
func loadTokenPath() string {
homedir, err := os.UserHomeDir()
if err != nil {
fmt.Println(I18n.T(I18n.Warning_UserHomeDir))
homedir = "."
}
fbconfigdir := filepath.Join(homedir, ".config/fastbuilder")
os.MkdirAll(fbconfigdir, 0700)
token := filepath.Join(fbconfigdir, "fbtoken")
return token
}