Added cPanel cred checking
This commit is contained in:
parent
cb1f9dfb88
commit
b2562a1d69
31
migratorapi/api/cpanel.py
Normal file
31
migratorapi/api/cpanel.py
Normal file
@ -0,0 +1,31 @@
|
||||
import requests
|
||||
import base64
|
||||
import json
|
||||
|
||||
class cPanelAccount():
|
||||
def __init__(self, cPanelHost, cPanelUser, cPanelPass):
|
||||
self.cPanelHost = cPanelHost
|
||||
self.cPanelUser = cPanelUser
|
||||
self.cPanelPass = cPanelPass
|
||||
self.baseURL = f"https://{self.cPanelHost}:2083/execute"
|
||||
self.headers = {"Authorization" : f"Basic {base64.b64encode((self.cPanelUser+ ':' + self.cPanelPass).encode('UTF-8')).decode()}"}
|
||||
|
||||
def checkCreds(self):
|
||||
try:
|
||||
resp = requests.get(f"{self.baseURL}/Themes/list", headers=self.headers).json()
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
return False
|
||||
return True
|
||||
|
||||
def getDomains(self):
|
||||
return requests.get(f"{self.baseURL}/DomainInfo/list_domains", headers=self.headers).json()
|
||||
|
||||
def getServerInformation(self):
|
||||
return requests.get(f"{self.baseURL}/ServerInformation/get_information", headers=self.headers).json()
|
||||
|
||||
def debugPrintData(self):
|
||||
print(self.headers)
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
|
||||
@ -17,5 +17,6 @@ urlpatterns = [
|
||||
path('migrations/pendingterm/', views.PendingTerm.as_view()),
|
||||
path('migrations/bookedslots/', views.MigrationTimeslotDetails.as_view()),
|
||||
path('migrations/gettimeslots/', views.getTimeslots.as_view()),
|
||||
path('migrations/checkcpanel/', views.checkCpanelDetails.as_view()),
|
||||
path('migrations/<str:pk>/', views.MigrationDetails.as_view()),
|
||||
]
|
||||
|
||||
@ -11,6 +11,7 @@ from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from .serializers import MigrationSerializer
|
||||
from .cpanel import cPanelAccount
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
@ -200,4 +201,14 @@ class MigrationTimeslotDetails(APIView):
|
||||
|
||||
class getTimeslots(APIView):
|
||||
def get(self, request):
|
||||
return Response(settings.MIG_TIMESLOTS)
|
||||
return Response(settings.MIG_TIMESLOTS)
|
||||
|
||||
class checkCpanelDetails(APIView):
|
||||
def post(self, request):
|
||||
print(request.data['hostname'], request.data['username'], request.data['password'])
|
||||
acc = cPanelAccount(request.data['hostname'], request.data['username'], request.data['password'])
|
||||
if acc.checkCreds():
|
||||
return Response(acc.getDomains())
|
||||
else:
|
||||
return Response({'Error': 'Please check the credentials'})
|
||||
|
||||
Reference in New Issue
Block a user