Browse Source

add capabilities to auto logout if authenticated user is missmatch with registered jid

pull/28/head
Dimas Restu H 4 years ago
parent
commit
871a0b834a
  1. 22
      cmd/main/main.go
  2. 1
      go.mod
  3. 2
      go.sum
  4. 37
      internal/routines.go

22
cmd/main/main.go

@ -26,6 +26,8 @@ import (
"syscall" "syscall"
"time" "time"
cron "github.com/robfig/cron/v3"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
@ -54,6 +56,11 @@ func (ev *EchoValidator) Validate(i interface{}) error {
func main() { func main() {
var err error var err error
// Intialize Cron
c := cron.New(cron.WithChain(
cron.Recover(cron.DiscardLogger),
), cron.WithSeconds())
// Initialize Echo // Initialize Echo
e := echo.New() e := echo.New()
@ -115,6 +122,9 @@ func main() {
// Running Startup Tasks // Running Startup Tasks
internal.Startup() internal.Startup()
// Running Routines Tasks
internal.Routines(c)
// Get Server Configuration // Get Server Configuration
var serverConfig Server var serverConfig Server
@ -138,17 +148,19 @@ func main() {
// Watch for Shutdown Signal // Watch for Shutdown Signal
sigShutdown := make(chan os.Signal, 1) sigShutdown := make(chan os.Signal, 1)
signal.Notify(sigShutdown, os.Interrupt)
signal.Notify(sigShutdown, syscall.SIGTERM)
signal.Notify(sigShutdown, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-sigShutdown <-sigShutdown
// Wait 5 Seconds Before Graceful Shutdown // Wait 5 Seconds Before Graceful Shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelShutdown()
// Try To Shutdown Server // Try To Shutdown Server
err = e.Shutdown(ctx)
err = e.Shutdown(ctxShutdown)
if err != nil { if err != nil {
log.Print(nil).Fatal(err.Error()) log.Print(nil).Fatal(err.Error())
} }
// Try To Shutdown Cron
c.Stop()
} }

1
go.mod

@ -10,6 +10,7 @@ require (
github.com/labstack/echo/v4 v4.7.2 github.com/labstack/echo/v4 v4.7.2
github.com/lib/pq v1.10.6 github.com/lib/pq v1.10.6
github.com/nickalie/go-webpbin v0.0.0-20220110095747-f10016bf2dc1 github.com/nickalie/go-webpbin v0.0.0-20220110095747-f10016bf2dc1
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/sunshineplan/imgconv v1.0.5 github.com/sunshineplan/imgconv v1.0.5

2
go.sum

@ -74,6 +74,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=

37
internal/routines.go

@ -0,0 +1,37 @@
package internal
import (
"github.com/robfig/cron/v3"
"github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/log"
pkgWhatsApp "github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/whatsapp"
)
func Routines(cron *cron.Cron) {
log.Print(nil).Info("Running Routine Tasks")
cron.AddFunc("0 * * * * *", func() {
// If WhatsAppClient Connection is more than 0
if len(pkgWhatsApp.WhatsAppClient) > 0 {
// Check Every Authenticated MSISDN
for jid, client := range pkgWhatsApp.WhatsAppClient {
// Get Real JID from Datastore
realJID := client.Store.ID.User
// Check WhatsAppClient Registered JID with Authenticated MSISDN
if jid != realJID {
// Mask JID for Logging Information
maskJID := realJID[0:len(realJID)-4] + "xxxx"
log.Print(nil).Info("Logging out WhatsApp Client for " + maskJID + " Due to Missmatch Authentication")
// Logout WhatsAppClient Device
_ = pkgWhatsApp.WhatsAppLogout(jid)
delete(pkgWhatsApp.WhatsAppClient, jid)
}
}
}
})
cron.Start()
}
Loading…
Cancel
Save