Skip to content

HTTP & SOCKS — Xray-core

通用代理协议。HTTP 支持 CONNECT 与较旧的透明代理模式;SOCKS 同时支持 v4a 与 v5(版本由客户端首字节自动判定)。

HTTP

入站

"protocol": "http"settings

字段类型默认值允许值描述
users[]*HTTPAccount[HTTPAccount] | …入站账户列表;对象形状与 `accounts` 相同。`users` 是近期 Xray 引入的较新名称,与 `accounts` 一并接受。
accounts[]*HTTPAccount[][HTTPAccount]用户列表。为空则禁用鉴权(入站变成开放代理)。
allowTransparentboolfalsetrue | false为 true 时入站接受不带 CONNECT 的请求,并按透明 HTTP 代理转发。
userLeveluint320<uint32>未按用户单独配置时的默认策略等级。

源码: infra/conf/http.go:25-30 · 锚定版本 v26.6.1 (94ffd50)

accounts[]

字段类型默认值允许值描述
userstring(required)<string>HTTP Basic 鉴权的用户名。
passstring(required)<string>HTTP Basic 鉴权的密码。

源码: infra/conf/http.go:13-16 · 锚定版本 v26.6.1 (94ffd50)

出站

"protocol": "http" 出站的 settings

字段类型默认值允许值描述
address*Address(unset)<host>简化形式 —— 上游代理主机。
portuint16(required with address)<port>上游代理端口。
leveluint320<uint32>策略等级。
emailstring(unset)<string>统计 / 日志中显示的标签。
userstring(unset)<string>上游鉴权用户名。
passstring(unset)<string>上游鉴权密码。
servers[]*HTTPRemoteConfig(use simplified shape)[HTTPRemoteConfig]完整形式,恰好一个服务器。
headersmap[string]string{}{<header>: <value>}每个请求附加的 HTTP 头。

源码: infra/conf/http.go:58-67 · 锚定版本 v26.6.1 (94ffd50)

servers[]

字段类型默认值允许值描述
address*Address(required)<host>上游代理主机。
portuint16(required)<port>上游代理端口。
users[]json.RawMessage[][HTTPAccount]可选鉴权用户(至多一个)。

源码: infra/conf/http.go:52-56 · 锚定版本 v26.6.1 (94ffd50)

SOCKS

入站

"protocol": "socks"settings

字段类型默认值允许值描述
authstringnoauthnoauth | password鉴权模式。未知值会静默回退到 `noauth`(`infra/conf/socks.go:45-47`)。
users[]*SocksAccount[SocksAccount] | …入站账户列表;对象形状与 `accounts` 相同。`users` 是近期 Xray 引入的较新名称,与 `accounts` 一并接受。
accounts[]*SocksAccount[][SocksAccount]`auth: password` 时的用户列表。
udpboolfalsetrue | false启用 SOCKS5 UDP-associate 支持。
ip*Address(unset)<address>UDP-associate 应答中宣告的公网地址。监听器位于 NAT 后时必填。
userLeveluint320<uint32>默认策略等级。

源码: infra/conf/socks.go:30-37 · 锚定版本 v26.6.1 (94ffd50)

accounts[]

字段类型默认值允许值描述
userstring(required)<string>用户名。
passstring(required)<string>密码。

源码: infra/conf/socks.go:13-16 · 锚定版本 v26.6.1 (94ffd50)

出站

"protocol": "socks" 出站的 settings

字段类型默认值允许值描述
address*Address(unset)<host>简化形式 —— 上游 SOCKS 主机。
portuint16(required with address)<port>上游 SOCKS 端口。
leveluint320<uint32>策略等级。
emailstring(unset)<string>统计标签。
userstring(unset)<string>鉴权用户名。
passstring(unset)<string>鉴权密码。
servers[]*SocksRemoteConfig(use simplified shape)[SocksRemoteConfig]完整形式,恰好一个服务器。

源码: infra/conf/socks.go:77-85 · 锚定版本 v26.6.1 (94ffd50)

servers[]

字段类型默认值允许值描述
address*Address(required)<host>上游 SOCKS 主机。
portuint16(required)<port>上游 SOCKS 端口。
users[]json.RawMessage[][SocksAccount]可选鉴权用户。

源码: infra/conf/socks.go:71-75 · 锚定版本 v26.6.1 (94ffd50)

示例

带两个用户的 HTTP 入站:

json
{
  "inbounds": [
    {
      "tag": "http-in",
      "listen": "127.0.0.1",
      "port": 1080,
      "protocol": "http",
      "settings": {
        "accounts": [
          { "user": "alice", "pass": "<password>" },
          { "user": "bob",   "pass": "<password>" }
        ],
        "allowTransparent": false
      }
    }
  ]
}

带 UDP-associate 的 SOCKS5 入站:

json
{
  "inbounds": [
    {
      "tag": "socks-in",
      "listen": "0.0.0.0",
      "port": 1081,
      "protocol": "socks",
      "settings": {
        "auth": "password",
        "accounts": [{ "user": "alice", "pass": "<password>" }],
        "udp": true,
        "ip": "203.0.113.10"
      }
    }
  ]
}

通过上游 HTTP 代理的出站:

json
{
  "outbounds": [
    {
      "tag": "via-http",
      "protocol": "http",
      "settings": {
        "address": "upstream.example.com",
        "port": 8080,
        "user": "alice",
        "pass": "<password>",
        "headers": { "X-Forwarded-Proto": "https" }
      }
    }
  ]
}

说明

  • 不带 accounts 的 HTTP 入站等于开放代理。请通过 listen: "127.0.0.1"、丢弃外部来源的路由规则、或两者组合来限制 访问。
  • allowTransparent: true 很少符合需求。它启用了 V2Ray 旧式的 "入站作为透明转发器" 模式,与显式代理的 CONNECT 方法冲突。 除非有特定场景,否则保持关闭。
  • SOCKS 上仅 auth: "password" 会强制凭据。未知鉴权方法字符串会 静默降级到 noauth —— 拼写错误不会报错 (infra/conf/socks.go:45-47)。
  • SOCKS 的 ip 字段是 UDP-associate 应答中宣告的公网可达地址。 缺失时,位于 NAT 远端的客户端收到的将是不可路由的内部地址。
  • HTTP 与 SOCKS 出站都使用 "简化形式 vs. servers[]" 形态 —— servers[] 必须恰好一个条目。

跨内核说明

  • sing-box 将 HTTP 与 SOCKS 合并到同一个源文件 (option/simple.go),并暴露第三种 "mixed" 入站,在同一端口上 同时服务两种协议。参见 HTTP & SOCKS — sing-box
  • mihomo 出站使用 username / password(而非 user / pass), 入站监听器拆分为三种类型(httpsocksmixed),并通过监听器 上的证书字段支持 TLS 包裹的 HTTP / SOCKS。参见 HTTP & SOCKS — mihomo

源码: infra/conf/http.go:13-67 · v26.6.1 (94ffd50)

由 Argsment 出品的 Core Tutorial