LapTrinhBlockchain

Chia sẻ kiến thức về Lập Trình Blockchain

Kiến thức phần mềm

Hướng dẫn cài đặt Proxy MTProto trên máy chủ Ubuntu – Theo tài liệu chính thức của Telegram

Hướng dẫn cài đặt Proxy MTProto trên máy chủ Ubuntu – Theo tài liệu chính thức của Telegram

Hướng dẫn cài đặt Proxy MTProto trên máy chủ Ubuntu – Theo tài liệu chính thức của Telegram

Chia sẻ bài viết
5
(68)

Tìm hiểu cách cài đặt Proxy MTProto trên Ubuntu VPS giúp bạn truy cập Telegram an toàn, nhanh chóng, bảo mật dữ liệu qua web và vượt qua mọi kiểm duyệt.

MTProto Proxy là gì?

MTProto là giao thức mã hóa của Telegram, được thiết kế để truyền dữ liệu một cách bảo mật và nhanh chóng. Proxy MTProto giúp người dùng truy cập Telegram ngay cả khi bị hạn chế, đặc biệt tại các quốc gia hạn chế quyền truy cập internet.

Tại sao nên dùng Proxy MTProto?

  • Bypass kiểm duyệt tại các quốc gia như Iran, Trung Quốc, Nga…
  • Ẩn danh khi sử dụng Telegram.
  • Tốc độ nhanh hơn proxy SOCKS5 thông thường.
  • Có thể chia sẻ public link cho cộng đồng cùng sử dụng.

Hướng dẫn cài đặt MTProto Proxy trên Ubuntu VPS

Để cài đặt Proxy MTProto, bạn cần:

  • VPS Ubuntu 20 trở lên với RAM ≥ 512MB
  • Quyền root hoặc user có quyền sudo
  • Đã mở port tương ứng, trong bài viết này tôi dùng port 8888

Các bước cài đặt như sau:

Bước 1: Cập nhật hệ thống và cài đặt các gói cần thiết

Bạn sử dụng lệnh sau để cập nhật hệ thống:

sudo apt update && sudo apt upgrade -y

Và sử dụng tiếp lệnh sau để cài đặt các gói cần thiết:

sudo apt install git curl build-essential -y

Bước 2: Tải mã nguồn của MtProto

Giả sử chúng ta lưu mã nguồn trong thư mục /ubuntu/tools và đầu tiên chúng ta phải chuyển tới thư mục này:

cd /ubuntu/tools

Bây giờ chúng ta tải mã nguồn bằng lệnh sau:

git clone https://github.com/TelegramMessenger/MTProxy.git
cd MTProxy

Tiếp theo chúng ta tải hai tệp cấu hình proxy-secretproxy-multi.conf bằng lệnh sau:

curl -s https://core.telegram.org/getProxySecret -o proxy-secret
curl -s https://core.telegram.org/getProxyConfig -o proxy-multi.conf

Bước 3: Chỉnh sửa một chút mã nguồn để loại bỏ 1 số lỗi

Nếu như bạn thực hiện build luôn mã nguồn thì chúng ta sẽ gặp 2 lỗi như sau:

  • Lỗi 1: Lỗi khi build
    multiple definition of `kdb_hosts_loaded’
  • Lỗi 2: Lỗi khi chạy ứng
    mtproto-proxy: common/pid.c:42: init_common_PID: Assertion `!(p & 0xffff0000)’ failed.

Do đó chúng ta cần fix các lỗi này trước khi build.

Đầu tiên chúng ta mở tệp Makefile, chúng ta thêm “-fcommon” vào cuối của cờ CFLAGS:

# Trước khi sửa
CFLAGS = $(ARCH) -O3 -std=gnu11 -Wall -mpclmul -march=core2 -mfpmath=sse -mssse3 -fno-strict-aliasing -fno-strict-overflow -fwrapv -DAES=1 -DCOMMIT=\"${COMMIT}\" -D_GNU_SOURCE=1 -D_FILE_OFFSET_BITS=64

# Sau khi sửa
CFLAGS = $(ARCH) -O3 -std=gnu11 -Wall -mpclmul -march=core2 -mfpmath=sse -mssse3 -fno-strict-aliasing -fno-strict-overflow -fwrapv -DAES=1 -DCOMMIT=\"${COMMIT}\" -D_GNU_SOURCE=1 -D_FILE_OFFSET_BITS=64 -fcommon

Tiếp theo ta mở tệp “common/pid.c“, ta tìm hàm init_common_PID và thực hiện comment dòng code:
assert (!(p & 0xffff0000));

void init_common_PID (void) {
  if (!PID.pid) {
    int p = getpid ();
    //assert (!(p & 0xffff0000));
    PID.pid = p;
  }
  if (!PID.utime) {
    PID.utime = time (0);
  }
}

Bước 4: Thực hiện build mã nguồn

Chúng ta thực hiện build mã nguồn bằng lệnh sau:

make

Sau khi build thành công bạn sẽ có tệp thực thi mtproto-proxy trong thư mục objs/bin.

