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. 55
      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,12 +45,12 @@ 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)->{
@Override
public void preRequest(FullHttpRequest request) {
String cookieHeader = request.headers().get(HttpHeaderNames.COOKIE); String cookieHeader = request.headers().get(HttpHeaderNames.COOKIE);
boolean authenticated = false; boolean authenticated = false;
if (cookieHeader != null) { if (cookieHeader != null) {
@ -72,38 +72,29 @@ public class RouteConfig {
authenticated = true; authenticated = true;
} }
} }
if (!LOGIN_URL.equals(request.uri()) && !authenticated) { if (!LOGIN_URL.equals(request.uri()) && !authenticated) {
throw new ContextException(ResponseInfo.CODE_UNAUTHORIZED); throw new ContextException(ResponseInfo.CODE_UNAUTHORIZED);
} }
logger.info("handle request for api {}", request.uri()); logger.info("handle request for api {}", request.uri());
}
}); });
// 获取配置详细信息 // 获取配置详细信息
ApiRoute.addRoute("/config/detail", new RequestHandler() { ApiRoute.addRoute("/config/detail",(request)->{
@Override
public ResponseInfo request(FullHttpRequest request) {
List<Client> clients = ProxyConfig.getInstance().getClients(); List<Client> clients = ProxyConfig.getInstance().getClients();
for (Client client : clients) { clients.forEach(client -> {
Channel channel = ProxyChannelManager.getCmdChannel(client.getClientKey()); Channel channel = ProxyChannelManager.getCmdChannel(client.getClientKey());
if (channel != null) { if (channel != null) {
client.setStatus(1);// online client.setStatus(1);// online
} else { } else {
client.setStatus(0);// offline 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)->{
@Override
public ResponseInfo request(FullHttpRequest request) {
byte[] buf = new byte[request.content().readableBytes()]; byte[] buf = new byte[request.content().readableBytes()];
request.content().readBytes(buf); request.content().readBytes(buf);
String config = new String(buf, Charset.forName("UTF-8")); String config = new String(buf, Charset.forName("UTF-8"));
@ -121,13 +112,9 @@ public class RouteConfig {
} }
return ResponseInfo.build(ResponseInfo.CODE_OK, "success"); return ResponseInfo.build(ResponseInfo.CODE_OK, "success");
}
}); });
ApiRoute.addRoute("/login", new RequestHandler() { ApiRoute.addRoute("/login",(request)->{
@Override
public ResponseInfo request(FullHttpRequest request) {
byte[] buf = new byte[request.content().readableBytes()]; byte[] buf = new byte[request.content().readableBytes()];
request.content().readBytes(buf); request.content().readBytes(buf);
String config = new String(buf); String config = new String(buf);
@ -149,33 +136,23 @@ public class RouteConfig {
} }
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 //退出登陆
public ResponseInfo request(FullHttpRequest request) { ApiRoute.addRoute("/logout",(request)->{
token = null; token = null;
return ResponseInfo.build(ResponseInfo.CODE_OK, "success"); return ResponseInfo.build(ResponseInfo.CODE_OK, "success");
}
}); });
ApiRoute.addRoute("/metrics/get", new RequestHandler() { ApiRoute.addRoute("/metrics/get",(request)->{
@Override
public ResponseInfo request(FullHttpRequest request) {
return ResponseInfo.build(MetricsCollector.getAllMetrics()); return ResponseInfo.build(MetricsCollector.getAllMetrics());
}
}); });
ApiRoute.addRoute("/metrics/getandreset", new RequestHandler() { ApiRoute.addRoute("/metrics/getandreset",(request)->{
@Override
public ResponseInfo request(FullHttpRequest request) {
return ResponseInfo.build(MetricsCollector.getAndResetAllMetrics()); 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