forked from axolotl-chat/axolotl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (228 loc) · 7.51 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"flag"
_ "image/jpeg"
_ "image/png"
"os"
"strings"
"sync"
astilectron "github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/nanu-c/axolotl/app/config"
"github.com/nanu-c/axolotl/app/push"
"github.com/nanu-c/axolotl/app/ui"
"github.com/nanu-c/axolotl/app/webserver"
"github.com/nanu-c/axolotl/app/worker"
"github.com/signal-golang/textsecure/crayfish"
)
func init() {
flag.StringVar(&config.MainQml, "qml", "qml/phoneui/main.qml", "The qml file to load.")
flag.StringVar(&config.Gui, "e", "", "Specify runtime environment. Use either electron, ut, lorca, qt or server")
flag.StringVar(&config.AxolotlWebDir, "axolotlWebDir", "./axolotl-web/dist", "Specify the directory to use for axolotl-web")
flag.BoolVar(&config.ElectronDebug, "eDebug", false, "Open electron development console")
flag.BoolVar(&config.PrintVersion, "version", false, "Print version info")
flag.StringVar(&config.ServerHost, "host", "127.0.0.1", "Host to serve UI from.")
flag.StringVar(&config.ServerPort, "port", "9080", "Port to serve UI from.")
flag.StringVar(&config.ElectronFlag, "electron-flag", "", "Specify electron flag. Use no-ozone to disable Ozone/Wayland platform")
flag.StringVar(&config.LogLevel, "d", "", "Specify debug level. Use either debug, info, warn, error or fatal")
}
func setup() {
config.SetupConfig()
if config.LogLevel != "" {
level, err := log.ParseLevel(config.LogLevel)
if err != nil {
log.Fatal(err)
}
log.SetLevel(level)
} else {
log.SetLevel(log.InfoLevel)
}
log.Infoln("[axolotl] Starting axolotl version", config.AppVersion)
}
func runBackend() {
errorChannel := make(chan error)
go worker.RunBackend(errorChannel)
if config.IsPushHelper {
go push.PushHelperProcess()
}
err := <-errorChannel
if err != nil {
os.Exit(1)
}
}
func runUI() error {
defer wg.Done()
if config.Gui != "ut" && config.Gui != "lorca" && config.Gui != "qt" {
ui.RunUi(config.Gui)
runElectron()
} else {
ui.RunUi(config.Gui)
}
endAxolotlGracefully()
return nil
}
func endAxolotlGracefully() {
log.Infoln("[axolotl] ending axolotl")
err := crayfish.Instance.Stop()
if err != nil {
log.Errorf("[axolotl] error stopping crayfish: %s", err)
}
os.Exit(0)
}
func runElectron() {
defer endAxolotlGracefully()
l := log.New()
electronPath := os.Getenv("XDG_DATA_HOME")
if len(electronPath) == 0 {
electronPath = config.ConfigDir + "/electron"
}
electronSwitches := []string{"--disable-dev-shm-usage", "--no-sandbox"}
if config.ElectronFlag == "no-ozone" {
electronSwitches = append(electronSwitches, "")
} else {
electronSwitches = append(electronSwitches, "--ozone-platform-hint=auto")
}
log.Infoln("[axolotl-electron] starting astilelectron with the following switches:", electronSwitches)
var astilElectronOptions = astilectron.Options{
AppName: "axolotl",
AppIconDefaultPath: "axolotl-web/public/axolotl.png", // If path is relative, it must be relative to the data directory
AppIconDarwinPath: "axolotl-web/public/axolotl.png", // Same here
BaseDirectoryPath: electronPath,
VersionElectron: "21.0.1",
VersionAstilectron: "0.56.0",
SingleInstance: true,
ElectronSwitches: electronSwitches,
}
var a *astilectron.Astilectron
var err error
if os.Getenv("AXOLOTL_ELECTRON_BUNDLED") == "true" {
err = bootstrap.Run(bootstrap.Options{
AstilectronOptions: astilElectronOptions,
Logger: l,
OnWait: func(astielectron *astilectron.Astilectron, _ []*astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
a = astielectron
return nil
},
})
} else {
a, err = astilectron.New(l, astilElectronOptions)
}
if err != nil {
log.Errorln(errors.Wrap(err, "[axolotl-electron]: creating astilectron failed"))
}
defer a.Close()
// Start astilectron
a.HandleSignals()
if err = a.Start(); err != nil {
log.Errorln(errors.Wrap(err, "[axolotl-electron] main: starting astilectron failed"))
}
a.On(astilectron.EventNameAppCrash, func(e astilectron.Event) (deleteListener bool) {
log.Errorln("[axolotl-electron] Electron App has crashed", e)
return
})
a.HandleSignals()
// New window
var w *astilectron.Window
title := "Axolotl"
center := true
height := 800
width := 600
if w, err = a.NewWindow("http://"+config.ServerHost+":"+config.ServerPort, &astilectron.WindowOptions{
Title: &title,
Center: ¢er,
Height: &height,
Width: &width,
}); err != nil {
log.Debugln("[axolotl-electron]", errors.Wrap(err, "main: new window failed"))
}
w.On(astilectron.EventNameAppCrash, func(e astilectron.Event) (deleteListener bool) {
log.Errorln("[axolotl-electron] Electron App has crashed")
return
})
w.On(astilectron.EventNameAppClose, func(e astilectron.Event) (deleteListener bool) {
log.Errorln("[axolotl-electron] Electron App was closed")
return
})
w.On(astilectron.EventNameWindowEventDidFinishLoad, func(e astilectron.Event) (deleteListener bool) {
log.Infoln("[axolotl-electron] Page loaded")
return
})
w.On(astilectron.EventNameWindowEventWillNavigate, func(e astilectron.Event) (deleteListener bool) {
log.Infoln("[axolotl-electron] Electron navigation", e.URL)
if strings.Contains(e.URL, "signalcaptchas.org") {
log.Infoln("[axolotl-electron] overriding onload", e.URL)
w.ExecuteJavaScript(
`
// override the default onload function
window.onload=function() {
var action = "registration";
var isDone = false;
var sitekey = "6LfBXs0bAAAAAAjkDyyI1Lk5gBAUWfhI_bIyox5W";
var widgetId = grecaptcha.enterprise.render("container", {
sitekey: sitekey,
size: "checkbox",
callback: function (token) {
isDone = true;
document.body.removeAttribute("class");
window.location = ["http://` + config.ServerHost + `:` + config.ServerPort + `/?token=signal-recaptcha-v2", sitekey, action, token].join(".");
},
});
}
// cleanup
var bodyTag = document.getElementsByTagName('body')[0];
bodyTag.innerHTML ='<div id="container"></div>'
grecaptcha = undefined
// reload recaptcha
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://www.google.com/recaptcha/enterprise.js?onload=onload&render=explicit";
bodyTag.appendChild(script);
`)
}
return
})
w.On(astilectron.EventNameWindowEventDidGetRedirectRequest, func(e astilectron.Event) (deleteListener bool) {
log.Infoln("[axolotl-electron] Electron redirect request ", e.URLNew)
return
})
w.On(astilectron.EventNameWindowEventWebContentsExecutedJavaScript, func(e astilectron.Event) (deleteListener bool) {
log.Infoln("[axolotl-electron] Electron navigation js")
return
})
// Create windows
if err = w.Create(); err != nil {
log.Errorln("[axolotl-electron]", errors.Wrap(err, "main: creating window failed"))
}
log.Debugln("[axolotl-electron] open dev tools", config.ElectronDebug)
if config.ElectronDebug {
w.OpenDevTools()
}
w.Session.ClearCache()
// Blocking pattern
a.Wait()
}
func runWebserver() {
defer endAxolotlGracefully()
log.Printf("[axolotl] Axolotl server started")
// Fetch the URL.
webserver.Run()
}
var wg sync.WaitGroup
func main() {
setup()
wg.Add(1)
go runBackend()
log.Println("[axolotl] Setup completed")
wg.Add(1)
go runWebserver()
if config.Gui != "server" {
wg.Add(1)
go runUI()
} else {
log.Printf("[axolotl] Axolotl frontend is at http://" + config.ServerHost + ":" + config.ServerPort + "/")
}
wg.Wait()
log.Println("[axolotl] Axolotl stopped")
}