更新配置文件为yml

master
qi_liang 4 years ago
parent 9e71918466
commit ba82c51021
  1. 2
      proxy-client/src/main/java/org/fengfei/lanproxy/client/ClientChannelMannager.java
  2. 1
      proxy-client/src/main/java/org/fengfei/lanproxy/client/ProxyClientContainer.java
  3. 2
      proxy-client/src/main/resources/config.yml
  4. 120
      proxy-common/src/main/java/org/fengfei/lanproxy/common/Config.java
  5. 13
      proxy-server/src/main/java/org/fengfei/lanproxy/server/SslContextCreator.java
  6. 22
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/ProxyConfig.java
  7. 28
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Admin.java
  8. 28
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Config.java
  9. 32
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/ConfigServer.java
  10. 38
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Server.java
  11. 51
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/ServerConfig.java
  12. 78
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Ssl.java
  13. 4
      proxy-server/src/main/resources/config.yml

@ -9,7 +9,6 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import org.fengfei.lanproxy.client.config.ClientConfig;
import org.fengfei.lanproxy.client.config.Server;
import org.fengfei.lanproxy.client.listener.ProxyChannelBorrowListener;
import org.fengfei.lanproxy.common.Config;
import org.fengfei.lanproxy.protocol.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -46,7 +45,6 @@ public class ClientChannelMannager {
private static volatile Channel cmdChannel;
// private static Config config = Config.getInstance();
private static ClientConfig clientConfig = ClientConfig.getInstance();
public static void borrowProxyChanel(Bootstrap bootstrap, final ProxyChannelBorrowListener borrowListener) {

@ -54,7 +54,6 @@ public class ProxyClientContainer implements Container, ChannelStatusListener {
private Bootstrap realServerBootstrap;
// private Config config = Config.getInstance();
private ClientConfig clientConfig = ClientConfig.getInstance();
private SSLContext sslContext;

@ -1,5 +1,5 @@
client:
key: client
key: db410c93c7e14c5da03939daf21287b0
ssl:
enable: false
jksPath: test.jks

@ -1,120 +0,0 @@
package org.fengfei.lanproxy.common;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
/**
* 读取配置文件 默认的config.properties 和自定义都支持
*
*/
public class Config {
private static final String DEFAULT_CONF = "config.properties";
private static Map<String, Config> instances = new ConcurrentHashMap<String, Config>();
private Properties configuration = new Properties();
private Config() {
initConfig(DEFAULT_CONF);
}
private Config(String configFile) {
initConfig(configFile);
}
private void initConfig(String configFile) {
InputStream is = Config.class.getClassLoader().getResourceAsStream(configFile);
try {
configuration.load(is);
is.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
/**
* 获得Configuration实例 默认为config.property
*
* @return Configuration实例
*/
public static Config getInstance() {
return getInstance(DEFAULT_CONF);
}
/**
* 自定义文件解析**.property
*
* @param configFile
* @return
*/
public static Config getInstance(String configFile) {
Config config = instances.get(configFile);
if (config == null) {
synchronized (instances) {
config = instances.get(configFile);
if (config == null) {
config = new Config(configFile);
instances.put(configFile, config);
}
}
}
return config;
}
/**
* 获得配置项
*
* @param key 配置关键字
*
* @return 配置项
*/
public String getStringValue(String key) {
return configuration.getProperty(key);
}
public String getStringValue(String key, String defaultValue) {
String value = this.getStringValue(key);
if (value == null) {
return defaultValue;
} else {
return value;
}
}
public int getIntValue(String key, int defaultValue) {
return LangUtil.parseInt(configuration.getProperty(key), defaultValue);
}
public int getIntValue(String key) {
return LangUtil.parseInt(configuration.getProperty(key));
}
public double getDoubleValue(String key, Double defaultValue) {
return LangUtil.parseDouble(configuration.getProperty(key), defaultValue);
}
public double getDoubleValue(String key) {
return LangUtil.parseDouble(configuration.getProperty(key));
}
public double getLongValue(String key, Long defaultValue) {
return LangUtil.parseLong(configuration.getProperty(key), defaultValue);
}
public double getLongValue(String key) {
return LangUtil.parseLong(configuration.getProperty(key));
}
public Boolean getBooleanValue(String key, Boolean defaultValue) {
return LangUtil.parseBoolean(configuration.getProperty(key), defaultValue);
}
public Boolean getBooleanValue(String key) {
return LangUtil.parseBoolean(configuration.getProperty(key));
}
}

@ -18,7 +18,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.fengfei.lanproxy.common.Config;
import org.fengfei.lanproxy.server.config.yml.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,7 +29,9 @@ public class SslContextCreator {
public SSLContext initSSLContext() {
logger.info("Checking SSL configuration properties...");
final String jksPath = Config.getInstance().getStringValue("server.ssl.jksPath");
ServerConfig config = ServerConfig.getInstance();
final String jksPath = config.getServer().getSsl().getJksPath();
logger.info("Initializing SSL context. KeystorePath = {}.", jksPath);
if (jksPath == null || jksPath.isEmpty()) {
// key_store_password or key_manager_password are empty
@ -40,8 +42,9 @@ public class SslContextCreator {
// if we have the port also the jks then keyStorePassword and
// keyManagerPassword
// has to be defined
final String keyStorePassword = Config.getInstance().getStringValue("server.ssl.keyStorePassword");
final String keyManagerPassword = Config.getInstance().getStringValue("server.ssl.keyManagerPassword");
final String keyStorePassword = config.getServer().getSsl().getKeyStorePassword();
final String keyManagerPassword = config.getServer().getSsl().getKeyManagerPassword();
if (keyStorePassword == null || keyStorePassword.isEmpty()) {
// key_store_password or key_manager_password are empty
@ -58,7 +61,7 @@ public class SslContextCreator {
// if client authentification is enabled a trustmanager needs to be
// added to the ServerContext
boolean needsClientAuth = Config.getInstance().getBooleanValue("server.ssl.needsClientAuth", false);
boolean needsClientAuth = config.getServer().getSsl().isNeedsClientAuth();
try {
logger.info("Loading keystore. KeystorePath = {}.", jksPath);

@ -13,12 +13,10 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.fengfei.lanproxy.common.Config;
import org.fengfei.lanproxy.common.JsonUtil;
import org.fengfei.lanproxy.server.config.yml.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.reflect.TypeToken;
/**
@ -82,18 +80,22 @@ public class ProxyConfig implements Serializable {
private List<ConfigChangedListener> configChangedListeners = new ArrayList<ConfigChangedListener>();
private ProxyConfig() {
ServerConfig serverConfig = ServerConfig.getInstance();
// 代理服务器主机和端口配置初始化
this.serverPort = Config.getInstance().getIntValue("server.port");
this.serverBind = Config.getInstance().getStringValue("server.bind", "0.0.0.0");
Server server = serverConfig.getServer();
this.serverPort = server.getPort();
this.serverBind = server.getBind();
Config config = serverConfig.getConfig();
// 配置服务器主机和端口配置初始化
this.configServerPort = Config.getInstance().getIntValue("config.server.port");
this.configServerBind = Config.getInstance().getStringValue("config.server.bind", "0.0.0.0");
ConfigServer configServer = config.getServer();
this.configServerPort = configServer.getPort();
this.configServerBind = configServer.getBind();
// 配置服务器管理员登录认证信息
this.configAdminUsername = Config.getInstance().getStringValue("config.admin.username");
this.configAdminPassword = Config.getInstance().getStringValue("config.admin.password");
Admin admin = config.getAdmin();
this.configAdminUsername = admin.getUsername();
this.configAdminPassword = admin.getPassword();
logger.info(
"config init serverBind {}, serverPort {}, configServerBind {}, configServerPort {}, configAdminUsername {}, configAdminPassword {}",

@ -0,0 +1,28 @@
package org.fengfei.lanproxy.server.config.yml;
/**
* @Description TODO
* @Author qi
**/
public class Admin {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -0,0 +1,28 @@
package org.fengfei.lanproxy.server.config.yml;
/**
* @Description TODO
* @Author qi
**/
public class Config {
private Admin admin;
private ConfigServer server;
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public ConfigServer getServer() {
return server;
}
public void setServer(ConfigServer server) {
this.server = server;
}
}

@ -0,0 +1,32 @@
package org.fengfei.lanproxy.server.config.yml;
/**
* @Description TODO
* @Author qi
**/
public class ConfigServer {
/**
* 绑定IP
*/
private String bind;
/**
* 端口号
*/
private int port;
public String getBind() {
return bind;
}
public void setBind(String bind) {
this.bind = bind;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}

@ -0,0 +1,38 @@
package org.fengfei.lanproxy.server.config.yml;
/**
* @Description TODO
* @Author qi
**/
public class Server {
private String bind;
private int port;
private Ssl ssl;
public String getBind() {
return bind;
}
public void setBind(String bind) {
this.bind = bind;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public Ssl getSsl() {
return ssl;
}
public void setSsl(Ssl ssl) {
this.ssl = ssl;
}
}

@ -0,0 +1,51 @@
package org.fengfei.lanproxy.server.config.yml;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
/**
* @Description
* @Author qi
**/
public class ServerConfig {
public static final String SERVER_YML_PATH = "/config.yml";
private Server server;
private Config config;
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Config getConfig() {
return config;
}
public void setConfig(Config config) {
this.config = config;
}
public static ServerConfig getInstance(String ymlPath){
InputStream inputStream = null;
try {
inputStream = ServerConfig.class.getResourceAsStream(ymlPath);
} catch (Exception e) {
e.printStackTrace();
}
Yaml yaml = new Yaml();
ServerConfig serverConfig = yaml.loadAs(inputStream, ServerConfig.class);
return serverConfig;
}
public static ServerConfig getInstance(){
return getInstance(SERVER_YML_PATH);
}
}

@ -0,0 +1,78 @@
package org.fengfei.lanproxy.server.config.yml;
/**
* @Description TODO
* @Author qi
**/
public class Ssl {
private boolean enable;
private String bind;
private Integer port;
private String jksPath;
private String keyStorePassword;
private String keyManagerPassword;
private boolean needsClientAuth;
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public String getBind() {
return bind;
}
public void setBind(String bind) {
this.bind = bind;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getJksPath() {
return jksPath;
}
public void setJksPath(String jksPath) {
this.jksPath = jksPath;
}
public String getKeyStorePassword() {
return keyStorePassword;
}
public void setKeyStorePassword(String keyStorePassword) {
this.keyStorePassword = keyStorePassword;
}
public String getKeyManagerPassword() {
return keyManagerPassword;
}
public void setKeyManagerPassword(String keyManagerPassword) {
this.keyManagerPassword = keyManagerPassword;
}
public boolean isNeedsClientAuth() {
return needsClientAuth;
}
public void setNeedsClientAuth(boolean needsClientAuth) {
this.needsClientAuth = needsClientAuth;
}
}

@ -14,5 +14,5 @@ config:
bind: 0.0.0.0
port: 8090
admin:
username: admin
password: admin
username: liangqi
password: a445958127

Loading…
Cancel
Save