forked from KNEMC/KLALB
300 lines
8.2 KiB
Java
300 lines
8.2 KiB
Java
package org.kne.cloud.network.klalb;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.MenuItem;
|
|
import java.awt.PopupMenu;
|
|
import java.awt.SystemTray;
|
|
import java.awt.TrayIcon;
|
|
import java.awt.event.KeyEvent;
|
|
import java.awt.event.KeyListener;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.net.BindException;
|
|
import java.net.ConnectException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.JComboBox;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.UIManager;
|
|
import javax.swing.plaf.TabbedPaneUI;
|
|
import javax.swing.plaf.metal.MetalTabbedPaneUI;
|
|
|
|
import org.kne.cloud.network.mport.ServiceElement;
|
|
import org.kne.cloud.network.mport.ThreadTool;
|
|
import org.kne.ui.KTabUI;
|
|
import org.kne.ui.XFrame;
|
|
import org.kne.ui.YScrollPane;
|
|
import org.knemcl.ui.ProcessingFrame;
|
|
import org.knemcl.ui.UIEnv;
|
|
|
|
import java.awt.AWTException;
|
|
import java.awt.BorderLayout;
|
|
import javax.swing.JTabbedPane;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPopupMenu;
|
|
import javax.swing.JButton;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.ComponentEvent;
|
|
import java.awt.event.ComponentListener;
|
|
import java.awt.event.ItemEvent;
|
|
import java.awt.event.ItemListener;
|
|
import java.awt.event.ActionEvent;
|
|
|
|
public class KLALBClientGUI extends XFrame {
|
|
private JComboBox comboBox;
|
|
BufferedImage bi;
|
|
|
|
private Thread t;
|
|
public KLALBClientGUI() {
|
|
setResizable(false);
|
|
setTitleColor(UIEnv.getDefaultTitleColor());
|
|
try {
|
|
bi = ImageIO.read(KLALBClientGUI.class.getResourceAsStream("/assets/KNEL.png"));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
setIconImage(bi);
|
|
|
|
getTitlelabel().setForeground(Color.WHITE);
|
|
getContentPane().setBackground(UIEnv.getDefaultTitleColor());
|
|
|
|
comboBox = new JComboBox();
|
|
comboBox.addItemListener(new ItemListener() {
|
|
|
|
@Override
|
|
public void itemStateChanged(ItemEvent e) {
|
|
|
|
}
|
|
});
|
|
|
|
reloadCB();
|
|
|
|
|
|
|
|
YScrollPane ysp = new YScrollPane(580);
|
|
ysp.setOpaque(false);
|
|
getContentPane().add(ysp);
|
|
JPanel wv = ysp.getView();
|
|
wv.addComponentListener(new ComponentListener() {
|
|
|
|
@Override
|
|
public void componentShown(ComponentEvent e) {
|
|
// TODO 自动生成的方法存根
|
|
|
|
}
|
|
|
|
@Override
|
|
public void componentResized(ComponentEvent e) {
|
|
// TODO 自动生成的方法存根
|
|
|
|
}
|
|
|
|
@Override
|
|
public void componentMoved(ComponentEvent e) {
|
|
repaint();
|
|
}
|
|
|
|
@Override
|
|
public void componentHidden(ComponentEvent e) {
|
|
// TODO 自动生成的方法存根
|
|
|
|
}
|
|
});
|
|
|
|
JPanel panel_1 = new JPanel();
|
|
panel_1.setOpaque(false);
|
|
getContentPane().add(panel_1, BorderLayout.NORTH);
|
|
panel_1.setLayout(new BorderLayout(0, 0));
|
|
panel_1.add(comboBox, BorderLayout.CENTER);
|
|
|
|
JButton btnNewButton = new JButton("\u6DFB\u52A0");
|
|
btnNewButton.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
String s = JOptionPane.showInputDialog(KLALBClientGUI.this, "输入一条线路的地址");
|
|
if (s != null) {
|
|
if (s.equals("")) {
|
|
JOptionPane.showMessageDialog(KLALBClientGUI.this, "输入不能为空", "错误", JOptionPane.WARNING_MESSAGE);
|
|
} else {
|
|
ProcessingFrame psf = new ProcessingFrame("正在下载线路列表");
|
|
psf.setIconImage(bi);
|
|
psf.getProgressBar().setIndeterminate(true);
|
|
psf.setVisible(true);
|
|
ThreadTool.makeVThreadIfSupport("线路列表获取", () -> {
|
|
try {
|
|
KLALBIPList.createIPList(s);
|
|
reloadCB();
|
|
} catch (Exception e1) {
|
|
JOptionPane.showMessageDialog(KLALBClientGUI.this, e1.toString(), "错误",
|
|
JOptionPane.ERROR_MESSAGE);
|
|
e1.printStackTrace();
|
|
} finally {
|
|
psf.setVisible(false);
|
|
}
|
|
}).start();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
panel_1.add(btnNewButton, BorderLayout.WEST);
|
|
|
|
JButton btnNewButton_1 = new JButton("\u542F\u52A8");
|
|
btnNewButton_1.setForeground(Color.GREEN);
|
|
btnNewButton_1.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent edf) {
|
|
if(t!=null) {
|
|
t.interrupt();
|
|
t=null;
|
|
return;
|
|
}
|
|
t=ThreadTool.makeVThreadIfSupport("客户端主线程", () -> {
|
|
btnNewButton_1.setEnabled(false);
|
|
comboBox.setEnabled(false);
|
|
String s = (String) comboBox.getSelectedItem();
|
|
|
|
|
|
|
|
File pdis = KLALBIPList.getSLdir(s);
|
|
File f = new File(pdis, "iplist.json");
|
|
if (f.exists()) {
|
|
System.out.println("加载线路列表:" + f);;KLALBClient kc=null;
|
|
try {
|
|
KLALBIPList.updateIPList(s);
|
|
STJson stj=new STJson(new String(Files.readAllBytes(Paths.get(f.toURI()))));
|
|
kc = new KLALBClient();
|
|
kc.getTls().addAll(stj.getTunnels());
|
|
List<Tunnel>lt=kc.getTls();
|
|
for (Iterator<Tunnel> iterator = lt.iterator(); iterator.hasNext();) {
|
|
Tunnel tunnel = (Tunnel) iterator.next();
|
|
TPanel tp=new TPanel(tunnel);
|
|
tp.getTname().setForeground(Color.RED);
|
|
wv.add(tp);
|
|
tunnel.setTlr(new TListener() {
|
|
|
|
@Override
|
|
public void setDelay(long delay) {
|
|
tp.setDelay(delay);
|
|
repaint();
|
|
}
|
|
|
|
@Override
|
|
public void setState(boolean state) {
|
|
if(state) {
|
|
tp.getTname().setForeground(Color.GREEN);
|
|
}else {
|
|
tp.getTname().setForeground(Color.RED);
|
|
}
|
|
repaint();
|
|
}
|
|
|
|
@Override
|
|
public void setTraffic(long up, long down, long ups, long downs) {
|
|
tp.setTraffic(up,down,ups,downs);
|
|
repaint();
|
|
}
|
|
|
|
});
|
|
}
|
|
ysp.updateUI();
|
|
ysp.repaint();
|
|
repaint();
|
|
kc.connectTunnel();
|
|
|
|
ProxyJson pj=new ProxyJson(new File(pdis,"services.json"), stj);
|
|
kc.getPet().addAll(pj.getPls());
|
|
kc.listenProxy();
|
|
|
|
btnNewButton_1.setForeground(Color.RED);
|
|
btnNewButton_1.setText("停止");
|
|
btnNewButton_1.setEnabled(true);
|
|
|
|
while(true) {
|
|
Thread.sleep(999);
|
|
List<Tunnel>ts=kc.getTls();
|
|
synchronized(ts) {
|
|
for(int i=0;i<ts.size();i++) {
|
|
Tunnel t=ts.get(i) ;
|
|
t.updateTraffics(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch (ConnectException e) {
|
|
JOptionPane.showMessageDialog(KLALBClientGUI.this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} catch (InterruptedException e) {
|
|
}finally {
|
|
if(kc!=null) {
|
|
kc.close();
|
|
}
|
|
wv.removeAll();
|
|
}
|
|
|
|
} else {
|
|
System.out.println("线路列表不存在");
|
|
}
|
|
btnNewButton_1.setForeground(Color.GREEN);
|
|
btnNewButton_1.setText("启动");
|
|
btnNewButton_1.setEnabled(true);
|
|
comboBox.setEnabled(true);
|
|
});t.start();
|
|
}
|
|
});
|
|
panel_1.add(btnNewButton_1, BorderLayout.EAST);
|
|
setTitle("KNE云网络负载均衡客户端V1.0");
|
|
setSize(600, 370);
|
|
setLocationRelativeTo(null);
|
|
|
|
setVisible(true);
|
|
|
|
if (SystemTray.isSupported()) {
|
|
SystemTray st = SystemTray.getSystemTray();
|
|
TrayIcon ti = new TrayIcon(bi);
|
|
ti.setImageAutoSize(true);
|
|
PopupMenu jpm = new PopupMenu();
|
|
MenuItem mi = new MenuItem("退出");
|
|
mi.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
System.exit(0);
|
|
}
|
|
});
|
|
jpm.add(mi);
|
|
ti.setPopupMenu(jpm);
|
|
try {
|
|
st.add(ti);
|
|
} catch (AWTException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
ti.addActionListener((x) -> {
|
|
setVisible(true);
|
|
});
|
|
}
|
|
|
|
repaint();
|
|
}
|
|
|
|
private void reloadCB() {
|
|
comboBox.removeAllItems();
|
|
File f = new File("KLALB");
|
|
f.mkdirs();
|
|
File[] fs = f.listFiles();
|
|
for (int i = 0; i < fs.length; i++) {
|
|
if (new File(fs[i], "iplist.json").exists()) {
|
|
comboBox.addItem(fs[i].getName());
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
new KLALBClientGUI();
|
|
}
|
|
}
|