-
Notifications
You must be signed in to change notification settings - Fork 0
/
squidconf.py
85 lines (71 loc) · 2.23 KB
/
squidconf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
#! /usr/bin/python
name = "K.Mohan Sai Krishna"
import os
import sys
import subprocess
import getpass
import apt
filename = "squid.conf"
#initializs the chain
def start():
installcheck();
return
# Check if the squid is already installed.
def installcheck():
if os.path.isdir("/etc/squid3/") == True:
print("squid directory exists")
backup();
else:
print("Seems squid is not installed. Going to install squid")
installsquid();
return
# Install squid if not installed.
def installsquid():
subprocess.call(["sudo","-E","apt-get","install","squid"])
backup();
return
# Backup the curent squid.conf file
def backup():
if os.path.isfile("/etc/squid3/squid.conf") == True:
print("Backing up your squid.conf file")
print("Renaming it as squid.conf.original")
subprocess.call(["sudo","mv","/etc/squid3/squid.conf","/etc/squid3/squid.conf.original"])
check();
elif os.path.isfile("/etc/squid3/squid.conf") == False:
check();
return
# Check if squid.conf exists incase the user runs this twice.
def check():
if os.path.isfile("/etc/squid3/squid.conf") == True:
subprocess.call(["sudo","rm","/etc/squid3/squid.conf"])
conf();
return
# Main work happens here
def conf():
port = raw_input('Enter the port to run your squid:')
par_proxy = raw_input('Enter your parent proxy address:')
par_port = raw_input('Enter your parent proxy port:')
user = raw_input('Enter your parent proxy user name:')
while 1:
passw1 = getpass.getpass('Enter your parent proxy password:')
passw2 = getpass.getpass('Enter proxy password again:')
if (passw1 == passw2):
break
else:
print("Passwords didnot match. Please enter them again")
file = open(filename,'a')
file.write("http_port " + port +"\n")
file.write("cache_peer "+par_proxy+" parent "+par_port+" 0 login="+user+":"+passw1+"\n")
file.write("never_direct allow all"+"\n")
file.write("http_access allow all")
file.close()
print("File saved !! now moving")
print("removing old file if exists")
subprocess.call(["sudo","rm","-f","/etc/squid3/squid.conf"])
subprocess.call(["sudo","mv","squid.conf","/etc/squid3/squid.conf"])
print("Succesfully configured !! yay")
return
start();
# cache = apt.Cache()
# cache.open()
# cache["squid"].is_installed # Evaluates true if squid is installed