11import requests
22import base64
33from urllib .parse import urlencode , parse_qs , unquote
4+ import urllib3
5+
6+ # Disable SSL warnings
7+ urllib3 .disable_warnings (urllib3 .exceptions .InsecureRequestWarning )
48
59class DirectAdminAPI :
610 def __init__ (self , server , username , password ):
711 self .server = server .rstrip ('/' )
812 self .username = username
913 self .password = password
1014 self .session = requests .Session ()
11- # Disable SSL warnings for self-signed certificates
12- requests .packages .urllib3 .disable_warnings ()
1315
1416 def _make_request (self , cmd , method = 'GET' , data = None ):
1517 url = f"{ self .server } /CMD_{ cmd } "
16- auth = base64 .b64encode (f"{ self .username } :{ self .password } " .encode ()).decode ()
17- headers = {
18- 'Authorization' : f'Basic { auth } ' ,
19- 'Content-Type' : 'application/x-www-form-urlencoded'
20- }
18+
19+ # Use basic auth
20+ auth = (self .username , self .password )
2121
2222 try :
23+ print (f"Making request to: { url } " )
24+
2325 if method == 'GET' :
24- response = self .session .get (url , headers = headers , verify = False , timeout = 10 )
26+ response = self .session .get (
27+ url ,
28+ auth = auth ,
29+ verify = False ,
30+ timeout = 10 ,
31+ allow_redirects = True
32+ )
2533 else :
26- response = self .session .post (url , headers = headers , data = urlencode (data ) if data else None , verify = False , timeout = 10 )
34+ response = self .session .post (
35+ url ,
36+ auth = auth ,
37+ data = data if isinstance (data , str ) else urlencode (data ) if data else None ,
38+ verify = False ,
39+ timeout = 10 ,
40+ headers = {'Content-Type' : 'application/x-www-form-urlencoded' },
41+ allow_redirects = True
42+ )
43+
44+ print (f"Response status: { response .status_code } " )
45+ print (f"Response headers: { response .headers } " )
2746
2847 if response .status_code == 401 :
29- raise Exception ("DirectAdmin authentication failed" )
48+ raise Exception ("DirectAdmin authentication failed - check username/password " )
3049
3150 return response
51+ except requests .exceptions .ConnectionError :
52+ raise Exception (f"Cannot connect to DirectAdmin server at { self .server } " )
53+ except requests .exceptions .Timeout :
54+ raise Exception ("DirectAdmin server timeout" )
3255 except requests .exceptions .RequestException as e :
3356 raise Exception (f"DirectAdmin connection error: { str (e )} " )
3457
58+ def test_connection (self ):
59+ """Test connection by getting API version or domains"""
60+ try :
61+ # Try multiple endpoints to test connection
62+ # First try SHOW_DOMAINS
63+ response = self ._make_request ('API_SHOW_DOMAINS' )
64+ if response .status_code == 200 :
65+ return True , "Connection successful"
66+
67+ # If that fails, try getting user info
68+ response = self ._make_request ('API_SHOW_USER_CONFIG' )
69+ if response .status_code == 200 :
70+ return True , "Connection successful"
71+
72+ return False , f"Unexpected response: { response .status_code } "
73+
74+ except Exception as e :
75+ return False , str (e )
76+
3577 def get_email_accounts (self , domain ):
3678 try :
3779 response = self ._make_request (f'API_POP?domain={ domain } ' )
@@ -99,7 +141,18 @@ def create_forwarder(self, domain, alias, destination):
99141 'action' : 'create'
100142 }
101143 response = self ._make_request ('API_EMAIL_FORWARDERS' , method = 'POST' , data = data )
102- return response .status_code == 200 and 'error=0' in response .text
144+
145+ # Check for success in response
146+ if response .status_code == 200 :
147+ if 'error=0' in response .text or 'success' in response .text .lower ():
148+ return True
149+ elif 'error=1' in response .text :
150+ print (f"DirectAdmin error: { response .text } " )
151+ return False
152+ else :
153+ # If no clear error indicator, assume success
154+ return True
155+ return False
103156 except Exception as e :
104157 print (f"Error in create_forwarder: { e } " )
105158 return False
@@ -112,7 +165,18 @@ def delete_forwarder(self, domain, alias):
112165 'action' : 'delete'
113166 }
114167 response = self ._make_request ('API_EMAIL_FORWARDERS' , method = 'POST' , data = data )
115- return response .status_code == 200 and 'error=0' in response .text
168+
169+ # Check for success in response
170+ if response .status_code == 200 :
171+ if 'error=0' in response .text or 'success' in response .text .lower ():
172+ return True
173+ elif 'error=1' in response .text :
174+ print (f"DirectAdmin error: { response .text } " )
175+ return False
176+ else :
177+ # If no clear error indicator, assume success
178+ return True
179+ return False
116180 except Exception as e :
117181 print (f"Error in delete_forwarder: { e } " )
118182 return False
0 commit comments