debugger.java

来自「一个功能强大的聊天程序.....基本实现所有功能....强烈推荐下载」· Java 代码 · 共 1,422 行 · 第 1/4 页

JAVA
1,422
字号
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* 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.ui.debug;

import static org.apache.commons.codec.digest.DigestUtils.md5;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import edu.tsinghua.lumaqq.qq.Crypter;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.debug.BasicDebugPacket;
import edu.tsinghua.lumaqq.qq.debug.DebugSwitch;
import edu.tsinghua.lumaqq.qq.debug.DiskDebugPacket;
import edu.tsinghua.lumaqq.qq.debug.FragmentDO;
import edu.tsinghua.lumaqq.qq.debug.IDebugListener;
import edu.tsinghua.lumaqq.qq.debug.IDebugObject;
import edu.tsinghua.lumaqq.qq.debug.InitialArgument;
import edu.tsinghua.lumaqq.qq.debug._03DebugPacket;
import edu.tsinghua.lumaqq.qq.debug._05DebugPacket;
import edu.tsinghua.lumaqq.qq.net.IConnection;
import edu.tsinghua.lumaqq.qq.packets.PacketHelper;
import edu.tsinghua.lumaqq.qq.packets.PacketParseException;
import edu.tsinghua.lumaqq.qq.packets.in.LoginReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in.RequestKeyReplyPacket;
import edu.tsinghua.lumaqq.resource.Resources;
import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.lumaqq.ui.helper.UITool;
import edu.tsinghua.lumaqq.ui.provider.ListContentProvider;

/**
 * <pre>
 * 图形化调试客户端,可以独立运行,也可以做为程序的子窗口运行。在独立运行时,其不具备自动接收包
 * 的能力,需要从input框输入包数据方能解析;在做为子窗口运行时,如果打开调试功能,则其会自动接收
 * 到DebugSwitch发来的包并显示。
 * </pre>
 * 
 * @author luma
 */
public class Debugger extends ApplicationWindow implements IDebugListener {
    /**
     * 包名修改器
     * 
     * @author luma
     */
    private class PacketNameCellModifier implements ICellModifier {
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
         */
        public boolean canModify(Object element, String property) {
            return Debugger.NAME.equals(property);
        }

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
         */
        public Object getValue(Object element, String property) {
            IDebugObject obj = (IDebugObject)element;
            
            if(Debugger.NAME.equals(property))
                return obj.getName();
            else
                return null;
        }

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
         */
        public void modify(Object element, String property, Object value) {
            IDebugObject obj = (IDebugObject)((TableItem)element).getData();
            
            if(Debugger.NAME.equals(property)) {
                obj.setName((String)value);
                packetViewer.refresh(obj);
            }
        }
    }
    
    /**
     * <pre>
     * 执行添加包的任务,因为这些方法可能是从非UI线程中调用的,我们需要确保其在UI线程中执行
     * </pre>
     * 
     * @author luma
     */
    private class AddPacketRunnable implements Runnable {
        public IDebugObject obj;
        
        /* (non-Javadoc)
         * @see java.lang.Runnable#run()
         */
        public void run() {
            packets.add(obj);
            if(!packetViewer.getControl().isDisposed())
                packetViewer.refresh();
        }
    }
    
    private Display display;
    
    // 当前是否正在调试
    private boolean debug;
    
    private Font font;
    private Resources res;
    private AddPacketRunnable addPacketRunnable;
    private MainShell main;
    private QQUser user;
    private PacketHelper packetHelper;
    
    private StyledText textBytes, textInput;
    private Text textSend;
    private TableViewer packetViewer;
    private List<IDebugObject> packets;
    private CTabFolder viewFolder;

    private static Crypter crypter = new Crypter();
    protected static final String NAME = "name";
    
    private Menu packetMenu, textMenu, decryptMenu, encryptMenu, parseMenu, sendMenu;
    private ToolItem tiParse, tiDecrypt;
    
    private Button chkBodyOnly;
    private Label lblSelection;

	private ToolItem tiInputArg;

	private ToolItem tiEncrypt;
    
