99import subprocess
1010import sys
1111import time
12+
1213import keyring
1314import pexpect
15+ import requests
1416from colorama import Fore
1517
1618# global constant variable
@@ -31,19 +33,19 @@ def __init__(self):
3133class LoginVpn :
3234 def __init__ (self ):
3335 self ._email_to = ""
34-
3536 self ._email_from = ""
3637 self ._email_password = ""
37- self ._login_message = ""
38- self ._logout_message = ""
38+ self ._lab_name = ""
39+ self ._lab_user = ""
3940
4041 self ._vpn_user = ""
4142 self ._vpn_password = ""
42- self ._dns = ""
43-
4443 self ._vpn_config = ""
4544 self ._vpn_command = ""
4645
46+ self ._dns = ""
47+ self ._slack_webhook_url = ""
48+
4749 def get_parameters (self , config_dict ):
4850 """
4951 retrieve the value from the input
@@ -53,25 +55,23 @@ def get_parameters(self, config_dict):
5355 email_keyring_name = config_dict ["email_keyring_name" ]
5456 self ._email_to = config_dict ["email_to" ]
5557 self ._email_from = keyring .get_password (email_keyring_name , 'username' )
56-
57- self ._email_password = keyring .get_password (email_keyring_name , self ._email_from )
58- self ._login_message = config_dict ["login_message_path" ]
59- self ._logout_message = config_dict ["logout_message_path" ]
58+ self ._email_password = keyring .get_password (email_keyring_name , 'password' )
59+ self ._lab_name = config_dict ["lab_name" ]
60+ self ._lab_user = config_dict ["lab_user" ]
6061
6162 vpn_keyring_name = config_dict ["vpn_keyring_name" ]
6263 self ._vpn_user = keyring .get_password (vpn_keyring_name , 'username' )
63- self ._vpn_password = keyring .get_password (vpn_keyring_name , self ._vpn_user )
64-
65- self ._dns = config_dict ["dns" ]
66-
64+ self ._vpn_password = keyring .get_password (vpn_keyring_name , 'password' )
6765 self ._vpn_config = config_dict ["ovpn_file_path" ]
6866
6967 self ._vpn_command = "openvpn" + " " + self ._vpn_config
7068
71- # validate the input
72- if not self ._vpn_user and self ._vpn_password and self ._vpn_config and self ._email_to and self ._email_from and \
73- self ._email_password and self ._login_message and self ._logout_message :
74- print (f"[x] please fill the input in the json config file !!" )
69+ self ._dns = config_dict ["dns" ]
70+ self ._slack_webhook_url = config_dict ["slack_webhook_url" ]
71+
72+ # validate the required input
73+ if not self ._vpn_user and self ._vpn_password and self ._vpn_config :
74+ print (f"[x] ovpn_file_path / vpn user credential must be valid" )
7575 sys .exit (0 )
7676
7777 def lab_connection (self ):
@@ -85,15 +85,29 @@ def lab_connection(self):
8585 set_dns_command = "sed -i \' 1s/^/nameserver " + self ._dns + "\\ n/\' /etc/resolv.conf"
8686 subprocess .check_output (set_dns_command , shell = True )
8787
88- print (Fore .GREEN , f"[+] sending email notification to { self ._email_to } " )
89- send_email_command = "sendEmail -f " + self ._email_from + " -t " + self ._email_to + \
90- " -u \' logged-In\' -o message-file=" + self ._login_message + \
91- " -s smtp.gmail.com:587 -o tls=yes -xu " + self ._email_from + \
92- " -xp " + self ._email_password
93-
94- subprocess .check_output (send_email_command , shell = True )
95-
96- print (Fore .LIGHTBLUE_EX , f"[*] connected to the lab, press ctrl+c to disconnect from the lab" )
88+ if self ._lab_name and self ._lab_user and self ._email_to and self ._email_from and self ._email_password :
89+ send_email_command = "sendEmail -f " + self ._email_from + " -t " + self ._email_to + \
90+ " -u " + self ._lab_name + "-bot: " + self ._lab_user + " has logged-in" + \
91+ " -m \' try harder\' " + \
92+ " -s smtp.gmail.com:587 -o tls=yes -xu " + self ._email_from + \
93+ " -xp " + self ._email_password
94+ print (Fore .LIGHTMAGENTA_EX , f"[+] sent email notification to { self ._email_to } " )
95+ subprocess .check_output (send_email_command , shell = True )
96+
97+ if self ._slack_webhook_url and self ._lab_name and self ._lab_user :
98+ bot = self ._lab_name + "-bot"
99+ data = {
100+ 'text' : f':ballot_box_with_check: `{ self ._lab_user } ` has *logged-in* '
101+ f':man-lifting-weights:' ,
102+ 'username' : bot ,
103+ }
104+
105+ response = requests .post (self ._slack_webhook_url , data = json .dumps (data ),
106+ headers = {'Content-Type' : 'application/json' })
107+ if str (response .text ) == 'ok' and str (response .status_code ) == '200' :
108+ print (Fore .LIGHTYELLOW_EX , f"[*] sending slack notification !!" )
109+
110+ print (Fore .WHITE , f"[*] connected to the lab, press ctrl+c to disconnect from the lab" )
97111
98112 # connect to the lab
99113 i = pexpect .spawn (self ._vpn_command )
@@ -107,12 +121,29 @@ def lab_connection(self):
107121
108122 except KeyboardInterrupt :
109123 print (Fore .RED , f"[*] received ctrl+c, disconnecting from lab " )
110- send_email_command = "sendEmail -f " + self ._email_from + " -t " + self ._email_to + \
111- " -u \' logged-Out\' -o message-file=" + self ._logout_message + \
112- " -s smtp.gmail.com:587 -o tls=yes -xu " + self ._email_from + \
113- " -xp " + self ._email_password
114- subprocess .check_output (send_email_command , shell = True )
115- print (Fore .GREEN , f"[*] sent email notification " )
124+
125+ if self ._lab_name and self ._lab_user and self ._email_to and self ._email_from and self ._email_password :
126+ send_email_command = "sendEmail -f " + self ._email_from + " -t " + self ._email_to + \
127+ " -u " + self ._lab_name + "-bot: " + self ._lab_user + " has logged-out" + \
128+ " -m \' sleep tight\' " + \
129+ " -s smtp.gmail.com:587 -o tls=yes -xu " + self ._email_from + \
130+ " -xp " + self ._email_password
131+
132+ subprocess .check_output (send_email_command , shell = True )
133+ print (Fore .GREEN , f"[+] sent email notification to { self ._email_to } " )
134+
135+ if self ._slack_webhook_url and self ._lab_name and self ._lab_user :
136+ bot = self ._lab_name + "-bot"
137+ data = {
138+ 'text' : f':negative_squared_cross_mark: `{ self ._lab_user } ` has *logged-out* '
139+ f':tired_face:' ,
140+ 'username' : bot ,
141+ }
142+
143+ response = requests .post (self ._slack_webhook_url , data = json .dumps (data ),
144+ headers = {'Content-Type' : 'application/json' })
145+ if str (response .text ) == 'ok' and str (response .status_code ) == '200' :
146+ print (Fore .BLUE , f"[*] sending slack notification !!" )
116147
117148 if self ._dns :
118149 print (Fore .GREEN , f"[+] unset the dns entry " )
0 commit comments