Advertisement
Guest User

kapuk99

a guest
Dec 31st, 2009
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. package net.sf.odinms.net;
  2.  
  3. import net.sf.odinms.client.MapleClient;
  4. import net.sf.odinms.net.channel.ChannelServer;
  5. import net.sf.odinms.net.login.LoginWorker;
  6. import net.sf.odinms.tools.HexTool;
  7. import net.sf.odinms.tools.MapleAESOFB;
  8. import net.sf.odinms.tools.MaplePacketCreator;
  9. import net.sf.odinms.tools.data.input.ByteArrayByteStream;
  10. import net.sf.odinms.tools.data.input.GenericSeekableLittleEndianAccessor;
  11. import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;
  12.  
  13. import org.apache.mina.common.IdleStatus;
  14. import org.apache.mina.common.IoHandlerAdapter;
  15. import org.apache.mina.common.IoSession;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18.  
  19. public class MapleServerHandler extends IoHandlerAdapter {
  20.     private final static short MAPLE_VERSION = 75;
  21.     private PacketProcessor processor;
  22.     private int channel = -1;
  23.  
  24.     public MapleServerHandler(PacketProcessor processor) {
  25.         this.processor = processor;
  26.     }
  27.    
  28.     public MapleServerHandler(PacketProcessor processor, int channel) {
  29.         this.processor = processor;
  30.         this.channel = channel;
  31.     }
  32.    
  33.     @Override
  34.     public void messageSent(IoSession session, Object message) throws Exception {
  35.         Runnable r = ((MaplePacket) message).getOnSend();
  36.         if (r != null) {
  37.             r.run();
  38.         }
  39.         super.messageSent(session, message);
  40.     }
  41.  
  42.         @Override
  43.         public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
  44.             System.out.println(((MapleClient) session.getAttribute(MapleClient.CLIENT_KEY)).getAccountName() + " caught an exception: " + cause.toString());
  45.             cause.printStackTrace();
  46.         }
  47.  
  48.     @Override
  49.     public void sessionOpened(IoSession session) throws Exception {
  50.                 System.out.println("IoSession with " + session.getRemoteAddress() + " opened.");
  51.                 if (channel > -1)
  52.                     if (ChannelServer.getInstance(channel).isShutdown()) {
  53.                                         session.close();
  54.                                         return;
  55.                     }
  56.                 byte key[] = {0x13, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, (byte) 0xB4, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00};
  57.                 byte ivRecv[] = {70, 114, 122, 82};
  58.                 byte ivSend[] = {82, 48, 120, 115};
  59.         ivRecv[3] = (byte) (Math.random() * 255);
  60.         ivSend[3] = (byte) (Math.random() * 255);
  61.         MapleAESOFB sendCypher = new MapleAESOFB(key, ivSend, (short) (0xFFFF - MAPLE_VERSION));
  62.         MapleAESOFB recvCypher = new MapleAESOFB(key, ivRecv, MAPLE_VERSION);
  63.         MapleClient client = new MapleClient(sendCypher, recvCypher, session);
  64.         client.setChannel(channel);
  65.                 if (client.hasBannedIP() || client.hasBannedMac())
  66.                     session.close();
  67.         session.write(MaplePacketCreator.getHello(MAPLE_VERSION, ivSend, ivRecv, false));
  68.         session.setAttribute(MapleClient.CLIENT_KEY, client);
  69.         session.setIdleTime(IdleStatus.READER_IDLE, 30);
  70.         session.setIdleTime(IdleStatus.WRITER_IDLE, 30);
  71.     }
  72.  
  73.     @Override
  74.     public void sessionClosed(IoSession session) throws Exception {
  75.         synchronized (session) {
  76.             MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
  77.  
  78.             if (client != null) {
  79.                 client.disconnect();
  80.                 LoginWorker.getInstance().deregisterClient(client);
  81.                 session.removeAttribute(MapleClient.CLIENT_KEY);
  82.             }
  83.         }
  84.         super.sessionClosed(session);
  85.     }
  86.  
  87.         @Override
  88.         public void messageReceived(IoSession session, Object message) throws Exception {
  89.             byte[] content = (byte[]) message;
  90.             SeekableLittleEndianAccessor slea = new GenericSeekableLittleEndianAccessor(new ByteArrayByteStream(content));
  91.             short packetId = slea.readShort();
  92.             MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
  93.             MaplePacketHandler packetHandler = processor.getHandler(packetId);
  94.             if (packetHandler != null && packetHandler.validateState(client))
  95.                 try {
  96.                     packetHandler.handlePacket(slea, client);
  97.                 } catch (Throwable t) {
  98.                 }
  99.     //        else if (packetHandler == null)
  100.     //            System.out.println(slea.toString());
  101.         }
  102.  
  103.         @Override
  104.         public void sessionIdle(final IoSession session, final IdleStatus status) throws Exception {
  105.             MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
  106.             if (client != null)
  107.                 client.sendPing();
  108.             super.sessionIdle(session, status);
  109.         }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement