Git Product home page Git Product logo

sing-box-installer's Introduction

sing-box-installer

https://github.com/RayWangQvQ/sing-box-installer

基于sing-boxdocker容器化搭建naiveproxyhysteria的保姆级教程。

1. 大概介绍下

1.1. 关于sing-box

开源地址:https://github.com/SagerNet/sing-box

sing-box是一个开源的通用代理部署平台,目的是在当今繁杂的各种代理协议之上,抽象出一个通用接口(interface),来统一各种协议的定义和配置。

有了它,我可以使用同一套配置规则,部署多个不同的协议。

1.2. 关于naiveproxy

开源地址:

naiveproxy据说是当前最安全的协议之一,了解到它还是去年(2022年)10月份那次大规模封禁,据说除了naiveproxy幸免,其他协议均有死伤(包括trojanXrayV2Ray TLS+WebsocketVLESSgRPC),详细可查看issue:net4people/bbs#129

naiveproxy-bbs-survivor.png

为了解决墙的主动探测,它在服务端它使用自己优化过Caddyforwardproxy),利用反代,将没有认证的流量转到一个正常的站点(伪装站点)。也就是,你用你的proxy客户端去访问,认证(用户名+密码)能通过,它就给你做代理;你不用客户端用正常浏览器(或用户名密码错误),只要认证不通过,它就给你反代到正常站点,瞒天过海。

关于TLS指纹问题的讨论,可以看下这个issus:v2ray/v2ray-core#2098

在issue里顺便也了解到,naiveproxy的作者原来也是trojan最初的几个作者之一,后来trojan有些设计上的争议,包括一些优化的想法,由于主程没时间,无法得到实施,于是naiveproxy的作者就自己开了个项目来实现这些想法,这个项目就是现在的naiveproxy

1.3. 关于hysteria

开源地址:https://github.com/apernet/hysteria

hysteria的优势是快,真的快。同一台机器,我的测试结果是,比我之前的xray快了2到3倍(网上有人测出快了10倍)。

它是基于quic协议,走udp,跟它名字一样(歇斯底里),并发去请求扔包,所以快。

已知问题是qos,来自服务商的限制,当请求流量过大时,会被限速、断流。以前看有图比是嫌清晰度不够,用了hysteria可能要反过来主动去自己调低清晰度了。

关于安全性,目前墙对UDP的管控技术还没有TCP那么成熟,所以相对来说算比较安全。

1.4. 关于sing-box的配置

文档:https://sing-box.sagernet.org/zh/configuration/

部署sing-box的关键,就是编写它的配置文件。

sing-box抽象出一套配置规则,类似v2ray,有DNS,有路由(router),有入站(inbound)和出站(outbound)。

