|
|
|
package com.zhonglai.luhui.neutrino.proxy.server.httpservice;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.sun.net.httpserver.HttpExchange;
|
|
|
|
import com.sun.net.httpserver.HttpHandler;
|
|
|
|
import com.zhonglai.luhui.neutrino.proxy.server.function.PortForwardManager;
|
|
|
|
import com.zhonglai.luhui.neutrino.proxy.server.httpservice.dto.ResponseMessage;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 端口映射操作
|
|
|
|
*/
|
|
|
|
public class PortForwardHandler implements HttpHandler {
|
|
|
|
@Override
|
|
|
|
public void handle(HttpExchange httpExchange) throws IOException {
|
|
|
|
try {
|
|
|
|
Map<String,String> params = getParamsMap(httpExchange);
|
|
|
|
String operate = params.get("operate");
|
|
|
|
switch (operate)
|
|
|
|
{
|
|
|
|
case "map": //端口映射
|
|
|
|
map(httpExchange,params);
|
|
|
|
return;
|
|
|
|
case "updateBandwidth": //限制带宽
|
|
|
|
updateBandwidth(httpExchange,params);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
response(httpExchange,new ResponseMessage("不支持的操作", 0));
|
|
|
|
}
|
|
|
|
}catch (Exception e)
|
|
|
|
{
|
|
|
|
response(httpExchange,new ResponseMessage("接口请求失败", 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void map(HttpExchange httpExchange, Map<String, String> params)throws IOException
|
|
|
|
{
|
|
|
|
|
|
|
|
int serverPort = Integer.parseInt(params.get("serverPort"));
|
|
|
|
String clientId =params.get("clientId");
|
|
|
|
int targetPort = Integer.parseInt(params.get("targetPort"));
|
|
|
|
int bandwidth = null != params.get("bandwidth") ? Integer.parseInt(params.get("bandwidth")) : 100 * 1024;
|
|
|
|
PortForwardManager.clientBandwidthMap.put(clientId, bandwidth);
|
|
|
|
|
|
|
|
new PortForwardManager().startPortForward(serverPort, clientId, targetPort);
|
|
|
|
response(httpExchange,new ResponseMessage("映射成功", 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateBandwidth(HttpExchange httpExchange, Map<String, String> params)throws IOException
|
|
|
|
{
|
|
|
|
String clientId = params.get("clientId");
|
|
|
|
int bandwidth = Integer.parseInt(params.get("bandwidth"));
|
|
|
|
PortForwardManager.clientBandwidthMap.put(clientId, bandwidth);
|
|
|
|
response(httpExchange,new ResponseMessage("带宽更新成功", 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void response(HttpExchange httpExchange, ResponseMessage responseMessage) throws IOException
|
|
|
|
{
|
|
|
|
String response = JSON.toJSONString(responseMessage);
|
|
|
|
OutputStream os = httpExchange.getResponseBody();
|
|
|
|
httpExchange.getResponseHeaders().set("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
byte[] bytes = response.getBytes(StandardCharsets.UTF_8);
|
|
|
|
httpExchange.sendResponseHeaders(200, bytes.length);
|
|
|
|
os.write(bytes);
|
|
|
|
os.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, String> getQueryParams(HttpExchange exchange) throws UnsupportedEncodingException {
|
|
|
|
String query = exchange.getRequestURI().getQuery(); // 获取 name=张三&age=18
|
|
|
|
return parseQuery(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, String> parseQuery(String query) throws UnsupportedEncodingException {
|
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
|
if (query == null || query.isEmpty()) return result;
|
|
|
|
|
|
|
|
for (String param : query.split("&")) {
|
|
|
|
String[] pair = param.split("=", 2);
|
|
|
|
if (pair.length == 2) {
|
|
|
|
result.put(URLDecoder.decode(pair[0], StandardCharsets.UTF_8.toString()),URLDecoder.decode(pair[1], StandardCharsets.UTF_8.toString()));
|
|
|
|
} else if (pair.length == 1) {
|
|
|
|
result.put(URLDecoder.decode(pair[0], StandardCharsets.UTF_8.toString()), "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, String> getParamsMap(HttpExchange httpExchange) throws IOException {
|
|
|
|
String method = httpExchange.getRequestMethod();
|
|
|
|
Map<String, String> params = null;
|
|
|
|
if ("POST".equalsIgnoreCase(method))
|
|
|
|
{
|
|
|
|
params = getPostParams(httpExchange);
|
|
|
|
} else if ("GET".equalsIgnoreCase(method))
|
|
|
|
{
|
|
|
|
params = getQueryParams(httpExchange);
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, String> getPostParams(HttpExchange exchange) throws IOException {
|
|
|
|
String body = readRequestBody(exchange);
|
|
|
|
return parseQuery(body); // 和 GET 参数一样解析
|
|
|
|
}
|
|
|
|
|
|
|
|
private String readRequestBody(HttpExchange exchange) throws IOException {
|
|
|
|
BufferedReader reader = new BufferedReader(
|
|
|
|
new InputStreamReader(exchange.getRequestBody(), StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
sb.append(line);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|