152 lines
3.7 KiB
Go
152 lines
3.7 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"net/smtp"
|
||
|
"net/url"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"path"
|
||
|
"runtime"
|
||
|
"syscall"
|
||
|
"time"
|
||
|
|
||
|
"gitea.urkob.com/mcr-swiss/gogstea/config"
|
||
|
"gitea.urkob.com/mcr-swiss/gogstea/internal/mail"
|
||
|
emailsender "gitea.urkob.com/urko/emailsender/pkg/email"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
ctx, cancel := context.WithCancel(signalContext(context.Background()))
|
||
|
defer cancel()
|
||
|
|
||
|
cfgFile := os.Getenv("CONFIG_FILE")
|
||
|
if cfgFile == "" {
|
||
|
// Get root path
|
||
|
_, filename, _, _ := runtime.Caller(0)
|
||
|
cfgFile = path.Join(path.Dir(filename), "configs", "app.yml")
|
||
|
}
|
||
|
cfg, err := config.LoadConfig(cfgFile)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
ms := mail.NewMailService(
|
||
|
cfg.Email.From,
|
||
|
emailsender.NewSecure(emailsender.MailServiceConfig{
|
||
|
Auth: smtp.PlainAuth("", cfg.Email.User, cfg.Email.Password, cfg.Email.Host),
|
||
|
Host: cfg.Email.Host,
|
||
|
Port: fmt.Sprint(cfg.Email.Port),
|
||
|
From: cfg.Email.From,
|
||
|
}),
|
||
|
)
|
||
|
|
||
|
wd, err := os.Getwd()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
outputFile, err := os.Create(wd + "/notification-results.txt")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer outputFile.Close()
|
||
|
|
||
|
// http req
|
||
|
cli := http.DefaultClient
|
||
|
|
||
|
parsedURL, err := url.Parse(cfg.Gitea.URL + "/admin/users")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, parsedURL.String(), http.NoBody)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
req.Header.Add("Authorization", "token "+cfg.Gitea.ApiKey)
|
||
|
req.Header.Add("Content-Type", "application/json")
|
||
|
resp, err := cli.Do(req)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
if resp.StatusCode != http.StatusOK {
|
||
|
bts, _ := io.ReadAll(resp.Body)
|
||
|
|
||
|
if _, err := outputFile.WriteString(fmt.Sprintf("couln't get users | %d: %s\n", resp.StatusCode, string(bts))); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bts, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
var users []struct {
|
||
|
ID int `json:"id"`
|
||
|
Login string `json:"login"`
|
||
|
LoginName string `json:"login_name"`
|
||
|
FullName string `json:"full_name"`
|
||
|
Email string `json:"email"`
|
||
|
AvatarURL string `json:"avatar_url"`
|
||
|
Language string `json:"language"`
|
||
|
IsAdmin bool `json:"is_admin"`
|
||
|
LastLogin time.Time `json:"last_login"`
|
||
|
Created time.Time `json:"created"`
|
||
|
Restricted bool `json:"restricted"`
|
||
|
Active bool `json:"active"`
|
||
|
ProhibitLogin bool `json:"prohibit_login"`
|
||
|
Location string `json:"location"`
|
||
|
Website string `json:"website"`
|
||
|
Description string `json:"description"`
|
||
|
Visibility string `json:"visibility"`
|
||
|
FollowersCount int `json:"followers_count"`
|
||
|
FollowingCount int `json:"following_count"`
|
||
|
StarredReposCount int `json:"starred_repos_count"`
|
||
|
Username string `json:"username"`
|
||
|
}
|
||
|
if err := json.Unmarshal(bts, &users); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
// Loop over users and notify
|
||
|
for _, u := range users {
|
||
|
emailBody := "HTML MESSAGE"
|
||
|
// Send the new password via email.
|
||
|
// if err := ms.SendMsg(email, []byte(fmt.Sprintf("Esta esta es tu nueva contraseña: %s", newPassword))); err != nil {
|
||
|
if err := ms.Send(
|
||
|
u.Email,
|
||
|
"Password changed",
|
||
|
emailBody,
|
||
|
); err != nil {
|
||
|
if _, err := outputFile.WriteString(fmt.Sprintf("Error sending email for user %s: %v", u.Email, err)); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func signalContext(ctx context.Context) context.Context {
|
||
|
ctx, cancel := context.WithCancel(ctx)
|
||
|
|
||
|
sigs := make(chan os.Signal, 1)
|
||
|
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||
|
|
||
|
go func() {
|
||
|
<-sigs
|
||
|
signal.Stop(sigs)
|
||
|
close(sigs)
|
||
|
cancel()
|
||
|
}()
|
||
|
|
||
|
return ctx
|
||
|
}
|