Added XMPP service to handle clients and configurations
This commit is contained in:
4
.vscode/settings.json
vendored
Normal file
4
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"go.gopath": "/home/nedim/go",
|
||||
"go.useLanguageServer": true
|
||||
}
|
||||
88
main.go
88
main.go
@@ -1,83 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
appConfig "github.com/xmpploadtesting/config"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
xmppService "github.com/xmpploadtesting/services"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
var client *xmpp.Client
|
||||
var err error
|
||||
|
||||
// LOAD APPLICATION CONFIGURATION
|
||||
appConfig.Load()
|
||||
|
||||
// Setup xmpp config
|
||||
config := &xmpp.Config{
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port,
|
||||
Domain: appConfig.AppConfig.Service.Domain,
|
||||
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Jid: "admin@localhost",
|
||||
Credential: xmpp.Password("openadmin123"),
|
||||
StreamLogger: os.Stdout,
|
||||
Insecure: true,
|
||||
}
|
||||
xmppService.Init()
|
||||
xmppServiceInstance := xmppService.Instance()
|
||||
xmppService.SendOnlinePresenceStanza(xmppServiceInstance.XMPPClients[0].Client, xmppServiceInstance.XMPPClients[0].Config.Jid)
|
||||
|
||||
// Setup xmpp router
|
||||
router := xmpp.NewRouter()
|
||||
router.HandleFunc("message", handleMessage)
|
||||
log.Printf("Adding message")
|
||||
// // Setup new xmpp client
|
||||
// if client, err = xmpp.NewClient(config, router, errorHandler); err != nil {
|
||||
// log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err))
|
||||
// }
|
||||
|
||||
// Setup new xmpp client
|
||||
if client, err = xmpp.NewClient(config, router, errorHandler); err != nil {
|
||||
log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err))
|
||||
}
|
||||
// // Client connection
|
||||
// if err := client.Connect(); err != nil {
|
||||
// msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
||||
|
||||
// Client connection
|
||||
if err := client.Connect(); err != nil {
|
||||
msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
||||
// fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
||||
// return
|
||||
// }
|
||||
|
||||
fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
||||
return
|
||||
}
|
||||
// onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: config.Jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
|
||||
|
||||
onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: config.Jid, Type: stanza.StanzaType(stanza.PresenceShowAway)})
|
||||
// // Send onlinePresence packet indefiletly delayed by DelayBetweenMeassges from configuration
|
||||
// for {
|
||||
|
||||
// Send onlinePresence packet indefiletly delayed by DelayBetweenMeassges from configuration
|
||||
for {
|
||||
|
||||
err = client.Send(onlinePresencePacket)
|
||||
log.Printf("Sending online presence stanza: %v", err)
|
||||
// Delay before sending another message
|
||||
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
|
||||
}
|
||||
}
|
||||
|
||||
func handleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||
log.Printf("Handle message")
|
||||
msg, ok := p.(stanza.Message)
|
||||
if !ok {
|
||||
log.Printf(" message not OK")
|
||||
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
|
||||
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
|
||||
err := s.Send(reply)
|
||||
log.Printf("Error sending message %v", err)
|
||||
}
|
||||
|
||||
func errorHandler(err error) {
|
||||
fmt.Println(err.Error())
|
||||
// err = client.Send(onlinePresencePacket)
|
||||
// log.Printf("Sending online presence stanza: %v", err)
|
||||
// // Delay before sending another message
|
||||
// time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
|
||||
// }
|
||||
//
|
||||
}
|
||||
|
||||
120
services/xmppsService.go
Normal file
120
services/xmppsService.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
appConfig "github.com/xmpploadtesting/config"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
// XMPPService - struct containing
|
||||
type XMPPService struct {
|
||||
Router *xmpp.Router
|
||||
XMPPClients []XMPPClient
|
||||
}
|
||||
|
||||
// XMPPClient - struct containing client and client configuration
|
||||
type XMPPClient struct {
|
||||
Config xmpp.Config
|
||||
Client *xmpp.Client
|
||||
}
|
||||
|
||||
// Instance - get instance of xmpp service
|
||||
func Instance() *XMPPService {
|
||||
return &xmppService
|
||||
}
|
||||
|
||||
var xmppService XMPPService
|
||||
|
||||
// Init Initialise XMPP servie, and router
|
||||
func Init() {
|
||||
// TODO load configs and init clients from the config file
|
||||
xmppService = XMPPService{
|
||||
Router: xmpp.NewRouter(),
|
||||
// Configs: []xmpp.Config{
|
||||
// {
|
||||
// TransportConfiguration: xmpp.TransportConfiguration{
|
||||
// Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port,
|
||||
// Domain: appConfig.AppConfig.Service.Domain,
|
||||
// TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
// },
|
||||
// Jid: "admin@localhost",
|
||||
// Credential: xmpp.Password("openadmin123"),
|
||||
// StreamLogger: os.Stdout,
|
||||
// Insecure: true,
|
||||
// },
|
||||
// },
|
||||
}
|
||||
xmppService.Router.HandleFunc("message", handleMessage)
|
||||
|
||||
xmppService.XMPPClients = make([]XMPPClient, 0)
|
||||
xmppClient := XMPPClient{
|
||||
Config: xmpp.Config{
|
||||
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port,
|
||||
Domain: appConfig.AppConfig.Service.Domain,
|
||||
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Jid: "admin@localhost",
|
||||
Credential: xmpp.Password("openadmin123"),
|
||||
StreamLogger: os.Stdout,
|
||||
Insecure: true,
|
||||
},
|
||||
}
|
||||
|
||||
client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler)
|
||||
|
||||
// Client connection
|
||||
if err := client.Connect(); err != nil {
|
||||
msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
||||
|
||||
fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to initialise client for %v", xmppClient.Config.Jid)
|
||||
}
|
||||
xmppClient.Client = client
|
||||
xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient)
|
||||
|
||||
}
|
||||
|
||||
// SendOnlinePresenceStanza - send online presence stnza with data, to server
|
||||
func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
|
||||
onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
|
||||
|
||||
//TODO All clients should send messages at once
|
||||
for {
|
||||
|
||||
err := client.Send(onlinePresencePacket)
|
||||
log.Printf("Sending online presence stanza: %v", err)
|
||||
// Delay before sending another message
|
||||
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
|
||||
}
|
||||
}
|
||||
|
||||
func handleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||
log.Printf("Handle message")
|
||||
msg, ok := p.(stanza.Message)
|
||||
if !ok {
|
||||
log.Printf(" message not OK")
|
||||
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
|
||||
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
|
||||
err := s.Send(reply)
|
||||
log.Printf("Error sending message %v", err)
|
||||
}
|
||||
|
||||
func errorHandler(err error) {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
Reference in New Issue
Block a user