V2Ray部署使用WebSocket+TLS+Web来科学上网

使用WebSocket+TLS+Web的方式看上去更安全(其实也没什么卵用)

方法一:使用caddy作为WEB服务器
用caddy转发端口到443,在域名目录下放置一些网页文件,这样也不会出现404 503等错误页面,看上去更真实些。
首先将域名的A记录指向VPS的公网IP地址,确保80  443端口没有被其它应用程序占用。
测试环境:Debian 8 x86x64
安装V2Ray官网提供的一键脚本
bash <(curl -L -s https://install.direct/go.sh)

此脚本会自动安装以下文件:
/usr/bin/v2ray/v2ray:V2Ray 程序;
/usr/bin/v2ray/v2ctl:V2Ray 工具;
/etc/v2ray/config.json:配置文件;
/usr/bin/v2ray/geoip.dat:IP 数据文件
/usr/bin/v2ray/geosite.dat:域名数据文件
此脚本会配置自动运行脚本。自动运行脚本会在系统重启之后,自动运行 V2Ray。目前自动运行脚本只支持带有 Systemd 的系统,以及 Debian / Ubuntu 全系列。

运行脚本位于系统的以下位置:

/etc/systemd/system/v2ray.service: Systemd
/etc/init.d/v2ray: SysV
脚本运行完成后,你需要:

编辑 /etc/v2ray/config.json 文件来配置你需要的代理方式;
运行 service v2ray start 来启动 V2Ray 进程;
之后可以使用 service v2ray start|stop|status|reload|restart|force-reload 控制 V2Ray 的运行。
/etc/v2ray/config.json:服务端配置文件示范如下
{
  "log" : {
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log",
    "loglevel": "warning"               
  },
  "inbound": {
    "port": 1110,
    "protocol": "vmess", 
    "settings": {
     "clients": [
      {
      "id": "你的uid",
      "level": 1,
      "alterId": 64,
      "email": "0001@qq.com"
      }
     ]
    },
    "streamSettings": {
      "network": "ws",
      "wsSettings": {
        "connectionReuse": false,
        "path": "/ws"
      }
    } 
  },
  "outbound": {
    "protocol": "freedom",
    "settings": {}
  },
  "inboundDetour": [
    {
      "protocol": "shadowsocks",
      "port": 1112,
      "settings": {
        "method": "chacha20",
        "password": "1234567",   
        "udp": false
      }
    },
    {
      "protocol": "shadowsocks",
      "port": 1113,
      "settings": {
        "method": "chacha20",
        "password": "1234567",   
        "udp": false
      }
    }
  ],
  "outboundDetour": [
    {
      "protocol": "blackhole",
      "settings": {},
      "tag": "blocked"
    }
  ],
  "routing": {
    "strategy": "rules",
    "settings": {
      "rules": [
        {
          "type": "field",
          "ip": [
            "0.0.0.0/8",
            "10.0.0.0/8",
            "100.64.0.0/10",
            "127.0.0.0/8",
            "169.254.0.0/16",
            "172.16.0.0/12",
            "192.0.0.0/24",
            "192.0.2.0/24",
            "192.168.0.0/16",
            "198.18.0.0/15",
            "198.51.100.0/24",
            "203.0.113.0/24",
            "::1/128",
            "fc00::/7",
            "fe80::/10"
          ],
          "outboundTag": "blocked"
        }
      ]
    }
  }
}
保存后service v2ray restart重启V2Ray
V2Ray客户端配置文件
{
  "inbound": {
    "port": 1080,
    "listen": "0.0.0.0",
    "protocol": "socks",
    "settings": {
      "auth": "noauth",
      "udp": true,
      "ip": "127.0.0.1",
      "clients": null
    },
    "streamSettings": null
  },
  "outbound": {
    "tag": "wsout",
    "protocol": "vmess",
    "settings": {
      "vnext": [
        {
          "address": "你的网站域名",
          "port": 443,
          "users": [
            {
              "id": "你的uid",
              "alterId": 64,
              "security": "aes-128-gcm"
            }
          ]
        }
      ]
    },
    "streamSettings": {
      "network": "ws",
      "security": "tls",
      "tlsSettings": {
        "serverName": "你的网站域名",
        "allowInsecure": true
    },
      "wsSettings": {
        "connectionReuse": true,
        "path": "/ws"
      }
    },
    "mux": {
      "enabled": true
    }
  },
  "outboundDetour": [
    {
      "protocol": "freedom",
      "settings": {
        "response": null
      },
      "tag": "direct"
    },
    {
      "protocol": "blackhole",
      "settings": {
        "response": {
          "type": "http"
        }
      },
      "tag": "blockout"
    }
  ],
  "dns": {
    "servers": [
      "8.8.8.8",
      "8.8.4.4",
      "localhost"
    ]
  },
  "routing": {
    "strategy": "rules",
    "settings": {
      "domainStrategy": "IPIfNonMatch",
      "rules": [
        {
          "type": "field",
          "port": null,
          "outboundTag": "direct",
          "ip": [
            "0.0.0.0/8",
            "10.0.0.0/8",
            "100.64.0.0/10",
            "127.0.0.0/8",
            "169.254.0.0/16",
            "172.16.0.0/12",
            "192.0.0.0/24",
            "192.0.2.0/24",
            "192.168.0.0/16",
            "198.18.0.0/15",
            "198.51.100.0/24",
            "203.0.113.0/24",
            "::1/128",
            "fc00::/7",
            "fe80::/10"
          ],
          "domain": null
        }
      ]
    }
  }
}
=== 安装Caddy ===
CentOS 6+ / Debian 6+ / Ubuntu 14.04 +

wget -N --no-check-certificate https://softs.fun/Bash/caddy_install.sh && chmod +x caddy_install.sh && bash caddy_install.sh install http.filemanager

# 如果上面这个脚本无法下载,尝试使用备用下载:
wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/caddy_install.sh && chmod +x caddy_install.sh && bash caddy_install.sh install http.filemanager
我使用的是逗比的一键脚本,自带开机启动。
使用说明
启动:/etc/init.d/caddy start

停止:/etc/init.d/caddy stop

重启:/etc/init.d/caddy restart

查看状态:/etc/init.d/caddy status

查看Caddy启动日志: tail -f /tmp/caddy.log

安装目录:/usr/local/caddy

Caddy配置文件位置:/usr/local/caddy/Caddyfile

Caddy自动申请SSL证书位置:/.caddy/acme/acme-v01.api.letsencrypt.org/sites/xxx.xxx(域名)/

Caddy配置文件位置:/usr/local/caddy/Caddyfile 的内容示范如下
https://域名 {
 root /usr/local/caddy/www/域名       #域名根目录的绝对路径
 timeouts none
 tls kiri_so@outlook.com
 gzip
 index index.html
 tls /.caddy/acme/acme-v01.api.letsencrypt.org/sites/域名/ 域名.crt  /.caddy/acme/acme-v01.api.letsencrypt.org/sites/域名/域名.key
 proxy /ws localhost:1234 {
  websocket
  header_upstream -Origin
  }
}
保存后启动:/etc/init.d/caddy start
如果顺利的话2分钟后就能访问自己的域名了。也可 以科学上网了
参考自https://steemit.com/v2ray/@wuming/v2ray-websocket-tls-caddy-443#3

