[非教程] OpenWrt 安裝XRay Reality Server

本帖最後由 semson 於 2023-11-1 13:18 編輯

講左幾個月話要試一試佢,呢兩日終於有時間落手實驗在OpenWrt上裝XRay Reality Server,結果亦如預期一樣正面。下面筆記給大家參考。

警告: 用自己IP穿墻, 有機會被墻ban。請自行評估風險。
註意: 本POST非step-by-step 教程亦不做科普, 你要有中等OpenWrt或Linux經驗。

前言
主流翻墻工具都要自架一個偽網站,門檻十分高。在router上起server更加高上加高,因為要全手動操作。之前有EPC師兄分享Merlin起V2Ray/XRay server 的教程,相當佩服! 直到XRay Reality 出現,終於有簡單又適合router 安裝的翻墻server 。

安裝流程
基於Linux既 service都係咁上下安裝流程: install binary -> config files -> init script -> open firewall port。同理Merlin 應該都適用。

要求

  • Reality 要XRay-core  >1.8.0才支援,OpenWrt 22.03及之後的版本能在官方 Feeds 直接下載
  • 已設置好DDNS,實測duckdns同dynu可過墻
  • Router 剩餘空間: 官方Feeds安裝到overlayfs要13MB(未親測), 手動安裝到ext4要37M空間
  • Router RAM >= 128MB, 後面說明原因
  • 安裝好xray-core的dependencies: opkg update && opkg install libc ca-bundle
  • 確保指定的tcp port已打開,如本POST的443
  • 如你跟我一樣裝xray-core到外置儲存, 需用Linux 文件系統如ext4


Install Binary: xray-core, geoip.dat及geosite.dat
22.03及之後的版本可以通過官方Feeds安裝。而我就小試牛刀用了全手動安裝。點解咁複雜? 因為目標router是19.07,官方Feeds沒有xray-core; 同埋我的router ROM不夠大,要手動裝到外置儲存(/dev/sda1)。

首先確認router的arch:
  1. root@OpenWrt:~# grep ARCH /etc/openwrt_release
  2. DISTRIB_ARCH='mipsel_24kc'
複製代碼
我用緊MT7621 router, 對應既arch 是mipsel_24kc。


下載非官方ipk到/tmp, 因為實測官方的不能在19.07上正確啟動
  1. cd /tmp
  2. wget [url]https://github.com/yichya/openwrt-xray/releases/download/v1.8.4/openwrt-xray_1.8.4-1_mipsel_24kc.ipk[/url]

  3. tar zxpvf openwrt-xray_1.8.4-1_mipsel_24kc.ipk #得到data.tar.gz
  4. tar zxpvf data.tar.gz #得到/usr/bin/xray binary

  5. mkdir -p /mnt/sda1/opt/xray #建立放置xray binary的目錄
  6. mv usr/bin/xray /mnt/sda1/opt/xray #將xray放到此目錄
複製代碼
geoip.dat及geosite.dat: 好多地方都可下載。以下示範用xray-core開發者提供的版本。
  1. cd /tmp
  2. wget [url]https://github.com/XTLS/Xray-core/releases/download/v1.8.4/Xray-linux-mips32le.zip[/url] #下載xray-core開發者編譯的版本
  3. unzip Xray-linux-mips32le.zip #得到geoip.dat及geosite.dat

  4. mkdir -p /mnt/sda1/opt/xray/share #建立放置geoip.dat及geosite.dat
  5. mv geoip.dat geosite.dat /mnt/sda1/opt/xray/share #將geoip.dat及geosite.dat放到此目錄
複製代碼
取得config files及init script。亦可以直接用我下面提供的code。
  1. cd /tmp
  2. wget [url]https://downloads.openwrt.org/releases/23.05.0/packages/mipsel_24kc/packages/xray-core_1.8.4-1_mipsel_24kc.ipk[/url] #下載官方的ipk
  3. tar zxpvf xray-core_1.8.4-1_mipsel_24kc.ipk #得到data.tar.gz
  4. tar zxpvf data.tar.gz #得到/etc目錄

  5. mv etc/config/xray /etc/config #將官方ipk裡的xray daemon config放到目標OpenWrt系統
  6. mv etc/init.d/xray /etc/init.d #將官方ipk裡的init script放到目標OpenWrt系統
複製代碼
/etc/config/xray 留意#備註了所有修改的地方
  1. config xray 'enabled'
  2.         option enabled '1' #修改成1=enable

  3. config xray 'config'
  4.         option confdir '/etc/xray'
  5.         list conffiles '/etc/xray/config.json'
  6.         option datadir '/mnt/sda1/opt/xray/share' #修改成存放geoip.dat及geosite.dat路徑
  7.         option dialer ''
  8.         option format 'json'
