|
|
@ -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()); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|