方法二:使用宝塔bt面板里的nginx或Apache作为web服务器
1、浏览器登陆BT面板
安装完bt面板后,根据提示,打开浏览器登陆bt面板
安装nginx1.12(或最新版本);取消勾选php、mysql,除非需要同时建立个人网站,如wordpress;
安装完nginx后,网站——添加站点:x.com
成功添加站点x.com站点后,设置——SSL——Let’s Encrypt,成功申请ssl后,保存。
再点击站点设置的「配置文件」选项(即nginx配置),在最后一个}前添加如下代码:
这个是Nginx:
注意去面板开放端口。不然没办法使用
  location /ws {   
         proxy_redirect off;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
         proxy_set_header Host $http_host; 
         if ($http_host = "www.baidu.com" ) {proxy_pass http://127.0.0.1:33531; 
                } 
           }
这个是Apache: 填入以下内容 只需要红色字体的(端口改成跟你v2ray服务端配置文件一样的),注意去面板开放端口。不然没办法使用
<VirtualHost *:443>

        ServerName yourdomain.com.
        ServerAlias yourdomain.com.
        ##SSL Engine Switch:
        #Enable/Disable SSL for this virtual host.
        SSLEngine On

        RewriteEngine On
          RewriteCond %{HTTP:Upgrade} =websocket [NC]
         RewriteRule /(.*)           ws://localhost:3579/$1 [P,L]
        RewriteCond %{HTTP:Upgrade} !=websocket [NC]
        RewriteRule /(.*)           http://localhost:3579/$1 [P,L]


        ##Proxy to port 3579
        #Replace xxx.xxx.xxx.xxx with your servers IP adress or hostname
        #Replace yourCertname with your servers SSL certificate name
        SSLProxyEngine On
        Proxypass /ray http://127.0.0.1:3579
        ProxyPassReverse /ray http://127.0.0.1:3579

        SSLCertificateFile /etc/v2ray/v2ray.crt
        SSLCertificateKeyFile /etc/v2ray/v2ray.key

</VirtualHost>
启动网站,重启apache服务
 以上代码中的参数必须和下文的v2ray配置文件一样
path:path可自定义,这里是/ws,但必须与服务端、客户端的path同样;
host:host是http header参数,可自定义任意域名,这里是www.baidu.com,但必须与服务端、客户端配置同样,可理解为密码;
33531:v2ray服务端的端口port,可自定义; 在bt面板——安全选项,放行33531自定义端口。
2、修改V2RAY配置文件 打开面板文件,在路径中输入/etc/v2ray/,编辑以下文件,并保存 服务端:/etc/v2ray/config.json 为了直观,直接在代码后面添加注释,复制粘贴后切记删除注释部分://....
{
  "log" : {
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log",
    "loglevel": "warning"
  },
  "inbound": {
    "port": 33531, //可自定义,但必须与上文nginx抱持一致
    "protocol": "vmess",
    "settings": {
      "clients": [
        {
          "id": "08723509-b860-49ac-98a7-7af7486beb38", //建议默认,uuid也可自定义同样格式,但必须与客户端配置保持一致
          "level": 1,
          "alterId": 64
        }
      ]
    },
    "streamSettings":{
      "network":"ws",
      "wsSettings": {
      "path": "/ws", //path可自定义,这里是/ws,但必须与nginx、客户端的path同样
"headers": {
"Host": "www.baidu.com" //host可自定义任意域名,但必须与nginx、客户端配置同样
      }
    }
  }},
  "outbound": {
    "protocol": "freedom",
    "settings": {}
  },
  "outboundDetour": [
    {
      "protocol": "blackhole",
      "settings": {},
      "tag": "blocked"
    }
  ],
  "routing": {
    "strategy": "rules",
    "settings": {
      "rules": [
        {
          "type": "field",
          "ip": [
            "0.0.0.0/8",
            "10.0.0.0/8",
            "100.64.0.0/10",
            "127.0.0.0/8",
            "169.254.0.0/16",
            "172.16.0.0/12",
            "192.0.0.0/24",
            "192.0.2.0/24",
            "192.168.0.0/16",
            "198.18.0.0/15",
            "198.51.100.0/24",
            "203.0.113.0/24",
            "::1/128",
            "fc00::/7",
            "fe80::/10"
          ],
          "outboundTag": "blocked"
        }
      ]
    }
  }
}
客户端:修改config.json文件 客户端下载:v2ray-core,解压后,编辑config文件,并保存 为了直观,直接在代码后面添加注释,复制粘贴后切记删除注释部分://....
{
  "inbound": {
    "listen": "127.0.0.1",
    "settings": {
      "udp": false,
      "auth": "noauth",
      "ip": "127.0.0.1"
    },
    "protocol": "socks",
    "port": 1080
  },
 
  "outbound": {
    "mux": {
      "enabled": true, //true开启mux,false为关闭
      "concurrency": 8
    },
    "protocol": "vmess",
    "settings": {
      "vnext": [
        {
          "address": "x.com", //你申请的域名
          "users": [
            {
              "id": "08723509-b860-49ac-98a7-7af7486beb38", //uuid必须与服务端配置文件一样
              "alterId": 64,
              "security": "ChaCha20-Poly1305" //可自定义加密方法
            }
          ],
          "port": 443 //客户端端口port必须是nginx ssl监听端口,bt面板申请ssl后默认443端口,客户端不能填写33531
        }
      ]
    },
    "tag": "direct",
    "streamSettings": {
      "wsSettings": {
        "path": "/ws", //path可自定义,这里是/ws,但必须与nginx配置、服务端的path一样
        "headers": {
          "Host": "www.baidu.com" //host可自定义任意域名,但必须与nginx、服务端配置同样
        }
      },
      "tlsSettings": {
        "serverName": "x.com", //你申请的域名
        "allowInsecure": true
      },
      "security": "tls",
      "network": "ws"
    }
  },
  "outboundDetour": [
    {
      "tag": "direct",
      "protocol": "freedom",
      "settings": {
      }
    }
  ],
 "routing": {
    "strategy": "rules",
    "settings": {
      "domainStrategy": "IPIfNonMatch",
      "rules": [
        {
          "type": "field",
          "outboundTag": "direct",
          "port": "1-52"
        },
        {
          "type": "field",
          "outboundTag": "direct",
          "port": "54-79"
        },
        {
          "type": "field",
          "outboundTag": "direct",
          "port": "81-442"
        },
        {
          "type": "field",
          "outboundTag": "direct",
          "port": "444-65535"
        },
        {
          "type": "field",
          "outboundTag": "direct",
          "domain": [
            "geosite:cn"
          ]
        },
        {
          "type": "field",
          "ip": [
            "0.0.0.0/8",
            "10.0.0.0/8",
            "100.64.0.0/10",
            "127.0.0.0/8",
            "169.254.0.0/16",
            "172.16.0.0/12",
            "192.0.0.0/24",
            "192.0.2.0/24",
            "192.168.0.0/16",
            "198.18.0.0/15",
            "198.51.100.0/24",
            "203.0.113.0/24",
            "::1/128",
            "fc00::/7",
            "fe80::/10",
            "geoip:cn"
          ],
          "outboundTag": "direct"
        }
      ]
    }
  },
  "log": {
    "loglevel": "warning"
  },
  "policy": {
    "levels": {
      "0": {
        "uplinkOnly": 0
      }
    }
  }
}
不同系统客户端可随意自定义不同的加密方式,上文客户端配置为ChaCha20-Poly1305 security:加密方式,客户端将使用配置的加密方式发送数据,服务器端自动识别,无需配置。可选的值有: “aes-128-cfb” “aes-128-gcm”:推荐在 PC 上使用 “chacha20-poly1305″:推荐在手机端使用 “auto”:默认值,自动选择 “none”:不加密 以上路由规则已经为智能连接 服务器操作 可直接透过bt面板,重启服务器,一次性使配置文件生效 或 分别重启相关服务 重启v2ray,ssh客户端:service v2ray restart 重启nginx:透过BT面板

