2023-01-07 21:03:51 +05:30
package pages
import (
2023-01-21 22:19:17 +05:30
"github.com/ProjectSegfault/publapi/utils"
2023-01-07 21:03:51 +05:30
"github.com/containrrr/shoutrrr"
2023-02-05 19:00:47 +05:30
"github.com/gofiber/fiber/v2"
2023-01-07 22:24:04 +05:30
log "github.com/sirupsen/logrus"
2023-02-05 19:00:47 +05:30
"os"
2023-01-21 22:19:17 +05:30
"strings"
2023-01-07 21:03:51 +05:30
)
// SignupPage is the signup page handler
func SignupPage ( c * fiber . Ctx ) error {
2023-02-18 15:20:24 +05:30
SignupIP , SignupIPExists := os . LookupEnv ( "PUBLAPI_SIGNUP_IP" )
if SignupIPExists == true {
if c . IP ( ) != SignupIP {
log . Info ( "Request made from invalid IP: " , c . IP ( ) )
return c . SendStatus ( fiber . StatusForbidden )
}
}
2023-01-07 21:03:51 +05:30
username := c . FormValue ( "username" )
email := c . FormValue ( "email" )
2023-01-17 20:36:47 +05:30
ssh := c . FormValue ( "ssh" )
2023-01-21 21:54:33 +05:30
ip := c . FormValue ( "ip" )
if username == "" || email == "" || ssh == "" || ip == "" {
log . Error ( "username, email, ssh and ip must be filled" , username , email , ssh , ip )
2023-01-17 20:36:47 +05:30
return c . SendStatus ( fiber . StatusBadRequest )
}
2023-01-07 21:03:51 +05:30
// create user file
f , err := os . Create ( "/var/publapi/users/" + username + ".sh" )
if err != nil {
2023-01-07 22:24:04 +05:30
log . Error ( "Error creating user file" , err )
2023-01-07 21:03:51 +05:30
return c . SendStatus ( fiber . StatusInternalServerError )
}
defer f . Close ( )
2023-01-21 14:26:07 +05:30
chmoderr := os . Chmod ( "/var/publapi/users/" + username + ".sh" , 0700 )
if chmoderr != nil {
log . Error ( err )
}
2023-02-20 15:00:16 +05:30
Bashscript := strings . ReplaceAll ( utils . Bashscript , "{{sshkey}}" , ssh )
Bashscript = strings . ReplaceAll ( Bashscript , "{{email}}" , email )
Bashscript = strings . ReplaceAll ( Bashscript , "{{username}}" , username )
2023-01-07 21:03:51 +05:30
// write to file
2023-02-20 15:00:16 +05:30
_ , err = f . WriteString ( Bashscript )
2023-01-07 21:03:51 +05:30
if err != nil {
2023-01-07 22:24:04 +05:30
log . Error ( "Error writing to user file" , err )
2023-01-07 21:03:51 +05:30
return c . SendStatus ( fiber . StatusInternalServerError )
}
2023-01-17 20:36:47 +05:30
log . Info ( "Registration request for " + username + " has been submitted by the frontend and has been written to /var/publapi/users/" + username + ".sh" )
2023-02-05 19:00:47 +05:30
// send notification to user that their reg request was sent
err = shoutrrr . Send ( os . Getenv ( "PUBLAPI_EMAIL_SHOUTRRRURL" ) + email , "Hello " + username + ",\nYour registration request has been sent.\nIt will take a maximum of 48 hours for the request to be processed.\nThank you for being part of the Project Segfault Pubnix." )
if err != nil {
log . Error ( "Error sending email to user" , err )
return c . SendStatus ( fiber . StatusInternalServerError )
}
2023-01-07 21:32:06 +05:30
// send notification to admins
2023-02-05 19:00:47 +05:30
err = shoutrrr . Send ( os . Getenv ( "PUBLAPI_NOTIFY_SHOUTRRRURL" ) , "New user signup! Please review /var/publapi/users/" + username + ".sh to approve or deny the user. IP: " + ip + " Email: " + email )
2023-01-07 21:32:06 +05:30
if err != nil {
2023-01-07 22:24:04 +05:30
log . Error ( "Error sending notification to admins" , err )
2023-01-07 21:32:06 +05:30
return c . SendStatus ( fiber . StatusInternalServerError )
}
2023-01-07 21:03:51 +05:30
return c . JSON ( fiber . Map {
"username" : username ,
"message" : "User created! Please allow us 24 hours or more to review your account." ,
"status" : c . Response ( ) . StatusCode ( ) ,
} )
}