Bước 5: Tạo secret key

Bây giờ chúng ta cần tạo một secret key 16 bytes dùng cho bước tiếp theo. Lệnh tạo như sau:

head -c 16 /dev/urandom | xxd -ps

Giả sử key tạo ra trong bước này như sau:

9a36333b37a77111965ef2380ab19d80

Bước 5: Chạy MtProto Proxy

Đầu tiên chúng ta tạo một user riêng là mtproxy để chạy MtProto Proxy bằng lệnh:

sudo useradd -m -s /bin/false mtproxy
sudo chown -R mtproxy:mtproxy /opt/MTProxy

Sau đó chúng ta có thể chạy thủ công bằng lệnh sau:

/objs/bin/mtproto-proxy -u mtproxy -H 8888 -S 9a36333b37a77111965ef2380ab19d80 --aes-pwd proxy-secret -M 1 proxy-multi.conf

Các tham số trong lệnh trên như sau:

  • -u mtproxy: Chạy dưới user là mtproxy
  • -H 8888: Port public, để Telegram truy cập tới
  • -S 9a36333b37a77111965ef2380ab19d80: Secret key được tạo ra trong Bước 5
  • -M 1: Số lượng worker (CPU Core) sử dụng. Như cấu hình này tối đa ứng dụng chỉ sử dụng 1 CPU Core thôi.

Bước này chạy okie thì ta tắt ứng dụng sang Bước 6.

Bước 6: Chay MtProto Proxy dưới dạng service

Để chạy dịch vụ lâu dài, chúng ta cần phải chạy dưới dạng service. Đầu tiên chúng ta tạo tệp /etc/systemd/system/mtproto-proxy.service với nội dung như sau:

[Unit]
Description=MtProto-Proxy
After=network.target
 
[Service]
WorkingDirectory=/ubuntu/tools/MTProxy
Type=simple
ExecStart=/ubuntu/tools/MTProxy/objs/bin/mtproto-proxy -u mtproxy -H 8888 -S 9a36333b37a77111965ef2380ab19d80 --aes-pwd proxy-secret -M 1 proxy-multi.conf
 
[Install]
WantedBy=multi-user.target

Chạy lệnh sau để kích hoạt dịch vụ:

sudo systemctl enable mtproto-proxy

Bây giờ ta start dịch vụ bằng lệnh:

sudo service mtproto-proxy start

Xem trạng thái dịch vụ bằng lệnh:

sudo service mtproto-proxy status

Bước 7: Cấu hình tường lửa và bảo mật

Việc cấu hình này thì tùy vào từng dịch vụ nơi tạo VPS. Thường các dịch vụ sẽ có một giao diện riêng để quản lý việc này.

Trường hợp VPS quản lý firewall bằng phần mềm UFW thì bạn có thể sử dụng lệnh:

sudo ufw allow 8888/tcp

Bước 8: Kết nối Telegram với MtProto Proxy vừa cài đặt

Bây giờ chúng ta có thể tạo 1 trong hai link như sau:




tg://proxy?server=<YOUR_PUBLIC_IP>&port=8443&secret=<YOUR_SECRET_FROM_STEP_12>
https://t.me/proxy?server=<YOUR_PUBLIC_IP>&port=8443&secret=<YOUR_SECRET_FROM_STEP_12>

Giả sử IP public của VPS tôi cài đặt là xxx thì link như sau:
tg://proxy?server=42.114.15.10&port=8888&secret=9a36333b37a77111965ef2380ab19d80
https://t.me/proxy?server=42.114.15.10&port=8888&secret=9a36333b37a77111965ef2380ab19d80

Bạn chỉ cần mở link này trên trình duyệt hoặc trong Telegram, Telegram sẽ tự động hiển thị hộp thoại confirm bạn kết nối tới Proxy này.

Hướng dẫn fix một số lỗi khi build và chạy MtProto Proxy

Hầu hết các lỗi dưới đều đã được tôi fix ngay trong hướng dẫn, nếu bạn làm đúng theo hướng dẫn cài đặt của tôi ở trên, bạn sẽ không gặp phải các lỗi này.