複製代碼
/etc/init.d/xray
  1. #!/bin/sh /etc/rc.common

  2. USE_PROCD=1
  3. START=99

  4. CONF="xray"
  5. PROG="/mnt/sda1/opt/xray/xray" #修改成存放xray binary的路徑

  6. start_service() {
  7.         config_load "$CONF"

  8.         local enabled
  9.         config_get_bool enabled "enabled" "enabled" "0"
  10.         [ "$enabled" -eq "1" ] || return 1

  11.         local confdir
  12.         local conffiles
  13.         local datadir
  14.         local dialer
  15.         local format

  16.         config_get confdir "config" "confdir"
  17.         config_get conffiles "config" "conffiles"
  18.         config_get datadir "config" "datadir" "/mnt/sda1/opt/xray/share" #修改成存放geoip.dat及geosite.dat路徑
  19.         config_get dialer "config" "dialer"
  20.         config_get format "config" "format" "json"

  21.         procd_open_instance "$CONF"
  22.         procd_set_param command "$PROG" run
  23.         [ -n "$confdir" ] && procd_append_param command -confdir "$confdir"
  24.         [ -n "$conffiles" ] && {
  25.                 for i in $conffiles
  26.                 do
  27.                         procd_append_param command -config "$i"
  28.                 done
  29.         }
  30.         [ -n "$format" ] && procd_append_param command -format "$format"
  31.         [ -n "$dialer" ] && procd_set_param env XRAY_BROWSER_DIALER="$dialer"
  32.         procd_set_param env XRAY_LOCATION_ASSET="$datadir"
  33.         procd_set_param file $conffiles

  34.         procd_set_param limits core="unlimited"
  35.         procd_set_param limits nofile="1000000 1000000"
  36.         procd_set_param stdout 1
  37.         procd_set_param stderr 1
  38.         procd_set_param respawn

  39.         procd_close_instance
  40. }

  41. reload_service() {
  42.         stop
  43.         start
  44. }

  45. service_triggers() {
  46.         procd_add_reload_trigger "$CONF"
  47. }
複製代碼
/etc/xray/config.json
  1. {
  2.     "log": {
  3.         "loglevel": "none" // 不要在flash memory上開logging
  4.     },
  5.     "routing": {
  6.         "domainStrategy": "IPIfNonMatch",
  7.         "rules": [
  8.             {
  9.                 "type": "field",
  10.                 "ip": [
  11.                     "geoip:cn",
  12.                     "geoip:private"
  13.                 ],
  14.                 "outboundTag": "block"
  15.             }
  16.         ]
  17.     },
  18.     "inbounds": [
  19.         {
  20.             "listen": "0.0.0.0",
  21.             "port": 443, // 擔心tcp/443安全問題可以改用high port,但增加被墻的風險
  22.             "protocol": "vless",
  23.             "settings": {
  24.                 "clients": [
  25.                     {
  26.                         "id": "5551491f-7715-4e24-acb8-0f58aebe0425", // 可用xray uuid指令自己generate
  27.                         "flow": "xtls-rprx-vision"
  28.                     }
  29.                 ],
  30.                 "decryption": "none"
  31.             },
  32.             "streamSettings": {
  33.                 "network": "tcp",
  34.                 "security": "reality",
  35.                 "realitySettings": {
  36.                     "dest": "hk.trip.com:443", // 最好改用自己找到的偽裝網站
  37.                     "serverNames": [
  38.                         "trip.com",
  39.                         "hk.trip.com"
  40.                     ],
  41.                     "privateKey": "CHmkBno7OvQDrIn7WXU6SPkebKKiH_AgEA1ClfHdBHw", // 可用xray x25519指令自己generate, 對應的Public key是"JOu2R5Ld0dwJ42wYvewATlQwBVUs7ioWVNkvoySHAl8"
  42.                     "shortIds": [
  43.                         ""
  44.                     ]
  45.                 }
  46.             },
  47.             "sniffing": {
  48.                 "enabled": true,
  49.                 "destOverride": [
  50.                     "http",
  51.                     "tls",
  52.                     "quic"

  53.                 ]
  54.             }
  55.         }
  56.     ],
  57.     "outbounds": [
  58.         {
  59.             "protocol": "freedom",
  60.             "tag": "direct"
  61.         },
  62.         {
  63.             "protocol": "blackhole",
  64.             "tag": "block"
  65.         }
  66.     ],
  67.     "policy": {
  68.         "levels": {
  69.             "0": {
  70.                 "handshake": 3,
  71.                 "connIdle": 180
  72.             }
  73.         }
  74.     }
  75. }
