Add first part for XMPP side
This commit is contained in:
parent
44a8e325c6
commit
4be53d2f7e
23
http/http.go
23
http/http.go
|
|
@ -9,6 +9,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
LogInfo = "\t[HTTP INFO]\t"
|
||||||
|
LogError = "\t[HTTP ERROR]\t"
|
||||||
|
LogDebug = "\t[HTTP DEBUG]\t"
|
||||||
|
|
||||||
PARAM_JID = "jid"
|
PARAM_JID = "jid"
|
||||||
METHOD_ACCESS = "method"
|
METHOD_ACCESS = "method"
|
||||||
DOMAIN_ACCESS = "domain"
|
DOMAIN_ACCESS = "domain"
|
||||||
|
|
@ -32,12 +36,18 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func authHandler(w http.ResponseWriter, r *http.Request) {
|
func authHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
jid := strings.Join(r.Form[PARAM_JID], "")
|
||||||
|
method := strings.Join(r.Form[METHOD_ACCESS], "")
|
||||||
|
domain := strings.Join(r.Form[DOMAIN_ACCESS], "")
|
||||||
|
transaction := strings.Join(r.Form[TRANSACTION_ID], "")
|
||||||
|
log.Printf("%sAuth %s", LogDebug, jid)
|
||||||
|
|
||||||
chanAnswer := make(chan bool)
|
chanAnswer := make(chan bool)
|
||||||
|
|
||||||
ChanRequest <- strings.Join(r.Form[PARAM_JID], "")
|
ChanRequest <- jid
|
||||||
ChanRequest <- strings.Join(r.Form[METHOD_ACCESS], "")
|
ChanRequest <- method
|
||||||
ChanRequest <- strings.Join(r.Form[DOMAIN_ACCESS], "")
|
ChanRequest <- domain
|
||||||
ChanRequest <- strings.Join(r.Form[TRANSACTION_ID], "")
|
ChanRequest <- transaction
|
||||||
ChanRequest <- chanAnswer
|
ChanRequest <- chanAnswer
|
||||||
|
|
||||||
answer := <- chanAnswer
|
answer := <- chanAnswer
|
||||||
|
|
@ -51,13 +61,14 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
|
log.Printf("%sRunning", LogInfo)
|
||||||
http.HandleFunc("/", indexHandler) // set router
|
http.HandleFunc("/", indexHandler) // set router
|
||||||
http.HandleFunc("/toto", authHandler)
|
http.HandleFunc("/toto", authHandler)
|
||||||
|
|
||||||
port := strconv.Itoa(HttpPortBind)
|
port := strconv.Itoa(HttpPortBind)
|
||||||
log.Println("Listenning on port "+port)
|
log.Printf("%sListenning on port %s", LogInfo, port)
|
||||||
err := http.ListenAndServe(":"+port, nil) // set listen port
|
err := http.ListenAndServe(":"+port, nil) // set listen port
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("ListenAndServe: ", err)
|
log.Fatal("%sListenAndServe: ", LogError, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
# XMPP informations
|
||||||
|
xmpp_server_address=192.168.1.2
|
||||||
|
xmpp_server_port=5347
|
||||||
|
xmpp_hostname=xmppsteam.kingpenguin.tk
|
||||||
|
xmpp_secret=xmpp4steam_password
|
||||||
|
xmpp_debug=true
|
||||||
39
main.go
39
main.go
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
|
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
|
||||||
|
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
|
||||||
|
|
||||||
"github.com/jimlawless/cfg"
|
"github.com/jimlawless/cfg"
|
||||||
|
|
||||||
|
|
@ -26,32 +27,46 @@ func init() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to load configuration file.", err)
|
log.Fatal("Failed to load configuration file.", err)
|
||||||
}
|
}
|
||||||
// TODO make config
|
|
||||||
|
// XMPP config
|
||||||
|
xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"]
|
||||||
|
xmpp.JidStr = mapConfig["xmpp_hostname"]
|
||||||
|
xmpp.Secret = mapConfig["xmpp_secret"]
|
||||||
|
xmpp.Debug = mapConfig["xmpp_debug"] == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func request() {
|
func request() {
|
||||||
for {
|
for {
|
||||||
jid := <- http.ChanRequest
|
client := new(xmpp.Client)
|
||||||
log.Println(jid)
|
|
||||||
method := <- http.ChanRequest
|
client.JID = getChanString(http.ChanRequest)
|
||||||
log.Println(method)
|
client.Method = getChanString(http.ChanRequest)
|
||||||
domain := <- http.ChanRequest
|
client.Domain = getChanString(http.ChanRequest)
|
||||||
log.Println(domain)
|
client.Transaction = getChanString(http.ChanRequest)
|
||||||
transaction := <- http.ChanRequest
|
|
||||||
log.Println(transaction)
|
|
||||||
|
|
||||||
chanResult := <- http.ChanRequest
|
chanResult := <- http.ChanRequest
|
||||||
// TODO make the XMPP request
|
|
||||||
if v, ok := chanResult.(chan bool); ok {
|
if v, ok := chanResult.(chan bool); ok {
|
||||||
v <- false
|
client.ChanReply = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go client.QueryClient()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getChanString(c chan interface{}) string {
|
||||||
|
ret := ""
|
||||||
|
i := <- c
|
||||||
|
if v, ok := i.(string); ok {
|
||||||
|
ret = v
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// TODO start ressources
|
|
||||||
go http.Run()
|
go http.Run()
|
||||||
|
go xmpp.Run()
|
||||||
go request()
|
go request()
|
||||||
|
|
||||||
sigchan := make(chan os.Signal, 1)
|
sigchan := make(chan os.Signal, 1)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package xmpp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
JID string
|
||||||
|
Method string
|
||||||
|
Domain string
|
||||||
|
Transaction string
|
||||||
|
|
||||||
|
ChanReply chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (client *Client) QueryClient() {
|
||||||
|
log.Printf("%sQuery JID %s", LogInfo, client.JID)
|
||||||
|
client.ChanReply <- false
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
package xmpp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
|
||||||
|
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LogInfo = "\t[XMPP COMPONENT INFO]\t"
|
||||||
|
LogError = "\t[XMPP COMPONENT ERROR]\t"
|
||||||
|
LogDebug = "\t[XMPP COMPONENT DEBUG]\t"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Addr = "127.0.0.1:5347"
|
||||||
|
JidStr = ""
|
||||||
|
Secret = ""
|
||||||
|
|
||||||
|
SoftVersion = ""
|
||||||
|
|
||||||
|
jid xmpp.JID
|
||||||
|
stream = new(xmpp.Stream)
|
||||||
|
comp = new(xmpp.XMPP)
|
||||||
|
|
||||||
|
ChanAction = make(chan string)
|
||||||
|
|
||||||
|
Debug = true
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func Run() {
|
||||||
|
log.Printf("%sRunning", LogInfo)
|
||||||
|
// Create stream and configure it as a component connection.
|
||||||
|
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
|
||||||
|
stream = must(xmpp.NewStream(Addr, &xmpp.StreamConfig{LogStanzas: Debug})).(*xmpp.Stream)
|
||||||
|
comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP)
|
||||||
|
|
||||||
|
mainXMPP()
|
||||||
|
log.Printf("%sReach main method's end", LogInfo)
|
||||||
|
go Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func mainXMPP() {
|
||||||
|
for x := range comp.In {
|
||||||
|
switch v := x.(type) {
|
||||||
|
case *xmpp.Presence:
|
||||||
|
|
||||||
|
case *xmpp.Message:
|
||||||
|
|
||||||
|
case *xmpp.Iq:
|
||||||
|
switch v.PayloadName().Space {
|
||||||
|
case xmpp.NSDiscoItems:
|
||||||
|
execDiscoCommand(v)
|
||||||
|
|
||||||
|
case xmpp.NSVCardTemp:
|
||||||
|
reply := v.Response(xmpp.IQTypeResult)
|
||||||
|
vcard := &xmpp.VCard{}
|
||||||
|
reply.PayloadEncode(vcard)
|
||||||
|
comp.Out <- reply
|
||||||
|
|
||||||
|
case xmpp.NSJabberClient:
|
||||||
|
reply := v.Response(xmpp.IQTypeResult)
|
||||||
|
reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "HTTP authentification component", Version: SoftVersion})
|
||||||
|
comp.Out <- reply
|
||||||
|
|
||||||
|
default:
|
||||||
|
reply := v.Response(xmpp.IQTypeError)
|
||||||
|
reply.PayloadEncode(xmpp.NewError("cancel", xmpp.FeatureNotImplemented, ""))
|
||||||
|
comp.Out <- reply
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.Printf("%srecv: %v", LogDebug, x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func must(v interface{}, err error) interface{} {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(LogError, err)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func SendMessage(to, subject, message string) {
|
||||||
|
m := xmpp.Message{From: jid.Domain, To: to, Body: message, Type: "chat"}
|
||||||
|
|
||||||
|
if subject != "" {
|
||||||
|
m.Subject = subject
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%sSenp message %v", LogInfo, m)
|
||||||
|
comp.Out <- m
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func execDiscoCommand(iq *xmpp.Iq) {
|
||||||
|
log.Printf("%sDiscovery item iq received", LogInfo)
|
||||||
|
reply := iq.Response(xmpp.IQTypeResult)
|
||||||
|
discoItem := &xmpp.DiscoItems{Node: xmpp.NodeAdHocCommand}
|
||||||
|
reply.PayloadEncode(discoItem)
|
||||||
|
comp.Out <- reply
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue