Skip to content

Instantly share code, notes, and snippets.

@aliqandil
Last active December 7, 2018 17:58
Show Gist options
  • Select an option

  • Save aliqandil/a3e172a641286876d23bd93dbcdc0a0a to your computer and use it in GitHub Desktop.

Select an option

Save aliqandil/a3e172a641286876d23bd93dbcdc0a0a to your computer and use it in GitHub Desktop.
Pure Python Script to Create an MTProto Proxy server for telegram with a single command. (Command on Line 3)
# To get and run this script, excecute:
#
# sudo bash -c 'python <(curl "https://gist.github.com/aliqandil/a3e172a641286876d23bd93dbcdc0a0a/raw/create_mtproto_proxy_server.py" -sL -N)'
#
from __future__ import print_function
from contextlib import closing
from collections import defaultdict
import socket, json, os, sys, shlex, hashlib, random
#Checking admin rights:
if os.getuid() != 0:
print(" -- This script must be run with sudo access -- ")
exit(1)
if sys.version_info[0] == 3:
import urllib.request
def GET(url): return urllib.request.urlopen(url).read().decode('utf8')
def MD5(s): return hashlib.md5(s.encode('utf8')).hexdigest()
def JDUMP(obj): return json.dumps(configfile).encode('utf8')
else:
import urllib2
def GET(url): return urllib2.urlopen(url).read()
def MD5(s): return hashlib.md5(s).hexdigest()
def JDUMP(obj): return json.dumps(configfile)
input = raw_input
print ( "Checking good available ports..." )
#Checking if good ports are usable:
suggestedPorts = []
for port in [443, 80, 8080, 1080, 6969, 18580]:
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
if sock.connect_ex(('localhost', port)) != 0:
suggestedPorts.append( port )
print ( "Possible ports:\n - {}".format( '\n - '.join([ str(i) for i in suggestedPorts]) ) )
print ( "Checking available hosts..." )
#Finding all possible hosts you may want to use:
suggestedHosts, possibleHosts = [], []
try:
possibleHosts.append( os.popen('hostname').read().strip() )
except Exception as e:
print ("Failed to get 'hostname' from your os.")
try:
temp = socket.gethostname().strip()
if temp not in possibleHosts:
possibleHosts.append( temp )
except Exception as e:
print ("Failed to get 'hostname' from socket.")
your_ip = json.loads( GET("http://ip-api.com/json") )['query']
#revese dns:
for ip_host in GET('https://api.hackertarget.com/reversedns/?q=' + your_ip).split('\n'):
if your_ip in ip_host:
possibleHosts.append( ip_host.split(your_ip, 1)[1].strip() )
#reverse ip lookup:
for domain in GET('https://api.hackertarget.com/reverseiplookup/?q=' + your_ip).split('\n'):
if domain == "API count exceeded": print("Getting more domains failed!"); break
if domain and your_ip not in domain:
possibleHosts.append( domain )
print ( "Found {} possible host(s).".format( len(possibleHosts) ) )
print ( "Testing possible hosts dns records..." )
ML = 0
for d in possibleHosts: ML = len( d ) if ML < len( d ) else ML
#and a final dns lookup
for host in possibleHosts:
print( " - " + host.center(ML) + ' -> ', end = '' )
for entry in GET("https://api.hackertarget.com/dnslookup/?q=" + host).split('\n'):
if your_ip in entry:
suggestedHosts.append( host )
print("Working!")
break
else:
if entry == 'API count exceeded':
suggestedHosts.append( host )
print( "Not sure if working!" )
else:
print( "Not working!" )
suggestedHosts.insert(0,your_ip)
print("Now time to choose your host and port, these are my suggestions, you can choose your own.")
print( "What will your host be? (0-{}): ".format( len(suggestedHosts) ) )
print(" 0 - Let me choose myself.")
for i, host in enumerate(suggestedHosts, 1):
print( " {} - {} ".format(i, host) )
number = int(input("Answer: "))
if number == 0:
HOST = input("Your Host: ")
else:
HOST = suggestedHosts[number-1]
print( "What will your port number? (0-{}): ".format( len(suggestedPorts) ) )
print(" 0 - Let me choose myself.")
for i, port in enumerate(suggestedPorts, 1):
print( " {} - {} ".format(i, port) )
number = int(input("Answer: "))
if number == 0:
PORT = int(input("Your Port: "))
else:
PORT = suggestedPorts[number-1]
install_npm = True
if os.popen('which npm').read().strip():
npmv = os.popen('npm -v').read().strip()
print( "It looks like you have npm {} installed, do you want me to try to install it anyway? [y/n] (default: Y):".format( npmv ) )
install_npm = True if input().upper() != "N" else False
if install_npm:
osr = os.system("curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -")
if osr != 0:
print("Something went wrong, Abort or Ignore? [a/i] (default: A):")
if input().upper() != "I":
exit( 1 )
osr = os.system("apt-get install -y nodejs git")
if osr != 0:
print("Something went wrong, Abort or Ignore? [a/i] (default: A):")
if input().upper() != "I":
exit( 1 )
install_pm2 = True
if os.popen('which pm2').read().strip():
print( "It looks like you have pm2 {} installed, do you want me to try to install it anyway? [y/n] (default: N):".format( npmv ) )
install_pm2 = False if input().upper() != "Y" else True
if install_pm2:
osr = os.system("npm install pm2 -g")
if osr != 0:
print("Something went wrong, Abort or Ignore? [a/i] (default: A):")
if input().upper() != "I":
exit( 1 )
osr = os.system("git clone https://github.com/FreedomPrevails/JSMTProxy.git")
if osr != 0:
print("Something went wrong, Abort or Ignore? [a/i] (default: A):")
if input().upper() != "I":
exit( 1 )
SECRET = MD5("Salty!" + HOST + str(possibleHosts) + str(random.randint( -9999999, +9999999 )) + input("Type Something Random for more security: "))
configfile = { 'port' : PORT, 'secret' : SECRET }
with open( 'JSMTProxy/config.json', 'wb' ) as fw:
fw.write( JDUMP(configfile) )
print( """\n\n\nDone. To start the server, navigate to the 'JSMTProxy' folder:
cd JSMTProxy/
Then run:
$> pm2 start mtproxy.js -i max
To Stop the service:
$> pm2 stop mtproxy.js
To list current processes:
$> pm2 list
To see the log:
$> pm2 log #id
And this is your telegram proxy link:
https://t.me/proxy?server={}&port={}&secret={}""".format( HOST, PORT, SECRET ) )
@aliqandil
Copy link
Author

This is not useful anymore, since "JSMProxy" Is not useful anymore as it does not support adding random padding to the packets, your server can be blocked by "DPI".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment