Java tutorial
/** * This file is part of Ogar. * * Ogar is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Ogar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Ogar. If not, see <http://www.gnu.org/licenses/>. */ package ru.calypso.ogar.server.net; import org.apache.log4j.Logger; import com.google.common.net.HttpHeaders; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.PingWebSocketFrame; import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; /** * @author OgarProject, done by Calypso - Freya Project team */ @SuppressWarnings("rawtypes") public class WebSocketHandler extends SimpleChannelInboundHandler { private static Logger _log = Logger.getLogger(WebSocketHandler.class); private WebSocketServerHandshaker handshaker; @Override protected void channelRead0(ChannelHandlerContext ctx, Object req) throws Exception { if (req instanceof FullHttpRequest) { FullHttpRequest request = (FullHttpRequest) req; // ----- Client authenticity check code ----- // !!!!! WARNING !!!!! // THE BELOW SECTION OF CODE CHECKS TO ENSURE THAT CONNECTIONS ARE COMING // FROM THE OFFICIAL AGAR.IO CLIENT. IF YOU REMOVE OR MODIFY THE BELOW // SECTION OF CODE TO ALLOW CONNECTIONS FROM A CLIENT ON A DIFFERENT DOMAIN, // YOU MAY BE COMMITTING COPYRIGHT INFRINGEMENT AND LEGAL ACTION MAY BE TAKEN // AGAINST YOU. THIS SECTION OF CODE WAS ADDED ON JULY 9, 2015 AT THE REQUEST // OF THE AGAR.IO DEVELOPERS. String origin = request.headers().get(HttpHeaders.ORIGIN); switch (origin) { // TODO move to config case "http://agar.io": case "https://agar.io": case "http://localhost": case "https://localhost": case "http://127.0.0.1": case "https://127.0.0.1": case "http://ogar.pp.ua": break; default: _log.info(String.format("User kicked by invalid origin! IP %s, ORIGIN %s", ctx.channel().remoteAddress(), origin)); ctx.channel().close(); return; } // -----/Client authenticity check code ----- WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( "ws://" + request.headers().get(HttpHeaders.HOST) + "/", null, true); handshaker = wsFactory.newHandshaker(request); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), request); } } else if (req instanceof WebSocketFrame) { WebSocketFrame frame = (WebSocketFrame) req; if (req instanceof CloseWebSocketFrame) { if (handshaker != null) { handshaker.close(ctx.channel(), ((CloseWebSocketFrame) req).retain()); } } else if (req instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); } else { ctx.fireChannelRead(frame.retain()); } } } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } }