Add HTTP port to config file

This commit is contained in:
Chteufleur 2016-06-20 19:20:13 +02:00
parent b9f3db7998
commit df795bf0ce
5 changed files with 132 additions and 132 deletions

View File

@ -1,14 +1,14 @@
package http package http
import ( import (
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp" "git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
const ( const (
@ -16,68 +16,65 @@ const (
LogError = "\t[HTTP ERROR]\t" LogError = "\t[HTTP ERROR]\t"
LogDebug = "\t[HTTP DEBUG]\t" LogDebug = "\t[HTTP DEBUG]\t"
PARAM_JID = "jid" PARAM_JID = "jid"
METHOD_ACCESS = "method" METHOD_ACCESS = "method"
DOMAIN_ACCESS = "domain" DOMAIN_ACCESS = "domain"
TRANSACTION_ID = "transaction_id" TRANSACTION_ID = "transaction_id"
ROUTE_ROOT = "/" ROUTE_ROOT = "/"
ROUTE_AUTH = "/toto" ROUTE_AUTH = "/auth"
RETURN_VALUE_OK = "OK" RETURN_VALUE_OK = "OK"
RETURN_VALUE_NOK = "NOK" RETURN_VALUE_NOK = "NOK"
) )
var ( var (
HttpPortBind = 9090 HttpPortBind = 9090
ChanRequest = make(chan interface{}, 5) ChanRequest = make(chan interface{}, 5)
TimeoutSec = 60 TimeoutSec = 60
) )
func indexHandler(w http.ResponseWriter, r *http.Request) { func indexHandler(w http.ResponseWriter, r *http.Request) {
// TODO // TODO
fmt.Fprintf(w, "Welcome to HTTP authentification over XMPP") fmt.Fprintf(w, "Welcome to HTTP authentification over XMPP")
} }
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], "") jid := strings.Join(r.Form[PARAM_JID], "")
method := strings.Join(r.Form[METHOD_ACCESS], "") method := strings.Join(r.Form[METHOD_ACCESS], "")
domain := strings.Join(r.Form[DOMAIN_ACCESS], "") domain := strings.Join(r.Form[DOMAIN_ACCESS], "")
transaction := strings.Join(r.Form[TRANSACTION_ID], "") transaction := strings.Join(r.Form[TRANSACTION_ID], "")
log.Printf("%sAuth %s", LogDebug, jid) log.Printf("%sAuth %s", LogDebug, jid)
chanAnswer := make(chan bool) chanAnswer := make(chan bool)
ChanRequest <- jid ChanRequest <- jid
ChanRequest <- method ChanRequest <- method
ChanRequest <- domain ChanRequest <- domain
ChanRequest <- transaction ChanRequest <- transaction
ChanRequest <- chanAnswer ChanRequest <- chanAnswer
select { select {
case answer = <- chanAnswer: case answer = <-chanAnswer:
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
case <- time.After(time.Duration(TimeoutSec) * time.Second): case <-time.After(time.Duration(TimeoutSec) * time.Second):
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
delete(xmpp.WaitMessageAnswers, transaction) delete(xmpp.WaitMessageAnswers, transaction)
} }
} }
func Run() { func Run() {
log.Printf("%sRunning", LogInfo) log.Printf("%sRunning", LogInfo)
http.HandleFunc(ROUTE_ROOT, indexHandler) http.HandleFunc(ROUTE_ROOT, indexHandler)
http.HandleFunc(ROUTE_AUTH, authHandler) http.HandleFunc(ROUTE_AUTH, authHandler)
port := strconv.Itoa(HttpPortBind) port := strconv.Itoa(HttpPortBind)
log.Printf("%sListenning on port %s", LogInfo, 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("%sListenAndServe: ", LogError, err) log.Fatal("%sListenAndServe: ", LogError, err)
} }
} }

View File

@ -4,3 +4,5 @@ xmpp_server_port=5347
xmpp_hostname=xmppsteam.kingpenguin.tk xmpp_hostname=xmppsteam.kingpenguin.tk
xmpp_secret=xmpp4steam_password xmpp_secret=xmpp4steam_password
xmpp_debug=true xmpp_debug=true
http_port=9090

100
main.go
View File