客户端使用截图
ios
kitsunebi(推荐,本人无ios设备,所以没法截图)
shadowrocket(选择vmess协议,但是未必具备以上全部功能)
Android
v2rayNG(推荐),如图
点击+,选择手动输入,根据上文范例应填写如下:







Macos
v2ray-core(推荐,因为可使用最新的功能)
解压,根据上文的客户端config配置,修改文件config.json;
打开v2ray程序
优点:使用最新功能,比如自定义http header
缺点:是命令行程序,透过config配置文件,没有程序设置界面
所以需要如下附加软件搭配实现科学上网

无视下面内容,收藏用:

在Apache服务器上实现tls Websocket转发v2ray代理

因为Nginx的rewrite规则实在是太头疼了,服务器上还要用Apache放别的网站内容,相比之下还是用Apache转发v2ray代理更简单一点。
首先请根据这个教程:v2ray白话文教程注册好域名和生成证书。
安装好Apache之后,在服务器上开启以下apache模组:
sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod proxy_wstunnel
sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo a2enmod headers
Apache配置
nano /etc/apache2/sites-available/yourdomain.com.conf
填入以下内容
<VirtualHost *:443>

        ServerName yourdomain.com.
        ServerAlias yourdomain.com.
        ##SSL Engine Switch:
        #Enable/Disable SSL for this virtual host.
        SSLEngine On

        RewriteEngine On
          RewriteCond %{HTTP:Upgrade} =websocket [NC]
         RewriteRule /(.*)           ws://localhost:3579/$1 [P,L]
        RewriteCond %{HTTP:Upgrade} !=websocket [NC]
        RewriteRule /(.*)           http://localhost:3579/$1 [P,L]


        ##Proxy to port 3579
        #Replace xxx.xxx.xxx.xxx with your servers IP adress or hostname
        #Replace yourCertname with your servers SSL certificate name
        SSLProxyEngine On
        Proxypass /ray http://127.0.0.1:3579
        ProxyPassReverse /ray http://127.0.0.1:3579

        SSLCertificateFile /etc/v2ray/v2ray.crt
        SSLCertificateKeyFile /etc/v2ray/v2ray.key