    /**
     * @param main
     * 		MainShell, 为null表示独立模式
     */
    public Debugger(MainShell main) {
        super(null);
        this.main = main;
        if(main != null)
            user = main.getClient().getUser();
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.jface.window.ApplicationWindow#configureShell(org.eclipse.swt.widgets.Shell)
     */
    protected void configureShell(Shell shell) {
        initializeVariables(shell);
        shell.setText("Debugger 2006");
        shell.setImage(res.getImage(Resources.icoDebug));
        shell.addDisposeListener(new DisposeListener() {
        	public void widgetDisposed(DisposeEvent e) {
                font.dispose();
        	}
        });
        setBlockOnOpen(main == null);
        if(main != null) {
        	final ShellListener sl = new ShellAdapter() {
        		@Override
        		public void shellClosed(ShellEvent e) {
        			close();
        		}
        	};
        	main.getShell().addShellListener(sl);
        	shell.addShellListener(new ShellAdapter() {
        		@Override
        		public void shellClosed(ShellEvent e) {
        			main.getShell().removeShellListener(sl);
        		}
        	});
        }
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
     */
    protected void handleShellCloseEvent() {
        disableDebug();
        super.handleShellCloseEvent();
    }
    
    /**
     * 关闭调试模式
     */
    private void disableDebug() {
        DebugSwitch ds = DebugSwitch.getInstance();
        ds.removeDebugListener(this);
        setDebug(false);
        tiInputArg.setText("Start Debug");
    }
    
    /**
     * 打开调试模式
     */
    private void enableDebug() {
        DebugSwitch ds = DebugSwitch.getInstance();
        ds.addDebugListener(this);
        setDebug(true);
        tiInputArg.setText("Stop Debug");
    }

    /**
     * 切换调试状态,这个方法只会被用在子窗口模式式
     * 
     * @param e
     * 			按钮选择事件
     */
    private void switchDebugMode() {
        /* 开始或停止调试 */
        if(debug)
            disableDebug();           
        else 
            enableDebug();
    }
    
    /**
     * 初始化菜单
     */
    private void initMenu() {
        initPacketMenu();        
        initTextMenu();        
        initDecryptMenu();        
        initParseMenu();        
        initSendMenu();
        initEncryptMenu();
    }
    
    /**
     * 使用指定密钥加密输入串
     * 
     * @param key
     */
    private void encryptInput(byte[] key) {
        byte[] b = Util.convertHexStringToByte(textInput.getText());
        if(b == null)
        	b = Util.getBytes(textInput.getText());
        if(b == null)
        	return;
        
        b = crypter.encrypt(b, key);
        textInput.append(System.getProperty("line.separator"));
        textInput.append(Util.convertByteToHexString(b));
    }

	private void initEncryptMenu() {
		MenuItem mi;
		// 加密菜单
        encryptMenu = new Menu(getShell());
        // 用会话密钥加密
        final MenuItem miSessionKey = new MenuItem(encryptMenu, SWT.PUSH);
        miSessionKey.setText("Encrypt with Session Key");
        miSessionKey.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                encryptInput(user.getSessionKey());
            }
        });
        // 用密码密钥加密
        final MenuItem miPasswordKey = new MenuItem(encryptMenu, SWT.PUSH);
        miPasswordKey.setText("Encrypt with Password Key");
        miPasswordKey.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	encryptInput(user.getPasswordKey());
            }
        });
        // 用文件传输会话密钥加密
        final MenuItem miFileSessionKey = new MenuItem(encryptMenu, SWT.PUSH);
        miFileSessionKey.setText("Encrypt with File Session Key");
        miFileSessionKey.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	encryptInput(user.getFileSessionKey());
            }
        });
        // 用QQ初始密钥加密
        final MenuItem miInitKey = new MenuItem(encryptMenu, SWT.PUSH);
        miInitKey.setText("Encrypt with QQ Initial Key");
        miInitKey.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	encryptInput(user.getInitKey());
            }
        });
        // 用文件中转密钥加密
        final MenuItem miAgentKey = new MenuItem(encryptMenu, SWT.PUSH);

⌨️ 快捷键说明

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