ssl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #!/bin/bash
  2. #19/12/2019
  3. drop_port(){
  4. local portasVAR=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" |grep -v "COMMAND" | grep "LISTEN")
  5. local NOREPEAT
  6. local reQ
  7. local Port
  8. while read port; do
  9. reQ=$(echo ${port}|awk '{print $1}')
  10. Port=$(echo {$port} | awk '{print $9}' | awk -F ":" '{print $2}')
  11. [[ $(echo -e $NOREPEAT|grep -w "$Port") ]] && continue
  12. NOREPEAT+="$Port\n"
  13. case ${reQ} in
  14. cupsd)continue;;
  15. systemd-r)continue;;
  16. stunnel4|stunnel)continue;;
  17. *)DPB+=" $reQ:$Port";;
  18. esac
  19. done <<< "${portasVAR}"
  20. }
  21. ssl_stunel(){
  22. [[ $(mportas|grep stunnel4|head -1) ]] && {
  23. clear
  24. msg -bar
  25. print_center -ama "Parando Stunnel"
  26. msg -bar
  27. service stunnel4 stop & >/dev/null 2>&1
  28. fun_bar 'apt-get purge stunnel4 -y' 'UNINSTALL STUNNEL4 '
  29. msg -bar
  30. print_center -verd "Stunnel detenido con Exito!"
  31. msg -bar
  32. sleep 2
  33. return 1
  34. }
  35. title "INSTALADOR SSL By @Rufu99"
  36. print_center -azu "Seleccione puerto de redireccion de trafico"
  37. msg -bar
  38. drop_port
  39. n=1
  40. for i in $DPB; do
  41. proto=$(echo $i|awk -F ":" '{print $1}')
  42. proto2=$(printf '%-12s' "$proto")
  43. port=$(echo $i|awk -F ":" '{print $2}')
  44. echo -e " $(msg -verd "[$n]") $(msg -verm2 ">") $(msg -ama "$proto2")$(msg -azu "$port")"
  45. drop[$n]=$port
  46. num_opc="$n"
  47. let n++
  48. done
  49. msg -bar
  50. while [[ -z $opc ]]; do
  51. msg -ne " opcion: "
  52. read opc
  53. tput cuu1 && tput dl1
  54. if [[ -z $opc ]]; then
  55. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  56. unset opc
  57. sleep 2
  58. tput cuu1 && tput dl1
  59. continue
  60. elif [[ ! $opc =~ $numero ]]; then
  61. msg -verm2 " selecciona solo numeros entre 1 y $num_opc"
  62. unset opc
  63. sleep 2
  64. tput cuu1 && tput dl1
  65. continue
  66. elif [[ "$opc" -gt "$num_opc" ]]; then
  67. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  68. sleep 2
  69. tput cuu1 && tput dl1
  70. unset opc
  71. continue
  72. fi
  73. done
  74. title "INSTALADOR SSL By @Rufu99"
  75. echo -e "\033[1;33m Puerto de redireccion de trafico: \033[1;32m${drop[$opc]}"
  76. msg -bar
  77. while [[ -z $opc2 ]]; do
  78. echo -ne "\033[1;37m Ingrese un puerto para SSL: " && read opc2
  79. tput cuu1 && tput dl1
  80. [[ $(mportas|grep -w "${opc2}") = "" ]] && {
  81. echo -e "\033[1;33m $(fun_trans "Puerto de ssl:")\033[1;32m ${opc2} OK"
  82. } || {
  83. echo -e "\033[1;33m $(fun_trans "Puerto de ssl:")\033[1;31m ${opc2} FAIL" && sleep 2
  84. tput cuu1 && tput dl1
  85. unset opc2
  86. }
  87. done
  88. # openssl x509 -in 2.crt -text -noout |grep -w 'Issuer'|awk -F 'O = ' '{print $2}'|cut -d ',' -f1
  89. msg -bar
  90. fun_bar 'apt-get install stunnel4 -y' 'INSTALL STUNNEL4 '
  91. echo -e "client = no\n[SSL]\ncert = /etc/stunnel/stunnel.pem\naccept = ${opc2}\nconnect = 127.0.0.1:${drop[$opc]}" > /etc/stunnel/stunnel.conf
  92. db="$(ls ${ADM_crt})"
  93. opcion="n"
  94. if [[ ! "$(echo "$db"|grep ".crt")" = "" ]]; then
  95. cert=$(echo "$db"|grep ".crt")
  96. key=$(echo "$db"|grep ".key")
  97. msg -bar
  98. print_center -azu "CERTIFICADO SSL ENCONTRADO"
  99. msg -bar
  100. echo -e "$(msg -azu "CERT:") $(msg -ama "$cert")"
  101. echo -e "$(msg -azu "KEY:") $(msg -ama "$key")"
  102. msg -bar
  103. msg -ne "Continuar, usando estre certificado [S/N]: "
  104. read opcion
  105. if [[ $opcion != @(n|N) ]]; then
  106. cp ${ADM_crt}/$cert ${ADM_tmp}/stunnel.crt
  107. cp ${ADM_crt}/$key ${ADM_tmp}/stunnel.key
  108. fi
  109. fi
  110. if [[ $opcion != @(s|S) ]]; then
  111. openssl genrsa -out ${ADM_tmp}/stunnel.key 2048 > /dev/null 2>&1
  112. (echo "" ; echo "" ; echo "" ; echo "" ; echo "" ; echo "" ; echo "@cloudflare" )|openssl req -new -key ${ADM_tmp}/stunnel.key -x509 -days 1000 -out ${ADM_tmp}/stunnel.crt > /dev/null 2>&1
  113. fi
  114. cat ${ADM_tmp}/stunnel.key ${ADM_tmp}/stunnel.crt > /etc/stunnel/stunnel.pem
  115. sed -i 's/ENABLED=0/ENABLED=1/g' /etc/default/stunnel4
  116. service stunnel4 restart > /dev/null 2>&1
  117. msg -bar
  118. print_center -verd "INSTALADO CON EXITO"
  119. msg -bar
  120. rm -rf ${ADM_tmp}/stunnel.crt > /dev/null 2>&1
  121. rm -rf ${ADM_tmp}/stunnel.key > /dev/null 2>&1
  122. sleep 3
  123. return 1
  124. }
  125. add_port(){
  126. title "INSTALADOR SSL By @Rufu99"
  127. print_center -azu "Seleccione puerto de redireccion de trafico"
  128. msg -bar
  129. drop_port
  130. n=1
  131. for i in $DPB; do
  132. proto=$(echo $i|awk -F ":" '{print $1}')
  133. proto2=$(printf '%-12s' "$proto")
  134. port=$(echo $i|awk -F ":" '{print $2}')
  135. echo -e " $(msg -verd "[$n]") $(msg -verm2 ">") $(msg -ama "$proto2")$(msg -azu "$port")"
  136. drop[$n]=$port
  137. num_opc="$n"
  138. let n++
  139. done
  140. msg -bar
  141. while [[ -z $opc ]]; do
  142. msg -ne " opcion: "
  143. read opc
  144. tput cuu1 && tput dl1
  145. if [[ -z $opc ]]; then
  146. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  147. unset opc
  148. sleep 2
  149. tput cuu1 && tput dl1
  150. continue
  151. elif [[ ! $opc =~ $numero ]]; then
  152. msg -verm2 " selecciona solo numeros entre 1 y $num_opc"
  153. unset opc
  154. sleep 2
  155. tput cuu1 && tput dl1
  156. continue
  157. elif [[ "$opc" -gt "$num_opc" ]]; then
  158. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  159. sleep 2
  160. tput cuu1 && tput dl1
  161. unset opc
  162. continue
  163. fi
  164. done
  165. title "INSTALADOR SSL By @Rufu99"
  166. echo -e "\033[1;33m Puerto de redireccion de trafico: \033[1;32m${drop[$opc]}"
  167. msg -bar
  168. while [[ -z $opc2 ]]; do
  169. echo -ne "\033[1;37m Ingrese un puerto para SSL: " && read opc2
  170. tput cuu1 && tput dl1
  171. [[ $(mportas|grep -w "${opc2}") = "" ]] && {
  172. echo -e "\033[1;33m $(fun_trans "Puerto de ssl:")\033[1;32m ${opc2} OK"
  173. } || {
  174. echo -e "\033[1;33m $(fun_trans "Puerto de ssl:")\033[1;31m ${opc2} FAIL" && sleep 2
  175. tput cuu1 && tput dl1
  176. unset opc2
  177. }
  178. done
  179. echo -e "client = no\n[SSL+]\ncert = /etc/stunnel/stunnel.pem\naccept = ${opc2}\nconnect = 127.0.0.1:${drop[$opc]}" >> /etc/stunnel/stunnel.conf
  180. service stunnel4 restart > /dev/null 2>&1
  181. msg -bar
  182. print_center -verd "PUERTO AGREGADO CON EXITO"
  183. enter
  184. return 1
  185. }
  186. start-stop(){
  187. clear
  188. msg -bar
  189. if [[ $(service stunnel4 status|grep -w 'Active'|awk -F ' ' '{print $2}') = 'inactive' ]]; then
  190. if service stunnel4 start &> /dev/null ; then
  191. print_center -verd "Servicio stunnel4 iniciado"
  192. else
  193. print_center -verm2 "Falla al iniciar Servicio stunnel4"
  194. fi
  195. else
  196. if service stunnel4 stop &> /dev/null ; then
  197. print_center -verd "Servicio stunnel4 detenido"
  198. else
  199. print_center -verm2 "Falla al detener Servicio stunnel4"
  200. fi
  201. fi
  202. enter
  203. return 1
  204. }
  205. del_port(){
  206. sslport=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" |grep -v "COMMAND" | grep "LISTEN"|grep -E 'stunnel|stunnel4')
  207. if [[ $(echo "$sslport"|wc -l) -lt '2' ]];then
  208. clear
  209. msg -bar
  210. print_center -ama "Un solo puerto para eliminar\ndesea detener el servicio? "
  211. msg -bar
  212. msg -ne " opcion [S/N]: " && read a
  213. if [[ "$a" = @(S|s) ]]; then
  214. clear
  215. msg -bar
  216. if service stunnel4 stop &> /dev/null ; then
  217. print_center -verd "Servicio stunnel4 detenido"
  218. else
  219. print_center -verm2 "Falla al detener Servicio stunnel4"
  220. fi
  221. fi
  222. enter
  223. return 1
  224. fi
  225. title "seleccione el num de puerto a quitar"
  226. n=1
  227. while read i; do
  228. port=$(echo $i|awk -F ' ' '{print $9}'|cut -d ':' -f2)
  229. echo -e " $(msg -verd "[$n]") $(msg -verm2 ">") $(msg -azu "$port")"
  230. drop[$n]=$port
  231. num_opc="$n"
  232. let n++
  233. done <<< $(echo "$sslport")
  234. back
  235. while [[ -z $opc ]]; do
  236. msg -ne " opcion: "
  237. read opc
  238. tput cuu1 && tput dl1
  239. if [[ -z $opc ]]; then
  240. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  241. unset opc
  242. sleep 2
  243. tput cuu1 && tput dl1
  244. continue
  245. elif [[ ! $opc =~ $numero ]]; then
  246. msg -verm2 " selecciona solo numeros entre 1 y $num_opc"
  247. unset opc
  248. sleep 2
  249. tput cuu1 && tput dl1
  250. continue
  251. elif [[ "$opc" -gt "$num_opc" ]]; then
  252. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  253. sleep 2
  254. tput cuu1 && tput dl1
  255. unset opc
  256. continue
  257. fi
  258. done
  259. in=$(( $(cat "/etc/stunnel/stunnel.conf"|grep -n "accept = ${drop[$opc]}"|cut -d ':' -f1) - 3 ))
  260. en=$(( $in + 4))
  261. sed -i "$in,$en d" /etc/stunnel/stunnel.conf
  262. sed -i '2 s/\[SSL+\]/\[SSL\]/' /etc/stunnel/stunnel.conf
  263. title "Puerto ssl ${drop[$opc]} eliminado"
  264. if service stunnel4 restart &> /dev/null ; then
  265. print_center -verd "Servicio stunnel4 reiniciado"
  266. else
  267. print_center -verm2 "Falla al reiniciar Servicio stunnel4"
  268. fi
  269. enter
  270. return 1
  271. }
  272. edit_port(){
  273. sslport=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" |grep -v "COMMAND" | grep "LISTEN"|grep -E 'stunnel|stunnel4')
  274. title "seleccione el num de puerto a editar"
  275. n=1
  276. while read i; do
  277. port=$(echo $i|awk -F ' ' '{print $9}'|cut -d ':' -f2)
  278. echo -e " $(msg -verd "[$n]") $(msg -verm2 ">") $(msg -azu "$port")"
  279. drop[$n]=$port
  280. num_opc="$n"
  281. let n++
  282. done <<< $(echo "$sslport")
  283. back
  284. while [[ -z $opc ]]; do
  285. msg -ne " opcion: "
  286. read opc
  287. tput cuu1 && tput dl1
  288. if [[ -z $opc ]]; then
  289. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  290. unset opc
  291. sleep 2
  292. tput cuu1 && tput dl1
  293. continue
  294. elif [[ ! $opc =~ $numero ]]; then
  295. msg -verm2 " selecciona solo numeros entre 1 y $num_opc"
  296. unset opc
  297. sleep 2
  298. tput cuu1 && tput dl1
  299. continue
  300. elif [[ "$opc" -gt "$num_opc" ]]; then
  301. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  302. sleep 2
  303. tput cuu1 && tput dl1
  304. unset opc
  305. continue
  306. fi
  307. done
  308. title "Configuracion actual"
  309. in=$(( $(cat "/etc/stunnel/stunnel.conf"|grep -n "accept = ${drop[$opc]}"|cut -d ':' -f1) + 1 ))
  310. en=$(sed -n "${in}p" /etc/stunnel/stunnel.conf|cut -d ':' -f2)
  311. print_center -ama "${drop[$opc]} >>> $en"
  312. msg -bar
  313. drop_port
  314. n=1
  315. for i in $DPB; do
  316. port=$(echo $i|awk -F ":" '{print $2}')
  317. [[ "$port" = "$en" ]] && continue
  318. proto=$(echo $i|awk -F ":" '{print $1}')
  319. proto2=$(printf '%-12s' "$proto")
  320. echo -e " $(msg -verd "[$n]") $(msg -verm2 ">") $(msg -ama "$proto2")$(msg -azu "$port")"
  321. drop[$n]=$port
  322. num_opc="$n"
  323. let n++
  324. done
  325. msg -bar
  326. unset opc
  327. while [[ -z $opc ]]; do
  328. msg -ne " opcion: "
  329. read opc
  330. tput cuu1 && tput dl1
  331. if [[ -z $opc ]]; then
  332. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  333. unset opc
  334. sleep 2
  335. tput cuu1 && tput dl1
  336. continue
  337. elif [[ ! $opc =~ $numero ]]; then
  338. msg -verm2 " selecciona solo numeros entre 1 y $num_opc"
  339. unset opc
  340. sleep 2
  341. tput cuu1 && tput dl1
  342. continue
  343. elif [[ "$opc" -gt "$num_opc" ]]; then
  344. msg -verm2 " selecciona una opcion entre 1 y $num_opc"
  345. sleep 2
  346. tput cuu1 && tput dl1
  347. unset opc
  348. continue
  349. fi
  350. done
  351. sed -i "$in s/$en/${drop[$opc]}/" /etc/stunnel/stunnel.conf
  352. title "Puerto de redirecion modificado"
  353. if service stunnel4 restart &> /dev/null ; then
  354. print_center -verd "Servicio stunnel4 reiniciado"
  355. else
  356. print_center -verm2 "Falla al reiniciar Servicio stunnel4"
  357. fi
  358. enter
  359. return 1
  360. }
  361. restart(){
  362. clear && msg -bar
  363. if service stunnel4 restart &> /dev/null ; then
  364. print_center -verd "Servicio stunnel4 reiniciado"
  365. else
  366. print_center -verm2 "Falla al reiniciar Servicio stunnel4"
  367. fi
  368. enter
  369. return 1
  370. }
  371. edit_nano(){
  372. nano /etc/stunnel/stunnel.conf
  373. restart
  374. return 1
  375. }
  376. title "INSTALADOR SSL By @Rufu99"
  377. echo -e "$(msg -verd " [1]") $(msg -verm2 ">") $(msg -verd "INSTALAR") $(msg -ama "-") $(msg -verm2 "DESINSTALAR")"
  378. n=1
  379. if [[ $(dpkg -l|grep 'stunnel'|awk -F ' ' '{print $2}') ]]; then
  380. msg -bar3
  381. echo -e "$(msg -verd " [2]") $(msg -verm2 ">") $(msg -verd "AGREGAR PUERTOS SSL")"
  382. echo -e "$(msg -verd " [3]") $(msg -verm2 ">") $(msg -verm2 "QUITAR PUERTOS SSL")"
  383. msg -bar3
  384. echo -e "$(msg -verd " [4]") $(msg -verm2 ">") $(msg -ama "EDITAR PUERTO DE REDIRECCION")"
  385. echo -e "$(msg -verd " [5]") $(msg -verm2 ">") $(msg -azu "EDITAR MANUAL (NANO)")"
  386. msg -bar3
  387. echo -e "$(msg -verd " [6]") $(msg -verm2 ">") $(msg -azu "INICIAR/PARAR SERVICIO SSL")"
  388. echo -e "$(msg -verd " [7]") $(msg -verm2 ">") $(msg -azu "REINICIAR SERVICIO SSL")"
  389. n=7
  390. fi
  391. back
  392. opcion=$(selection_fun $n)
  393. case $opcion in
  394. 1)ssl_stunel;;
  395. 2)add_port;;
  396. 3)del_port;;
  397. 4)edit_port;;
  398. 5)edit_nano;;
  399. 6)start-stop;;
  400. 7)restart;;
  401. 0) return 1;;
  402. esac