在开始阅读之前,你可能会对“用Nginx转发V2Ray的WebSocket连接”感兴趣,所以我先把这个链接附在这里:
https://oing9179.github.io/blog/2017/03/v2ray-as-WebSocket-Proxy-behind-Nginx/
网络上关于用lighttpd转发V2Ray的ws的中文资料几乎没有。如果你正在看这篇文章,你大概考虑过这样一个问题:我的网站是用lighttpd跑的,80和443端口已经被lighttpd占用了,能不能让V2Ray也连接443端口,让lighttpd把流量转发给V2Ray呢?(而且也许这样比较以假乱真?)
这个操作的基本思路大概可以用下面的图表示:
v2ray client <---> lighttpd <---> v2ray server
在lighttpd版本1.4.46之前,你不能这么做;但是版本1.4.46及之后的版本允许你这么做(见lighttpd mod_proxy的说明)。
下面我将一步步介绍怎么对lighttpd进行配置使其转发ws。本文假定你使用CentOS 7,并假定你已经在你的CentOS 7上配置了EPEL的软件源。
1. 准备工作
首先,检查你的lighttpd版本:
lighttpd -v
如果你发现你lighttpd的版本低于1.4.46,你首先需要升级。
yum upgrade lighttpd
如果升级之后的版本仍然低于1.4.46,说明最新版的lighttpd仍然在EPEL-testing这个仓库中,而你没有激活这个仓库(这个仓库默认是不激活的)。
找到/etc/yum.repos.d/epel-testing.repo,它可能长这个样子:
[epel-testing] name=Extra Packages for Enterprise Linux 7 - Testing - $basearch #baseurl=http://download.fedoraproject.org/pub/epel/testing/7/$basearch metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel7&arch=$basearch failovermethod=priority enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 [epel-testing-debuginfo] name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Debug #baseurl=http://download.fedoraproject.org/pub/epel/testing/7/$basearch/debug metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-debug-epel7&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 gpgcheck=1 [epel-testing-source] name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Source #baseurl=http://download.fedoraproject.org/pub/epel/testing/7/SRPMS metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-source-epel7&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 gpgcheck=1
用你喜欢的文本编辑器(vim、nano、emacs……)编辑这个文件,把[epel-testing]部分中的enabled=0改成enabled=1,然后重新生成yum缓存:
yum makecache
然后再更新lighttpd:
yum upgrade lighttpd
2. 服务器端的V2Ray
你需要在V2Ray的服务端的inbound或者inboudeDetour部分作如下配置
{
"protocol": "vmess",
"port": 12345, // v2ray监听12345端口。此端口作为lighttpd转发的目的地
"settings": {
"clients": [
{
"id": "你的UUID",
"level": 1,
"alterId":你的alterID,
"email": "随便填写一个email(不一定需要真的存在)"
}
]
},
"streamSettings": {
"network": "ws",
"wsSettings": {
"connectionReuse": false,
"path": "/abcd", // 下文说明有什么用
"headers": {
"Host": "www.youku.com" // 下文说明有什么用
}
}
}
3. 服务器端的lighttpd
cd到/etc/lighttpd,编辑modules.conf,找到mod_proxy的那部分。把include那一行前面的注释记号“#”删掉:
## ## mod_proxy ## include "conf.d/proxy.conf"
再cd到/etc/lighttpd/conf.d,编辑proxy.conf。你的proxy.conf看起来可能是这个样子的:
####################################################################### ## ## Proxy Module ## --------------- ## ## See https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModProxy ## server.modules += ( "mod_proxy" ) ## ## a value between 0 and 65535 to set the debug-level in the Proxy module. ## Currently only 0 and 1 are used. Use 1 to enable some debug output, 0 to ## disable it. ## #proxy.debug = 1 ## ## might be one of 'hash', 'round-robin' or 'fair' (default). ## #proxy.balance = "fair" ## ## Handle all jsp requests via 192.168.0.101 ## #proxy.server = ( ".jsp" => # ( "tomcat" => # ( # "host" => "192.168.0.101", # "port" => 80 # ) # ) # ) ## #######################################################################
在最后加上这些东西:
# Handle v2ray websocket
$HTTP["url"] == "/abcd" {
$HTTP["host"] == "www.youku.com" {
proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "12345" ) ) )
proxy.header = ( "upgrade" => "enable" )
}
else {
url.redirect = ( "" => "/" )
}
}
偏技术性的说明:
只有当请求的URL是/abcd时(你将在/var/log/lighttpd/access.log中看到GET /abcd),且http header中的host为www.youku.com时,才把websocket的数据部分交给本地(服务器端的本地)的12345端口去处理——12345正是服务器端的V2Ray监听的端口。这样做一定程度上能够避免被侦测。
如果虽然请求了/abcd但http header中的host是什么别的东西,那么就把来访者重定向到你的网站的主页。当然,你也可以把他们重定向到一个特殊的页面,从而你可以从lighttpd日志中获悉是什么人在试图侦测你的网站——如果你真的发现了有这样不怀好意的来访者,请提高警惕!
配置完成,重启lighttpd:
systemctl restart lighttpd
你可以用
systemctl status lighttpd -l
判断是不是重启成功了。
4. 客户端的V2Ray
接下来,配置客户端的V2Ray。在你的outbound中填入以下内容
"outbound": {
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "服务器的IP或域名",
"port": 443,
"users": [
{
"id": "你的UUID",
"alterId": 你的alterID,
"security": "你的加密算法,可填写auto"
}
]
}
]
},
"streamSettings": {
"network": "ws",
"wsSettings": {
"connectionReuse": false,
"path": "/abcd", // 注意和上文lighttpd配置的一致
"headers": {
"Host": "www.youku.com" // 注意和上文lighttpd配置的一致
}
},
"security": "tls",
"tlsSettings": {
"serverName": "服务器的域名", // 必须和你申请证书时用的域名一致
"allowInsecure": false
}
}
}
现在,你应该可以使用V2Ray通过lighttpd连接你的服务器了。