⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicoutpacket.java

📁 MilyQQ是一个使用控制台的QQ客户端,基于LumaQQ的核心JQL包开发,只有一个可执行的jar包(MilyQQ.jar),携带方便,由于是Java程序,因此理论上应该可以运行于所有平台,不过基于
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*                    notXX
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//package edu.tsinghua.lumaqq.qq.packets;

import java.nio.ByteBuffer;

//import edu.tsinghua.lumaqq.qq.QQ;
//import edu.tsinghua.lumaqq.qq.Util;
//import edu.tsinghua.lumaqq.qq.beans.QQUser;


/**
 * 基本协议族的输出包基类
 * 每一个发出包都有一个头部和一个尾部, 头部的格式为:
 * <ol>
 * <li>头部
 * <ol>
 * <li>标志字节, 0x00. (==0x2)
 * <li>客户端版本代码, 0x01~0x02.
 * <li>命令类型, 0x03~0x04.
 * <li>包序号, 0x05~0x06.
 * <li>用户QQ号, 0x07~0x0A
 * </ol>
 * <li>包的内容
 * <li>尾部, 0x00. (==0x3)
 * </ol>
 * 头部, QQ号和尾部都是不加密的, 除此之外内容部分都将用会话密钥加密, 其中登录包要用密码密钥加密
 * 相对基类增加了是否需要重发的属性.
 *
 * @author notxx
 * @author luma
 */
public abstract class BasicOutPacket extends OutPacket {
    private int qqNum;

	/**
	 * 构造一个参数指定的包.
	 * @param command
	 *                   命令.
	 * @param ack
	 *                   是否需要回应.
	 */
	protected BasicOutPacket(char command, boolean ack, QQUser user) {
		super(QQ.QQ_HEADER_BASIC_FAMILY, command, ack, user);
	}

	protected BasicOutPacket(ByteBuffer buf, QQUser user) throws PacketParseException {
	    this(buf, buf.limit() - buf.position(), user);
	}

	protected BasicOutPacket(ByteBuffer buf, int length, QQUser user) throws PacketParseException {
	    super(buf, length, user);
	}

	/* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#validateHeader()
     */
	@Override
    protected boolean validateHeader() {
        return qqNum == user.getQQ();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#getBodyBytes(java.nio.ByteBuffer, int)
     */
	@Override
    protected byte[] getBodyBytes(ByteBuffer buf, int length) {
	    // 得到包体长度
	    int bodyLen = length - QQ.QQ_LENGTH_BASIC_FAMILY_OUT_HEADER - QQ.QQ_LENGTH_BASIC_FAMILY_TAIL;
	    if(!user.isUdp()) bodyLen -= 2;
	    // 得到加密的包体内容
	    byte[] body = new byte[bodyLen];
	    buf.get(body);
	    return body;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#getUDPLength(int)
     */
	@Override
    protected int getLength(int bodyLength) {
        return QQ.QQ_LENGTH_BASIC_FAMILY_OUT_HEADER + QQ.QQ_LENGTH_BASIC_FAMILY_TAIL + bodyLength + (user.isUdp() ? 0 : 2);
    }

	/* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#parseTail(java.nio.ByteBuffer)
     */
	@Override
    protected void parseTail(ByteBuffer buf) throws PacketParseException {
        buf.get();
    }

	/* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.Packet#getPacketName()
     */
	@Override
    public String getPacketName() {
        return "Unknown Outcoming Packet - Basic Family";
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#decryptBody(byte[], int, int)
     */
	@Override
    protected byte[] decryptBody(byte[] body, int offset, int length) {
        switch(command) {
            case QQ.QQ_CMD_REQUEST_LOGIN_TOKEN:
                byte[] undecrypted = new byte[length];
            	System.arraycopy(body, offset, undecrypted, 0, length);
            	return undecrypted;
            case QQ.QQ_CMD_LOGIN:
		        byte[] ret = new byte[QQ.QQ_LENGTH_KEY + QQ.QQ_LENGTH_LOGIN_DATA];
		        System.arraycopy(body, 0, ret, 0, QQ.QQ_LENGTH_KEY);
		        byte[] b = crypter.decrypt(body, QQ.QQ_LENGTH_KEY, length - QQ.QQ_LENGTH_KEY, user.getInitKey());
		        System.arraycopy(b, 0, ret, QQ.QQ_LENGTH_KEY, b.length);
		        return ret;
		    default:
		        return crypter.decrypt(body, offset, length, user.getSessionKey());
		}
    }

	/* (non-Javadoc)
	 * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#encryptBody(byte[], int, int)
	 */
	@Override
	protected byte[] encryptBody(byte[] b, int offset, int length) {
	    switch(command) {
	        case QQ.QQ_CMD_REQUEST_LOGIN_TOKEN:
	        case QQ.QQ_CMD_LOGIN:
		        byte[] ret = new byte[length];
		        System.arraycopy(b, offset, ret, 0, length);
		        return ret;
		    default:
		        return crypter.encrypt(b, offset, length, user.getSessionKey());
	    }
	}

	/* (non-Javadoc)
	 * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#putHead(java.nio.ByteBuffer)
	 */
	@Override
	protected void putHead(ByteBuffer buf) {
	    if(!user.isUdp())
	        buf.putChar((char)0);
		buf.put(getHeader());
		buf.putChar(source);
		buf.putChar(command);
		buf.putChar(sequence);
		buf.putInt(user.getQQ());
	}

	/* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#postFill(java.nio.ByteBuffer, int)
     */
	@Override
    protected void postFill(ByteBuffer buf, int startPos) {
	    // 如果是tcp包,到包的开头处填上包长度,然后回到目前的pos
	    if(!user.isUdp()) {
	        int len = buf.position() - startPos;
	        buf.putChar(startPos, (char)len);
	    }
    }

	/* (non-Javadoc)
	 * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#putTail(java.nio.ByteBuffer)
	 */
	@Override
	protected void putTail(ByteBuffer buf) {
		buf.put(QQ.QQ_TAIL_BASIC_FAMILY);
	}

	/* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#parseHeader(java.nio.ByteBuffer)
     */
	@Override
    protected void parseHeader(ByteBuffer buf) throws PacketParseException {
	    if(!user.isUdp())
	        buf.getChar();
	    header = buf.get();
		source = buf.getChar();
		command = buf.getChar();
		sequence = buf.getChar();
		qqNum = buf.getInt();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.Packet#getHeadLength()
     */
	@Override
    protected int getHeadLength() {
        return QQ.QQ_LENGTH_BASIC_FAMILY_OUT_HEADER + (user.isUdp() ? 0 : 2);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.Packet#getTailLength()
     */
	@Override
    protected int getTailLength() {
        return QQ.QQ_LENGTH_BASIC_FAMILY_TAIL;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
	@Override
    public String toString() {
//        return "包类型: " + Util.getCommandString(command) + " 序号: " + (int)sequence;
        return "";
	}

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.Packet#getCryptographStart()
     */
	@Override
    protected int getCryptographStart() {
        return -1;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -