proxy.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #!/bin/bash
  2. clear
  3. SCPinst="/etc/VPS-MX/protocolos"
  4. mportas () {
  5. unset portas
  6. portas_var=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" |grep -v "COMMAND" | grep "LISTEN")
  7. while read port; do
  8. var1=$(echo $port | awk '{print $1}') && var2=$(echo $port | awk '{print $9}' | awk -F ":" '{print $2}')
  9. [[ "$(echo -e $portas|grep "$var1 $var2")" ]] || portas+="$var1 $var2\n"
  10. done <<< "$portas_var"
  11. i=1
  12. echo -e "$portas"
  13. }
  14. BARRA="\e[0;31m=====================================================\e[0m"
  15. pid_kill () {
  16. [[ -z $1 ]] && refurn 1
  17. pids="$@"
  18. for pid in $(echo $pids); do
  19. kill -9 $pid &>/dev/null
  20. done
  21. }
  22. det_py(){
  23. pidproxy3=$(ps x | grep "lacasitamx.py" | grep -v "grep" | awk -F "pts" '{print $1}') && [[ ! -z $pidproxy3 ]] && pid_kill $pidproxy3
  24. echo -e "\033[1;91m SOCKS DETENIDO"
  25. rm /etc/VPS-MX/PortPD.log &>/dev/null
  26. rm -rf /etc/VPS-MX/protocolos/lacasitamx.py
  27. }
  28. pytho_py(){
  29. clear
  30. echo ""
  31. if [[ ! -e /etc/VPS-MX/fix ]]; then
  32. echo ""
  33. ins(){
  34. apt-get install python -y
  35. apt-get install python-pip -y
  36. }
  37. ins &>/dev/null && echo -e "INSTALANDO FIX" | pv -qL 40
  38. sleep 1.s
  39. [[ ! -e /etc/VPS-MX/fix ]] && touch /etc/VPS-MX/fix
  40. else
  41. echo ""
  42. fi
  43. clear
  44. echo ""
  45. echo ""
  46. msg -tit
  47. echo -e "$BARRA"
  48. echo -e "\033[1;31m HTTP | PYTHON | CUSTOM\033[0m"
  49. echo -e "$BARRA"
  50. #puerto a usar para proxy python
  51. while true; do
  52. echo -ne "\033[1;37m"
  53. read -p " ESCRIBE SU PUERTO: " proxycolor
  54. echo -e ""
  55. [[ $(mportas|grep -w "$proxycolor") ]] || break
  56. echo -e " ESTE PUERTO YA ESTÁ EN USO"
  57. unset proxycolor
  58. done
  59. #puerto local
  60. echo -e "$BARRA"
  61. while true; do
  62. echo -ne "\033[1;36m"
  63. read -p " Introduzca El puerto Local (22|443|80): " PORTLOC
  64. echo ""
  65. if [[ ! -z $PORTLOC ]]; then
  66. if [[ $(echo $PORTLOC|grep [0-9]) ]]; then
  67. [[ $(mportas|grep $PORTLOC|head -1) ]] && break || echo -e "ESTE PUERTO NO EXISTE"
  68. fi
  69. fi
  70. done
  71. #
  72. portlocal="$(mportas|grep $PORTLOC|awk '{print $2}'|head -1)"
  73. echo -e "$BARRA"
  74. read -p " Escribe el HTTP Response? (101|200|300): " encabezad
  75. tput cuu1 && tput dl1
  76. if [[ -z $encabezad ]]; then
  77. encabezad="200"
  78. echo -e " \e[31mResponse Default:\033[1;32m ${encabezad}"
  79. else
  80. echo -e " \e[31mResponse Elegido:\033[1;32m ${encabezad}"
  81. fi
  82. echo -e "$BARRA"
  83. echo -ne " Introduzca un texto para el status en HTML:\n \033[1;37m" && read mensage
  84. tput cuu1 && tput dl1
  85. if [[ -z $mensage ]]; then
  86. mensage="@lacasitamx"
  87. echo -e " \e[31mMensage Default: \033[1;32m${mensage} "
  88. else
  89. echo -e " \e[31mMensage: \033[1;32m ${mensage}"
  90. fi
  91. echo -e "$BARRA"
  92. [[ ! -e /etc/VPS-MX/protocolos/lacasitamx.py ]] && rm -rf /etc/VPS-MX/protocolos/lacasitamx.py
  93. #
  94. # Inicializando o Proxy
  95. (
  96. less << CPM > /etc/VPS-MX/protocolos/lacasitamx.py
  97. # -*- coding: utf-8 -*-
  98. import socket, threading, thread, select, signal, sys, time, getopt
  99. LISTENING_ADDR = '0.0.0.0'
  100. LISTENING_PORT = int("$proxycolor")
  101. PASS = ''
  102. BUFLEN = 4096 * 4
  103. TIMEOUT = 60
  104. DEFAULT_HOST = '127.0.0.1:$portlocal'
  105. msg = "HTTP/1.1 $encabezad $mensage\r\n\r\nHTTP/1.1 $encabezad !!!conexion exitosa!!!\r\n\r\n"
  106. RESPONSE = str(msg)
  107. class Server(threading.Thread):
  108. def __init__(self, host, port):
  109. threading.Thread.__init__(self)
  110. self.running = False
  111. self.host = host
  112. self.port = port
  113. self.threads = []
  114. self.threadsLock = threading.Lock()
  115. self.logLock = threading.Lock()
  116. def run(self):
  117. self.soc = socket.socket(socket.AF_INET)
  118. self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  119. self.soc.settimeout(2)
  120. self.soc.bind((self.host, self.port))
  121. self.soc.listen(0)
  122. self.running = True
  123. try:
  124. while self.running:
  125. try:
  126. c, addr = self.soc.accept()
  127. c.setblocking(1)
  128. except socket.timeout:
  129. continue
  130. conn = ConnectionHandler(c, self, addr)
  131. conn.start()
  132. self.addConn(conn)
  133. finally:
  134. self.running = False
  135. self.soc.close()
  136. def printLog(self, log):
  137. self.logLock.acquire()
  138. print log
  139. self.logLock.release()
  140. def addConn(self, conn):
  141. try:
  142. self.threadsLock.acquire()
  143. if self.running:
  144. self.threads.append(conn)
  145. finally:
  146. self.threadsLock.release()
  147. def removeConn(self, conn):
  148. try:
  149. self.threadsLock.acquire()
  150. self.threads.remove(conn)
  151. finally:
  152. self.threadsLock.release()
  153. def close(self):
  154. try:
  155. self.running = False
  156. self.threadsLock.acquire()
  157. threads = list(self.threads)
  158. for c in threads:
  159. c.close()
  160. finally:
  161. self.threadsLock.release()
  162. class ConnectionHandler(threading.Thread):
  163. def __init__(self, socClient, server, addr):
  164. threading.Thread.__init__(self)
  165. self.clientClosed = False
  166. self.targetClosed = True
  167. self.client = socClient
  168. self.client_buffer = ''
  169. self.server = server
  170. self.log = 'Connection: ' + str(addr)
  171. def close(self):
  172. try:
  173. if not self.clientClosed:
  174. self.client.shutdown(socket.SHUT_RDWR)
  175. self.client.close()
  176. except:
  177. pass
  178. finally:
  179. self.clientClosed = True
  180. try:
  181. if not self.targetClosed:
  182. self.target.shutdown(socket.SHUT_RDWR)
  183. self.target.close()
  184. except:
  185. pass
  186. finally:
  187. self.targetClosed = True
  188. def run(self):
  189. try:
  190. self.client_buffer = self.client.recv(BUFLEN)
  191. hostPort = self.findHeader(self.client_buffer, 'X-Real-Host')
  192. if hostPort == '':
  193. hostPort = DEFAULT_HOST
  194. split = self.findHeader(self.client_buffer, 'X-Split')
  195. if split != '':
  196. self.client.recv(BUFLEN)
  197. if hostPort != '':
  198. passwd = self.findHeader(self.client_buffer, 'X-Pass')
  199. if len(PASS) != 0 and passwd == PASS:
  200. self.method_CONNECT(hostPort)
  201. elif len(PASS) != 0 and passwd != PASS:
  202. self.client.send('HTTP/1.1 400 WrongPass!\r\n\r\n')
  203. elif hostPort.startswith('127.0.0.1') or hostPort.startswith('localhost'):
  204. self.method_CONNECT(hostPort)
  205. else:
  206. self.client.send('HTTP/1.1 403 Forbidden!\r\n\r\n')
  207. else:
  208. print '- No X-Real-Host!'
  209. self.client.send('HTTP/1.1 400 NoXRealHost!\r\n\r\n')
  210. except Exception as e:
  211. self.log += ' - error: ' + e.strerror
  212. self.server.printLog(self.log)
  213. pass
  214. finally:
  215. self.close()
  216. self.server.removeConn(self)
  217. def findHeader(self, head, header):
  218. aux = head.find(header + ': ')
  219. if aux == -1:
  220. return ''
  221. aux = head.find(':', aux)
  222. head = head[aux+2:]
  223. aux = head.find('\r\n')
  224. if aux == -1:
  225. return ''
  226. return head[:aux];
  227. def connect_target(self, host):
  228. i = host.find(':')
  229. if i != -1:
  230. port = int(host[i+1:])
  231. host = host[:i]
  232. else:
  233. if self.method=='CONNECT':
  234. port = 443
  235. else:
  236. port = 80
  237. port = 8080
  238. port = 8799
  239. port = 3128
  240. (soc_family, soc_type, proto, _, address) = socket.getaddrinfo(host, port)[0]
  241. self.target = socket.socket(soc_family, soc_type, proto)
  242. self.targetClosed = False
  243. self.target.connect(address)
  244. def method_CONNECT(self, path):
  245. self.log += ' - CONNECT ' + path
  246. self.connect_target(path)
  247. self.client.sendall(RESPONSE)
  248. self.client_buffer = ''
  249. self.server.printLog(self.log)
  250. self.doCONNECT()
  251. def doCONNECT(self):
  252. socs = [self.client, self.target]
  253. count = 0
  254. error = False
  255. while True:
  256. count += 1
  257. (recv, _, err) = select.select(socs, [], socs, 3)
  258. if err:
  259. error = True
  260. if recv:
  261. for in_ in recv:
  262. try:
  263. data = in_.recv(BUFLEN)
  264. if data:
  265. if in_ is self.target:
  266. self.client.send(data)
  267. else:
  268. while data:
  269. byte = self.target.send(data)
  270. data = data[byte:]
  271. count = 0
  272. else:
  273. break
  274. except:
  275. error = True
  276. break
  277. if count == TIMEOUT:
  278. error = True
  279. if error:
  280. break
  281. def main(host=LISTENING_ADDR, port=LISTENING_PORT):
  282. print "\n:-------PythonProxy-------:\n"
  283. print "Listening addr: " + LISTENING_ADDR
  284. print "Listening port: " + str(LISTENING_PORT) + "\n"
  285. print ":-------------------------:\n"
  286. server = Server(LISTENING_ADDR, LISTENING_PORT)
  287. server.start()
  288. while True:
  289. try:
  290. time.sleep(2)
  291. except KeyboardInterrupt:
  292. print 'Stopping...'
  293. server.close()
  294. break
  295. if __name__ == '__main__':
  296. main()
  297. CPM
  298. ) > $HOME/proxy.log &
  299. chmod +x /etc/VPS-MX/protocolos/lacasitamx.py &>/dev/nulll
  300. screen -dmS pydic-"$proxycolor" python ${SCPinst}/lacasitamx.py "$proxycolor" "$mensage" && echo ""$proxycolor" "$mensage"" >> /etc/VPS-MX/PortPD.log
  301. }
  302. clear
  303. echo -e "$BARRA"
  304. msg -tit
  305. echo -e "$BARRA"
  306. pidproxy=$(ps x | grep -w "lacasitamx.py" | grep -v "grep" | awk -F "pts" '{print $1}') && [[ ! -z $pidproxy ]] && plox="\e[1;32m[ON]" || plox="\e[1;31m[OFF]"
  307. #
  308. echo -e "\e[1;33m [1] \e[1;31m > \e[1;36mHTTP | PYTHON | CUSTOM $plox\e[0m"
  309. echo -e "\e[1;33m [2] \e[1;31m > \e[1;31mDESINSTALAR RECURSO\e[0m"
  310. echo -e "\e[1;33m [0] \e[1;31m > \e[1;37mVOLVER\e[0m"
  311. echo -e "$BARRA"
  312. read -p "SELECIONE UNA OPCION : " pix
  313. case $pix in
  314. 0) ;;
  315. 1) pytho_py ;;
  316. 2) det_py ;;
  317. esac
  318. #