【问题标题】:AuthorizedHandler Blocked wrong request! url: /socket.io/AuthorizedHandler 阻止了错误的请求!网址:/socket.io/
【发布时间】:2016-01-23 18:39:15
【问题描述】:

我正在使用 mrniko/netty-socketio (Java) 来启动这样的 websocket 服务器:

config = new Configuration();
config.setHostname("localhost");
config.setPort(8001);
server = new SocketIOServer(config);
server.addListeners(serviceClass);
server.start();

然后我使用(推荐的)socketio/socket.io-client(JavaScript)尝试像这样连接到 websocket 服务器(都在同一台服务器上):

var socket = io("http://localhost:8001");

连接在服务器上被“阻塞”,服务器打印:

8239 [nioEventLoopGroup-5-1] WARN com.corundumstudio.socketio.handler.AuthorizeHandler  - Blocked wrong request! url: /socket.io/, ip: /127.0.0.1:48915
28889 [nioEventLoopGroup-5-2] WARN com.corundumstudio.socketio.handler.AuthorizeHandler  - Blocked wrong request! url: /socket.io/, ip: /127.0.0.1:48916

随着客户端不断重试连接,这种情况会无休止地发生。

我似乎无法让服务器接受连接。我试过了:

var socket = io("ws://localhost:8001");

但这给出了相同的结果。对于这两种情况,我还尝试在 URL 后面加上斜杠 - 没有区别。我还尝试了在服务器和客户端都使用“localhost”或“127.0.0.1”的所有组合,等等。

JavaScript 页面本身由localhost:8000 上的 http 服务器提供。这似乎不是跨站点问题,因为这会在浏览器上产生完全不同的错误。

有谁知道出了什么问题以及如何解决?

【问题讨论】:

    标签: javascript websocket socket.io


    【解决方案1】:

    在我的例子中,网络监控每 10 秒访问一次该端口。我暂时将 log4j.properties 更改为 ERROR 级别的日志记录,但想为网络提供一个不会导致过多警告日志记录的使用路径。不确定这是否是最好的方法,但这就是我最终要做的。

    config.setAllowCustomRequests(true); 通过允许自定义请求,在 Authorizehandler 中绕过了显示警告的代码。

    我创建了一个自定义管道,它允许我使用自定义管道切换出错误的UrlHandler,以提供用于监控的安全路径。

    public class CustomSocketIOChannelInitializer extends SocketIOChannelInitializer {
    
       CustomWrongUrlHandler customWrongUrlHandler = null;
    
       public CustomSocketIOChannelInitializer(Configuration configuration) {
          customWrongUrlHandler = new CustomWrongUrlHandler(configuration);
       }
    
       protected void initChannel(Channel ch) throws Exception {
           ChannelPipeline pipeline = ch.pipeline();
           addSslHandler(pipeline);
           addSocketioHandlers(pipeline);
           // Replace wrong url handler with our custom one to allow network monitoring without logging warnings.
           pipeline.replace(SocketIOChannelInitializer.WRONG_URL_HANDLER, "CUSTOM_WRONG_URL_HANDLER", customWrongUrlHandler);
       }
    

    这是我的自定义处理程序:

    @Sharable
    public class CustomWrongUrlHandler extends ChannelInboundHandlerAdapter {
    
        private final Logger log = LoggerFactory.getLogger(getClass());
    
        Configuration configuration = null;
    
        /**
        * @param configuration
        */
       public CustomWrongUrlHandler(Configuration configuration) {
           this.configuration = configuration;
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    
           if (msg instanceof FullHttpRequest) {
              FullHttpRequest req = (FullHttpRequest) msg;
              Channel channel = ctx.channel();
              QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri());
    
              // Don't log when port is pinged for monitoring. Must use context that starts with /ping.
              if (configuration.isAllowCustomRequests() && queryDecoder.path().startsWith("/ping")) {
                  HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
                  channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
                  req.release();
                  //log.info("Blocked wrong request! url: {}, ip: {}", queryDecoder.path(), channel.remoteAddress());
                  return;
              } 
    
              // This is the last channel handler in the pipe so if it is not ping then log warning.
              HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
              ChannelFuture f = channel.writeAndFlush(res);
              f.addListener(ChannelFutureListener.CLOSE);
              req.release();
              log.warn("Blocked wrong socket.io-context request! url: {}, params: {}, ip: {}", channel.remoteAddress() + " " + queryDecoder.path(), queryDecoder.parameters());
          }
        }
    }
    
    
     CustomSocketIOChannelInitializer customSocketIOChannelInitializer = new CustomSocketIOChannelInitializer(config);
      server.setPipelineFactory(customSocketIOChannelInitializer);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2014-12-27
      • 2014-07-26
      • 2021-02-19
      • 2015-06-08
      • 2021-01-21
      • 2012-12-06
      相关资源
      最近更新 更多