V2ray-core: 2秒内产生了186K行的警告log: “too many open files”, 以至于log文件占满磁盘。

Created on 25 Feb 2019  ·  13Comments  ·  Source: v2ray/v2ray-core

1) 你正在使用哪个版本的 V2Ray?(如果服务器和客户端使用了不同版本,请注明)
4.14

2) 你的使用场景是什么?比如使用 Chrome 通过 Socks/VMess 代理观看 YouTube 视频。
做透明网关, TLS+WS+WEB

3) 你看到的不正常的现象是什么?(请描述具体现象,比如访问超时,TLS 证书错误等)
V2ray会在瞬间生成120M~250M的log文件, 里面都是:

2019/02/25 00:35:52 [Warning] v2ray.com/core/transport/internet/tcp: failed to accepted raw connections > accept tcp [::]:PORTFORCLIENT: accept4: too many open files

最近的一次在2秒内生成了186k行这样的log,
很快磁盘就被写满了.

4) 你期待看到的正确表现是怎样的?

5) 请附上你的配置(提交 Issue 前请隐藏服务器端IP地址)。

服务器端配置:

{
  "log" : {
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log",
    "loglevel": "warning"
  },
  "inbounds": [
    {
      "port": PORTFROMWEBSERVER,
      "listen":"127.0.0.1",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "UUID",
            "level": 1,
            "alterId": 64
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "wsSettings": {
          "path":"/FOLDER"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {}
    },
    {
      "protocol": "blackhole",
      "settings": {},
      "tag": "blocked"
    }
  ],
  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "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"
        }
      ]
    }
  }
}

客户端配置:

    // 在这里附上客户端配置
{
  "log": {
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log",
    "loglevel": "warning"
  },

  "inbounds": [
    {
      "port": PORTFORSELF,
      "listen": "0.0.0.0",
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": true,
        "ip": "127.0.0.1",
        "clients": null
      },
      "streamSettings": null
    },
    {
      "port": PORTFORCLIENT,
      "protocol": "dokodemo-door",
      "settings": {
        "network": "tcp,udp",
        "followRedirect": true
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      }
    },
    {
      "protocol": "dokodemo-door",
      "port": PORTFOR88,
      "listen": "127.0.0.1",
      "settings": {
        "address": "8.8.8.8",
        "port": 53,
        "network": "udp",
        "timeout": 0
      }
    },
    {
      "protocol": "dokodemo-door",
      "port": PORTFOR114,
      "listen": "127.0.0.1",
      "settings": {
        "address": "114.114.114.114",
        "port": 53,
        "network": "udp",
        "timeout": 0
      }
    }
  ],
  "outbounds": [
    {
      "tag": "proxy",
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "mysite.mydomain",
            "port": PORTFORTLS,
            "users": [
              {
                "id": "UUID",
                "alterId": 64,
                "security": "chacha20-poly1305"
              }
            ]
          }
        ],
        "servers": null
      },
      "streamSettings": {
        "network": "ws",
        "security": "tls",
        "tcpSettings": null,
        "kcpSettings": null,
        "tlsSettings":{
          "serverName":"mysite.mydomain",
          "allowInsecure":true
        },
        "wsSettings": {
          "connectionReuse": true,
          "path": "/FOLDER",
          "headers": null
        }
      },
      "mux": {
        "enabled": false
      }
    },
    {
      "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": {
    "domainStrategy": "IPIfNonMatch",
    "settings": {
      "rules": [
        {
           "type":"field",
           "outboundTag":"proxy",
           "ip": [
             "8.8.8.8/32",
             "8.8.4.4/32"
           ]
        },
        {
           "type":"field",
           "outboundTag":"direct",
           "ip": [
             "114.114.114.114/32"
           ]
        },
        {
           "type": "field",
           "outboundTag": "proxy",
           "domain": [
              ""
          ]
        },
        {
          "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
        }
      ]
    }
  }
}

6) 请附上出错时软件输出的错误日志。在 Linux 中,日志通常在 /var/log/v2ray/error.log 文件中。

服务器端错误日志:
服务端的错误日志还在2月1日, 没有今天的.

客户端错误日志:

2019/02/25 00:35:52 [Warning] v2ray.com/core/transport/internet/tcp: failed to accepted raw connections > accept tcp [::]:PORTFORCLIENT: accept4: too many open files
......186k行一样的记录......
2019/02/25 00:35:54 [Warning] v2ray.com/core/transport/internet/tcp: failed to accepted raw connections > accept tcp [::]:PORTFORCLIENT: accept4: too many open files

7) 请附上访问日志。在 Linux 中,日志通常在 /var/log/v2ray/access.log 文件中。
文件极短, 而且只有20天前的记录, 没有今天的.

8) 其它相关的配置文件(如 Nginx)和相关日志。

9) 如果 V2Ray 无法启动,请附上 --test 输出。

10) 如果 V2Ray 服务运行不正常,请附上 journal 日志。
服务正常,

Most helpful comment

和你有一样的问题,这个做透明代理 好象和iptables的写法 有关,还有据那个白话文教程中所说
添加 dokodemo door 协议的入站配置 ,并开启 sniffing;还要在所有 outbound 的 streamSettins 添加 SO_MARK。
看你的客户端文件 中没有
我也没解决

我也遇到这个问题,就是你说的这个方法,需要在所有的 outbound 里面增加这个配置。否则这个 outbound 发出的请求还会被 iptables 继续转发回来,这样就死循环了,很快就超过打开文件上限了。

我这里是好用的,不清楚你为什么不好用。虽然是个老贴,回复一下希望对相同问题的人有用。

All 13 comments

和你有一样的问题,这个做透明代理 好象和iptables的写法 有关,还有据那个白话文教程中所说
添加 dokodemo door 协议的入站配置 ,并开启 sniffing;还要在所有 outbound 的 streamSettins 添加 SO_MARK。
看你的客户端文件 中没有
我也没解决

我不是学计算机的, iptables 对我来说跟天书一样, 尤其时 mangle 表.
我一直不理解为什么要建一个 v2ray 的链,
另外, 我安装了upnp, 我觉得如果把信息都导入了 v2ray 链的话,
upnp的部分我就更迷糊了.
我的 iptables 规则是这样的, 反正一直是好用的.

iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-ports PORTFORCLIENT
iptables -t nat -A PREROUTING -p udp -m set --match-set gfwlist dst -j REDIRECT --to-ports PORTFORCLIENT
iptables -t nat -A OUTPUT -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-ports PORTFORCLIENT
iptables -t nat -A OUTPUT -p udp -m set --match-set gfwlist dst -j REDIRECT --to-ports PORTFORCLIENT
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -d 0.0.0.0/0 -j SNAT --to-source 192.168.250.250

我没有在mangle表里建任何规则, 所有规则都在 nat 表中.
dns查询是dnsmasq+ipset, 通过规则直接把黑名单网站的查询导入到 127.0.0.1#PORTFOR88 , 走v2ray.
网站访问就如 iptables 规则所示, 直接把带有 gfwlist 标识的转到 PORTFORCLIENT 端口, 走v2ray.

我总觉得是什么错误, 有可能是服务器没连接上什么的, 导致出现了死循环类的,
刚刚又出现了 log 文件在3分钟内从 400+ 字节, 暴涨到 646M 字节,
而且这个增长不是持续的, 会在某个时间突然增长, 然后稳定一段时间, 可能几分钟或半小时,
然后再突然暴涨.

补充一点, 原来 v2ray 在低版本时是没有这个问题的, 大概时 18年8月之前的版本,
19年初升级了 v2ray , 同时也升级的 json 文件后, 才又这样的问题.

启动V2Ray 前执行下面两行命令

LimitNOFILE=1048576
LimitNPROC=512

好的, 我试试, 谢谢先

@5high 果然好用啊, 已经两个小时没有问题了.
多谢多谢.