如果之前使用过v2ray,对这些概念很熟悉,那么你可以很轻松切换到sing-box; 如果你是个新手,完全不了解这些概念,那么我建议你先去读读v2ray的文档(https://www.v2ray.com)。

因为当前sing-box的文档还处于待完善阶段,只有对各配置字段的解释,并不会告诉你它是什么以及为什么要这么配。

2. 部署服务端

2.1. 思路

我个人推荐使用docker容器化部署,容器化有很多好处,这里就不多说了。

下面会基于sing-box的官方docker镜像,使用docker-compose进行容器构建。

官方镜像地址:https://github.com/orgs/SagerNet/packages?repo_name=sing-box

如果你的机器没有装过docker,请先安装docker,安装指令:

curl -sSL https://get.docker.com/ | sh
systemctl start docker
systemctl enable docker

然后基于docker容器run sing-box的官方镜像,我们只需要配置好配置文件config.json即可。

下面有两种模式:一键脚本部署和手动部署,任选其一即可。

2.2. 一键脚本部署

# create a dir
mkdir -p ./sing-box && cd ./sing-box

# install
bash <(curl -sSL https://raw.githubusercontent.com/RayWangQvQ/sing-box-installer/main/install.sh)

运行后会让输入参数:

  • 域名:需要自己DNS解析好到自己的服务器ip
  • 邮箱:用来申请证书的(会自动申请并更新)
  • proxy用户名:自己设置
  • proxy密码:自己设置
  • obfs:hysteria用来混淆的,相当于密码,自己设置

2.3. 手动部署

2.3.1. 文件目录

需要在服务器构建如下目录结构:

sing-box
├── data
    ├── config.json
    ├── entry.sh
└── tls
└── docker-compose.yml

其中,data/config.jsonsing-box的配置文件,所有节点配置信息都在里面。

data/entry.sh是容器启动脚本。

tls文件夹用于存储tls证书,sing-box可以自动颁发证书,你也可以使用自己现有的证书。如果自动颁发,就空文件夹就行,运行后该目录下会生成证书文件;如果要使用现有证书,可以将证书拷贝到当前文件夹下。

2.3.2. docker compose

docker-compose.yml参考内容如下:

version: '3'

services:
  sing-box:
    image: ghcr.io/sagernet/sing-box
    container_name: sing-box
    restart: unless-stopped
    network_mode: "host"
    # ports:
      # - 80:80
      # - 443:443
      # - 8090:8090
      # - 10080-10099:10080-10099/udp
    volumes:
      - ./data:/data
      - ./tls:/tls
    cap_add:
      - NET_ADMIN
    devices:
      - /dev/net/tun
    entrypoint: ["/bin/bash", "/data/entry.sh"]

其中,网络模式使用了network_mode: "host",直接使用了宿主机的网络环境,需要关闭宿主机的防火墙,命令如下:

# CentOS:
systemctl disable firewalld

# Debian/Ubuntu:
sudo ufw disable

如果host模式有问题,也可以切换到指定ports模式(注释掉network_mode,然后删掉下方prots的注释)

2.3.3. entry.sh

参考内容如下:

#!/bin/bash
set -e

configFilePath="/data/config.json"
logFilePath="/data/sing-box.json"

echo "entry"
sing-box version

# https://sing-box.sagernet.org/configuration/
echo -e "\nconfig:"
sing-box check -c $configFilePath || cat $configFilePath
sing-box format -c /data/config.json -w
cat $configFilePath

echo -e "\nstarting"
sing-box run -c $configFilePath
tail -f $logFilePath

会输出sing-box版本,检查并格式化配置文件,启动sing-box,并追踪日志。

2.3.4. config.json

最关键的配置文件,参考内容如下:

{
    "log": {
      "level": "trace",
      "output": "/data/sing-box.log",
      "timestamp": true
    },
    "inbounds": [
      {
        "type": "hysteria",
        "tag": "hysteria-in",
        "listen": "0.0.0.0",
        "listen_port": 10080,
        "domain_strategy": "ipv4_only",
        "up_mbps": 50,
        "down_mbps": 50,
        "obfs": "nicetofuckyou",
        "users": [
          {
            "name": "<proxy_name>",
            "auth_str": "<proxy_pwd>"
          }
        ],
        "tls": {
          "enabled": true,
          "server_name": "<domain>",
          "acme": {
            "domain": "<domain>",
            "data_directory": "/tls",
            "default_server_name": "<domain>",
            "email": "<email>"
          }
        }
      },
      {
        "type": "naive",
        "tag": "naive-in",
        "listen": "0.0.0.0",
        "listen_port": 8090,
        "domain_strategy": "ipv4_only",
        "users": [
          {
            "username": "<proxy_name>",
            "password": "<proxy_pwd>"
          }
        ],
        "network": "tcp",
        "tls": {
          "enabled": true,
          "server_name": "<domain>",
          "acme": {
            "domain": "<domain>",
            "data_directory": "/tls",
            "default_server_name": "<domain>",
            "email": "<email>"
          }
        }
      }
    ],
    "outbounds": [
      {
        "type": "direct",
        "tag": "direct"
      },
      {
        "type": "block",
        "tag": "block"
      },
      {
        "type": "dns",
        "tag": "dns-out"
      }
    ],
    "route": {
      "geoip": {
        "path": "/data/geoip.db",
        "download_url": "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",
        "download_detour": "direct"
      },
      "geosite": {
        "path": "/data/geosite.db",
        "download_url": "https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db",
        "download_detour": "direct"
      },
      "rules": [
        {
          "protocol": "dns",
          "outbound": "dns-out"
        }
      ],
      "final": "direct",
      "auto_detect_interface": true
    }
  }
  

其中,有几处需要替换的地方:

  • <proxy_name>替换为代理的用户名,自己取,如Ray
  • <proxy_pwd>替换为代理的密码,自己取,如1234@qwer
  • <domain>替换为域名
  • <email>替换为邮箱
  • obfshysteria混淆字符串,可以自定义

如上就配置了两个节点,一个基于udp的10080端口hysteria节点,一个基于tcp的8090端口naive节点。

如果你的云上有安全策略,请确保这两个端口都开放了。

证书的话,如果tls目录下没有现有证书,会自动颁发。

其他配置可以查阅官方文档了解。

2.3.5. 运行

docker-compose.yml同级目录下,执行:

docker compose up -d

等待容器启动。

如果一切正常,就是启动成功,可以去使用自己的客户端连接了。(就是这么简单)

其他参考指令:

# 查看当前运行中的容器
docker ps

# 查看容器启动日志
docker logs sing-box

# 追踪容器运行日志(使用Ctrl C退出追踪)
docker logs -f sing-box

# 进入容器
docker exec -it sing-box bash

3. 客户端

3.1. 安卓-SagerNet

3.1.1. hysteria

sing-box-client-sagernet-hysteria.jpg

其中:

  • 混淆密码是配置中的obfs
  • 认证荷载是配置中的auth_str

3.1.2. naive

sing-box-client-sagernet-naive.jpg

其中,密码是配置中的password

3.2. IOS-小火箭

todo

3.3. Win-Nekoray

nekoray

3.3.1. hysteria

nekoray-hy

配置文件内容如下:

{
  "server": "127.0.0.1:%mapping_port%",
  "server_name": "sample.zai7lou.ml",
  "obfs": "nicetofuckyou",
  "auth_str": "1234@qwer",
  "alpn": "h2",
  "up_mbps": 50,
  "down_mbps": 50,
  "socks5": {
    "listen": "127.0.0.1:%socks_port%"
  }
}

里面的占位符(%mapping_port%%socks_port%)不要动,127.0.0.1也不要动,这个是本地的代理链,不是服务器的ip和端口。

server_name改成自己的域名

obfsauth_str改成之前服务端配置的

3.3.2. naive

nekeray-naive

3.4. Win-V2RayN

3.4.1. hysteria

hysteria-v2rayn-add.png

配置文件内容如下:

{
  "server": "sample.zai7lou.ml:10080",
  "server_name": "sample.zai7lou.ml",
  "obfs": "nicetofuckyou",
  "auth_str": "1234@qwer",
  "alpn": "h2",
  "up_mbps": 100,
  "down_mbps": 100,
  "socks5": {
    "listen": "127.0.0.1:10808"
  },
  "http": {
    "listen": "127.0.0.1:10809"
  }
}

3.4.2. naive

naive-v2rayn-add.png

配置文件内容如下:

{
  "listen": "socks://127.0.0.1:10808",
  "proxy": "https://Ray:1234@[email protected]:8090"
}

3.5. Mac-Nekoray

同上,跟windows下的Nekoray一样。

4. FAQ

4.1. hy的端口hop

sing-box暂不支持,目前只能多加几个hy节点,然后在客户端做负载均衡。

sing-box-installer's People

Contributors

raywangqvq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.