jdk8语法优化

master
qi_liang 4 years ago
parent 31f3bed666
commit 33b370b5c3
  1. 4
      proxy-client/src/main/resources/config.yml
  2. 4
      proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyChannelManager.java
  3. 13
      proxy-server/src/main/java/org/fengfei/lanproxy/server/SslContextCreator.java
  4. 179
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/routes/RouteConfig.java
  5. 10
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Admin.java
  6. 10
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Config.java
  7. 2
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/ConfigServer.java
  8. 2
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Server.java
  9. 30
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/yml/Ssl.java
  10. 2
      proxy-server/src/main/resources/config.yml

@ -1,9 +1,9 @@
client: client:
key: db410c93c7e14c5da03939daf21287b0 key: 67cb61ca7bc840d5b32a4f970b83de10
ssl: ssl:
enable: false enable: false
jksPath: test.jks jksPath: test.jks
keyStorePassword: 98c93fdc78f34e0c8619c32bcf65ff7a keyStorePassword: 98c93fdc78f34e0c8619c32bcf65ff7a
server: server:
host: 106.12.122.216 host: 121.4.203.66
port: 4900 port: 4900

@ -51,9 +51,9 @@ public class ProxyChannelManager {
public synchronized void onChanged() { public synchronized void onChanged() {
Iterator<Entry<String, Channel>> ite = cmdChannels.entrySet().iterator(); Iterator<Entry<String, Channel>> ite = cmdChannels.entrySet().iterator();
while (ite.hasNext()) { while (ite.hasNext()) {
Channel proxyChannel = ite.next().getValue(); Entry<String, Channel> entry = ite.next();
Channel proxyChannel = entry.getValue();
String clientKey = proxyChannel.attr(CHANNEL_CLIENT_KEY).get(); String clientKey = proxyChannel.attr(CHANNEL_CLIENT_KEY).get();
// 去除已经去掉的clientKey配置 // 去除已经去掉的clientKey配置
Set<String> clientKeySet = ProxyConfig.getInstance().getClientKeySet(); Set<String> clientKeySet = ProxyConfig.getInstance().getClientKeySet();
if (!clientKeySet.contains(clientKey)) { if (!clientKeySet.contains(clientKey)) {

@ -18,7 +18,9 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.TrustManagerFactory;
import org.fengfei.lanproxy.server.config.yml.Server;
import org.fengfei.lanproxy.server.config.yml.ServerConfig; import org.fengfei.lanproxy.server.config.yml.ServerConfig;
import org.fengfei.lanproxy.server.config.yml.Ssl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -30,8 +32,9 @@ public class SslContextCreator {
public SSLContext initSSLContext() { public SSLContext initSSLContext() {
logger.info("Checking SSL configuration properties..."); logger.info("Checking SSL configuration properties...");
ServerConfig config = ServerConfig.getInstance(); ServerConfig config = ServerConfig.getInstance();
Server server = config.getServer();
final String jksPath = config.getServer().getSsl().getJksPath(); Ssl ssl = server.getSsl();
final String jksPath = ssl.getJksPath();
logger.info("Initializing SSL context. KeystorePath = {}.", jksPath); logger.info("Initializing SSL context. KeystorePath = {}.", jksPath);
if (jksPath == null || jksPath.isEmpty()) { if (jksPath == null || jksPath.isEmpty()) {
// key_store_password or key_manager_password are empty // key_store_password or key_manager_password are empty
@ -43,8 +46,8 @@ public class SslContextCreator {
// keyManagerPassword // keyManagerPassword
// has to be defined // has to be defined
final String keyStorePassword = config.getServer().getSsl().getKeyStorePassword(); final String keyStorePassword = ssl.getKeyStorePassword();
final String keyManagerPassword = config.getServer().getSsl().getKeyManagerPassword(); final String keyManagerPassword = ssl.getKeyManagerPassword();
if (keyStorePassword == null || keyStorePassword.isEmpty()) { if (keyStorePassword == null || keyStorePassword.isEmpty()) {
// key_store_password or key_manager_password are empty // key_store_password or key_manager_password are empty
@ -61,7 +64,7 @@ public class SslContextCreator {
// if client authentification is enabled a trustmanager needs to be // if client authentification is enabled a trustmanager needs to be
// added to the ServerContext // added to the ServerContext
boolean needsClientAuth = config.getServer().getSsl().isNeedsClientAuth(); boolean needsClientAuth = ssl.isNeedsClientAuth();
try { try {
logger.info("Loading keystore. KeystorePath = {}.", jksPath); logger.info("Loading keystore. KeystorePath = {}.", jksPath);

@ -45,137 +45,114 @@ public class RouteConfig {
/** 管理员不能同时在多个地方登录 */ /** 管理员不能同时在多个地方登录 */
private static String token; private static String token;
/**
* 初始化
*/
public static void init() { public static void init() {
ApiRoute.addMiddleware(new RequestMiddleware() { ApiRoute.addMiddleware((request)->{
String cookieHeader = request.headers().get(HttpHeaderNames.COOKIE);
@Override boolean authenticated = false;
public void preRequest(FullHttpRequest request) { if (cookieHeader != null) {
String cookieHeader = request.headers().get(HttpHeaderNames.COOKIE); String[] cookies = cookieHeader.split(";");
boolean authenticated = false; for (String cookie : cookies) {
if (cookieHeader != null) { String[] cookieArr = cookie.split("=");
String[] cookies = cookieHeader.split(";"); if (AUTH_COOKIE_KEY.equals(cookieArr[0].trim())) {
for (String cookie : cookies) { if (cookieArr.length == 2 && cookieArr[1].equals(token)) {
String[] cookieArr = cookie.split("="); authenticated = true;
if (AUTH_COOKIE_KEY.equals(cookieArr[0].trim())) {
if (cookieArr.length == 2 && cookieArr[1].equals(token)) {
authenticated = true;
}
} }
} }
} }
}
String auth = request.headers().get(HttpHeaderNames.AUTHORIZATION); String auth = request.headers().get(HttpHeaderNames.AUTHORIZATION);
if (!authenticated && auth != null) { if (!authenticated && auth != null) {
String[] authArr = auth.split(" "); String[] authArr = auth.split(" ");
if (authArr.length == 2 && authArr[0].equals(ProxyConfig.getInstance().getConfigAdminUsername()) && authArr[1].equals(ProxyConfig.getInstance().getConfigAdminPassword())) { if (authArr.length == 2 && authArr[0].equals(ProxyConfig.getInstance().getConfigAdminUsername()) && authArr[1].equals(ProxyConfig.getInstance().getConfigAdminPassword())) {
authenticated = true; authenticated = true;
}
}
if (!LOGIN_URL.equals(request.uri()) && !authenticated) {
throw new ContextException(ResponseInfo.CODE_UNAUTHORIZED);
} }
logger.info("handle request for api {}", request.uri());
} }
if (!LOGIN_URL.equals(request.uri()) && !authenticated) {
throw new ContextException(ResponseInfo.CODE_UNAUTHORIZED);
}
logger.info("handle request for api {}", request.uri());
}); });
// 获取配置详细信息 // 获取配置详细信息
ApiRoute.addRoute("/config/detail", new RequestHandler() { ApiRoute.addRoute("/config/detail",(request)->{
List<Client> clients = ProxyConfig.getInstance().getClients();
@Override clients.forEach(client -> {
public ResponseInfo request(FullHttpRequest request) { Channel channel = ProxyChannelManager.getCmdChannel(client.getClientKey());
List<Client> clients = ProxyConfig.getInstance().getClients(); if (channel != null) {
for (Client client : clients) { client.setStatus(1);// online
Channel channel = ProxyChannelManager.getCmdChannel(client.getClientKey()); } else {
if (channel != null) { client.setStatus(0);// offline
client.setStatus(1);// online
} else {
client.setStatus(0);// offline
}
} }
return ResponseInfo.build(ProxyConfig.getInstance().getClients()); });
} return ResponseInfo.build(ProxyConfig.getInstance().getClients());
}); });
// 更新配置 // 更新配置
ApiRoute.addRoute("/config/update", new RequestHandler() { ApiRoute.addRoute("/config/update",(request)->{
byte[] buf = new byte[request.content().readableBytes()];
@Override request.content().readBytes(buf);
public ResponseInfo request(FullHttpRequest request) { String config = new String(buf, Charset.forName("UTF-8"));
byte[] buf = new byte[request.content().readableBytes()]; List<Client> clients = JsonUtil.json2object(config, new TypeToken<List<Client>>() {
request.content().readBytes(buf); });
String config = new String(buf, Charset.forName("UTF-8")); if (clients == null) {
List<Client> clients = JsonUtil.json2object(config, new TypeToken<List<Client>>() { return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error json config");
});
if (clients == null) {
return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error json config");
}
try {
ProxyConfig.getInstance().update(config);
} catch (Exception ex) {
logger.error("config update error", ex);
return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, ex.getMessage());
}
return ResponseInfo.build(ResponseInfo.CODE_OK, "success");
} }
});
ApiRoute.addRoute("/login", new RequestHandler() { try {
ProxyConfig.getInstance().update(config);
@Override } catch (Exception ex) {
public ResponseInfo request(FullHttpRequest request) { logger.error("config update error", ex);
byte[] buf = new byte[request.content().readableBytes()]; return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, ex.getMessage());
request.content().readBytes(buf); }
String config = new String(buf);
Map<String, String> loginParams = JsonUtil.json2object(config, new TypeToken<Map<String, String>>() {
});
if (loginParams == null) {
return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error login info");
}
String username = loginParams.get("username"); return ResponseInfo.build(ResponseInfo.CODE_OK, "success");
String password = loginParams.get("password"); });
if (username == null || password == null) {
return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error username or password");
}
if (username.equals(ProxyConfig.getInstance().getConfigAdminUsername()) && password.equals(ProxyConfig.getInstance().getConfigAdminPassword())) { ApiRoute.addRoute("/login",(request)->{
token = UUID.randomUUID().toString().replace("-", ""); byte[] buf = new byte[request.content().readableBytes()];
return ResponseInfo.build(token); request.content().readBytes(buf);
} String config = new String(buf);
Map<String, String> loginParams = JsonUtil.json2object(config, new TypeToken<Map<String, String>>() {
});
if (loginParams == null) {
return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error login info");
}
String username = loginParams.get("username");
String password = loginParams.get("password");
if (username == null || password == null) {
return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error username or password"); return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error username or password");
} }
});
ApiRoute.addRoute("/logout", new RequestHandler() {
@Override if (username.equals(ProxyConfig.getInstance().getConfigAdminUsername()) && password.equals(ProxyConfig.getInstance().getConfigAdminPassword())) {
public ResponseInfo request(FullHttpRequest request) { token = UUID.randomUUID().toString().replace("-", "");
token = null; return ResponseInfo.build(token);
return ResponseInfo.build(ResponseInfo.CODE_OK, "success");
} }
});
ApiRoute.addRoute("/metrics/get", new RequestHandler() { return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS, "Error username or password");
@Override
public ResponseInfo request(FullHttpRequest request) {
return ResponseInfo.build(MetricsCollector.getAllMetrics());
}
}); });
ApiRoute.addRoute("/metrics/getandreset", new RequestHandler() { //退出登陆
ApiRoute.addRoute("/logout",(request)->{
token = null;
return ResponseInfo.build(ResponseInfo.CODE_OK, "success");
});
@Override ApiRoute.addRoute("/metrics/get",(request)->{
public ResponseInfo request(FullHttpRequest request) { return ResponseInfo.build(MetricsCollector.getAllMetrics());
return ResponseInfo.build(MetricsCollector.getAndResetAllMetrics());
}
}); });
ApiRoute.addRoute("/metrics/getandreset",(request)->{
return ResponseInfo.build(MetricsCollector.getAndResetAllMetrics());
});
} }
} }

@ -1,13 +1,17 @@
package org.fengfei.lanproxy.server.config.yml; package org.fengfei.lanproxy.server.config.yml;
/** /**
* @Description TODO * @Description 登陆账号
* @Author qi * @Author qi
**/ **/
public class Admin { public class Admin {
/**
* 账号
*/
private String username; private String username;
/**
* 密码
*/
private String password; private String password;
public String getUsername() { public String getUsername() {

@ -1,13 +1,17 @@
package org.fengfei.lanproxy.server.config.yml; package org.fengfei.lanproxy.server.config.yml;
/** /**
* @Description TODO * @Description 配置视图
* @Author qi * @Author qi
**/ **/
public class Config { public class Config {
/**
* 登陆用户
*/
private Admin admin; private Admin admin;
/**
* 服务器配置
*/
private ConfigServer server; private ConfigServer server;
public Admin getAdmin() { public Admin getAdmin() {

@ -1,7 +1,7 @@
package org.fengfei.lanproxy.server.config.yml; package org.fengfei.lanproxy.server.config.yml;
/** /**
* @Description TODO * @Description 服务器配置
* @Author qi * @Author qi
**/ **/
public class ConfigServer { public class ConfigServer {

@ -1,7 +1,7 @@
package org.fengfei.lanproxy.server.config.yml; package org.fengfei.lanproxy.server.config.yml;
/** /**
* @Description TODO * @Description 服务器配置
* @Author qi * @Author qi
**/ **/
public class Server { public class Server {

@ -1,23 +1,37 @@
package org.fengfei.lanproxy.server.config.yml; package org.fengfei.lanproxy.server.config.yml;
/** /**
* @Description TODO * @Description ssl配置
* @Author qi * @Author qi
**/ **/
public class Ssl { public class Ssl {
/**
* 是否开启ssl
*/
private boolean enable; private boolean enable;
/**
* 绑定IP地址
*/
private String bind; private String bind;
/**
* 端口号
*/
private Integer port; private Integer port;
/**
* jsk文件路径
*/
private String jksPath; private String jksPath;
/**
* key密码
*/
private String keyStorePassword; private String keyStorePassword;
/**
* key管理密码
*/
private String keyManagerPassword; private String keyManagerPassword;
/**
* 需要客户端身份验证
*/
private boolean needsClientAuth; private boolean needsClientAuth;
public boolean isEnable() { public boolean isEnable() {

@ -2,7 +2,7 @@ server:
bind: 0.0.0.0 bind: 0.0.0.0
port: 4900 port: 4900
ssl: ssl:
enable: true enable: false
bind: 0.0.0.0 bind: 0.0.0.0
port: 4993 port: 4993
jksPath: test.jks jksPath: test.jks

Loading…
Cancel
Save