ssl.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. msg -bar
  184. sleep 3
  185. return 1
  186. }
  187. title "INSTALADOR SSL By @Rufu99"
  188. menu_func "ININICIAR O PARAR SSL" "AGREGAR PUERTOS SSL"
  189. msg -bar
  190. echo -ne "$(msg -verd " [0]") $(msg -verm2 ">") " && msg -bra "\033[1;41mVOLVER"
  191. msg -bar
  192. msg -ne " Opcion: "
  193. read opcao
  194. case $opcao in
  195. 1)ssl_stunel;;
  196. 2)add_port;;
  197. 0) return 1;;
  198. esac