和你有一样的问题,这个做透明代理 好象和iptables的写法 有关,还有据那个白话文教程中所说
添加 dokodemo door 协议的入站配置 ,并开启 sniffing;还要在所有 outbound 的 streamSettins 添加 SO_MARK。
看你的客户端文件 中没有
我也没解决

我也遇到这个问题,就是你说的这个方法,需要在所有的 outbound 里面增加这个配置。否则这个 outbound 发出的请求还会被 iptables 继续转发回来,这样就死循环了,很快就超过打开文件上限了。

我这里是好用的,不清楚你为什么不好用。虽然是个老贴,回复一下希望对相同问题的人有用。

你好,我解释一下我的思路,我的透明代理包括 dnsmasq/ipset,v2ray 和 iptables
1.DNS, DNS采用黑名单模式,通过ipset 设置被墙的网站走 127.0.0.1:DNSPORT,并存储在 SCISITES 表中;其他的走ISP的DNS
2.v2ray,v2ray 的 inbounds 监测两个端口,第一个是 DNSPORT,当查询被墙网站的IP时,v2ray 由DNSPORT监听到UDP协议的DNS查询,然后转发到VPS。第二个是科学上网的SCIPORT,当访问SCISITES 中的 IP 时,iptables 根据 SCISITES 标识,将包转发至 127.0.0.1:SCIPORT,由v2ray截获,然后转发至VPS。
3.iptables,我的iptables没有V2RAY链,因为用不到,只有根据 SCISITES 标识,将包转发至 SCIPORT。转发在nat表中进行。

我认为不应该发生死循环,
比如访问google,dnsmasq从设置文件中发现是被墙的网站,然后从本地DNSPORT解析DNS,信息被v2ray截获,转发到VPS,这个过程不会触发SNAT以外的任何iptables规则。当查询得到IP后,该IP被标识为SCISITES,浏览器开始访问这个IP,此时iptables根据匹配规则将其重定向至 本机的SCIPORT端口,由v2ray截获,并向VPS转发,此时不会再触发任何SNAT以外的iptables规则。
因此虽然,我没有设置各种V2RAY链,没有设置各种MARK,没有mangle表规则,我依然认为不应该发生死循环。

@flukaxkx 这里说的是通过 sniffing 方式解决 dns 污染的,我看你的思路应该是没有使用这个方案。

使用 sniffing 方式,一般 iptables 规则都是配置的除了内网请求都转发给 v2ray 的。这样就需要区分是用户的请求,还是 v2ray 发起的转发请求。

你如果自己使用 dnsmasq/ipset 分流,那实际上 v2ray 里面确实不用做什么路由的配置,因为进来的流量都是需要转发给代理的。

好的,明白了,多谢。
之前我对 sniffing 这部分的理解有问题。

我把 inbounds 部分 sniffing 选项去掉以后确实不会瞬间出现大量 "too many open files" 错误了,但访问一些网站的时候还是会在 log 中出现 10 行左右的 "too many open files" 错误。基本上问题已经可以解决了。

但是,我还是不明白为什么会造成死循环。
按说即使增加了 sniffing , v2ray 应该是找 “dns” 部分的 8.8.8.8 来解析域名, 然后根据 "routing" 部分的设置将信息转发到 VPS 。而这条信息在经过 iptables 的时候不应该触发任何 SNAT 以外的规则,也就是说不会造成死循环啊。

