Changeset 5579
- Timestamp:
- Jul 24, 2017, 8:57:57 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
zero-lliurex-transparent-proxy/trunk/fuentes/install-files/usr/sbin/transparent-proxy-manager.py
r5570 r5579 15 15 16 16 class squidManager(): 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 17 def __init__(self,callback): 18 threading.Thread.__init__(self) 19 self.systemSquid="squid" 20 self.sslSquid="squid-ssl" 21 self.systemSquidConf="/etc/squid/squid.conf" 22 self.sslSquidConf="/etc/squid-ssl/squid.conf" 23 self.sslCert="/etc/squid-ssl/ssl_cert/lliurexCA.pem" 24 self.systemSquidService="squid" 25 self.sslSquidService="squid-ssl" 26 self.server_ip='' 27 self.sslPort=3129 28 self.dbg=1 29 self.callback=callback 30 #def __init__ 31 32 def _debug(self,msg): 33 if self.dbg==1: 34 print("DBG: "+str(msg)) 35 #def _debug 36 37 def _exe_cmd(self,cmd): 38 status=-1 39 self._debug("Executing "+cmd) 40 cmdOptions='' 41 try: 42 with open(os.devnull, 'wb') as hide_output: 43 if ("|") in cmd: 44 status = subprocess.Popen(cmd.split(' '), stdout=hide_output, stderr=hide_output,shell=True).wait() 45 else: 46 status = subprocess.Popen(cmd.split(' '), stdout=hide_output, stderr=hide_output).wait() 47 except: 48 status=1 49 self._debug("$?: "+str(status)) 50 return status 51 #def _exe_cmd 52 53 def enable_transparent_proxy(self): 54 self._debug("Enabling proxy") 55 if not self._is_squidssl_installed(): 56 self._install_squidssl() 57 if not self.is_service_running(self.sslSquidService): 58 self._generate_SSL_cert() 59 #Copy the original squid.conf and make the needed changes 60 self._generate_SquidSSl_config() 61 #Add iptables redirection 62 self._add_iptables_redirection() 63 #Disable squid 64 self._disable_service(self.systemSquidService) 65 #Enable squid-ssl 66 self._debug("Enabling "+self.sslSquidService+" service") 67 self._enable_service(self.sslSquidService) 68 else: 69 self._debug("Service is already running") 70 GObject.idle_add(self.callback,1) 71 #def enable_transparent_proxy 72 73 def _generate_SquidSSl_config(self): 74 self._debug("Configuring "+self.sslSquidService) 75 try: 76 shutil.copy (self.systemSquidConf,self.sslSquidConf) 77 f=open(self.sslSquidConf,"r") 78 squidConfOrig=f.readlines() 79 f.close() 80 squidConfMod=[] 81 net_ip='' 82 for line in squidConfOrig: 83 if line.startswith("http_port"): 84 if '127.0.' not in line: 85 server_ip=line.split(' ')[1] 86 self.server_ip=server_ip.split(':')[0] 87 lineHttps="##Transparent https -->\nhttps_port "+self.server_ip+":"+str(self.sslPort)+" intercept ssl-bump generate-host-certificates=on dynamic_cert_mem_cache_size=4MB cert="+self.sslCert+" key="+self.sslCert+"\n#ssl_bump client-first all\nssl_bump splice all\n## <--" 88 line=line.rstrip("\n")+" intercept\n" 89 line=line+lineHttps 90 squidConfMod.append(line) 91 f=open(self.sslSquidConf,"w") 92 f.writelines(squidConfMod) 93 f.close() 94 except Exception as e: 95 print(str(e)) 96 #def _generate_SquidSSl_config 97 98 def _generate_SSL_cert(self): 99 if not os.path.isfile(self.sslCert): 100 if not os.path.isdir(os.path.dirname(self.sslCert)): 101 os.makedirs(os.path.dirname(self.sslCert)) 102 self._exe_cmd('openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 -extensions v3_ca -keyout '+self.sslCert+' -out '+self.sslCert+' -batch') 103 103 #Initialize directory for caching certificates 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 104 ssl_db='/var/lib/ssl_db' 105 self._exe_cmd('/usr/lib/squid-ssl/ssl_crtd-ssl -c -s '+ssl_db) 106 #Change owner of ssl_db dir 107 shutil.chown(ssl_db,user="nobody") 108 #def _generate_SSL_cert 109 110 def _add_iptables_redirection(self): 111 self._debug("Adding iptables rules") 112 net_ip=self.server_ip.split('.')[0:3] 113 net_ip='.'.join(net_ip)+".0" 114 self._exe_cmd('iptables -t nat -A PREROUTING -s '+net_ip+'/16 -p tcp -m tcp --dport 443 -j REDIRECT --to-ports '+str(self.sslPort)) 115 self._exe_cmd('iptables -t nat -A PREROUTING -s '+net_ip+'/16 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128') 116 #def _add_iptables_redirection 117 118 def disable_transparent_proxy(self): 119 if self.is_service_running(self.sslSquidService): 120 #stop the service 121 self._disable_service(self.sslSquidService) 122 #Clean the firewall 123 try: 124 self._exe_cmd('iptables -t nat -F') 125 except Exception as e: 126 print(str(e)) 127 #Remove the conf files 128 self._debug("Removing config files") 129 if os.path.isfile(self.sslSquidConf): 130 os.remove(self.sslSquidConf) 131 #Restore system's squid 132 self._enable_service(self.systemSquidService) 133 GObject.idle_add(self.callback) 134 #def disable_transparent_proxy 135 136 def _disable_service(self,service): 137 self._debug("Disabling "+service) 138 try: 139 self._exe_cmd('service '+service+' stop') 140 self._exe_cmd('update-rc.d '+service+' remove') 141 except Exception as e: 142 print(str(e)) 143 #def _disable_service 144 145 def _enable_service(self,service): 146 self._debug("Enabling "+service) 147 try: 148 self._exe_cmd("update-rc.d "+service+" defaults 30") 149 self._exe_cmd("invoke-rc.d "+service+" restart") 150 except Exception as e: 151 print(str(e)) 152 #def _enable_service 153 154 def is_service_running(self,name): 155 retval=False 156 try: 157 status=self._exe_cmd('service '+name+' status') 158 if status==0: 159 retval=True 160 except Exception as e: 161 print(str(e)) 162 self._debug("Squid-ssl running: "+str(retval)) 163 return retval 164 #def is_service_running 165 166 def _is_squidssl_installed(self): 167 retval=True 168 status=-1 169 try: 170 status=self._exe_cmd('dpkg --get-selections squid-ssl | grep deinstall') 171 if status==0: 172 retval=False 173 else: 174 #If package has never been installed dpkg returns an error, so we need to recheck 175 status=self._exe_cmd('dpkg -L squid-ssl') 176 if status!=0: 177 retval=False 178 except Exception as e: 179 print(str(e)) 180 self._debug("Squid-ssl installed: "+str(retval)) 181 self._debug(status) 182 return retval 183 #def _is_squidssl_installed 184 185 def _install_squidssl(self): 186 self._debug("Installing needed packages") 187 self._exe_cmd('zero-repos-update') 188 self._exe_cmd('zero-installer install squid-ssl') 189 #def _install_squidssl 190 190 191 191 class mainWindow(Gtk.Window): 192 193 194 195 196 197 198 199 # 200 201 202 203 204 205 206 207 # 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 192 def __init__(self): 193 self.dbg=0 194 Gtk.Window.__init__(self,title=_("Transparent Proxy")) 195 self.set_position(Gtk.WindowPosition.CENTER) 196 self.connect("delete-event", Gtk.main_quit) 197 vbox=Gtk.VBox() 198 img_area=Gtk.Box(spacing=6) 199 # img=Gtk.Image.new_from_file('/usr/share/icons/Vibrancy-Colors/status/36/network-receive.png') 200 img=Gtk.Image(stock=Gtk.STOCK_DISCONNECT) 201 img_area.add(img) 202 img_area.add(Gtk.Label(_("Transparent proxy management"))) 203 img_area.set_border_width(5) 204 img_area.show_all() 205 frame=Gtk.Frame() 206 frame.set_border_width(5) 207 # frame.set_label(_("Transparent proxy management")) 208 box = Gtk.Grid() 209 box.set_border_width(5) 210 box.set_column_spacing(20) 211 box.set_row_spacing(30) 212 self.add(vbox) 213 vbox.add(img_area) 214 vbox.add(frame) 215 frame.add(box) 216 box.attach(Gtk.Label(_("Enable Transparent Proxy")),0,1,1,1) 217 self.sw_Enable=Gtk.Switch() 218 box.attach(self.sw_Enable,1,1,1,1) 219 self.lbl_State=Gtk.Label('') 220 box.attach(self.lbl_State,0,2, 3,2) 221 self.spinner = Gtk.Spinner() 222 box.attach(self.spinner, 0, 3, 2, 2) 223 self.squidManager=squidManager(self._callback) 224 self.sw_Enable.set_active(self.squidManager.is_service_running(self.squidManager.sslSquidService)) 225 if self.sw_Enable.get_state(): 226 service_label="Service up and running" 227 else: 228 service_label="Service deactivated" 229 self.lbl_State.set_text(_(service_label)) 230 self.sw_Enable.connect("state-set",self._on_sw_state) 231 self.show_all() 232 #def __init__ 233 234 def _debug(self,msg): 235 if self.dbg==1: 236 print("DBG: "+str(msg)) 237 #def _debug 238 239 def _on_sw_state(self,widget,data): 240 self._debug("State changed") 241 widget.set_sensitive(False) 242 self.spinner.start() 243 sw_state=widget.get_state() 244 if not sw_state: 245 self._debug("Enabling transparent proxy") 246 self.lbl_State.set_text(_("Enabling transparent proxy")) 247 th=threading.Thread(target=self.squidManager.enable_transparent_proxy) 248 th.start() 249 else: 250 self.lbl_State.set_text(_("Disabling transparent proxy")) 251 th=threading.Thread(target=self.squidManager.disable_transparent_proxy) 252 th.start() 253 self._debug("Done") 254 #def _on_sw_state 255 256 def _callback(self,action=None): 257 self.spinner.stop() 258 if action: 259 self.lbl_State.set_text(_("Service up and running")) 260 else: 261 self.lbl_State.set_text(_("Service deactivated")) 262 self.sw_Enable.set_sensitive(True) 263 #def _callback 264 264 265 265 def read_key(): 266 267 268 269 270 271 272 266 try: 267 f=open("/etc/n4d/key") 268 f.close() 269 #hack 270 return True 271 except: 272 return False 273 273 274 274 status=read_key() 275 275 276 276 if not status: 277 278 279 280 281 282 283 284 285 277 print("[!] You need root privileges to run this program [!]") 278 label = Gtk.Label(_("You need root privileges to run transparfent-proxy-manager")) 279 dialog = Gtk.Dialog("Warning", None, Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT, (Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT)) 280 dialog.vbox.pack_start(label,True,True,10) 281 label.show() 282 dialog.set_border_width(6) 283 response = dialog.run() 284 dialog.destroy() 285 sys.exit(0) 286 286 287 287 GObject.threads_init()
Note: See TracChangeset
for help on using the changeset viewer.