HTTP & SOCKS — Xray-core
通用代理协议。HTTP 支持 CONNECT 与较旧的透明代理模式;SOCKS 同时支持 v4a 与 v5(版本由客户端首字节自动判定)。
HTTP
入站
"protocol": "http" 的 settings:
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
users | []*HTTPAccount | — | [HTTPAccount] | … | 入站账户列表;对象形状与 `accounts` 相同。`users` 是近期 Xray 引入的较新名称,与 `accounts` 一并接受。 |
accounts | []*HTTPAccount | [] | [HTTPAccount] | 用户列表。为空则禁用鉴权(入站变成开放代理)。 |
allowTransparent | bool | false | true | false | 为 true 时入站接受不带 CONNECT 的请求,并按透明 HTTP 代理转发。 |
userLevel | uint32 | 0 | <uint32> | 未按用户单独配置时的默认策略等级。 |
源码: infra/conf/http.go:25-30 · 锚定版本 v26.6.1 (94ffd50)
accounts[]
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
user | string | (required) | <string> | HTTP Basic 鉴权的用户名。 |
pass | string | (required) | <string> | HTTP Basic 鉴权的密码。 |
源码: infra/conf/http.go:13-16 · 锚定版本 v26.6.1 (94ffd50)
出站
"protocol": "http" 出站的 settings:
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
address | *Address | (unset) | <host> | 简化形式 —— 上游代理主机。 |
port | uint16 | (required with address) | <port> | 上游代理端口。 |
level | uint32 | 0 | <uint32> | 策略等级。 |
email | string | (unset) | <string> | 统计 / 日志中显示的标签。 |
user | string | (unset) | <string> | 上游鉴权用户名。 |
pass | string | (unset) | <string> | 上游鉴权密码。 |
servers | []*HTTPRemoteConfig | (use simplified shape) | [HTTPRemoteConfig] | 完整形式,恰好一个服务器。 |
headers | map[string]string | {} | {<header>: <value>} | 每个请求附加的 HTTP 头。 |
源码: infra/conf/http.go:58-67 · 锚定版本 v26.6.1 (94ffd50)
servers[]
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
address | *Address | (required) | <host> | 上游代理主机。 |
port | uint16 | (required) | <port> | 上游代理端口。 |
users | []json.RawMessage | [] | [HTTPAccount] | 可选鉴权用户(至多一个)。 |
源码: infra/conf/http.go:52-56 · 锚定版本 v26.6.1 (94ffd50)
SOCKS
入站
"protocol": "socks" 的 settings:
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
auth | string | noauth | noauth | password | 鉴权模式。未知值会静默回退到 `noauth`(`infra/conf/socks.go:45-47`)。 |
users | []*SocksAccount | — | [SocksAccount] | … | 入站账户列表;对象形状与 `accounts` 相同。`users` 是近期 Xray 引入的较新名称,与 `accounts` 一并接受。 |
accounts | []*SocksAccount | [] | [SocksAccount] | `auth: password` 时的用户列表。 |
udp | bool | false | true | false | 启用 SOCKS5 UDP-associate 支持。 |
ip | *Address | (unset) | <address> | UDP-associate 应答中宣告的公网地址。监听器位于 NAT 后时必填。 |
userLevel | uint32 | 0 | <uint32> | 默认策略等级。 |
源码: infra/conf/socks.go:30-37 · 锚定版本 v26.6.1 (94ffd50)
accounts[]
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
user | string | (required) | <string> | 用户名。 |
pass | string | (required) | <string> | 密码。 |
源码: infra/conf/socks.go:13-16 · 锚定版本 v26.6.1 (94ffd50)
出站
"protocol": "socks" 出站的 settings:
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
address | *Address | (unset) | <host> | 简化形式 —— 上游 SOCKS 主机。 |
port | uint16 | (required with address) | <port> | 上游 SOCKS 端口。 |
level | uint32 | 0 | <uint32> | 策略等级。 |
email | string | (unset) | <string> | 统计标签。 |
user | string | (unset) | <string> | 鉴权用户名。 |
pass | string | (unset) | <string> | 鉴权密码。 |
servers | []*SocksRemoteConfig | (use simplified shape) | [SocksRemoteConfig] | 完整形式,恰好一个服务器。 |
源码: infra/conf/socks.go:77-85 · 锚定版本 v26.6.1 (94ffd50)
servers[]
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
address | *Address | (required) | <host> | 上游 SOCKS 主机。 |
port | uint16 | (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), 入站监听器拆分为三种类型(http、socks、mixed),并通过监听器 上的证书字段支持 TLS 包裹的 HTTP / SOCKS。参见 HTTP & SOCKS — mihomo。
源码: infra/conf/http.go:13-67 · v26.6.1 (94ffd50)
