-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
231 lines (193 loc) · 4.96 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
package main
import (
"encoding/base64"
"fmt"
"git.ronaksoft.com/river/web-wasm/msg"
"git.ronaksoft.com/river/web-wasm/river"
"math/rand"
"syscall/js"
"time"
)
var (
_river *river.River
)
func main() {
rand.Seed(time.Now().UnixNano())
_river = new(river.River)
done := make(chan struct{}, 0)
global := js.Global()
global.Set("wasmLoad", js.FuncOf(load))
global.Set("wasmSetServerTime", js.FuncOf(setServerTime))
global.Set("wasmAuth", js.FuncOf(auth))
global.Set("wasmDecode", js.FuncOf(decode))
global.Set("wasmEncode", js.FuncOf(encode))
global.Set("wasmGenSrpHash", js.FuncOf(generateSrpHash))
global.Set("wasmGenInputPassword", js.FuncOf(generateInputPassword))
js.Global().Call("jsLoaded", nil)
<-done
fmt.Println("Bye Wasm !")
}
func load(this js.Value, args []js.Value) interface{} {
connInfo := args[0].String()
serverPubKeys := args[1].String()
err := _river.Load(connInfo, serverPubKeys)
if err != nil {
return err.Error()
}
return nil
}
func setServerTime(this js.Value, args []js.Value) interface{} {
serverTime := args[0].Int()
_river.ConnInfo.SetServerTime(int64(serverTime))
return nil
}
func auth(this js.Value, args []js.Value) interface{} {
go func(inps []js.Value) {
id := inps[0].Int()
step := inps[1].Int()
var (
bytes []byte
err error
enc []byte
)
switch step {
case 1:
bytes = _river.AuthStep1(dispatchProgress)
case 2:
enc, err = base64.StdEncoding.DecodeString(args[2].String())
if err != nil {
return
}
bytes, err = _river.AuthStep2(enc, dispatchProgress)
if err != nil {
return
}
case 3:
enc, err = base64.StdEncoding.DecodeString(args[2].String())
if err != nil {
return
}
bytes, err = _river.AuthStep3(enc, dispatchProgress)
if err != nil {
return
}
}
js.Global().Call("jsAuth", id, step, base64.StdEncoding.EncodeToString(bytes))
}(args)
return nil
}
func decode(this js.Value, args []js.Value) interface{} {
go func(inps []js.Value) {
withParse := inps[0].Bool()
enc, err := base64.StdEncoding.DecodeString(inps[1].String())
if err != nil {
return
}
env, err := _river.Decode(enc)
if err != nil || env == nil {
return
}
reqId := inps[2].Int()
if withParse {
parseEnvelope(env)
} else {
if reqId != 0 {
env.RequestID = uint64(reqId)
}
js.Global().Call("jsDecode", false, env.RequestID, env.Constructor, base64.StdEncoding.EncodeToString(env.Message))
}
}(args)
return nil
}
func encode(this js.Value, args []js.Value) interface{} {
go func(inps []js.Value) {
withSend := inps[0].Bool()
env := new(msg.MessageEnvelope)
env.RequestID = uint64(args[1].Int())
env.Constructor = int64(args[2].Int())
enc, err := base64.StdEncoding.DecodeString(args[3].String())
if err != nil {
return
}
env.Message = enc
if len(args) > 4 {
teamId := args[4].String()
teamAccessHash := args[5].String()
if teamId != "0" && teamAccessHash != "0" {
env.Header = river.TeamHeader(teamId, teamAccessHash)
}
}
bytes, err := _river.Encode(env)
if err != nil {
return
}
js.Global().Call("jsEncode", withSend, env.RequestID, base64.StdEncoding.EncodeToString(bytes))
}(args)
return nil
}
func generateSrpHash(this js.Value, args []js.Value) interface{} {
go func(inps []js.Value) {
id := inps[0].Int()
pass, err := base64.StdEncoding.DecodeString(inps[1].String())
if err != nil {
return
}
algorithm := inps[2].Int()
algorithmData, err := base64.StdEncoding.DecodeString(inps[3].String())
if err != nil {
return
}
res, err := _river.GenSrpHash(pass, int64(algorithm), algorithmData)
if err != nil {
return
}
js.Global().Call("jsGenSrpHash", id, base64.StdEncoding.EncodeToString(res))
}(args)
return nil
}
func generateInputPassword(this js.Value, inps []js.Value) interface{} {
go func(inps []js.Value) {
id := inps[0].Int()
pass, err := base64.StdEncoding.DecodeString(inps[1].String())
if err != nil {
return
}
accountPass, err := base64.StdEncoding.DecodeString(inps[2].String())
if err != nil {
return
}
res, err := _river.GenInputPassword(pass, accountPass)
if err != nil {
return
}
js.Global().Call("jsGenInputPassword", id, base64.StdEncoding.EncodeToString(res))
}(inps)
return nil
}
func dispatchProgress(progress int64) {
js.Global().Call("jsAuthProgress", progress)
}
func parseEnvelope(m *msg.MessageEnvelope) {
switch m.Constructor {
case msg.C_MessageContainer:
x := new(msg.MessageContainer)
err := x.Unmarshal(m.Message)
if err != nil {
fmt.Println("Error", err.Error())
return
}
for _, envelope := range x.Envelopes {
parseEnvelope(envelope)
}
case msg.C_UpdateContainer:
x := new(msg.UpdateContainer)
err := x.Unmarshal(m.Message)
if err != nil {
fmt.Println("Error", err.Error())
return
}
js.Global().Call("jsUpdate", base64.StdEncoding.EncodeToString(m.Message))
default:
js.Global().Call("jsDecode", true, m.RequestID, m.Constructor, base64.StdEncoding.EncodeToString(m.Message))
}
}