Skip to content

Commit

Permalink
add FSClient with command line interface mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zqhong committed Jan 11, 2016
1 parent 5ef350a commit aaea363
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 205 deletions.
294 changes: 91 additions & 203 deletions src/net/fs/client/ClientNoUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,234 +2,122 @@

package net.fs.client;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import org.pcap4j.core.Pcaps;

import net.fs.rudp.Route;
import net.fs.utils.Tools;
import net.fs.utils.Tools;
import net.miginfocom.swing.MigLayout;
import net.fs.utils.MLog;

import com.alibaba.fastjson.JSONObject;

public class ClientNoUI implements ClientUII{
MapClient mapClient;

ClientConfig config;

String configFilePath="client_config.json";
ClientNoUI(){
public class ClientNoUI extends ClientUI{

@Override
void run(){
systemName=System.getProperty("os.name").toLowerCase();
MLog.info("System: "+systemName+" "+System.getProperty("os.version"));
ui=this;

checkQuanxian();
loadConfig();
Route.localDownloadSpeed=config.downloadSpeed;
Route.localUploadSpeed=config.uploadSpeed;
// mapClient=new MapClient(config.getSocks5Port());
// mapClient.setUi(this);
// mapClient.setMapServer(config.getServerAddress(), config.getServerPort(),config.getRemotePort() ,config.getPasswordMd5(),config.getPasswordMd5_Proxy(),config.isDirect_cn());
}

void openUrl(String url){
try {
Desktop.getDesktop().browse(new URI(url));
} catch (IOException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
}

public void setMessage(String message){
//MLog.info("状态: "+message);
}

ClientConfig loadConfig(){
ClientConfig cfg=new ClientConfig();
if(!new File(configFilePath).exists()){
JSONObject json=new JSONObject();
try {
saveFile(json.toJSONString().getBytes(), configFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
String content=readFileUtf8(configFilePath);
JSONObject json=JSONObject.parseObject(content);
cfg.setServerAddress(json.getString("server_address"));
cfg.setServerPort(json.getIntValue("server_port"));
cfg.setRemotePort(json.getIntValue("remote_port"));
if(json.containsKey("direct_cn")){
cfg.setDirect_cn(json.getBooleanValue("direct_cn"));
}
cfg.setDownloadSpeed(json.getIntValue("download_speed"));
cfg.setUploadSpeed(json.getIntValue("upload_speed"));
if(json.containsKey("socks5_port")){
cfg.setSocks5Port(json.getIntValue("socks5_port"));
}
config=cfg;
} catch (Exception e) {
e.printStackTrace();
}
return cfg;
}

public static String readFileUtf8(String path) throws Exception{
String str=null;
FileInputStream fis=null;
DataInputStream dis=null;
try {
File file=new File(path);

int length=(int) file.length();
byte[] data=new byte[length];
model=new MapRuleListModel();
tcpMapRuleListTable=new MapRuleListTable(this,model);


fis=new FileInputStream(file);
dis=new DataInputStream(fis);
dis.readFully(data);
str=new String(data,"utf-8");

} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
if(fis!=null){
checkFireWallOn();
if(!success_firewall_windows){
MLog.info("启动windows防火墙失败,请先运行防火墙服务.");
System.exit(0);
}
if(!success_firewall_osx){
MLog.info("启动ipfw/pf防火墙失败,请先安装.");
System.exit(0);
}

Thread thread=new Thread(){
public void run(){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
Pcaps.findAllDevs();
b1=true;
} catch (Exception e3) {
e3.printStackTrace();

}
}
if(dis!=null){
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
};
thread.start();
try {
thread.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
//JOptionPane.showMessageDialog(mainFrame,System.getProperty("os.name"));
if(!b1){
try {
String msg="启动失败,请先安装libpcap";
if(systemName.contains("windows")){
msg="启动失败,请先安装winpcap";
}
MLog.info(msg);
if(systemName.contains("windows")){
try {
Runtime.getRuntime().exec("winpcap_install.exe",null);
} catch (IOException e) {
e.printStackTrace();
}
}
System.exit(0);
} catch (Exception e2) {
e2.printStackTrace();
}
}


try {
mapClient=new MapClient(this);
} catch (final Exception e1) {
e1.printStackTrace();
capException=e1;
//System.exit(0);;
}

return str;
}

void saveFile(byte[] data,String path) throws Exception{
FileOutputStream fos=null;
try {
fos=new FileOutputStream(path);
fos.write(data);
} catch (Exception e) {
throw e;
} finally {
if(fos!=null){
fos.close();
if(!mapClient.route_tcp.capEnv.tcpEnable){
MLog.info("无可用网络接口,只能使用udp协议.");
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

public void updateUISpeed(int conn,int downloadSpeed,int uploadSpeed){
// String string="连接数:"+conn+" 下载:"+Tools.getSizeStringKB(downloadSpeed)+"/S"
// +" 上传:"+Tools.getSizeStringKB(uploadSpeed)+"/S";
// if(downloadSpeedField!=null){
// downloadSpeedField.setText(string);
// }
}
mapClient.setUi(this);

mapClient.setMapServer(config.getServerAddress(), config.getServerPort(),config.getRemotePort() ,null,null,config.isDirect_cn(),config.getProtocal().equals("tcp"),
null);


JButton createButton(String name){
JButton button=new JButton(name);
button.setMargin(new Insets(0,5,0,5));
button.setFocusPainted(false);
return button;
}


setSpeed(config.getDownloadSpeed(),config.getUploadSpeed());

loadMapRule();

if(config.getDownloadSpeed()==0||config.getUploadSpeed()==0){
new SpeedSetFrame(ui,mainFrame);
}

void initUI(){
SwingUtilities.invokeLater(new Runnable() {

public void run() {
Font font = new Font("宋体",Font.PLAIN,12);
UIManager.put("ToolTip.font",font);
UIManager.put("Table.font",font);
UIManager.put("TableHeader.font",font);
UIManager.put("TextField.font",font);
UIManager.put("ComboBox.font",font);
UIManager.put("TextField.font",font);
UIManager.put("PasswordField.font",font);
UIManager.put("TextArea.font,font",font);
UIManager.put("TextPane.font",font);
UIManager.put("EditorPane.font",font);
UIManager.put("FormattedTextField.font",font);
UIManager.put("Button.font",font);
UIManager.put("CheckBox.font",font);
UIManager.put("RadioButton.font",font);
UIManager.put("ToggleButton.font",font);
UIManager.put("ProgressBar.font",font);
UIManager.put("DesktopIcon.font",font);
UIManager.put("TitledBorder.font",font);
UIManager.put("Label.font",font);
UIManager.put("List.font",font);
UIManager.put("TabbedPane.font",font);
UIManager.put("MenuBar.font",font);
UIManager.put("Menu.font",font);
UIManager.put("MenuItem.font",font);
UIManager.put("PopupMenu.font",font);
UIManager.put("CheckBoxMenuItem.font",font);
UIManager.put("RadioButtonMenuItem.font",font);
UIManager.put("Spinner.font",font);
UIManager.put("Tree.font",font);
UIManager.put("ToolBar.font",font);
UIManager.put("OptionPane.messageFont",font);
UIManager.put("OptionPane.buttonFont",font);

ToolTipManager.sharedInstance().setInitialDelay(200);
}

});
}

@Override
public boolean login() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean updateNode(boolean testSpeed) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isOsx_fw_pf() {
// TODO Auto-generated method stub
return false;
}

public void setMessage(String message){}

@Override
public boolean isOsx_fw_ipfw() {
// TODO Auto-generated method stub
return false;
void setSpeed(int downloadSpeed,int uploadSpeed){
config.setDownloadSpeed(downloadSpeed);
config.setUploadSpeed(uploadSpeed);
Route.localDownloadSpeed=downloadSpeed;
Route.localUploadSpeed=config.uploadSpeed;
}
}

2 changes: 1 addition & 1 deletion src/net/fs/client/ClientUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public class ClientUI implements ClientUII , WindowListener{
updateUrl="http://fs.d1sm.net/finalspeed/update.properties";
}

ClientUI(){
void run(){
systemName=System.getProperty("os.name").toLowerCase();
MLog.info("System: "+systemName+" "+System.getProperty("os.version"));
ui=this;
Expand Down
3 changes: 2 additions & 1 deletion src/net/fs/client/FSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public class FSClient {

public static void main(String[] args) {
new ClientUI();
//new ClientUI().run();
new ClientNoUI().run();
}

}

0 comments on commit aaea363

Please sign in to comment.