-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjux.go
247 lines (212 loc) · 5.7 KB
/
jux.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
package main
import (
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"github.com/google/uuid"
"github.com/ilyakaznacheev/cleanenv"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
"io"
"log"
"os"
"syscall"
)
type Client struct {
sendChannel chan WSMessage //from client to server
receiveChannel chan WSMessage //from server to client
xmppClient *xmpp.Client
}
type MessageType int
const (
Ctrl MessageType = iota
Msg
)
type WSMessage struct {
MessageType `json:"messageType"`
*CtrlMessage `json:"ctrlMessage,omitempty"`
*XMPPMessage `json:"xmppMessage,omitempty"`
}
type CtrlMessage struct {
Action string `json:"action,omitempty"`
}
type XMPPMessage struct {
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Handle string `json:"handle,omitempty"`
Body string `json:"body,omitempty"`
}
var clients = map[string]Client{}
type Config struct {
XMPP struct {
Address string `env:"XMPP_ADDRESS" yaml:"address" env-default:"localhost:5432" env-description:"Server Address to connect to"`
Domain string `env:"XMPP_DOMAIN" yaml:"domain" env-required:"true" env-description:"Server Name of the XMPP Server (the server could serve for another domain than be reachable of)"`
Jid string `env:"JID" yaml:"jid" env-required:"true"`
Password string `env:"XMPP_PASSWORD" yaml:"password" env-required:"true"`
} `yaml:"xmpp"`
}
func main() {
var cfg Config
err := cleanenv.ReadConfig("config.yml", &cfg)
if err != nil {
log.Fatal("Config: " + err.Error())
}
debuglog, err := os.OpenFile("./debug.log", syscall.O_RDWR, 0)
if err != nil {
log.Print(err)
}
xmppConfig := xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{
Address: cfg.XMPP.Address,
Domain: cfg.XMPP.Domain,
},
Jid: cfg.XMPP.Jid,
Credential: xmpp.Password(cfg.XMPP.Password),
StreamLogger: debuglog,
Insecure: true,
// TLSConfig: tls.Config{InsecureSkipVerify: true},
}
//TODO gc old ws connections
app := fiber.New()
app.Use(cors.New())
//TODO use Ctrl Message via websocket to create new connection
app.Post("/newConnection", func(c *fiber.Ctx) error {
id := clientHandler(&xmppConfig)
log.Println("Got ID: ", id)
return c.SendString(id)
})
app.Use("/ws", func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
if websocket.IsWebSocketUpgrade(c) {
c.Locals("allowed", true)
return c.Next()
}
return fiber.ErrUpgradeRequired
})
app.Get("/ws/:id", websocket.New(func(c *websocket.Conn) {
// websocket.Conn bindings https://pkg.go.dev/github.com/fasthttp/websocket?tab=doc#pkg-index
id := c.Params("id")
client, ok := clients[id]
if !ok {
c.Close()
return
}
log.Println("Connection for id: " + id)
go func() {
var message WSMessage
var err error
for {
if err = c.ReadJSON(&message); err != nil {
log.Println("read:", err)
if err == io.ErrUnexpectedEOF {
//TODO log last connection
break
}
//TODO detect normal closed connection
continue
}
client.sendChannel <- message
log.Printf("recv: %s", message)
}
}()
for {
msg := <- client.receiveChannel
if err = c.WriteJSON(msg); err != nil {
log.Println("write:", err)
//TODO what errors can possibly occur
}
}
}))
log.Fatal(app.Listen(":3000"))
}
func genID() string {
uid, err := uuid.NewUUID()
if err != nil {
log.Fatal("Couldn't generate UUID: " + err.Error())
}
return uid.String()
}
func clientHandler(config *xmpp.Config) string {
client := Client{
sendChannel: make(chan WSMessage),
receiveChannel: make(chan WSMessage),
}
router := xmpp.NewRouter()
router.HandleFunc("message", func (s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
if !ok {
log.Printf("Ignoring packet: %T\n", p)
return
}
//TODO extract username
message := WSMessage{
MessageType: Msg,
XMPPMessage: &XMPPMessage{
From: "",
To: config.Jid,
Handle: msg.From,
Body: msg.Body,
},
}
client.receiveChannel <- message
})
xmppClient, err := xmpp.NewClient(config, router, errorHandler)
if err != nil {
log.Fatalf("%+v", err)
}
client.xmppClient = xmppClient
// If you pass the xmppClient to a connection manager, it will handle the reconnect policy
// for you automatically.
cm := xmpp.NewStreamManager(xmppClient, func(c xmpp.Sender) {
joinMUC(c)
})
go func() {
for {
msg := <- client.sendChannel
sendMessage(client.xmppClient, msg.XMPPMessage)
}
}()
go func() {
log.Fatal(cm.Run())
}()
clientID := genID()
clients[clientID] = client
return clientID
}
func sendMessage(xmppClient *xmpp.Client, message *XMPPMessage) {
msg := stanza.NewMessage(stanza.Attrs{
Id: genID(),
From: xmppClient.Session.BindJid,
To: message.To,
Type: stanza.MessageTypeGroupchat,
})
msg.Body = message.Body
err := xmppClient.Send(msg)
if err != nil {
log.Fatal("Error on sending XMPPMessage: " + err.Error())
}
}
func joinMUC(s xmpp.Sender) {
client, ok := s.(*xmpp.Client)
if !ok {
log.Fatal("post connect sender not a client, cannot proceed")
}
id := genID()
//prepare presence for joining the MUC
presence := stanza.NewPresence(stanza.Attrs{
To: "[email protected]/bot-"+id,//fill the fields accordingly
From: client.Session.BindJid,
Id: id,
})
//as stated in the XEP0045 documentation, you have to tell that you are able to speak muc
presence.Extensions = append(presence.Extensions, stanza.MucPresence{})
//send the stuff and actually join the MUC
err := client.Send(presence)
if err != nil {
log.Fatal(err)
}
}
func errorHandler(err error) {
log.Fatal(err.Error())
}