package com.nic.show;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.nic.control.MyPacket;
import com.nic.control.NetworkCard;
import com.nic.control.PacketAnalyze;
import com.nic.control.PacketCapture;
import jpcap.NetworkInterface;
import jpcap.packet.Packet;
public class MyInterface extends JFrame {
JMenuBar menubar; // 菜单条
JMenu menuFile1; // 菜单
JMenuItem[] item; // 菜单项
JMenuItem pro1, pro2, pro3;
JTextField searchText;
JLabel protocolLabel, srcIpLabel, dstIpLabel, keyLabel;
JButton searchButton;
JButton save;
JTextField protjtf, srcipjtf, dstipjtf, keyjtf;
JTextArea info;
JPanel panel;
JPanel panelDetail;
JScrollPane scrollPane;
JTable table;
NetworkInterface[] devices;
Object[][] datalist = {};
DefaultTableModel tableModel;
PacketCapture allpackets;
String[] head = new String[] { "序号", "时间", "源IP", "目的IP", "协议", "长度" };
HashMap<String, String> toChiese;
public MyInterface() {
init();
}
void getChiese() {
toChiese = new HashMap<String, String>();
toChiese.put("srcIP", "源IP地址");
toChiese.put("dstIP", "目的IP地址");
toChiese.put("srcMAC", "源MAC地址");
toChiese.put("dstMAC", "目的MAC地址");
toChiese.put("protocol", "协议");
toChiese.put("identifie", "标识");
toChiese.put("headLength", "头长度");
toChiese.put("dataLength", "数据长度");
toChiese.put("moreFrag", "后继分片");
toChiese.put("version", "版本");
toChiese.put("srcPort", "源端口");
toChiese.put("dstPort", "目的端口");
toChiese.put("data", "数据");
toChiese.put("rsv_tos", "服务类型");
}
void init() {
allpackets = new PacketCapture();
this.setTitle("MySniffer");
this.setBounds(250, 150, 1200, 1500);
menubar = new JMenuBar();
// 根据网卡进行过滤
menuFile1 = new JMenu(" 网卡 ");
NetworkInterface[] devices = new NetworkCard().getDevices();
item = new JMenuItem[devices.length];
for (int i = 0; i < devices.length; i++) {
item[i] = new JMenuItem(i + ": " + devices[i].name + "(" + devices[i].description + ")");
menuFile1.add(item[i]);
item[i].addActionListener(new CardActionListener(devices[i]));
}
protocolLabel = new JLabel("协议");
srcIpLabel = new JLabel(" 源地址:");
dstIpLabel = new JLabel(" 目的地址:");
keyLabel = new JLabel(" 关键字:");
searchButton = new JButton(" 过滤 ");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String protocol = protjtf.getText();
String srcIP = srcipjtf.getText();
String dstIP = dstipjtf.getText();
String keyword = keyjtf.getText();
allpackets.setFilter("protocol", protocol);
allpackets.setFilter("srcIP", srcIP);
allpackets.setFilter("dstIP", dstIP);
allpackets.setFilter("keyWord", keyword);
while (tableModel.getRowCount() > 0) {
tableModel.removeRow(tableModel.getRowCount() - 1);
}
allpackets.reLoadList();
}
});
protjtf = new JTextField();
srcipjtf = new JTextField();
dstipjtf = new JTextField();
keyjtf = new JTextField();
// 将菜单添加到菜单条上
menubar.add(menuFile1);
menubar.add(protocolLabel);
menubar.add(protjtf);
menubar.add(srcIpLabel);
menubar.add(srcipjtf);
menubar.add(dstIpLabel);
menubar.add(dstipjtf);
menubar.add(keyLabel);
menubar.add(keyjtf);
menubar.add(searchButton);
setJMenuBar(menubar);
tableModel = new DefaultTableModel(datalist, head);
table = new JTable(tableModel) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
allpackets.setTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 60));// 设置表格的大小
table.setRowHeight(30);// 设置每行的高度为20
table.setRowMargin(5);// 设置相邻两行单元格的距离
table.setRowSelectionAllowed(true);// 设置可否被选择.默认为false
table.setSelectionBackground(Color.cyan);// 设置所选择行的背景色
table.setSelectionForeground(Color.red);// 设置所选择行的前景色
table.setShowGrid(true);// 是否显示网格线
table.doLayout();
scrollPane = new JScrollPane(table);
// panel = new JPanel(new GridLayout(2, 1));
panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(1200, 700));
panel.setBackground(Color.black);
panelDetail = new JPanel();
panelDetail.setLayout(new BorderLayout());
info = new JTextArea(10, 1200);
info.setText("详细信息:\n");
save = new JButton("保存到本地");
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
panelDetail.add(new JScrollPane(info), BorderLayout.CENTER);
panelDetail.add(save, BorderLayout.SOUTH);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(panelDetail, BorderLayout.SOUTH);
setContentPane(panel);
pack();
getChiese();
/*
* 给表格添加监听器,双击时,显示报的详细内容
*/
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
if (ev.getClickCount() == 2) {
int row = table.getSelectedRow();
info.setText("");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e3) {
String text = info.getText();
int name = (int) System.currentTimeMillis();
try {
FileOutputStream fos = new FileOutputStream("C://Users/cctv/Desktop/" + name + ".txt");
fos.write(text.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
ArrayList<MyPacket> packetlist = allpackets.getpacketlist();
Map<String, String> hm1 = new HashMap<String, String>();
Map<String, String> hm2 = new HashMap<String, String>();
int id = Integer.parseInt((String) tableModel.getValueAt(row, 0)) - 1;
MyPacket myPacket = packetlist.get(id);
PacketAnalyze pa = new PacketAnalyze(myPacket);
info.append("id=" + myPacket.getId() + "\r\n");
info.append("------------------------------------------------------------------------------\r\n");
info.append("-------------------------------IP头信息:-------------------------------------\r\n");
info.append("------------------------------------------------------------------------------\r\n");
hm1 = pa.IPanalyze();
for (Map.Entry<String, String> me : hm1.entrySet()) {
info.append((toChiese.containsKey(me.getKey()) ? toChiese.get(me.getKey()) : me.getKey())
+ " : " + me.getValue() + "\r\n");
}
hm2 = pa.packetClass();
info.append(
"\n\r------------------------------------------------------------------------------\r\n");
info.append("---------------------------------" + hm2.get("protocol")
+ "头信息:----------------------------------\r\n");
info.append("------------------------------------------------------------------------------\r\n");
for (Map.Entry<String, String> me : hm2.entrySet()) {
info.append((toChiese.containsKey(me.getKey()) ? toChiese.get(me.getKey()) : me.getKey())
+ " : " + me.getValue() + "\r\n");
}
}
}
});
setResizable(true);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private class CardActionListener implements ActionListener {
NetworkInterface device;
CardActionListener(NetworkInterface device) {