Search in sources :

Example 1 with AgentAuthorize

use of io.jpom.system.AgentAuthorize in project Jpom by dromara.

the class AutoRegSeverNode method autoPushToServer.

/**
 * 自动推送插件端信息到服务端
 *
 * @param url 服务端url
 */
public static void autoPushToServer(String url) {
    url = StrUtil.removeSuffix(url, CharPool.SINGLE_QUOTE + "");
    url = StrUtil.removePrefix(url, CharPool.SINGLE_QUOTE + "");
    UrlBuilder urlBuilder = UrlBuilder.ofHttp(url);
    // 
    LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address -> {
        // 非loopback地址,指127.*.*.*的地址
        return !address.isLoopbackAddress() && // 需为IPV4地址
        address instanceof Inet4Address;
    });
    Set<String> ips = localAddressList.stream().map(InetAddress::getHostAddress).filter(StrUtil::isNotEmpty).collect(Collectors.toSet());
    urlBuilder.addQuery("ips", CollUtil.join(ips, StrUtil.COMMA));
    AgentAuthorize agentAuthorize = AgentAuthorize.getInstance();
    urlBuilder.addQuery("loginName", agentAuthorize.getAgentName());
    urlBuilder.addQuery("loginPwd", agentAuthorize.getAgentPwd());
    int port = ConfigBean.getInstance().getPort();
    urlBuilder.addQuery("port", port + "");
    // 
    String build = urlBuilder.build();
    String body = HttpUtil.createGet(build).execute().body();
    Console.log("push result:" + body);
}
Also used : Inet4Address(java.net.Inet4Address) AgentAuthorize(io.jpom.system.AgentAuthorize) UrlBuilder(cn.hutool.core.net.url.UrlBuilder) InetAddress(java.net.InetAddress)

Example 2 with AgentAuthorize

use of io.jpom.system.AgentAuthorize in project Jpom by dromara.

the class AutoRegSeverNode method reg.

/**
 * 向服务端注册插件端
 */
@PreLoadMethod
private static void reg() {
    AgentExtConfigBean instance = AgentExtConfigBean.getInstance();
    String agentId = instance.getAgentId();
    String serverUrl = instance.getServerUrl();
    if (StrUtil.isEmpty(agentId) || StrUtil.isEmpty(serverUrl)) {
        // 如果二者缺一不注册
        return;
    }
    String oldInstallId = null;
    File file = FileUtil.file(ConfigBean.getInstance().getDataPath(), AgentConfigBean.SERVER_ID);
    JSONObject serverJson = null;
    if (file.exists()) {
        try {
            serverJson = (JSONObject) JsonFileUtil.readJson(file.getAbsolutePath());
        } catch (FileNotFoundException e) {
            serverJson = new JSONObject();
        }
        oldInstallId = serverJson.getString("installId");
    }
    HttpRequest installRequest = instance.createServerRequest(ServerOpenApi.INSTALL_ID);
    String body1 = installRequest.execute().body();
    JsonMessage jsonMessage = JSON.parseObject(body1, JsonMessage.class);
    if (jsonMessage.getCode() != HttpStatus.HTTP_OK) {
        DefaultSystemLog.getLog().error("获取Server 安装id失败:" + jsonMessage);
        return;
    }
    String installId = jsonMessage.dataToString();
    boolean eqInstall = StrUtil.equals(oldInstallId, installId);
    // 
    URL url = URLUtil.toUrlForHttp(instance.getAgentUrl());
    String protocol = url.getProtocol();
    HttpRequest serverRequest = instance.createServerRequest(ServerOpenApi.UPDATE_NODE_INFO);
    serverRequest.form("id", agentId);
    serverRequest.form("name", "节点:" + agentId);
    serverRequest.form("openStatus", 1);
    serverRequest.form("protocol", protocol);
    serverRequest.form("url", url.getHost() + CharPool.COLON + url.getPort());
    AgentAuthorize agentAuthorize = AgentAuthorize.getInstance();
    serverRequest.form("loginName", agentAuthorize.getAgentName());
    serverRequest.form("loginPwd", agentAuthorize.getAgentPwd());
    serverRequest.form("type", eqInstall ? "update" : "add");
    String body = serverRequest.execute().body();
    DefaultSystemLog.getLog().info("自动注册Server:" + body);
    JsonMessage regJsonMessage = JSON.parseObject(body, JsonMessage.class);
    if (regJsonMessage.getCode() == HttpStatus.HTTP_OK) {
        if (serverJson == null) {
            serverJson = new JSONObject();
        }
        if (!eqInstall) {
            serverJson.put("installId", installId);
            serverJson.put("regTime", DateTime.now().toString());
        } else {
            serverJson.put("updateTime", DateTime.now().toString());
        }
        JsonFileUtil.saveJson(file.getAbsolutePath(), serverJson);
    } else {
        DefaultSystemLog.getLog().error("自动注册插件端失败:{}", body);
    }
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject) AgentAuthorize(io.jpom.system.AgentAuthorize) JsonMessage(cn.jiangzeyin.common.JsonMessage) FileNotFoundException(java.io.FileNotFoundException) AgentExtConfigBean(io.jpom.system.AgentExtConfigBean) File(java.io.File) URL(java.net.URL) PreLoadMethod(cn.jiangzeyin.common.PreLoadMethod)

Aggregations

AgentAuthorize (io.jpom.system.AgentAuthorize)2 UrlBuilder (cn.hutool.core.net.url.UrlBuilder)1 HttpRequest (cn.hutool.http.HttpRequest)1 JsonMessage (cn.jiangzeyin.common.JsonMessage)1 PreLoadMethod (cn.jiangzeyin.common.PreLoadMethod)1 JSONObject (com.alibaba.fastjson.JSONObject)1 AgentExtConfigBean (io.jpom.system.AgentExtConfigBean)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 Inet4Address (java.net.Inet4Address)1 InetAddress (java.net.InetAddress)1 URL (java.net.URL)1