又仔细想了一下,有以下几个问题:

  1. 原设置文件中 VPS 域名没有加入 routing 的 direct 部分中。
  2. routing 的 direct 部分 ports 设置是没有任和意义,反倒可能引发问题。
    因此将设置文件改成下面这样:
{
  "log": {
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log",
    "loglevel": "warning"
  },

  "inbounds": [
    {
      "port": PORTFORCLIENT,
      "listen": "127.0.0.1",
      "protocol": "dokodemo-door",
      "settings": {
        "network": "tcp,udp",
        "followRedirect": true
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      }
    },
    {
      "port": PORTFORCLIENT,
      "listen": "ROUTERLANIP",
      "protocol": "dokodemo-door",
      "settings": {
        "network": "tcp,udp",
        "followRedirect": true
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      }
    },
    {
      "protocol": "dokodemo-door",
      "port": PORTFOR88,
      "listen": "127.0.0.1",
      "settings": {
        "address": "8.8.8.8",
        "port": 53,
        "network": "udp",
        "timeout": 0
      }
    },
    {
      "protocol": "dokodemo-door",
      "port": PORTFOR114,
      "listen": "127.0.0.1",
      "settings": {
        "address": "114.114.114.114",
        "port": 53,
        "network": "udp",
        "timeout": 0
      }
    }
  ],
  "outbounds": [
    {
      "tag": "proxy",
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "mysite.mydomain",
            "port": PORTFORTLS,
            "users": [
              {
                "id": "UUID",
                "alterId": 64,
                "security": "chacha20-poly1305"
              }
            ]
          }
        ],
        "servers": null
      },
      "streamSettings": {
        "network": "ws",
        "security": "tls",
        "tcpSettings": null,
        "kcpSettings": null,
        "tlsSettings":{
          "serverName":"mysite.mydomain",
          "allowInsecure":true
        },
        "wsSettings": {
          "connectionReuse": true,
          "path": "/FOLDER",
          "headers": null
        }
      },
      "mux": {
        "enabled": false
      }
    },
    {
      "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": {
    "domainStrategy": "IPIfNonMatch",
    "settings": {
      "rules": [
         {
            "type":"field",
            "outboundTag":"proxy",
            "ip": [
              "8.8.8.8/32",
              "8.8.4.4/32"
            ],
            "domain": [
              ""
            ]
         },
         {
            "type":"field",
            "outboundTag":"direct",
            "domain": [
               "VPS.DOMAIN.NAME"
            ],
            "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"
            ]
         }
      ]
    }
  }
}

经过实测,不再出现 “too many open files” 的问题了!

按说即使增加了 sniffing , v2ray 应该是找 “dns” 部分的 8.8.8.8 来解析域名, 然后根据 "routing" 部分的设置将信息转发到 VPS 。而这条信息在经过 iptables 的时候不应该触发任何 SNAT 以外的规则,也就是说不会造成死循环啊。

循环的主要原因是这样,一般 iptables 配置是这样

    iptables -t nat -N V2RAY
    iptables -t nat -A V2RAY -d 10.10.8.1/24 -j RETURN
    iptables -t nat -A V2RAY -p tcp -j RETURN -m mark --mark 0xff
    iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345
    iptables -t nat -A PREROUTING -p tcp -j V2RAY
    iptables -t nat -A OUTPUT -p tcp -j V2RAY

这里面,注意那个 -m mark --mark 0xff 那行,如果流量有这个标记会直接返回不继续往下处理了,也就不会走到 v2ray 了。

如果在 v2ray 里面没有给流量做标记,不管是通过代理出去的还是通过 freedom 出去的,只要没有标记的,到这条还会继续往下处理,会导致流量再次发给 v2ray,这样不就循环了么。

你有兴趣可以看看我用的配置文件 https://github.com/wd/f-k-g-f-w/blob/master/config.json ,这里面列的差不多是最小配置了,routing 里面我用了 wd.dat 是 https://github.com/wd/f-k-g-f-w/blob/master/wd.dat 这个,然后第二个 routing 里面的 ip 配置是处理 telegram 的,如果有其他需要需要通过代理的 ip,可以直接加。把 freedom 放 outbounds 第一个,会作为没有匹配到任何路由规则的流量的出口。这里面也不用配置什么 dns,因为 v2ray 会把 dns 解析的事情放到代理那边做。

OK,学习了,多谢。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

limaofu picture limaofu  ·  3Comments

nielspeen picture nielspeen  ·  4Comments

shuangyuxiaoyi picture shuangyuxiaoyi  ·  4Comments

supersndqd picture supersndqd  ·  3Comments

gzzchh picture gzzchh  ·  3Comments