複製代碼
Client: v2rayN / v2rayNG
請參考此網站:https://cscot.pages.dev/2023/03/02/Xray-REALITY-tutorial/
如遇到問題,可以用logread |grep xray睇log。/etc/xray/config.json 裡指定loglevel可以睇到更多資訊。

Benchmark
簡單測試,mt7621 speedtest最高約38Mbps,此時所有cpu core/thread 都係100%。 RAM用約100MB,佔用大戶係adguard home + banip + xray-core。不專業結論適合3-4個client輕量用,平均每人分到10Mbps。router效能低過mt7621就不用考慮了。

本帖最後由 semson 於 2023-11-1 12:16 編輯

補充講下OpenWrt 22.03版本後的安裝方法。

安裝xray-core:
  1. opkg update
  2. opkg install xray-core
複製代碼
xray-geodata在22.03後沒有提供, 可以用v2ray的替代:
  1. opkg install v2ray-geoip v2ray-geosite
複製代碼
/etc/config/xray
  1. 參考上面的POST
複製代碼
/etc/xray/config.json
  1. 參考上面的POST
複製代碼
附mt7621 測速:
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP

自己ROUTER起XRAY, 除權非你有dynamic IP, 否則用幾次就用唔到.
最後都係比錢買機場, 簡單方便.

TOP

本帖最後由 semson 於 2023-11-1 10:57 編輯

回覆 3# jerrychan0610


OK, 我加咗個warning。

見仁見智啦。l2pt, ipsec都有人敢用黎過墙。
又未至於用幾次就用唔到, 我用左超過1年都未遇到被ban。

TOP

強帖支持~ thanks for sharing!

關於被ban ip,我理解XRAY+REALITY一個唔好處係冇得套Cloudflare來利用CF的IP,如果想保護 IP“安全”D,可以參考埋tomleehk在隔離分享的:
http://www.telecom-cafe.com/foru ... &extra=page%3D1
Openwrt xray server + gRPC + Reality + Vision + Nginx (TLS1.3) + acme + Cloudflare

減少被BAN IP的方法包括:如冇需要唔好set全局代理,讓有需要導流的網站先經 自己的VPN server

TOP

本帖最後由 semson 於 2023-11-1 11:47 編輯

回覆 5# upsagel


加左張圖俾新手睇。示範所用的client是v2rayNG。

建議裝banIP加強OpenWrt的網絡保安 (opkg install luci-app-banip)。呢個pkg名改得唔好, 唔係人地ban你, 係你用ipset去block黑名單IP。
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP

最好加返fallbacks,對家下一步可能又係active probing,或者AI learn TLS handshake 同final destination IP pattern,攻守加快中

via HKEPC IRF 5.1.14 - Android(5.1.2)

TOP

最好加返fallbacks,對家下一步可能又係active probing,或者AI learn TLS handshake 同final destination  ...
uKernel 發表於 2023-11-1 12:09



    考慮 fallbacks 黎講我認爲 naive proxy 做得最好,尤其適合我呢種本身有domain有website的用家,而且比較冷門可能被針對的機會會低DD?
介紹:
https://upsangel.com/security/vp ... %e6%95%99%e7%a8%8b/

TOP

尼排好似話用hysteria 2好D
不過我就起我個windows nas做server
sing-box就仲研究緊
個config檔一味都話錯真係搞config最煩

TOP

本帖最後由 semson 於 2023-11-1 22:39 編輯

回覆 9# ricky1992


試下參考佢啲example? 我見佢幾個工具都有maintain。
https://github.com/chika0801

我發現json格式的config裡面有啲 "//" comment 會抄個 file, 如下面[]之間的。浪費好多時間先知係佢問題。
  1. "streamSettings": {
  2.     "network": "h2",
  3.     "security": "reality",
  4.     "realitySettings": {
  5.     "dest": "www.lovelive-anime.jp:443",
  6.     "serverNames": [ // 客户端可用的 serverName 列表,暂不支持 * 通配符
  7.          "www.lovelive-anime.jp" // Chrome - 输入 "dest" 的网址 - F12 - 安全 - F5 - 主要来源(安全),填 证书 SAN 的值
  8.      ],
  9.     "privateKey": "2KZ4uouMKgI8nR-LDJNP1_MHisCJOmKGj9jUjZLncVU",
  10.     "shortIds": [ // 客户端可用的 shortId 列表,可用于区分不同的客户端
  11.           "6ba85179e30d4fc2" // 0 到 f,长度为 2 的倍数,长度上限为 16,可留空,或执行 openssl rand -hex 8 生成
  12.      ]
  13.      }
  14. ,
複製代碼

TOP