Lỗi 1: multiple definition of `kdb_hosts_loaded’

Lỗi này xẩy ra khi bạn thực hiện build MtProto Proxy bằng lệnh make, chi tiết lỗi như sau:

cc -o objs/bin/mtproto-proxy objs/mtproto/mtproto-proxy.o objs/mtproto/mtproto-config.o objs/net/net-tcp-rpc-ext-server.o objs/lib/libkdb.a objs/lib/libkdb.a  -ggdb -rdynamic -lm -lrt -lcrypto -lz -lpthread -lcrypto
/usr/bin/ld: objs/mtproto/mtproto-config.o:(.bss+0x200a0): multiple definition of `kdb_hosts_loaded'; objs/mtproto/mtproto-proxy.o:(.bss+0x4f01b48): first defined here
/usr/bin/ld: objs/net/net-tcp-rpc-ext-server.o:(.bss+0x0): multiple definition of `kdb_hosts_loaded'; objs/mtproto/mtproto-proxy.o:(.bss+0x4f01b48): first defined here
/usr/bin/ld: objs/lib/libkdb.a(resolver.o):(.bss+0x0): multiple definition of `kdb_hosts_loaded'; objs/mtproto/mtproto-proxy.o:(.bss+0x4f01b48): first defined here
/usr/bin/ld: objs/lib/libkdb.a(parse-config.o):(.bss+0x28): multiple definition of `kdb_hosts_loaded'; objs/mtproto/mtproto-proxy.o:(.bss+0x4f01b48): first defined here
/usr/bin/ld: objs/lib/libkdb.a(net-rpc-targets.o):(.bss+0x820): multiple definition of `total_vv_tree_nodes'; objs/lib/libkdb.a(net-connections.o):(.bss+0xc34a8): first defined here
/usr/bin/ld: objs/lib/libkdb.a(net-http-server.o):(.bss+0x18): multiple definition of `http_queries'; objs/mtproto/mtproto-proxy.o:(.bss+0x488): first defined here
/usr/bin/ld: objs/lib/libkdb.a(engine-rpc.o):(.bss+0x0): multiple definition of `total_vv_tree_nodes'; objs/lib/libkdb.a(net-connections.o):(.bss+0xc34a8): first defined here
/usr/bin/ld: objs/lib/libkdb.a(net-stats.o):(.bss+0x8): multiple definition of `total_vv_tree_nodes'; objs/lib/libkdb.a(net-connections.o):(.bss+0xc34a8): first defined here
/usr/bin/ld: objs/lib/libkdb.a(pid.o):(.bss+0x0): multiple definition of `PID'; objs/lib/libkdb.a(net-stats.o):(.bss+0x30): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:94: objs/bin/mtproto-proxy] Error 1

Tôi gặp lỗi này lần đầu khi build, và tôi tìm được cách fix từ Can’t compile on Linux kov-1 5.10.0-12-amd64. Để fix lỗi này, chúng ta chỉ cần thêm “-fcommon” vào cuối của CFLAGS trong tệp Makefile. Sau khi thêm CFLAGS có giá trị như sau:

CFLAGS = $(ARCH) -O3 -std=gnu11 -Wall -mpclmul -march=core2 -mfpmath=sse -mssse3 -fno-strict-aliasing -fno-strict-overflow -fwrapv -DAES=1 -DCOMMIT=\"${COMMIT}\" -D_GNU_SOURCE=1 -D_FILE_OFFSET_BITS=64 -fcommon

Lỗi 2: mtproto-proxy: common/pid.c:42: init_common_PID: Assertion `!(p & 0xffff0000)’ failed

Lỗi này xảy ra khi lần đầu tiên tôi chạy ứng dụng mtproto-proxy, chi tiết lỗi như sau:

mtproto-proxy: common/pid.c:42: init_common_PID: Assertion `!(p & 0xffff0000)' failed.

Sau khi tham khảo 2 link: init_common_PID: Assertion `!(p & 0xffff0000)’ failedassert (!(p & 0xffff0000)); /*where p is pid*/ #41 tôi biết được sai khác kích thước biến p và số 0xffff0000. Cách đơn giản nhất là xóa dòng kiểm tra này là được.

Ta mở tệp “common/pid.c“, ta tìm hàm init_common_PID và thực hiện comment dòng code:
assert (!(p & 0xffff0000))

void init_common_PID (void) {
  if (!PID.pid) {
    int p = getpid ();
    //assert (!(p & 0xffff0000));
    PID.pid = p;
  }
  if (!PID.utime) {
    PID.utime = time (0);
  }
}

Sử dụng một số MtProto Proxy miễn phí

Nếu bạn không có dữ liệu cần bảo mật thì bạn có thể sử dụng một số MtProto Proxy miễn phí. Một số nguồn uy tín:

  • MTPro.XYZ: Cung cấp danh sách proxy MTProto được cập nhật thường xuyên. Chỉ cần kích vào link sẽ tự động thêm Proxy vào Telegram. Trang này có channel trên telegram: https://t.me/mtpro_xyz
  • MTProtoProxy.com: Tổng hợp các proxy MTProto miễn phí từ nhiều quốc gia.

Khi sử dụng Proxy miễn phí thì ngoài vấn đề về rủi ro bảo mật thì tốc độ thường chậm và không ổn định, dùng được 1 thời gian thì Proxy sẽ tèo và phải thay thế bằng Proxy mới.

Và khó chịu hơn nữa là bạn sẽ phải nhìn thấy quảng cáo rất khó chịu:

Bài viết này có hữu ích với bạn?

Kích vào một biểu tượng ngôi sao để đánh giá bài viết!

Xếp hạng trung bình 5 / 5. Số phiếu: 68

Bài viết chưa có đánh giá! Hãy là người đầu tiên đánh giá bài viết này.

Trả lời

Giao diện bởi Anders Norén