Fast Reverse Proxy (FRP)

Hôm nay dạo 1 vòng trên mạng bỗng phát hiện 1 thứ rất hay, có thể làm giảm đi 1 số thứ mà tôi đang phải overthinking hằng ngày.

Vấn đề:

Tôi luôn lo lắng về website của tôi, 1 ngày nào đó dung lượng lưu trữ của nó sẽ cạn, vì ổ cứng server tôi đang thuê là không nhiều (tầm 20GB) với cấu hình 2 vcpus và 2GB ram, tiền thuê ngót nghét mỗi tháng cũng tầm 300k. Vì tôi là thằng thích up ảnh, nhạc, phim, … lên wordpress để chia sẻ, cho nên dung lượng đó không bao giờ là đủ, mướn thêm S3 cũng tốn tiền. Vừa hay đang có con NAS Synology DS923+ ở nhà, ổ cứng bao la, chạy 24/7 nữa => Có cách nào xà xẻo nó được không?

Giải pháp:

CÓ. FRP chính là vị cứu tinh này, nó giúp tạo 1 network nội bộ giữa NAS và VPS, giúp VPS có thể SMB về NAS nhà tôi, chỉ cần mount và symbolic link 1 phát cho thư mục uploads của wordpress là ngon cơm =]]]

Fast Reverse Proxy Architecture

Hiểu đơn giản là như thế này, frp này nó sẽ đào 1 cái tunnel giữa nas và vps, mapping port SMB của NAS sang 1 port nào đó của VPS, và khi chúng ta connect SMB port đó của VPS nó sẽ forward về port SMB của NAS.

Nhược điểm:

Đương nhiên là cái nào cũng có 2 mặt rồi =]]] vì đây là ổ cứng mạng nên tốc độ sẽ bị giới hạn bởi tốc độ mạng nhà tôi và vps =]] và chắc chắn cũng sẽ có độ trễ :3

Rồi giờ làm sao =]]]

Nói nhiều đủ rồi =]]], h bắt tay vào làm thôi =]]]

Vào git của fatedier/frp: https://github.com/fatedier/frp/releases/tag/v0.61.2

Vì server của tôi là Ubuntu x64 => download link này: https://github.com/fatedier/frp/releases/download/v0.61.2/frp_0.61.2_linux_amd64.tar.gz

Down về, giải nén ra, chỉnh sửa nội dung file frps.toml

bindPort = <server-tcp-port>
quicBindPort = <server-udp-port>
transport.quic.keepaliveperiod = 10
transport.quic.maxidletimeout = 30
transport.quic.maxincomingstreams = 100000
proxyBindAddr = "127.0.0.1" # Để remotePort ko bị public ra ngoài
auth.method = "token"
auth.token = "<secret-token-here>"

Chỉnh sửa file frps.toml

serverAddr = "<server-ip-or-dns>"
serverPort = <server-udp-port>
transport.protocol = "quic"
transport.quic.keepalivePeriod = 10
transport.quic.maxIdleTimeout = 30
transport.quic.maxIncomingStreams = 100000
auth.method = "token"
auth.token = "<secret-token-here>"

[[proxies]]
name = "smb"
type = "tcp"
localIP = "127.0.0.1"
localPort = 445 # SMB Port
remotePort = <smb-server-port> # Tự chọn port nha, nhớ con số này để tí connect

Tạo 1 frps.service ở trong /usr/lib/systemd/system để frps có thể start cùng Ubuntu.

[Unit]
Description=FRP Server (Fast Reverse Proxy)
After=network.target

[Service]
ExecStart=/home/hoshizora/frp_0.61.2_linux_amd64/frps -c /home/hoshizora/frp_0.61.2_linux_amd64/frps.toml
Restart=always
WorkingDirectory=/home/hoshizora/frp_0.61.2_linux_amd64

[Install]
WantedBy=multi-user.target
systemctl enable frps
service frps restart

Xong cái server ròi đó :3, h vào Synology -> Container Manager, pull cái fatedier/frpc trên docker hub về

Bấm vào nó chọn Run và làm theo hướng dẫn

Chọn auto restart để khởi động cùng Synology, hoặc nếu failed thì cũng tự restart lại luôn
Map file config frpc.toml lúc nãy vào docker
Quất cái high privilege và chọn network là host, Next -> Done là OK

Quay trở về server và chạy 2 lệnh dưới đây 1 lần duy nhất

mkdir /mnt/my-nas-storage
ln -s /mnt/my-nas-storage/wordpress/wp-content/uploads /home/mine/wordpress/wp-content/uploads

Tạo file .smbcredentials ở /home/mine vs mode 600 có nội dung là username, password của account SMB trên NAS như sau:

username=<username-here>
password=<password-here>

Cuối cùng là chạy lệnh mount và hưởng thụ thành quả 😀

mount -t cifs //localhost/My_NAS_Storage /mnt/my-nas-storage -o credentials=/home/mine/.smbcredentials,port=<smb-server-port>,iocharset=utf8,uid=1000,gid=1001,file_mode=0777,dir_mode=0777,vers=3.0

Để lại một bình luận