gateway: improve logging to be more privat

This commit is contained in:
Geno 2021-09-14 09:31:45 +02:00
parent 748e4ca5d7
commit 52fa083eae
3 changed files with 26 additions and 21 deletions

View File

@ -1,8 +1,13 @@
endpoint_url = "https://up.chat.sum7.eu" endpoint_url = "https://up.chat.sum7.eu"
jwt_secret = "CHANGEME"
[log]
level = 40
timestamp = false
[webserver] [webserver]
listen = ":8080" listen = ":8080"
access_log = true access_log = false
webroot = "" webroot = ""
# if ACME / Let's Encrypt used, uncomment listen # if ACME / Let's Encrypt used, uncomment listen

View File

@ -8,6 +8,7 @@ import (
apiStatus "dev.sum7.eu/genofire/golang-lib/web/api/status" apiStatus "dev.sum7.eu/genofire/golang-lib/web/api/status"
webM "dev.sum7.eu/genofire/golang-lib/web/metrics" webM "dev.sum7.eu/genofire/golang-lib/web/metrics"
"github.com/bdlm/log" "github.com/bdlm/log"
"github.com/bdlm/std/logger"
) )
var VERSION = "development" var VERSION = "development"
@ -17,6 +18,10 @@ type configData struct {
EndpointURL string `toml:"endpoint_url"` EndpointURL string `toml:"endpoint_url"`
XMPP XMPPService `toml:"xmpp"` XMPP XMPPService `toml:"xmpp"`
Webserver web.Service `toml:"webserver"` Webserver web.Service `toml:"webserver"`
Log struct {
Level uint32 `toml:"level"`
Timestamp bool `toml:"timestamp"`
} `toml:"log"`
} }
func main() { func main() {
@ -41,6 +46,12 @@ func main() {
if err := file.ReadTOML(configPath, config); err != nil { if err := file.ReadTOML(configPath, config); err != nil {
log.Panicf("open config file: %s", err) log.Panicf("open config file: %s", err)
} }
log.SetLevel(logger.Level(config.Log.Level))
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: !config.Log.Timestamp,
})
// just for more beautiful config file - jere // just for more beautiful config file - jere
config.XMPP.EndpointURL = config.EndpointURL config.XMPP.EndpointURL = config.EndpointURL
config.XMPP.JWTSecret = config.JWTSecret config.XMPP.JWTSecret = config.JWTSecret

View File

@ -42,20 +42,15 @@ func (s *XMPPService) Run() error {
return err return err
} }
defer func() { defer func() {
log.Info("Closing session…") log.Info("closing xmpp connection")
if err := s.session.Close(); err != nil { if err := s.session.Close(); err != nil {
log.Errorf("Error closing session: %q", err) log.Errorf("Error closing session: %q", err)
} }
log.Println("Closing conn…") log.Info("closing xmpp connection")
if err := s.session.Conn().Close(); err != nil { if err := s.session.Conn().Close(); err != nil {
log.Errorf("Error closing connection: %q", err) log.Errorf("Error closing connection: %q", err)
} }
}() }()
/* Send initial presence to let the server know we want to receive messages.
err = s.session.Send(context.TODO(), stanza.Presence{Type: stanza.AvailablePresence}.Wrap(nil))
if err != nil {
return err
}*/
log.Infof("connected with %s", s.session.LocalAddr()) log.Infof("connected with %s", s.session.LocalAddr())
return s.session.Serve(mux.New( return s.session.Serve(mux.New(
// register - get + set // register - get + set
@ -66,12 +61,6 @@ func (s *XMPPService) Run() error {
mux.IQFunc(stanza.GetIQ, xml.Name{Local: messages.LocalUnregister, Space: messages.Space}, s.handleUnregister), mux.IQFunc(stanza.GetIQ, xml.Name{Local: messages.LocalUnregister, Space: messages.Space}, s.handleUnregister),
// mux.IQFunc("", xml.Name{}, s.handleDisco), // mux.IQFunc("", xml.Name{}, s.handleDisco),
)) ))
/* -
return s.session.Serve(xmpp.HandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
log.Info(start)
return nil
}))
*/
} }
func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error { func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
@ -93,13 +82,13 @@ func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder,
tokenData := messages.TokenData{} tokenData := messages.TokenData{}
err := xml.NewTokenDecoder(t).Decode(&tokenData) err := xml.NewTokenDecoder(t).Decode(&tokenData)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Errorf("decoding message: %q", err) log.Warnf("decoding message: %q", err)
reply.Register.Error = &messages.ErrorData{Body: "unable decode"} reply.Register.Error = &messages.ErrorData{Body: "unable decode"}
return nil return nil
} }
token := tokenData.Body token := tokenData.Body
if token == "" { if token == "" {
log.Errorf("no token found: %v", token) log.Warnf("no token found: %v", token)
reply.Register.Error = &messages.ErrorData{Body: "no token"} reply.Register.Error = &messages.ErrorData{Body: "no token"}
return nil return nil
} }
@ -112,7 +101,7 @@ func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder,
endpoint := s.EndpointURL + "/UP?token=" + jwt endpoint := s.EndpointURL + "/UP?token=" + jwt
reply.IQ.Type = stanza.ResultIQ reply.IQ.Type = stanza.ResultIQ
reply.Register.Endpoint = &messages.EndpointData{Body: endpoint} reply.Register.Endpoint = &messages.EndpointData{Body: endpoint}
log.Infof("generate respone: %v", endpoint) log.Debugf("generate respone: %v", endpoint)
return nil return nil
} }
func (s *XMPPService) handleUnregister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error { func (s *XMPPService) handleUnregister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
@ -129,7 +118,7 @@ func (s *XMPPService) handleUnregister(iq stanza.IQ, t xmlstream.TokenReadEncode
log.Errorf("sending unregister response: %v", err) log.Errorf("sending unregister response: %v", err)
} }
}() }()
log.Infof("unregistered unhandled: %v", start) log.Debugf("unregistered unhandled: %v", start)
reply.Unregister.Error = "not implemented" reply.Unregister.Error = "not implemented"
return nil return nil
@ -147,16 +136,16 @@ func (s *XMPPService) handleDisco(iq stanza.IQ, t xmlstream.TokenReadEncoder, st
log.Errorf("sending response: %v", err) log.Errorf("sending response: %v", err)
} }
}() }()
log.Infof("recieved iq: %v", iq) log.Debugf("recieved iq: %v", iq)
return nil return nil
} }
// SendMessage of an UP Notification // SendMessage of an UP Notification
func (s *XMPPService) SendMessage(to jid.JID, token, content string) error { func (s *XMPPService) SendMessage(to jid.JID, token, content string) error {
log.WithFields(map[string]interface{}{ log.WithFields(map[string]interface{}{
"to": to, "to": to.String(),
"token": token, "token": token,
}).Info("forward message to xmpp") }).Debug("forward message to xmpp")
return s.session.Encode(context.TODO(), messages.Message{ return s.session.Encode(context.TODO(), messages.Message{
Message: stanza.Message{ Message: stanza.Message{
To: to, To: to,