mirror of
https://github.com/MizuchiLabs/mantrae.git
synced 2026-05-07 00:19:59 -05:00
feat: added auto update ip toggle
This commit is contained in:
@@ -19,23 +19,28 @@ import (
|
||||
func Login(a *config.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
q := a.Conn.GetQuery()
|
||||
var user db.User
|
||||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
var request db.User
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, "Failed to decode credentials", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if user.Username == "" || user.Password == "" {
|
||||
if request.Username == "" || request.Password == "" {
|
||||
http.Error(w, "Username or password cannot be empty", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
dbUser, err := q.GetUserByUsername(r.Context(), user.Username)
|
||||
user, err := q.GetUserByUsername(r.Context(), request.Username)
|
||||
if err != nil {
|
||||
http.Error(w, "User not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err = bcrypt.CompareHashAndPassword([]byte(dbUser.Password), []byte(user.Password)); err != nil {
|
||||
userPassword, err := q.GetUserPassword(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err = bcrypt.CompareHashAndPassword([]byte(userPassword), []byte(request.Password)); err != nil {
|
||||
http.Error(w, "Invalid username or password "+err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
@@ -45,19 +50,23 @@ func Login(a *config.App) http.HandlerFunc {
|
||||
expirationTime = time.Now().Add(7 * 24 * time.Hour)
|
||||
}
|
||||
|
||||
token, err := util.EncodeUserJWT(user.Username, a.Config.Secret, expirationTime)
|
||||
token, err := util.EncodeUserJWT(request.Username, a.Config.Secret, expirationTime)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := q.UpdateUserLastLogin(r.Context(), dbUser.ID); err != nil {
|
||||
if err := q.UpdateUserLastLogin(r.Context(), user.ID); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]any{
|
||||
"token": token,
|
||||
"user": user,
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(map[string]string{"token": token}); err != nil {
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -95,8 +104,9 @@ func VerifyJWT(a *config.App) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]any{"user": user}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(user); err != nil {
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -150,8 +160,12 @@ func VerifyOTP(a *config.App) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]any{
|
||||
"token": token,
|
||||
"user": user,
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(map[string]string{"token": token}); err != nil {
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -214,7 +228,7 @@ func SendResetEmail(a *config.App) http.HandlerFunc {
|
||||
config.From = setting.Value
|
||||
}
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
data := map[string]any{
|
||||
"Token": token,
|
||||
"Date": expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
|
||||
@@ -75,22 +75,7 @@ func UpdateUser(a *config.App) http.HandlerFunc {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
dbUser, err := q.GetUser(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if user.Password == "" {
|
||||
user.Password = dbUser.Password
|
||||
} else {
|
||||
hashedPassword, err := util.HashPassword(user.Password)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
user.Password = hashedPassword
|
||||
}
|
||||
if err := q.UpdateUser(r.Context(), user); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -4,10 +4,11 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/MizuchiLabs/mantrae/internal/util"
|
||||
"github.com/MizuchiLabs/mantrae/pkg/build"
|
||||
)
|
||||
|
||||
// GetVersion returns the current version of Mantrae as a plain text response.
|
||||
// GetVersion returns the current version of Mantrae as a plain text response
|
||||
func GetVersion(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
data := map[string]string{"version": build.Version}
|
||||
@@ -17,52 +18,16 @@ func GetVersion(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetPublicIP attempts to resolve the public IP address of a Traefik instance by its profile ID.
|
||||
// func GetPublicIP(w http.ResponseWriter, r *http.Request) {
|
||||
// id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
// if err != nil {
|
||||
// http.Error(w, fmt.Sprintf("Parse error: %s", err.Error()), http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// profile, err := db.Query.GetProfileByID(context.Background(), id)
|
||||
// if err != nil {
|
||||
// http.Error(w, fmt.Sprintf("Profile not found: %s", err.Error()), http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// // Parse the URL
|
||||
// u, err := url.Parse(profile.Url)
|
||||
// if err != nil {
|
||||
// http.Error(w, fmt.Sprintf("Invalid URL: %s", err.Error()), http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
|
||||
// // Check if it's an IP address
|
||||
// if net.ParseIP(u.Hostname()) != nil {
|
||||
// if !net.ParseIP(u.Hostname()).IsLoopback() {
|
||||
// writeJSON(w, map[string]string{"ip": u.Hostname()})
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
// // If it's a valid hostname, resolve to IP
|
||||
// ips, err := net.LookupHost(u.Hostname())
|
||||
// if err == nil && len(ips) > 0 {
|
||||
// if !net.ParseIP(ips[0]).IsLoopback() {
|
||||
// writeJSON(w, map[string]string{"ip": ips[0]})
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
// ip, err := util.GetPublicIP()
|
||||
// if err != nil {
|
||||
// http.Error(
|
||||
// w,
|
||||
// fmt.Sprintf("Failed to get public IP: %s", err.Error()),
|
||||
// http.StatusInternalServerError,
|
||||
// )
|
||||
// return
|
||||
// }
|
||||
// writeJSON(w, map[string]string{"ip": ip})
|
||||
// }
|
||||
// GetPublicIP attempts to resolve the public IP address of the current machine
|
||||
func GetPublicIP(w http.ResponseWriter, r *http.Request) {
|
||||
machineIPs, err := util.GetPublicIPsCached()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(machineIPs); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user