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:
key: db410c93c7e14c5da03939daf21287b0
key: 67cb61ca7bc840d5b32a4f970b83de10
ssl:
enable: false
jksPath: test.jks
keyStorePassword: 98c93fdc78f34e0c8619c32bcf65ff7a
server:
host: 106.12.122.216
host: 121.4.203.66
port: 4900

@ -51,9 +51,9 @@ public class ProxyChannelManager {
public synchronized void onChanged() {
Iterator<Entry<String, Channel>> ite = cmdChannels.entrySet().iterator();
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();
// 去除已经去掉的clientKey配置
Set<String> clientKeySet = ProxyConfig.getInstance().getClientKeySet();
if (!clientKeySet.contains(clientKey)) {

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

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

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

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

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

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

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

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

Loading…
Cancel
Save