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

📄 scannergui.java

📁 Java端口扫描器
💻 JAVA
字号:
package src;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.JDialog;
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.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class ScannerGUI extends JFrame {
	public static JTextArea textArea;
	private JTextField textField_2;
	private JTextField textField_1;
	private static JTextField textField;
	public static JLabel label_2;
	public static JLabel label_3;
	public static int MIN_PORT;
	public static int MAX_PORT;
	public static String IP_ADDRESS;
	private static int MAX_THREAD_NUM;
	public static JProgressBar progressBar; 
	public static int sum=0;
	public static JToggleButton toggleButton;
	private List threadList=new ArrayList();
	private static boolean flag=true;
	public static String custom_port="20,23,25,43,80,135,139";
	public static int sum_1=0;
	public static List list=new LinkedList();
	public static DlgCustomScanInfo d;
	
	public ScannerGUI(){
		super();
		setTitle("F-Mapper V1.0");

		final JMenuBar menuBar = new JMenuBar();
		menuBar.setBorderPainted(false);
		menuBar.setBackground(Color.BLACK);
		menuBar.setBorder(new BevelBorder(BevelBorder.LOWERED, Color.BLACK, Color.BLACK));
		setJMenuBar(menuBar);

		final JMenu menu = new JMenu();
		menu.setForeground(Color.GREEN);
		menu.setBackground(Color.BLACK);
		menu.setText("文件(F)");
		menuBar.add(menu);

		final JMenuItem menuItem_5 = new JMenuItem();
		menuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				if(validateText()){
					flag=true;
					toggleButton.setEnabled(false);
					textArea.setText("扫描结果:"+"\n");
					MIN_PORT=Integer.parseInt(textField_1.getText());
					MAX_PORT=Integer.parseInt(textField_2.getText());
					progressBar.setMinimum(MIN_PORT);
					progressBar.setMaximum(MAX_PORT);
					IP_ADDRESS=textField.getText();
				for(int i=0;i<MAX_THREAD_NUM;i++){
					ScanThread std=new ScanThread(ScannerGUI.this);
					std.start();
					threadList.add(std);
				}
				}else{
					textArea.setText("请正确输入...");
				}
			}
		});
		menuItem_5.setForeground(Color.GREEN);
		menuItem_5.setBackground(Color.BLACK);
		menuItem_5.setText("开始(B)");
		menu.add(menuItem_5);

		final JMenuItem zaiMenuItem = new JMenuItem();
		zaiMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				if(validateText()){
					flag=false;
					toggleButton.setText("继续(C)");
					toggleButton.setEnabled(true);
				}else{
					textArea.setText("请正确输入...");
				}
			}
		});
		zaiMenuItem.setForeground(Color.GREEN);
		zaiMenuItem.setBackground(Color.BLACK);
		zaiMenuItem.setText("暂停(S)");
		menu.add(zaiMenuItem);

		final JMenuItem menuItem_7 = new JMenuItem();
		menuItem_7.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				flag=false;
				for(int i=0;i<threadList.size();i++){
					threadList.remove(i);
				}
				toggleButton.setEnabled(true);
				progressBar.setValue(0);
				label_3.setText("扫描结束...");
			}
		});
		menuItem_7.setForeground(Color.GREEN);
		menuItem_7.setBackground(Color.BLACK);
		menuItem_7.setText("停止(T)");
		menu.add(menuItem_7);

		final JMenuItem menuItem_8 = new JMenuItem();
		menuItem_8.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				textArea.setText("");
			}
		});
		menuItem_8.setForeground(Color.GREEN);
		menuItem_8.setBackground(Color.BLACK);
		menuItem_8.setText("清空(C)");
		menu.add(menuItem_8);

		menu.addSeparator();

		final JMenuItem menuItem = new JMenuItem();
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				System.exit(0);
			}
		});
		menuItem.setForeground(Color.GREEN);
		menuItem.setBackground(Color.BLACK);
		menuItem.setText("退出(E)");
		menu.add(menuItem);

		final JMenu menu_1 = new JMenu();
		menu_1.setForeground(Color.GREEN);
		menu_1.setBackground(Color.BLACK);
		menu_1.setText("设置(C)");
		menuBar.add(menu_1);

		final JMenuItem menuItem_1 = new JMenuItem();
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				DlgConfig dc=new DlgConfig(ScannerGUI.this);
				dc.setSize(480,70);
				dc.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dc.setResizable(false);
				dc.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-dc.getWidth())/2,
				          (Toolkit.getDefaultToolkit().getScreenSize().height-dc.getHeight())/2);
				dc.pack();
				dc.setVisible(true);
			}
		});
		menuItem_1.setForeground(Color.GREEN);
		menuItem_1.setBackground(Color.BLACK);
		menuItem_1.setText("扫描参数(P)");
		menu_1.add(menuItem_1);

		final JMenu menu_3 = new JMenu();
		menu_3.setForeground(Color.GREEN);
		menu_3.setBackground(Color.BLACK);
		menu_3.setText("高级(A)");
		menuBar.add(menu_3);

		final JMenuItem menuItem_4 = new JMenuItem();
		menuItem_4.setForeground(Color.GREEN);
		menuItem_4.setBackground(Color.BLACK);
		menuItem_4.setText("并行扫描(C)");
		menu_3.add(menuItem_4);

		final JMenuItem menuItem_6 = new JMenuItem();
		menuItem_6.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				DlgCustomScanInfo d=new DlgCustomScanInfo();
				d.setSize(480,30);
				d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				d.setResizable(false);
				d.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-d.getWidth())/2,
				          (Toolkit.getDefaultToolkit().getScreenSize().height-d.getHeight())/2);
				d.pack();
				d.setVisible(true);
				executeCustomScan();
			}
		});
		menuItem_6.setForeground(Color.GREEN);
		menuItem_6.setBackground(Color.BLACK);
		menuItem_6.setText("自定义扫描(C)");
		menu_3.add(menuItem_6);

		final JMenu menu_4 = new JMenu();
		menu_4.setForeground(Color.GREEN);
		menu_4.setBackground(Color.BLACK);
		menu_4.setText("工具(T)");
		menuBar.add(menu_4);

		final JMenuItem menuItem_3 = new JMenuItem();
		menuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				DlgCommand dc=new DlgCommand(ScannerGUI.this);
				dc.setSize(480,30);
				dc.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dc.setResizable(false);
				dc.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-dc.getWidth())/2,
				          (Toolkit.getDefaultToolkit().getScreenSize().height-dc.getHeight())/2);
				dc.pack();
				dc.setVisible(true);
			}
		});
		menuItem_3.setForeground(Color.GREEN);
		menuItem_3.setBackground(Color.BLACK);
		menuItem_3.setText("DOS命令(C)");
		menu_4.add(menuItem_3);

		final JMenu menu_2 = new JMenu();
		menu_2.setForeground(Color.GREEN);
		menu_2.setBackground(Color.BLACK);
		menu_2.setText("帮助(H)");
		menuBar.add(menu_2);

		final JMenuItem menuItem_2 = new JMenuItem();
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				About a=new About(ScannerGUI.this);
				a.setSize(300,300);
				a.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				a.setResizable(false);
				a.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-a.getWidth())/2,
				          (Toolkit.getDefaultToolkit().getScreenSize().height-a.getHeight())/2);
				a.pack();
				a.setVisible(true);
			}
		});
		menuItem_2.setForeground(Color.GREEN);
		menuItem_2.setBackground(Color.BLACK);
		menuItem_2.setText("关于F-Mapper V1.0");
		menu_2.add(menuItem_2);

		final JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(Color.GREEN, 1, false));
		panel.setForeground(Color.GREEN);
		panel.setBackground(Color.BLACK);
		getContentPane().add(panel, BorderLayout.NORTH);

		final JLabel ipLabel = new JLabel();
		ipLabel.setForeground(Color.GREEN);
		ipLabel.setText("请输入IP地址:");
		panel.add(ipLabel);

		textField = new JTextField();
		textField.setColumns(17);
		try {
			textField.setText(InetAddress.getLocalHost().getHostAddress());
		} catch (UnknownHostException e1) {
			label_3.setText("不能得到本机IP");
		}
		panel.add(textField);

		final JLabel label = new JLabel();
		label.setForeground(Color.GREEN);
		label.setText("   端口范围:");
		panel.add(label);

		textField_1 = new JTextField();
		textField_1.setColumns(7);
		panel.add(textField_1);

		final JLabel label_1 = new JLabel();
		label_1.setForeground(Color.GREEN);
		label_1.setText("->");
		panel.add(label_1);

		textField_2 = new JTextField();
		textField_2.setColumns(7);
		panel.add(textField_2);

		final JPanel panel_1 = new JPanel();
		final FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel_1.setLayout(flowLayout);
		panel_1.setBackground(Color.BLACK);
		getContentPane().add(panel_1, BorderLayout.SOUTH);

		label_2 = new JLabel();
		label_2.setForeground(Color.GREEN);
		label_2.setText("任务进度:");
		panel_1.add(label_2);

		label_3 = new JLabel();
		label_3.setPreferredSize(new Dimension(90, 16));
		label_3.setForeground(Color.GREEN);
		label_3.setText("初始化完成...");
		panel_1.add(label_3);

		progressBar = new JProgressBar();
		progressBar.setPreferredSize(new Dimension(250, 18));
		progressBar.setForeground(Color.GREEN);
		progressBar.setBackground(Color.BLACK);
		panel_1.add(progressBar);

		toggleButton = new JToggleButton();
		toggleButton.setText("执行(E)");
		toggleButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				if(toggleButton.getText().equals("执行(E)")){
					if(validateText()){
						flag=true;
						toggleButton.setEnabled(false);
						textArea.setText("扫描结果:"+"\n");
						MIN_PORT=Integer.parseInt(textField_1.getText());
						MAX_PORT=Integer.parseInt(textField_2.getText());
						progressBar.setMinimum(MIN_PORT);
						progressBar.setMaximum(MAX_PORT);
						IP_ADDRESS=textField.getText();
				for(int i=0;i<MAX_THREAD_NUM;i++){
					ScanThread std=new ScanThread(ScannerGUI.this);
					std.start();
					threadList.add(std);
				}
				}else{
					textArea.setText("请正确输入...");
				}
				}else if(toggleButton.getText().equals("继续(C)")){
					flag=true;
					toggleButton.setEnabled(false);
					toggleButton.setText("执行(E)");
					CallWaitSetThread c=new CallWaitSetThread(ScannerGUI.this);
					c.start();
				}
			}
		});
		toggleButton.setPreferredSize(new Dimension(75, 18));
		toggleButton.setBackground(Color.WHITE);
		panel_1.add(toggleButton);

		final JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(Color.BLACK, 1, false));
		panel_2.setBackground(Color.BLACK);
		getContentPane().add(panel_2, BorderLayout.CENTER);

		textArea = new JTextArea();
		textArea.setForeground(Color.CYAN);
		textArea.setFont(new Font("@宋体", Font.PLAIN, 12));
		textArea.setEditable(false);
		textArea.setBackground(Color.BLACK);
		textArea.setRows(13);
		textArea.setLineWrap(true);
		textArea.setBorder(new LineBorder(Color.GREEN, 1, false));
		textArea.setColumns(43);
		textArea.setAutoscrolls(true);
		panel_2.add(textArea);
		
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}catch(Exception e){
			label_3.setText("初始化失败...");
		}
	}
	//Method
	public synchronized void add(){
		if(flag){
			MIN_PORT++;
		}else{
			try {
				this.wait();
			} catch (InterruptedException e) {
				label_3.setText("中断异常...");
			}
		}
	}
	
	public synchronized void sum(){
		sum++;
	}
	
	public synchronized void callThread(){
		this.notifyAll();
	}
	
	public void initConfig(){
			Document doc=XmlUtils.getDocument();
			if(doc!=null){
				NodeList nodelist=doc.getElementsByTagName("thread-num");
				String thread_num=nodelist.item(0).getFirstChild().getNodeValue();
				MAX_THREAD_NUM=Integer.parseInt(thread_num);
			}
	}
	
	public boolean validateText(){
		String str1=textField_1.getText();
		String str2=textField_2.getText();
		if((str1.matches("\\d") || str1.matches("\\d\\d") || str1.matches("\\d\\d\\d") || str1.matches("\\d\\d\\d\\d") || str1.matches("\\d\\d\\d\\d\\d")) 
			 && (str2.matches("\\d") || str2.matches("\\d\\d") || str2.matches("\\d\\d\\d") || str2.matches("\\d\\d\\d\\d") || str2.matches("\\d\\d\\d\\d\\d"))
			 && (Integer.parseInt(str1)<=65535) && (Integer.parseInt(str2)<=65535)){
			return true;
		}else{
			return false;
		}
	}
	
	public void executeCustomScan(){
		StringTokenizer token=new StringTokenizer(custom_port,",");
		while(token.hasMoreTokens()){
			String element=token.nextToken();
			list.add(element);
		}
		d.progressBar.setMaximum(list.size()-1);
		d.progressBar.setMinimum(0);
		if(list.size()>0){
			for(int i=0;i<1;i++){
				new CustomScanThread(list.size()).start();
			}
		}
	}
	
	public synchronized static void add1(){
		sum_1++;
	}
	
	public static String getIPAddress(){
		return textField.getText();
	}
}

⌨️ 快捷键说明

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