Skip to content

Commit

Permalink
打通lol客户端API接口
Browse files Browse the repository at this point in the history
  • Loading branch information
4379711 committed Feb 12, 2022
1 parent 17a7f4e commit 4e2e9d0
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
<maven.compiler.target>8</maven.compiler.target>
<fastjson.version>1.2.79</fastjson.version>
<lombok.version>1.18.22</lombok.version>
<okhttp.version>4.9.3</okhttp.version>
<!-- 在这里定义程序入口类 -->
<mainClass>LeagueClientHelper</mainClass>
</properties>

<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/yalong/site/LeagueClientHelper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package yalong.site;

import yalong.site.bo.LeagueClientBO;
import yalong.site.services.LinkLeagueClientService;
import yalong.site.utils.ProcessUtil;
import yalong.site.utils.RequestUtil;

import java.io.IOException;

/**
* @author yaLong
*/
public class LeagueClientHelper {
public static void main(String[] args) {
System.out.print("hello");
public static void main(String[] args) throws IOException {
LeagueClientBO leagueClientBO = ProcessUtil.getClientProcess();
LinkLeagueClientService leagueClientService = new LinkLeagueClientService();
RequestUtil requestUtil = new RequestUtil(leagueClientBO);
leagueClientService.setRequestUtil(requestUtil);
String loginInfo = leagueClientService.getLoginInfo();
}
}
13 changes: 13 additions & 0 deletions src/main/java/yalong/site/bo/LeagueClientBO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package yalong.site.bo;

import lombok.Data;

/**
* @author yaLong
*/
@Data
public class LeagueClientBO {
private String port;

private String token;
}
13 changes: 13 additions & 0 deletions src/main/java/yalong/site/services/LinkLeagueClientService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package yalong.site.services;

import lombok.Data;
import yalong.site.utils.RequestUtil;

import java.io.IOException;

/**
* 连接lol客户端服务
*
* @author yaLong
*/
@Data
public class LinkLeagueClientService {
private RequestUtil requestUtil;

public String getLoginInfo() throws IOException {
String s = requestUtil.doGet("/lol-login/v1/session");
System.out.println(s);
return s;
}

}
50 changes: 50 additions & 0 deletions src/main/java/yalong/site/utils/ProcessUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
package yalong.site.utils;

import yalong.site.bo.LeagueClientBO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* windows进程处理工具
*
* @author yaLong
*/
public class ProcessUtil {
public static Pattern appPortPattern = Pattern.compile("--app-port=(\\d+)");
public static Pattern tokenPattern = Pattern.compile("--remoting-auth-token=(\\w+)");

/**
* 通过进程名查询出进程的启动命令,解析出需要的客户端token和端口
*/
public static LeagueClientBO getClientProcess() throws IOException {
String cmd = "WMIC PROCESS WHERE name=\"LeagueClientUx.exe\" GET commandline";
BufferedReader reader = null;
Process process = null;
LeagueClientBO leagueClientBO = new LeagueClientBO();
try {
process = Runtime.getRuntime().exec(cmd);
// windows 命令必须gbk编码
reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gb2312"));
String line;

while ((line = reader.readLine()) != null) {
Matcher appPortMatcher = appPortPattern.matcher(line);
Matcher tokenPatternMatcher = tokenPattern.matcher(line);
if (tokenPatternMatcher.find()) {
leagueClientBO.setToken(tokenPatternMatcher.group(1));
}
if (appPortMatcher.find()) {
leagueClientBO.setPort(appPortMatcher.group(1));
}

}
return leagueClientBO;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
if (process != null) {
process.getErrorStream().close();
process.getOutputStream().close();
}
}

}
}
108 changes: 108 additions & 0 deletions src/main/java/yalong/site/utils/RequestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package yalong.site.utils;

import lombok.Data;
import okhttp3.*;
import yalong.site.bo.LeagueClientBO;

import javax.net.ssl.*;
import java.io.IOException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
* 网络请求工具
*
* @author yaLong
*/
@Data
public class RequestUtil {
private final String dataUrl = "https://127.0.0.1:2999/liveclientdata/allgamedata";
private String baseUrl;
private OkHttpClient client = myHttpClient();
private Headers defaultHeaders;

public RequestUtil(LeagueClientBO bo) {
baseUrl = "https://127.0.0.1:" + bo.getPort();
String basic = Credentials.basic("riot", bo.getToken());
defaultHeaders = new Headers.Builder()
.add("Content-Type", "'application/json")
.add("Accept", "application/json")
.add("Authorization", basic)
.build();
}

public String doGet(String endpoint) throws IOException {
Request request = new Request.Builder()
.url(baseUrl + endpoint)
.get()
.headers(defaultHeaders)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}

private OkHttpClient myHttpClient() {
return new OkHttpClient.Builder()
.sslSocketFactory(getSslSocketFactory(), getX509TrustManager())
.hostnameVerifier(getHostnameVerifier())
.retryOnConnectionFailure(Boolean.TRUE)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
}

private static SSLSocketFactory getSslSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, getTrustManager(), new SecureRandom());
return sslContext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static TrustManager[] getTrustManager() {
return new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
}

public static HostnameVerifier getHostnameVerifier() {
return (s, sslSession) -> true;
}

public static X509TrustManager getX509TrustManager() {
X509TrustManager trustManager = null;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
trustManager = (X509TrustManager) trustManagers[0];
} catch (Exception e) {
e.printStackTrace();
}

return trustManager;
}

}

0 comments on commit 4e2e9d0

Please sign in to comment.