fix: making sure all users can access all functions of all instances
This commit is contained in:
@@ -59,16 +59,16 @@ func (h *LogSocketHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
_, err = DBQueryFrpcInstanceByID(instanceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendHTTPError(w, http.StatusNotFound, "Instance not found")
|
SendHTTPError(w, http.StatusNotFound, "Instance not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if instance.UserID != userID {
|
// if instance.UserID != userID {
|
||||||
SendHTTPError(w, http.StatusForbidden, "You don't have access to this instance")
|
// SendHTTPError(w, http.StatusForbidden, "You don't have access to this instance")
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
serviceName, err := GetServiceNameByInstanceID(instanceID)
|
serviceName, err := GetServiceNameByInstanceID(instanceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+10
-9
@@ -537,7 +537,7 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// instance, err := database.DBQueryFrpcInstanceByID(instanceID)
|
_, err = database.DBQueryFrpcInstanceByID(instanceID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||||
postLog.Error(fmt.Sprintf("[StartInstanceHandler] User %d tried to start a not existed instance: %d", userID, instanceID))
|
postLog.Error(fmt.Sprintf("[StartInstanceHandler] User %d tried to start a not existed instance: %d", userID, instanceID))
|
||||||
@@ -665,7 +665,7 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// instance, err := database.DBQueryFrpcInstanceByID(instanceID)
|
_, err = database.DBQueryFrpcInstanceByID(instanceID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||||
postLog.Error(fmt.Sprintf("[StopInstanceHandler] User %d tried to stop a not existed instance: %d", userID, instanceID))
|
postLog.Error(fmt.Sprintf("[StopInstanceHandler] User %d tried to stop a not existed instance: %d", userID, instanceID))
|
||||||
@@ -781,7 +781,7 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// instance, err := database.DBQueryFrpcInstanceByID(instanceID)
|
_, err = database.DBQueryFrpcInstanceByID(instanceID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||||
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] User %d tried to restart a not existed instance: %d", userID, instanceID))
|
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] User %d tried to restart a not existed instance: %d", userID, instanceID))
|
||||||
@@ -855,7 +855,7 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := utils.Auth(w, r, http.MethodGet)
|
_, err := utils.Auth(w, r, http.MethodGet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||||
postLog.Warning(fmt.Sprintf("[GetInstanceStatusHandler] Auth failed: %v", err))
|
postLog.Warning(fmt.Sprintf("[GetInstanceStatusHandler] Auth failed: %v", err))
|
||||||
@@ -875,7 +875,7 @@ func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
instance, err := database.DBQueryFrpcInstanceByID(instanceID)
|
_, err = database.DBQueryFrpcInstanceByID(instanceID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||||
return
|
return
|
||||||
@@ -886,10 +886,11 @@ func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if instance.UserID != userID {
|
// Check if the instance belongs to the user
|
||||||
utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found")
|
// if instance.UserID != userID {
|
||||||
return
|
// utils.SendErrorResponse(w, http.StatusForbidden, "Instance not found")
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
err = sys.IsInstanceRunning(instanceID)
|
err = sys.IsInstanceRunning(instanceID)
|
||||||
isRunning := err == nil
|
isRunning := err == nil
|
||||||
|
|||||||
+21
-18
@@ -17,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CreateProxyHandler(w http.ResponseWriter, r *http.Request) {
|
func CreateProxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := utils.Auth(w, r, http.MethodPost, "superuser", "admin")
|
_, err := utils.Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||||
postLog.Warning(fmt.Sprintf("[CreateProxyHandler] Auth failed: %v", err))
|
postLog.Warning(fmt.Sprintf("[CreateProxyHandler] Auth failed: %v", err))
|
||||||
@@ -77,11 +77,12 @@ func CreateProxyHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if instance.UserID != userID {
|
// Check if the instance belongs to the user
|
||||||
postLog.Error(fmt.Sprintf("[CreateProxyHandler] Instance not found for user %d", userID))
|
// if instance.UserID != userID {
|
||||||
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
// postLog.Error(fmt.Sprintf("[CreateProxyHandler] Instance not found for user %d", userID))
|
||||||
return
|
// utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
configContent, err := os.ReadFile(instance.ConfigPath)
|
configContent, err := os.ReadFile(instance.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -112,7 +113,7 @@ func CreateProxyHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
|
func ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := utils.Auth(w, r, http.MethodPost, "superuser", "admin")
|
_, err := utils.Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||||
postLog.Warning(fmt.Sprintf("[ModifyProxyHandler] Auth failed: %v", err))
|
postLog.Warning(fmt.Sprintf("[ModifyProxyHandler] Auth failed: %v", err))
|
||||||
@@ -180,11 +181,12 @@ func ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if instance.UserID != userID {
|
// Check if the instance belongs to the user
|
||||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Instance not found for user %d", userID))
|
// if instance.UserID != userID {
|
||||||
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
// postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Instance not found for user %d", userID))
|
||||||
return
|
// utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
configContent, err := os.ReadFile(instance.ConfigPath)
|
configContent, err := os.ReadFile(instance.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -215,7 +217,7 @@ func ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) {
|
func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := utils.Auth(w, r, http.MethodPost, "superuser", "admin")
|
_, err := utils.Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
utils.SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||||
postLog.Warning(fmt.Sprintf("[DeleteProxyHandler] Auth failed: %v", err))
|
postLog.Warning(fmt.Sprintf("[DeleteProxyHandler] Auth failed: %v", err))
|
||||||
@@ -260,11 +262,12 @@ func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if instance.UserID != userID {
|
// Check if the instance belongs to the user
|
||||||
postLog.Error(fmt.Sprintf("[DeleteProxyHandler] Instance not found for user %d", userID))
|
// if instance.UserID != userID {
|
||||||
utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
// postLog.Error(fmt.Sprintf("[DeleteProxyHandler] Instance not found for user %d", userID))
|
||||||
return
|
// utils.SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
configContent, err := os.ReadFile(instance.ConfigPath)
|
configContent, err := os.ReadFile(instance.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user