Guest User

Untitled

a guest
Jul 15th, 2019
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.06 KB | None | 0 0
  1. OS=`uname -s`
  2. DISTRIB=`cat /etc/*release* | grep -i DISTRIB_ID | cut -f2 -d=`
  3. IP_FILE="/etc/squid/squid.IPFILE"
  4. PxyPort="3128"
  5. CONFIG_FILE="/etc/squid/squid.conf"
  6. PASSWD_FILE="/etc/squid/squid.passwd"
  7. USER_FILE="/etc/squid/userlist"
  8. TEMP_IPFILE="temp_ipfile"
  9. SQUIDDB="/etc/squid/squid.db"
  10. NETWORK_FILE="/etc/network/interfaces"
  11. INT_NAME=ens33
  12. AVL_IP=0
  13. SUBNET=32
  14. touch $SQUIDDB
  15. touch $PASSWD_FILE
  16. touch $USER_FILE
  17. touch $TEMP_IPFILE
  18. touch $USER_FILE
  19. >$TEMP_IPFILE
  20. >$USER_FILE
  21.  
  22. ### Run as ROOT user only ###
  23. checkRoot()
  24. {
  25. if [ `id -u` -ne 0 ]
  26. then
  27. echo "SCRIPT must be RUN as root user"
  28. exit 13
  29. else
  30. echo "USER: root"
  31. fi
  32. }
  33.  
  34. checkOS()
  35. {
  36. if [ "$OS" == "Linux" ] && [ "$DISTRIB" == "Ubuntu" ]
  37. then
  38. echo "Operating System = $DISTRIB $OS"
  39. else
  40. echo "Please run this script on Ubuntu Linux"
  41. exit 12
  42. fi
  43. }
  44. restartSquid()
  45. {
  46. echo
  47. echo "Restarting SQUID"
  48. systemctl reload squid.service
  49. #systemctl restart networking
  50. }
  51. copySquid()
  52. {
  53. cp -p $CONFIG_FILE "/etc/squid/squid.conf_$(date +%Y%M%H%m)"
  54. }
  55.  
  56. createBaseConf()
  57. {
  58. cat >> $CONFIG_FILE <<EOB
  59. forwarded_for off
  60. http_port 3128
  61. request_header_access Allow allow all
  62. request_header_access Authorization allow all
  63. request_header_access WWW-Authenticate allow all
  64. request_header_access Proxy-Authorization allow all
  65. request_header_access Proxy-Authenticate allow all
  66. request_header_access Cache-Control allow all
  67. request_header_access Content-Encoding allow all
  68. request_header_access Content-Length allow all
  69. request_header_access Content-Type allow all
  70. request_header_access Date allow all
  71. request_header_access Expires allow all
  72. request_header_access Host allow all
  73. request_header_access If-Modified-Since allow all
  74. request_header_access Last-Modified allow all
  75. request_header_access Location allow all
  76. request_header_access Pragma allow all
  77. request_header_access Accept allow all
  78. request_header_access Accept-Charset allow all
  79. request_header_access Accept-Encoding allow all
  80. request_header_access Accept-Language allow all
  81. request_header_access Content-Language allow all
  82. request_header_access Mime-Version allow all
  83. request_header_access Retry-After allow all
  84. request_header_access Title all
  85. request_header_access Connection allow all
  86. request_header_access Proxy-Connection allow all
  87. request_header_access User-Agent allow all
  88. request_header_access Cookie allow all
  89. request_header_access All deny all
  90. #FIRST_TIME
  91. auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/squid.passwd
  92. auth_param basic realm proxy
  93. EOB
  94. }
  95. saveDB()
  96. {
  97. cat $SQUIDDB | grep -w "$1" 1>/dev/null 2>/dev/null
  98. if [ `echo $?` -eq 0 ]
  99. then
  100. cat $SQUIDDB | grep -v -w "$1" > tempsquiddb
  101. cat tempsquiddb > $SQUIDDB
  102. else
  103. echo "$1:$2:$3:$4" >> $SQUIDDB
  104. fi
  105. rm -f tempsquiddb
  106. }
  107. getPxyInput()
  108. {
  109. read -p "Enter Starting Range of IP Address :" SRANGE
  110. rread -p "Enter Ending Range of IP Address :" ERANGE
  111. read -p "Enter Proxy Port Number :" PxyPort
  112. if [ $AUTH_METHOD -eq 1 ]
  113. then
  114. echo
  115. echo "1. Random user"
  116. echo "2. Manual Entry"
  117. read -p "Enter Option [ 1 or 2 ]:" rndUser
  118. if [ "$rndUser" == "1" ]
  119. then
  120. user1="solefood"
  121. user2=`cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 5 | head -n 1`
  122. PxyUser="$user1$user2"
  123. PxyPwd=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1`
  124. else
  125. read -p "Enter User Name for IP Range :" PxyUser
  126. read -p "Enter Password for $PxyUser:" PxyPwd
  127. fi
  128. createUser "$PxyUser" "$PxyPwd"
  129. else
  130. read -p "Enter IP Address to Authenticate for Block $SRANGE - $ERANGE :" Auth_IP
  131. PxyUser="$Auth_IP"
  132. PxyPwd="NA"
  133. fi
  134. }
  135. plumbIP()
  136. {
  137. ip addr add $1/${SUBNET} dev ${INT_NAME} 1>/dev/null 2>/dev/null
  138. }
  139. getInterface()
  140. {
  141. if [ "$1" == "DEL" ]
  142. then
  143. echo >/dev/null
  144. else
  145. AUTHVALUE=1
  146. case "$AUTHVALUE" in
  147. 1)
  148. AUTH_METHOD=1
  149. ;;
  150. 2)
  151. AUTH_METHOD=2
  152. ;;
  153. *)
  154. AUTH_METHOD=1
  155. ;;
  156. esac
  157. fi
  158.  
  159. }
  160. createUser()
  161. {
  162. htpasswd -b $PASSWD_FILE $1 $2 1>/dev/null
  163. printf "\n$1:$2\n" >> $USER_FILE
  164. }
  165. writeConfig()
  166. {
  167. if [ $AUTH_METHOD -eq 1 ]
  168. then
  169. printf "\nacl $3_$1 proxy_auth $3\n" >> $CONFIG_FILE
  170. printf "acl myip_$1 myip $1\n" >> $CONFIG_FILE
  171. printf "tcp_outgoing_address $1 myip_$1\n" >> $CONFIG_FILE
  172. printf "http_access allow $3_$1 myip_$1\n" >> $CONFIG_FILE
  173. else
  174. cat $CONFIG_FILE | egrep 'acl|tcp_outgoing_address|http_access|auth_param' >temp_configb
  175. cat $CONFIG_FILE | egrep -v 'acl|tcp_outgoing_address|http_access|auth_param' | cat -s >temp_configa
  176. cat temp_configa >$CONFIG_FILE
  177. printf "\nacl CL_$1 src $3\n" >> $CONFIG_FILE
  178. printf "acl myip_$1 myip $1\n" >> $CONFIG_FILE
  179. printf "tcp_outgoing_address $1 myip_$1\n" >> $CONFIG_FILE
  180. printf "http_access allow CL_$1 myip_$1\n" >> $CONFIG_FILE
  181. cat temp_configb >> $CONFIG_FILE
  182. fi
  183. cat -s $SQUIDDB > temp_configa
  184. cat temp_configa > $SQUIDDB
  185. rm -f temp_configb
  186. rm -f temp_configa
  187. }
  188. installSquid()
  189. {
  190. printf "installing SQUID 3"
  191. apt-get install squid3 -y 1>/dev/null 2>/dev/null
  192. if [ `echo $?` -eq 0 ] && [ -f $CONFIG_FILE ]
  193. then
  194. printf "\tOK\n"
  195. else
  196. echo "SQUID3 installation FAILED"
  197. exit 11
  198. fi
  199. apt-get install apache2-utils -y 1>/dev/null 2>/dev/null
  200. if [ ! -f /usr/lib/squid/basic_ncsa_auth ];then echo "Failed to Install APACHE2-UTILS"; exit 14;fi
  201. }
  202. processConfig()
  203. {
  204. NEXT_IP=$(($USED_IP+1))
  205. read -p "How many IP: " NUMIP
  206. if [ $NUMIP -le 0 ]; then createMenu;fi
  207. if [ $NUMIP -gt $AVL_IP ]; then createMenu;fi
  208. COUNT=0
  209. for IP in `cat $IP_FILE`
  210. do
  211. cat $SQUIDDB | grep -w "$IP" 1>/dev/null 2>/dev/null
  212. if [ `echo $?` -eq 0 ]
  213. then
  214. echo >/dev/null
  215. else
  216. plumbIP "$IP"
  217. if [ $AUTH_METHOD -eq 1 ]
  218. then
  219. writeConfig "$IP" "$PxyPort" "$PxyUser" "$PxyPwd"
  220. echo "$IP:$PxyPort:$PxyUser:$PxyPwd" >> tempiprange
  221. saveDB "$IP" "$PxyPort" "$PxyUser" "$PxyPwd"
  222. else
  223. writeConfig "$IP" "$PxyPort" "$Auth_IP" "NA"
  224. echo "$IP":"$PxyPort":"$Auth_IP":"NA" >> tempiprange
  225. saveDB "$IP" "$PxyPort" "$Auth_IP" "NA"
  226. fi
  227. COUNT=$((COUNT+1))
  228. if [ $COUNT -eq $NUMIP ];then break;fi
  229. fi
  230. done
  231. echo
  232. echo "New Proxy Configuration"
  233. printf "\n================================================\n"
  234. cat tempiprange
  235. >$TEMP_IPFILE
  236. >tempiprange
  237. }
  238. displayCurrentConfig()
  239. {
  240. C=1
  241. printf "\n================================================"
  242. printf "\nCurrent Configuration\nIP_Address\tUser\tPassword\n"
  243. for IPLIST in `cat $IP_FILE`
  244. do
  245. cat $SQUIDDB | grep -w "$IPLIST" 1> /dev/null 2>/dev/null
  246. if [ `echo $?` -eq 0 ]
  247. then
  248. GENPORT=`cat $SQUIDDB | grep -w "$IPLIST" |awk -F ":" '{print $3}'`
  249. GENUSER=`cat $SQUIDDB | grep -w "$IPLIST" |awk -F ":" '{print $4}'`
  250. GENPASS=`cat $SQUIDDB | grep -w "$IPLIST" |awk -F ":" '{print $5}'`
  251. else
  252. GENPORT=""
  253. GENUSER=""
  254. GENPASS=""
  255. fi
  256.  
  257. printf "$C\t$IPLIST\t$GENPORT\t$GENUSER\t$GENPASS\n" >>$TEMP_IPFILE
  258. C=$((C+1))
  259. done
  260. cat $SQUIDDB
  261. printf "\n================================================\n"
  262. }
  263. isFirstTime()
  264. {
  265. cat $CONFIG_FILE | grep "^#FIRST_TIME" 1>/dev/null 2>/dev/null
  266. if [ `echo $?` -ne 0 ]
  267. then
  268. >$CONFIG_FILE
  269. createBaseConf
  270. else
  271. echo
  272. fi
  273. }
  274. unplumbIP()
  275. {
  276. ip addr del $1/${SUBNET} dev $INT_NAME 1>/dev/null 2>/dev/null
  277. }
  278. deleteConfig()
  279. {
  280. read -p "Enter Username to delete:" delUsername
  281. cat $SQUIDDB | grep -w "$delUsername" 1>/dev/null 2>/dev/null
  282. if [ `echo $?` -ne 0 ];then echo "Wrong Input"; read -p "Press any key to continue" ;createMenu;fi
  283. delIP=`cat $SQUIDDB | grep -w "$delUsername" | awk -F: '{print $1}'`
  284. for IP in $delIP
  285. do
  286. unplumbIP "$IP"
  287. cat $CONFIG_FILE | grep -v -w "myip_${IP}" > temp_config
  288. cat temp_config > $CONFIG_FILE
  289. cat $CONFIG_FILE | grep -v -w "${delUsername}_${IP}" > temp_config
  290. cat temp_config > $CONFIG_FILE
  291. cat $IP_FILE | grep -v -w "$IP" > t_ipfile
  292. echo "$IP" >> t_ipfile
  293. cat t_ipfile > $IP_FILE
  294. done
  295. cat $SQUIDDB | grep -v -w "$delUsername" > temp_db
  296. cat temp_db > $SQUIDDB
  297. htpasswd -D $PASSWD_FILE $delUsername
  298.  
  299. >$TEMP_IPFILE
  300. >tempiprange
  301. >temp_config
  302. >temp_db
  303. >temp_network
  304. }
  305. getCurrentConfig()
  306. {
  307. AVL_IP=0
  308. TOTAL_IP=`cat $IP_FILE | wc -l`
  309. USED_IP=`cat $SQUIDDB | wc -l`
  310. AVL_IP=$((TOTAL_IP-USED_IP))
  311. if [ $AVL_IP -le 0 ];then createMenu;fi
  312. echo
  313. echo "Used IPs: $USED_IP"
  314. echo "Available IP$: $AVL_IP [ $USED_IP to $TOTAL_IP ]"
  315. }
  316. displaywithSL()
  317. {
  318. C=1
  319. printf "\n================================================"
  320. printf "\nCurrent Configuration\nSLNo\tIP_Address\tUser\n"
  321. for IPLIST in `cat $IP_FILE`
  322. do
  323. cat $SQUIDDB | grep -w "$IPLIST" 1> /dev/null 2>/dev/null
  324. if [ `echo $?` -eq 0 ]
  325. then
  326. GENPORT=`cat $SQUIDDB | grep -w "$IPLIST" |awk -F ":" '{print $3}'`
  327. GENUSER=`cat $SQUIDDB | grep -w "$IPLIST" |awk -F ":" '{print $4}'`
  328. GENPASS=`cat $SQUIDDB | grep -w "$IPLIST" |awk -F ":" '{print $5}'`
  329. else
  330. GENPORT=""
  331. GENUSER=""
  332. GENPASS=""
  333. fi
  334. printf "$C\t$IPLIST\t$GENPORT\t$GENUSER\t$GENPASS\n"
  335. printf "$C\t$IPLIST\t$GENPORT\t$GENUSER\t$GENPASS\n" >>$TEMP_IPFILE
  336. C=$((C+1))
  337. done
  338. printf "\n================================================\n"
  339. }
  340. getPxyInputSL()
  341. {
  342. read -p "Enter Starting Range of IP Address :" SRANGE
  343. read -p "Enter Ending Range of IP Address :" ERANGE
  344. if [ $SRANGE -gt $ERANGE ];then createMenu;fi
  345. if [ $AUTH_METHOD -eq 1 ]
  346. then
  347. read -p "Enter User Name for Block $SRANGE - $ERANGE :" PxyUser
  348. read -p "Enter Password for $PxyUser:" PxyPwd
  349. createUser "$PxyUser" "$PxyPwd"
  350. else
  351. read -p "Enter IP Address to Authenticate for Block $SRANGE - $ERANGE :" Auth_IP
  352. PxyUser="$Auth_IP"
  353. PxyPwd="NA"
  354. fi
  355. }
  356.  
  357. createMenu()
  358. {
  359. clear
  360. printf "1. Install Squid\n2. Create Proxy by Username\n3. View Configuration\n4. Exit\nEnter Your Option []: "
  361. read REPLY
  362. case "$REPLY" in
  363. 1)
  364. installSquid
  365. systemctl start squid
  366. systemctl enable squid
  367. ;;
  368. 2)
  369. if [ ! -f $CONFIG_FILE ];then echo "Squid Not Installed";exit 7;fi
  370. if [ ! -f /usr/bin/htpasswd ];then echo "Apache2 Utils Not Installed";exit 8;fi
  371. if [ ! -f $IP_FILE ];then echo "IP FILE not exist. Please create a file name squid.IPFILE in /etc/squid directory ";exit 8;fi
  372. copySquid
  373. isFirstTime
  374. getInterface
  375. getPxyInput
  376. getCurrentConfig
  377. processConfig
  378. restartSquid
  379. ;;
  380. 3)
  381. displayCurrentConfig
  382. >$TEMP_IPFILE
  383. ;;
  384. 4)
  385. rm -f temp_config
  386. rm -f temp_db
  387. rm -f temp_ipfile
  388. rm -f tempiprange
  389. exit 0
  390. ;;
  391. *) createMenu
  392. ;;
  393. esac
  394. }
  395. clear
  396. checkRoot
  397. #checkOS
  398. while true
  399. do
  400. createMenu
  401. read -p "Press any key to continue. Q to Quit" input
  402. if [[ $input = "q" ]] || [[ $input = "Q" ]]
  403. then
  404. break
  405. fi
  406. done
  407. echo
  408.  
  409. echo
Advertisement
Add Comment
Please, Sign In to add comment