From d669be0a8e215dcee8056271f101a4ad4bf2dcd4 Mon Sep 17 00:00:00 2001 From: qi_liang Date: Wed, 30 Oct 2019 23:38:22 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=96=B0=E5=A2=9E=E6=B3=A8=E8=A7=86=202.?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E5=8F=AF=E8=83=BD=E4=BC=9A?= =?UTF-8?q?=E6=8A=9B=E5=BC=82=E5=B8=B8=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fengfei/lanproxy/protocol/Constants.java | 3 + .../server/config/web/HttpRequestHandler.java | 7 +- .../server/config/web/routes/RouteConfig.java | 355 +++++++++--------- 3 files changed, 189 insertions(+), 176 deletions(-) diff --git a/proxy-protocol/src/main/java/org/fengfei/lanproxy/protocol/Constants.java b/proxy-protocol/src/main/java/org/fengfei/lanproxy/protocol/Constants.java index ac095e6..3510502 100644 --- a/proxy-protocol/src/main/java/org/fengfei/lanproxy/protocol/Constants.java +++ b/proxy-protocol/src/main/java/org/fengfei/lanproxy/protocol/Constants.java @@ -3,6 +3,9 @@ package org.fengfei.lanproxy.protocol; import io.netty.channel.Channel; import io.netty.util.AttributeKey; +/** + * 常量类 + */ public interface Constants { AttributeKey NEXT_CHANNEL = AttributeKey.newInstance("nxt_channel"); diff --git a/proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/HttpRequestHandler.java b/proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/HttpRequestHandler.java index d0c23e0..e2b733b 100644 --- a/proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/HttpRequestHandler.java +++ b/proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/HttpRequestHandler.java @@ -34,6 +34,11 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler 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 - } - } - 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 clients = JsonUtil.json2object(config, new TypeToken>() { - }); - 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() { - - @Override - public ResponseInfo request(FullHttpRequest request) { - byte[] buf = new byte[request.content().readableBytes()]; - request.content().readBytes(buf); - String config = new String(buf); - Map loginParams = JsonUtil.json2object(config, new TypeToken>() { - }); - 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"); - } - - if (username.equals(ProxyConfig.getInstance().getConfigAdminUsername()) && password.equals(ProxyConfig.getInstance().getConfigAdminPassword())) { - token = UUID.randomUUID().toString().replace("-", ""); - return ResponseInfo.build(token); - } - - 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"); - } - }); - - ApiRoute.addRoute("/metrics/get", new RequestHandler() { - - @Override - public ResponseInfo request(FullHttpRequest request) { - return ResponseInfo.build(MetricsCollector.getAllMetrics()); - } - }); - - ApiRoute.addRoute("/metrics/getandreset", new RequestHandler() { - - @Override - public ResponseInfo request(FullHttpRequest request) { - return ResponseInfo.build(MetricsCollector.getAndResetAllMetrics()); - } - }); - } - -} +package org.fengfei.lanproxy.server.config.web.routes; + +import java.nio.charset.Charset; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.fengfei.lanproxy.common.JsonUtil; +import org.fengfei.lanproxy.server.ProxyChannelManager; +import org.fengfei.lanproxy.server.config.ProxyConfig; +import org.fengfei.lanproxy.server.config.ProxyConfig.Client; +import org.fengfei.lanproxy.server.config.web.ApiRoute; +import org.fengfei.lanproxy.server.config.web.RequestHandler; +import org.fengfei.lanproxy.server.config.web.RequestMiddleware; +import org.fengfei.lanproxy.server.config.web.ResponseInfo; +import org.fengfei.lanproxy.server.config.web.exception.ContextException; +import org.fengfei.lanproxy.server.metrics.MetricsCollector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.gson.reflect.TypeToken; + +import io.netty.channel.Channel; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpHeaders; + +/** + * 接口实现 + * + * @author fengfei + * + */ +public class RouteConfig { + + /** + * 登陆url + */ + public static final String LOGIN_URL = "/login"; + + protected static final String AUTH_COOKIE_KEY = "token"; + + private static Logger logger = LoggerFactory.getLogger(RouteConfig.class); + + /** 管理员不能同时在多个地方登录 */ + private static String token; + + public static void init() { + + ApiRoute.addMiddleware(new RequestMiddleware() { + + @Override + public void preRequest(FullHttpRequest request) { + String cookieHeader = request.headers().get(HttpHeaders.Names.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(HttpHeaders.Names.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.getUri()) && !authenticated) { + throw new ContextException(ResponseInfo.CODE_UNAUTHORIZED); + } + + logger.info("handle request for api {}", request.getUri()); + } + }); + + // 获取配置详细信息 + ApiRoute.addRoute("/config/detail", new RequestHandler() { + + @Override + public ResponseInfo request(FullHttpRequest request) { + List 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 + } + } + 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 clients = JsonUtil.json2object(config, new TypeToken>() { + }); + 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() { + + @Override + public ResponseInfo request(FullHttpRequest request) { + byte[] buf = new byte[request.content().readableBytes()]; + request.content().readBytes(buf); + String config = new String(buf); + Map loginParams = JsonUtil.json2object(config, new TypeToken>() { + }); + 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"); + } + + if (username.equals(ProxyConfig.getInstance().getConfigAdminUsername()) && password.equals(ProxyConfig.getInstance().getConfigAdminPassword())) { + token = UUID.randomUUID().toString().replace("-", ""); + return ResponseInfo.build(token); + } + + 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"); + } + }); + + ApiRoute.addRoute("/metrics/get", new RequestHandler() { + + @Override + public ResponseInfo request(FullHttpRequest request) { + return ResponseInfo.build(MetricsCollector.getAllMetrics()); + } + }); + + ApiRoute.addRoute("/metrics/getandreset", new RequestHandler() { + + @Override + public ResponseInfo request(FullHttpRequest request) { + return ResponseInfo.build(MetricsCollector.getAndResetAllMetrics()); + } + }); + } + +}