forked from 4379711/lol-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/yalong/site/services/LinkLeagueClientService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |