124 Commits

Author SHA1 Message Date
Alexander Bersenev
027ee2713b use tls-only mode in the default config 2019-10-08 14:44:09 +05:00
Alexander Bersenev
3c4c92cdb2 adjust stats duration bucket sizes 2019-10-08 14:42:03 +05:00
Alexander Bersenev
3ef826cd6b more logical name for connection duration 2019-09-21 23:55:53 +05:00
Alexander Bersenev
9ec1e543bd expose the length of connections as metrics 2019-09-21 23:17:00 +05:00
Alexander Bersenev
fae04ed3c7 do not export proxy links by default 2019-09-20 18:57:37 +05:00
Alexander Bersenev
25685f370c reword the link desc 2019-09-20 18:10:37 +05:00
Alexander Bersenev
1feb8e5fc7 export proxy links as metrics 2019-09-20 17:52:30 +05:00
Alexander Bersenev
5e488203a2 check for socks mode before importing uvloop 2019-09-20 17:26:30 +05:00
Alexander Bersenev
94fd98a1fb correct spelling 2019-09-20 17:19:33 +05:00
Alexander Bersenev
4c5b0803fa add upstream socks proxy consistency checks 2019-09-20 17:17:16 +05:00
Alexander Bersenev
8fa8aabc8e ability to specify socks user and pass 2019-09-20 16:13:06 +05:00
Alexander Bersenev
f2fbaa923b refactor upstream proxy handling 2019-09-20 16:06:59 +05:00
Alexander Bersenev
79eaabdd23 move socks handling in the config parsing 2019-09-20 15:40:33 +05:00
Alexander Bersenev
23c7b0d53b make the socks module optional 2019-09-20 15:38:18 +05:00
pasha-zzz
516600a32d Add support for upstream SOCKS5 proxy (#143) 2019-09-20 15:28:29 +05:00
Alexander Bersenev
5fcd1c0158 change metrics pushes to pulls 2019-09-19 02:27:57 +05:00
Alexander Bersenev
781549f37f update readme 2019-09-19 00:08:20 +05:00
Alexander Bersenev
dc1223fd90 add hanshake_timeout metric 2019-09-18 23:22:06 +05:00
Alexander Bersenev
0d52ae0bc7 rename metrics, add connects_all metric 2019-09-18 23:13:32 +05:00
Alexander Bersenev
2b1469985d add an ability to export metrics to prometheus 2019-09-18 19:44:19 +05:00
Alexander Bersenev
4784491800 use only half of the digest as a key for used digest 2019-09-13 17:20:32 +05:00
Alexander Bersenev
7a2c6b9825 cast client port to int 2019-09-13 01:19:19 +05:00
Alexander Bersenev
37307a98fb print replay attackers summary 2019-09-08 14:28:46 +05:00
Alexander Bersenev
02b39168c3 print the possible replay-attackers summary instead of every time 2019-09-08 14:12:06 +05:00
Alexander Bersenev
163e7b7cce print a message about getting the cert 2019-09-08 03:12:19 +05:00
Alexander Bersenev
1ed13d9efa get rid from annoying time skew messages, print summary instead 2019-09-08 02:01:23 +05:00
Alexander Bersenev
51c8d68271 disable the first bad packet heuristics if the tls-only mode activated, it has time-based protection instead 2019-09-08 01:09:44 +05:00
Alexander Bersenev
50cd74051f add a message if uvloop is found 2019-08-30 16:05:08 +05:00
Alexander Bersenev
1d826866d1 print time skew message as one line 2019-08-26 17:00:34 +05:00
Alexander Bersenev
ff6b826e13 do not output canceled errors to get rid from scarry traceback on the proxy termination 2019-08-26 16:51:48 +05:00
Alexander Bersenev
3315ac1df6 add one more param into the undocummented mode of launch 2019-08-26 16:07:35 +05:00
Alexander Bersenev
4184875405 advice to use uvloop instead of PyPy interpreter. The uvloop gives a better speed and memory consumption 2019-08-25 03:05:45 +05:00
Alexander Bersenev
d34a15bca3 nicer exception printing 2019-08-23 23:55:26 +05:00
Alexander Bersenev
56bfab51d5 necer exception printing 2019-08-23 23:51:53 +05:00
Alexander Bersenev
4f8b1b16db modify the workaround 2019-08-23 18:12:53 +05:00
Alexander Bersenev
27f5d249a7 add a workaround against the clients who send zero as their itimestamp 2019-08-23 17:19:03 +05:00
Alexander Bersenev
c51f6f85b8 correct the byte in the initial server hello 2019-08-23 05:24:18 +05:00
Alexander Bersenev
44a52bf958 remove the workaround 2019-08-23 02:19:21 +05:00
Alexander Bersenev
8520a26837 ability to disable replay protection 2019-08-23 01:59:53 +05:00
Alexander Bersenev
068996ab36 fix small typo 2019-08-23 01:45:54 +05:00
Alexander Bersenev
4faa96732f workaround: the fifth telegram server doesn't answer on IPv6 2019-08-23 01:43:44 +05:00
Alexander Bersenev
bee0b3be6b cache the ip address of mask host 2019-08-23 01:18:24 +05:00
Alexander Bersenev
09fec8ca99 Merge branch 'master' of github.com:alexbers/mtprotoproxy 2019-08-21 16:15:31 +05:00
Alexander Bersenev
25d76bee09 do not create the copy of cryptography adapter classes 2019-08-21 16:14:48 +05:00
Alexander Bersenev
a680b3e854 Update README.md 2019-08-21 03:06:20 +05:00
Alexander Bersenev
3fe87954a2 Update README.md 2019-08-21 02:50:34 +05:00
Alexander Bersenev
3fb3da139f Update README.md 2019-08-21 02:05:57 +05:00
Alexander Bersenev
014e450e62 change readme 2019-08-21 02:03:55 +05:00
Alexander Bersenev
04491f8a6a use hand-made random generator to make randoms more unpredictable 2019-08-20 01:58:19 +05:00
Alexander Bersenev
e081d6b727 generate plausible x25519 public key for server hello also 2019-08-19 21:48:54 +05:00
Alexander Bersenev
ea28a7055a small readme fixes 2019-08-19 19:16:36 +05:00
Alexander Bersenev
45cb849ca9 change tls status from experimental to new 2019-08-19 18:50:52 +05:00
Alexander Bersenev
e66818326e generate plausible keys in the key share extension 2019-08-19 17:48:10 +05:00
Alexander Bersenev
4a1bf1ec6a print warning about default settings on stderr 2019-08-19 04:59:05 +05:00
Alexander Bersenev
854aaa1f24 add flush on print 2019-08-19 04:57:48 +05:00
Alexander Bersenev
5b0ad45cb9 cosmetic fix 2019-08-19 04:56:37 +05:00
Alexander Bersenev
26e00a7409 give some advices about non-secure settings on startup 2019-08-19 04:55:09 +05:00
Alexander Bersenev
ac6d20a897 redesing exception handling logic in handle_bad_client 2019-08-19 04:13:50 +05:00
Alexander Bersenev
06ed40c815 handle unknown ip situation, for example for unix-sockets 2019-08-19 03:44:08 +05:00
Alexander Bersenev
1938c7d3bb change comment 2019-08-19 03:33:04 +05:00
Alexander Bersenev
01fd1a34c2 be more clear about proxy protocol usage 2019-08-19 03:31:10 +05:00
Alexander Bersenev
1a0977b10e handle the connection resets 2019-08-19 00:25:54 +05:00
Alexander Bersenev
9dc8521c18 copy the way how the mask server closes the tcp connection to the client 2019-08-18 21:59:33 +05:00
Alexander Bersenev
4169e6acab move the import to the top of the file 2019-08-17 15:20:05 +05:00
Vladislav Grishenko
121a8974de add unix socket support (#127)
Config option LISTEN_UNIX_SOCK = "/path/to/socket.file" allows to listen
on specified unix socket in additional to (or instead of) configured ip
addresses. Listening on a socket can be useful for connection from local
reverse proxy w/o wasting tcp ports and network subsystem resources just
for inter-process communication.
Default value is empty - socket not used.
2019-08-17 15:11:49 +05:00
Alexander Bersenev
fdf5efe3d2 change max tls record size to make it look like complying https://tools.ietf.org/html/rfc8446\#section-5.2 instead of the section 5.1 2019-08-17 14:42:49 +05:00
Alexander Bersenev
015d0a2012 be more tolerate to time skewing. This should cover 90% of cases 2019-08-17 13:21:57 +05:00
Alexander Bersenev
dcad0bd51b if the client time is skewed, just print a message for a while. Additional analysis needed 2019-08-17 04:48:17 +05:00
Alexander Bersenev
da9e51ed03 better error texts 2019-08-17 02:07:17 +05:00
Alexander Bersenev
59306e6e67 take cert length from the masked host 2019-08-16 20:25:23 +05:00
Alexander Bersenev
7502d1dc31 add one more message about the client time skew 2019-08-16 18:11:50 +05:00
Alexander Bersenev
9df42cda79 time based protection against replay attack 2019-08-16 18:01:20 +05:00
Alexander Bersenev
a65f7a8e17 add one more nonce in the reserved nonces list 2019-08-16 16:45:22 +05:00
Alexander Bersenev
8a4bc77125 make default client handshake timeout random 2019-08-16 16:23:14 +05:00
Alexander Bersenev
294cb65738 rename the function from pseudo tls to the fake tls to make the terminology consistent 2019-08-16 16:15:09 +05:00
Alexander Bersenev
559c577df1 early break if the protocol is not tls 2019-08-16 15:17:07 +05:00
Alexander Bersenev
ccc8c3fb14 read initial tls header byte by byte 2019-08-16 15:07:27 +05:00
Alexander Bersenev
e061cd81c4 optimize reading a handshake by detecting tls early 2019-08-15 18:26:56 +05:00
Alexander Bersenev
7527d402d6 mimic as tls 1.3 instead of tls 1.2 to bypass uncrypted certificates problem 2019-08-15 16:23:44 +05:00
Vladislav Grishenko
f51a4bfe34 Add proxy protocol v1/v2 support (#119)
* add proxy protocol v1/v2 support

With fake-tls enabled, it was still quite hard to use mtprotoproxy
as backend behing some reverse https/tls proxy (nginx, haproxy, etc)
because it still need client address & port info.
With nginx already configured to use stream proxy with proxy protocol,
it was impossibe to connect due additional proxy header transmission
before real hadshake.
Adding general support of proxy protocol fixed both issues.

New config option PROXY_PROTOCOL = True enables transparent support,
unproxied incoming connections will still be accepted.
Since reverse proxy needs to be trusted, option disabled by default.

References:
* https://www.haproxy.com/blog/haproxy/proxy-protocol/
* http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt

* slightly optimize proxy v1 error path

* rework proxy handler

* deny direct connection with enabled PROXY_PROTOCOl per specs
* simplify proxy header checking
* use textual form of proxy v1 header
* drop useless find() call

* fix client address logging
2019-08-14 23:03:01 +05:00
Alexander Bersenev
d9fa5b222a remove unused variable 2019-08-14 04:07:36 +05:00
Alexander Bersenev
91ec36653e add slots to classes, thanks to MrMrRobat 2019-08-14 03:59:44 +05:00
Allineer
e43ae99911 Fix default port in the main script.
Different port in main and config files. It's changed to 3256.
2019-08-13 16:42:14 +05:00
Alexander Bersenev
ab52521a25 change the tls links to hex encoding until base64 is fixed 2019-08-13 16:26:38 +05:00
Alexander Bersenev
53184470e9 fixed cached readings in fake tls 2019-08-13 15:23:47 +05:00
Alexander Bersenev
48330f1e8a shrunk max tls record size according to https://tools.ietf.org/html/rfc8446\#section-5.1 2019-08-13 03:31:49 +05:00
Alexander Bersenev
4e2cb87685 change the comment 2019-08-13 02:52:41 +05:00
Alexander Bersenev
a030ae2978 redirect bad clients to some host 2019-08-13 02:06:17 +05:00
Alexander Bersenev
3d8961316e use urlsafe version of base64 encoder 2019-08-11 21:28:55 +05:00
Alexander Bersenev
d7c163c0dc add tls only mode 2019-08-11 21:22:10 +05:00
Alexander Bersenev
80062c95bc print proxy addresses and logins on reload 2019-07-26 14:51:41 +05:00
Alexander Bersenev
c1fdc4c0a3 move setting instant rst into a function 2019-07-25 02:09:10 +05:00
Alexander Bersenev
f5d41e9aa7 close tcp connection with rst when tls failed 2019-07-25 02:05:04 +05:00
Alexander Bersenev
4e754a75bd add an experimental note for the new proto 2019-07-24 03:16:03 +05:00
Alexander Bersenev
1a934f992d add fake-tls mode 2019-07-24 03:03:36 +05:00
Alexander Bersenev
c543bc1c3d launch script explicitely using python 2019-07-23 18:01:44 +05:00
Alexander Bersenev
96ba65aba0 add one more reserved string in the nonce beginnings 2019-07-23 10:26:58 +05:00
Alexander Bersenev
985e3eb546 add user data quotas 2019-07-22 21:34:09 +05:00
Alexander Bersenev
129f5cc981 use global var to enable direct connect 2019-07-22 18:55:27 +05:00
Alexander Bersenev
6fb022284e check if ipv6 is available every time before using 2019-07-22 18:24:03 +05:00
Alexander Bersenev
4691917c34 optimize docker containers for reloading 2019-07-22 17:21:43 +05:00
Alexander Bersenev
7d11ff07bd cancel all tasks on exit 2019-07-19 15:58:54 +05:00
Alexander Bersenev
1c875e3d31 do not listen if listen addr is zero 2019-07-19 15:46:34 +05:00
Alexander Bersenev
a2890cf213 redesign the config handling 2019-07-19 03:11:18 +05:00
Alexander Bersenev
026849cb54 rename USER_EXPIRATION to USER_EXPIRATIONS and reformat the code to comply pep8 2019-06-29 13:15:08 +05:00
Alexander Bersenev
4f1fea79eb Merge pull request #107 from pouryare/master
adding expiration date for users
2019-06-29 12:51:51 +05:00
pouryare
50df84bc80 Update mtprotoproxy.py 2019-06-28 16:41:41 +04:30
pouryare
6823117c63 Update mtprotoproxy.py 2019-06-27 06:23:45 +04:30
pouryare
d21eab60c2 Update mtprotoproxy.py
adding expiration date for users
2019-06-27 06:05:01 +04:30
Alexander Bersenev
312539c3b8 more reliable protection from replay attacks 2019-05-30 14:36:24 +05:00
Alexander Bersenev
a9e12bb1bb add warning about default secrets 2019-05-17 02:45:24 +05:00
Alexander Bersenev
6c5155ce07 rename USER_CONN_LIMITS to USER_MAX_TCP_CONNS 2019-05-17 02:35:39 +05:00
Alexander Bersenev
422c409480 update the ip address of one of the telegram servers 2019-05-17 00:27:40 +05:00
Alexander Bersenev
cdd4fef49e better comment 2019-05-15 17:42:33 +05:00
Alexander Bersenev
9da90d25d3 ability to specify user limits 2019-05-15 17:16:01 +05:00
Alexander Bersenev
eba7f9be69 protect from time skewing. The proxy protocol is very sensible to clock skew. If the skew is detected, disable advertising, making the connection directly to tg servers, instead of middle proxies 2019-05-12 01:42:20 +05:00
Alexander Bersenev
af8c102449 disable one fingerprinting protection by default because it causes trouble on some ios clinets 2019-05-09 03:29:53 +05:00
Alexander Bersenev
a01896522d changed the comment 2019-05-09 02:59:06 +05:00
Alexander Bersenev
6f70ff3003 adaptive buffer sizes 2019-05-09 02:51:36 +05:00
Alexander Bersenev
d48c177e36 comment out the message active fingerprinting - there is too many messages 2019-04-23 15:01:34 +05:00
Alexander Bersenev
f55ae68092 even more protect against replay-based fingerprinting 2019-04-20 15:02:13 +05:00
Alexander Bersenev
4cae6290b9 active fingerprinting detection and blocking 2019-04-20 04:44:11 +05:00
Alexander Bersenev
830d55fe77 fix ipv4 resolver url 2019-04-04 16:06:24 +05:00
Alexander Bersenev
66d9c03ff9 set secure mode on by default because most tg clients support this mode and many countries are able to detect proxies in non-secure mode 2019-03-10 23:02:27 +05:00
5 changed files with 1242 additions and 279 deletions

View File

@@ -1,15 +1,13 @@
FROM alpine:3.8
FROM alpine:3.10
RUN adduser tgproxy -u 10000 -D
RUN apk add --no-cache python3 py3-cryptography ca-certificates libcap
COPY mtprotoproxy.py config.py /home/tgproxy/
RUN chown -R tgproxy:tgproxy /home/tgproxy
RUN setcap cap_net_bind_service=+ep /usr/bin/python3.6
RUN setcap cap_net_bind_service=+ep /usr/bin/python3.7
USER tgproxy
WORKDIR /home/tgproxy/
CMD ["./mtprotoproxy.py"]
CMD ["python3", "mtprotoproxy.py"]

View File

@@ -1,17 +1,19 @@
# Async MTProto Proxy #
Fast and simple to setup mtproto proxy.
Fast and simple to setup MTProto proxy written in Python.
## Starting Up ##
1. `git clone -b stable https://github.com/alexbers/mtprotoproxy.git; cd mtprotoproxy`
2. *(optional, recommended)* edit *config.py*, set **PORT**, **USERS** and **AD_TAG**
3. `docker-compose up --build -d` (or just `python3 mtprotoproxy.py` if you don't like docker)
4. *(optional, shows telegram link to set the proxy)* `docker-compose logs`
3. `docker-compose up -d` (or just `python3 mtprotoproxy.py` if you don't like Docker)
4. *(optional, get a link to share the proxy)* `docker-compose logs`
![Demo](https://alexbers.com/mtprotoproxy/install_demo_v2.gif)
## Channel Advertising ##
To advertise a channel get a tag from **@MTProxybot** and write it to *config.py*.
To advertise a channel get a tag from **@MTProxybot** and put it to *config.py*.
## Performance ##
@@ -23,5 +25,5 @@ the VDS instance with 1 CPU core and 1024MB RAM.
The proxy can be launched:
- with a custom config: `python3 mtprotoproxy.py [configfile]`
- several times, clients will be automaticaly balanced between instances
- using *PyPy* interprteter
- with runtime statistics exported for [Prometheus](https://prometheus.io/): using [prometheus](https://github.com/alexbers/mtprotoproxy/tree/prometheus) branch
- with uvloop module to get an extra speed boost
- with runtime statistics exported to [Prometheus](https://prometheus.io/)

View File

@@ -6,9 +6,17 @@ USERS = {
"tg2": "0123456789abcdef0123456789abcdef"
}
# Makes the proxy harder to detect
# Can be incompatible with very old clients
SECURE_ONLY = True
# Makes the proxy even more hard to detect
# Compatible only with the recent clients
TLS_ONLY = True
# The domain for TLS, bad clients are proxied there
# Use random existing domain, proxy checks it on start
# TLS_DOMAIN = "www.google.com"
# Tag for advertising, obtainable from @MTProxybot
# AD_TAG = "3c09c680b76ee91a4c25ad51f742267d"
# Uncommenting this do make a proxy harder to detect
# But it can be incompatible with old clients
# SECURE_ONLY = True

View File

@@ -4,4 +4,7 @@ services:
build: .
restart: unless-stopped
network_mode: "host"
volumes:
- ./config.py:/home/tgproxy/config.py
- ./mtprotoproxy.py:/home/tgproxy/mtprotoproxy.py
# mem_limit: 1024m

File diff suppressed because it is too large Load Diff