@ -1,15 +1,15 @@
package main 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" "git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"github.com/jimlawless/cfg" "github.com/jimlawless/cfg"
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv" "strconv"
"syscall" "syscall"
"time" "time"
) )
@ -24,67 +24,71 @@ var (
) )
func init() { func init() {
err := cfg.Load(configurationFilePath, mapConfig) err := cfg.Load(configurationFilePath, mapConfig)
if err != nil { if err != nil {
log.Fatal("Failed to load configuration file.", err) log.Fatal("Failed to load configuration file.", err)
} }
// HTTP config // HTTP config
httpTimeout, err := strconv.Atoi(mapConfig["http_timeoute_sec"]) httpTimeout, err := strconv.Atoi(mapConfig["http_timeoute_sec"])
if err == nil { if err == nil {
log.Println("Define HTTP timeout to %d second", httpTimeout) log.Println("Define HTTP timeout to %d second", httpTimeout)
http.TimeoutSec = httpTimeout http.TimeoutSec = httpTimeout
} }
httpPort, err := strconv.Atoi(mapConfig["http_port"])
if err == nil {
log.Println("Define HTTP port to %d", httpPort)
http.HttpPortBind = httpPort
}
// XMPP config // XMPP config
xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"] xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"]
xmpp.JidStr = mapConfig["xmpp_hostname"] xmpp.JidStr = mapConfig["xmpp_hostname"]
xmpp.Secret = mapConfig["xmpp_secret"] xmpp.Secret = mapConfig["xmpp_secret"]
xmpp.Debug = mapConfig["xmpp_debug"] == "true" xmpp.Debug = mapConfig["xmpp_debug"] == "true"
} }
func request() { func request() {
for { for {
client := new(xmpp.Client) client := new(xmpp.Client)
client.JID = getChanString(http.ChanRequest) client.JID = getChanString(http.ChanRequest)
client.Method = getChanString(http.ChanRequest) client.Method = getChanString(http.ChanRequest)
client.Domain = getChanString(http.ChanRequest) client.Domain = getChanString(http.ChanRequest)
client.Transaction = getChanString(http.ChanRequest) client.Transaction = getChanString(http.ChanRequest)
chanResult := <- http.ChanRequest chanResult := <-http.ChanRequest
if v, ok := chanResult.(chan bool); ok { if v, ok := chanResult.(chan bool); ok {
client.ChanReply = v client.ChanReply = v
} }
go client.QueryClient() go client.QueryClient()
} }
} }
func getChanString(c chan interface{}) string { func getChanString(c chan interface{}) string {
ret := "" ret := ""
i := <- c i := <-c
if v, ok := i.(string); ok { if v, ok := i.(string); ok {
ret = v ret = v
} }
return ret return ret
} }
func main() { func main() {
go http.Run() go http.Run()
go xmpp.Run() go xmpp.Run()
go request() go request()
sigchan := make(chan os.Signal, 1) sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt) signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM) signal.Notify(sigchan, syscall.SIGTERM)
signal.Notify(sigchan, os.Kill) signal.Notify(sigchan, os.Kill)
<-sigchan <-sigchan
// TODO close all ressources // TODO close all ressources
log.Println("Exit main()") log.Println("Exit main()")
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }

View File

@ -1,50 +1,49 @@
package xmpp package xmpp
import ( import (
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp" "git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
"log" "log"
"strconv" "strconv"
) )
type Client struct { type Client struct {
JID string JID string
Method string Method string
Domain string Domain string
Transaction string Transaction string
ChanReply chan bool ChanReply chan bool
} }
func (client *Client) QueryClient() { func (client *Client) QueryClient() {
log.Printf("%sQuery JID %s", LogInfo, client.JID) log.Printf("%sQuery JID %s", LogInfo, client.JID)
clientJID, _ := xmpp.ParseJID(client.JID) clientJID, _ := xmpp.ParseJID(client.JID)
if clientJID.Resource == "" { if clientJID.Resource == "" {
client.askViaMessage() client.askViaMessage()
} else { } else {
client.askViaIQ() client.askViaIQ()
} }
} }
func (client *Client) askViaIQ() { func (client *Client) askViaIQ() {
stanzaID++ stanzaID++
stanzaIDstr := strconv.Itoa(stanzaID) stanzaIDstr := strconv.Itoa(stanzaID)
m := xmpp.Iq{Type: xmpp.IQTypeGet, To: client.JID, From: jid.Domain, Id: stanzaIDstr} m := xmpp.Iq{Type: xmpp.IQTypeGet, To: client.JID, From: jid.Domain, Id: stanzaIDstr}
confirm := &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain} confirm := &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
m.PayloadEncode(confirm) m.PayloadEncode(confirm)
WaitMessageAnswers[stanzaIDstr] = client WaitMessageAnswers[stanzaIDstr] = client
comp.Out <- m comp.Out <- m
} }
func (client *Client) askViaMessage() { func (client *Client) askViaMessage() {
m := xmpp.Message{From: jid.Domain, To: client.JID, Type: "normal"} m := xmpp.Message{From: jid.Domain, To: client.JID, Type: "normal"}
m.Thread = xmpp.SessionID() m.Thread = xmpp.SessionID()
m.Body = "Auth request for "+client.Domain+".\nTransaction identifier is: "+client.Transaction+"\nReply to this message to confirm the request." m.Body = "Auth request for " + client.Domain + ".\nTransaction identifier is: " + client.Transaction + "\nReply to this message to confirm the request."
m.Confir = &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain} m.Confir = &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
log.Printf("%sSenp message %v", LogInfo, m) log.Printf("%sSenp message %v", LogInfo, m)
WaitMessageAnswers[client.Transaction] = client WaitMessageAnswers[client.Transaction] = client
comp.Out <- m comp.Out <- m
} }

View File

@ -27,13 +27,11 @@ var (
ChanAction = make(chan string) ChanAction = make(chan string)
WaitMessageAnswers = make(map[string]*Client) WaitMessageAnswers = make(map[string]*Client)
Debug = true Debug = true
) )
func Run() { func Run() {
log.Printf("%sRunning", LogInfo) log.Printf("%sRunning", LogInfo)
// Create stream and configure it as a component connection. // Create stream and configure it as a component connection.