</VirtualHost>
启动网站,重启apache服务
sudo a2ensite yourdomain.com
sudo service apache2 restart
服务器端V2ray配置
  {
    "log" : {
      "access": "/var/log/v2ray/access.log",
      "error": "/var/log/v2ray/error.log",
      "loglevel": "warning"
  },
    "inbound": {
      "port": 3579,
      "listen":"127.0.0.1",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "your id",
            "level": 1,
            "alterId": 64
          }   
          ]
      },
      "streamSettings":{
        "network":"ws",
        "wsSettings":{
          "path":"/ray"
          }
        }
      },
  "outbound": {
    "protocol": "freedom",
    "settings": {}
    },
  "outboundDetour": [
    {
    "protocol": "blackhole",
    "settings": {},
    "tag": "blocked"
    }
  ]
}
V2ray客户端配置(部分)


"outbound": {
"protocol": "vmess",
"settings":{
  "vnext":[
          {
          "address":"yourdomain.com",
          "port": 443,
          "users": [
              {
              "id": "your id",
              "level": 1,
              "alterId": 64,
              "security":"auto"
              }
           ]
          }
          ]
          },
"streamSettings": {
  "network": "ws",
  "security":"tls",
  "tlsSettings":{
  "serverName":"yourdomain.com",
  "allowInsecure":true
  },
  "wsSettings":{
  "path":"/ray"
  }
  },
"mux": {"enabled":true}
},
转载收藏自https://www.sooele.com/index.php/2018/01/05/%E5%AE%9D%E5%A1%94bt%E9%9D%A2%E6%9D%BFv2ray%EF%BC%9Awebsockettlsnginx%E5%88%86%E6%B5%81%E8%87%AA%E5%AE%9A%E4%B9%89http-header/


Linux客户端的使用方法
先去官网下载对应的客户端文件
然后解压上传到目录,修改config.json配置文件
./root/v2ray/v2ray --config=/root/v2ray/config.json &
运行以上命令后台运行V2ray 

然后使用socks5

发表评论

0 评论