improve authentification

This commit is contained in:
Martin/Geno 2019-07-17 23:55:49 +02:00 committed by Mickaël Rémond
parent 6aa942dd58
commit 0fb90abcf7

View File

@ -13,6 +13,8 @@ import (
"gosrc.io/xmpp" "gosrc.io/xmpp"
) )
var configFile = ""
var jid = "" var jid = ""
var password = "" var password = ""
@ -20,8 +22,9 @@ var receiverMUC = false
var stdIn = false var stdIn = false
var cmd = &cobra.Command{ var cmd = &cobra.Command{
Use: "sendxmpp", Use: "sendxmpp <recieve,> [message]",
Args: cobra.MinimumNArgs(1), Example: `sendxmpp to@chat.sum7.eu "Hello World!"`,
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
receiver := strings.Split(args[0], ",") receiver := strings.Split(args[0], ",")
msgText := "" msgText := ""
@ -35,19 +38,23 @@ var cmd = &cobra.Command{
var err error var err error
client, err := xmpp.NewClient(xmpp.Config{ client, err := xmpp.NewClient(xmpp.Config{
Jid: jid, Jid: viper.GetString("jid"),
Password: password, Password: viper.GetString("password"),
}, xmpp.NewRouter()) }, xmpp.NewRouter())
if err != nil { if err != nil {
log.Panicf("error on startup xmpp client: %s", err) log.Errorf("error on startup xmpp client: %s", err)
return
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(1) wg.Add(1)
cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) { cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
defer wg.Done()
log.Info("client connected") log.Info("client connected")
if receiverMUC { if receiverMUC {
for _, muc := range receiver { for _, muc := range receiver {
joinMUC(c, muc, "sendxmpp") joinMUC(c, muc, "sendxmpp")
@ -56,7 +63,6 @@ var cmd = &cobra.Command{
if !stdIn { if !stdIn {
send(c, receiver, msgText) send(c, receiver, msgText)
wg.Done()
return return
} }
@ -68,11 +74,11 @@ var cmd = &cobra.Command{
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Errorf("error on reading stdin: %s", err) log.Errorf("error on reading stdin: %s", err)
} }
wg.Done()
}) })
go func() { go func() {
err := cm.Run() err := cm.Run()
log.Panic("closed connection:", err) log.Panic("closed connection:", err)
wg.Done()
}() }()
wg.Wait() wg.Wait()
@ -82,14 +88,35 @@ var cmd = &cobra.Command{
} }
func init() { func init() {
cmd.Flags().StringVarP(&jid, "jid", "", "", "using jid (required)") cobra.OnInitialize(initConfig)
viper.BindPFlag("jid", cmd.Flags().Lookup("jid")) cmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is ~/.config/fluxxmpp.yml)")
// cmd.MarkFlagRequired("jid")
cmd.Flags().StringVarP(&password, "password", "", "", "using password for your jid (required)") cmd.Flags().StringP("jid", "", "", "using jid (required)")
viper.BindPFlag("jid", cmd.Flags().Lookup("jid"))
cmd.Flags().StringP("password", "", "", "using password for your jid (required)")
viper.BindPFlag("password", cmd.Flags().Lookup("password")) viper.BindPFlag("password", cmd.Flags().Lookup("password"))
// cmd.MarkFlagRequired("password")
cmd.Flags().BoolVarP(&stdIn, "stdin", "i", false, "read from stdin instatt of 2. argument") cmd.Flags().BoolVarP(&stdIn, "stdin", "i", false, "read from stdin instatt of 2. argument")
cmd.Flags().BoolVarP(&receiverMUC, "muc", "m", false, "reciever is a muc (join it before sending messages)") cmd.Flags().BoolVarP(&receiverMUC, "muc", "m", false, "reciever is a muc (join it before sending messages)")
} }
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if configFile != "" {
viper.SetConfigFile(configFile)
}
viper.SetConfigName("fluxxmpp")
viper.AddConfigPath("/etc/")
viper.AddConfigPath("$HOME/.config")
viper.AddConfigPath(".")
viper.SetEnvPrefix("FLUXXMPP")
viper.AutomaticEnv()
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
log.Warnf("no configuration found (somebody could read your password from progress argument list): %s", err)
}
}