mirror of
https://github.com/xfl03/MCCustomSkinLoader.git
synced 2026-11-21 17:22:14 +00:00
Added Http Cache
Use new http request platform
This commit is contained in:
1 parent
87a40fc3c7
commit
bdead01ef9
9 files changed
+273
-145
No files matched your search
@@ -11,6 +11,7 @@ import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import customskinloader.CustomSkinLoader;
|
||||
import customskinloader.loader.ProfileLoader;
|
||||
import customskinloader.utils.HttpRequestUtil;
|
||||
import customskinloader.utils.HttpUtil0;
|
||||
|
||||
public class Config {
|
||||
@@ -46,6 +47,8 @@ public class Config {
|
||||
config.initLocalFolder();
|
||||
if(config.ignoreHttpsCertificate)
|
||||
HttpUtil0.ignoreHttpsCertificate();
|
||||
if(config.enableCacheAutoClean)
|
||||
HttpRequestUtil.CACHE_DIR.delete();
|
||||
|
||||
//Output config
|
||||
CustomSkinLoader.logger.info("Enable:"+config.enable+
|
||||
|
||||
@@ -16,8 +16,11 @@ import customskinloader.config.SkinSiteProfile;
|
||||
import customskinloader.loader.jsonapi.*;
|
||||
import customskinloader.profile.ModelManager0;
|
||||
import customskinloader.profile.UserProfile;
|
||||
import customskinloader.utils.HttpRequestUtil;
|
||||
import customskinloader.utils.HttpTextureUtil;
|
||||
import customskinloader.utils.HttpUtil0;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpRequest;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpResponce;
|
||||
|
||||
public class JsonAPILoader implements ProfileLoader.IProfileLoader {
|
||||
|
||||
@@ -62,7 +65,8 @@ public class JsonAPILoader implements ProfileLoader.IProfileLoader {
|
||||
}
|
||||
json=IOUtils.toString(new FileInputStream(jsonFile));
|
||||
}else{
|
||||
json=HttpUtil0.readHttp(jsonUrl,ssp.userAgent,type.jsonAPI.getPayload(ssp));
|
||||
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(jsonUrl).setUserAgent(ssp.userAgent).setPayload(type.jsonAPI.getPayload(ssp)));
|
||||
json=responce.content;
|
||||
}
|
||||
if(json==null||json.equals("")){
|
||||
CustomSkinLoader.logger.info("Profile not found.");
|
||||
|
||||
@@ -10,6 +10,9 @@ import customskinloader.CustomSkinLoader;
|
||||
import customskinloader.config.SkinSiteProfile;
|
||||
import customskinloader.profile.ModelManager0;
|
||||
import customskinloader.profile.UserProfile;
|
||||
import customskinloader.utils.HttpRequestUtil;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpRequest;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpResponce;
|
||||
import customskinloader.utils.HttpTextureUtil;
|
||||
import customskinloader.utils.HttpUtil0;
|
||||
|
||||
@@ -27,9 +30,9 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader {
|
||||
if(skinFile.exists()&&skinFile.isFile())
|
||||
profile.skinUrl=HttpTextureUtil.getLocalLegacyFakeUrl(skin, HttpTextureUtil.getHash(skin, skinFile.length(), skinFile.lastModified()));
|
||||
}else{
|
||||
String fakeSkinUrl=HttpUtil0.getFakeUrl(skin,ssp.userAgent);
|
||||
if(fakeSkinUrl!=null&&!fakeSkinUrl.equals(""))
|
||||
profile.skinUrl=fakeSkinUrl;
|
||||
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setUserAgent(ssp.userAgent).setLoadContent(false));
|
||||
if(responce.success)
|
||||
profile.skinUrl=HttpTextureUtil.getLegacyFakeUrl(skin);
|
||||
}
|
||||
profile.model=profile.hasSkinUrl()?ssp.model:null;
|
||||
}
|
||||
@@ -40,9 +43,9 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader {
|
||||
if(capeFile.exists()&&capeFile.isFile())
|
||||
profile.capeUrl=HttpTextureUtil.getLocalLegacyFakeUrl(cape, HttpTextureUtil.getHash(cape, capeFile.length(), capeFile.lastModified()));
|
||||
}else{
|
||||
String fakeCapeUrl=HttpUtil0.getFakeUrl(cape,ssp.userAgent);
|
||||
if(fakeCapeUrl!=null&&fakeCapeUrl.equals(""))
|
||||
profile.capeUrl=fakeCapeUrl;
|
||||
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(cape).setUserAgent(ssp.userAgent).setLoadContent(false));
|
||||
if(responce.success)
|
||||
profile.capeUrl=HttpTextureUtil.getLegacyFakeUrl(cape);
|
||||
}
|
||||
}
|
||||
if(ModelManager0.isElytraSupported()&&StringUtils.isNoneEmpty(ssp.elytra)){
|
||||
@@ -52,9 +55,9 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader {
|
||||
if(elytraFile.exists()&&elytraFile.isFile())
|
||||
profile.elytraUrl=HttpTextureUtil.getLocalLegacyFakeUrl(elytra, HttpTextureUtil.getHash(elytra, elytraFile.length(), elytraFile.lastModified()));
|
||||
}else{
|
||||
String fakeCapeUrl=HttpUtil0.getFakeUrl(elytra,ssp.userAgent);
|
||||
if(fakeCapeUrl!=null&&fakeCapeUrl.equals(""))
|
||||
profile.elytraUrl=fakeCapeUrl;
|
||||
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(elytra).setUserAgent(ssp.userAgent).setLoadContent(false));
|
||||
if(responce.success)
|
||||
profile.elytraUrl=HttpTextureUtil.getLegacyFakeUrl(elytra);
|
||||
}
|
||||
}
|
||||
if(profile.isEmpty()){
|
||||
|
||||
@@ -24,6 +24,9 @@ import customskinloader.CustomSkinLoader;
|
||||
import customskinloader.config.SkinSiteProfile;
|
||||
import customskinloader.profile.ModelManager0;
|
||||
import customskinloader.profile.UserProfile;
|
||||
import customskinloader.utils.HttpRequestUtil;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpRequest;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpResponce;
|
||||
import customskinloader.utils.HttpUtil0;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
@@ -59,11 +62,11 @@ public class MojangAPILoader implements ProfileLoader.IProfileLoader {
|
||||
|
||||
public static GameProfile loadGameProfile(String username) throws Exception{
|
||||
//Doc (http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time)
|
||||
String json0=HttpUtil0.readHttp("https://api.mojang.com/users/profiles/minecraft/"+username,null);
|
||||
if(json0==null||json0.equals(""))
|
||||
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest("https://api.mojang.com/users/profiles/minecraft/"+username).setCacheTime(0));
|
||||
if(StringUtils.isEmpty(responce.content))
|
||||
return null;
|
||||
Gson gson=new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
|
||||
GameProfile gameProfile=gson.fromJson(json0, GameProfile.class);
|
||||
GameProfile gameProfile=gson.fromJson(responce.content, GameProfile.class);
|
||||
if(gameProfile.getId()==null)
|
||||
return null;
|
||||
return new GameProfile(gameProfile.getId(),gameProfile.getName());
|
||||
|
||||
@@ -23,6 +23,9 @@ import com.mojang.authlib.properties.Property;
|
||||
import com.mojang.util.UUIDTypeAdapter;
|
||||
|
||||
import customskinloader.CustomSkinLoader;
|
||||
import customskinloader.utils.HttpRequestUtil;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpRequest;
|
||||
import customskinloader.utils.HttpRequestUtil.HttpResponce;
|
||||
import customskinloader.utils.HttpTextureUtil;
|
||||
import customskinloader.utils.HttpUtil0;
|
||||
|
||||
@@ -71,7 +74,7 @@ public class DynamicSkullManager {
|
||||
File skinFile=new File(CustomSkinLoader.DATA_DIR,skin);
|
||||
if(skinFile.isFile()&&skinFile.length()>0){
|
||||
//Skin found
|
||||
String fakeUrl=HttpTextureUtil.getLocalLegacyFakeUrl(skin, HttpTextureUtil.getHash(skin, skinFile.length(), skinFile.lastModified()));
|
||||
String fakeUrl=HttpTextureUtil.getLocalFakeUrl(skin);
|
||||
result.skins.set(i, fakeUrl);
|
||||
}else{
|
||||
//Could not find skin
|
||||
@@ -80,7 +83,8 @@ public class DynamicSkullManager {
|
||||
}
|
||||
}else{
|
||||
//Online Skin
|
||||
if(!HttpUtil0.saveHttp(skin, HttpTextureUtil.getCacheFile(FilenameUtils.getBaseName(skin)))){
|
||||
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setCacheFile(HttpTextureUtil.getCacheFile(FilenameUtils.getBaseName(skin))).setCacheTime(0).setLoadContent(false));
|
||||
if(!responce.success){
|
||||
//Could not load skin
|
||||
result.skins.remove(i--);
|
||||
continue;
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
package customskinloader.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.io.Charsets;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import customskinloader.CustomSkinLoader;
|
||||
|
||||
public class HttpRequestUtil {
|
||||
public static class HttpRequest{
|
||||
public String url;
|
||||
public String userAgent=null;
|
||||
public String payload=null;
|
||||
public boolean loadContent=true;
|
||||
|
||||
public int cacheTime=600;//-1=No Cache 0=Always Cache t=Default Cache Time(second)
|
||||
public File cacheFile=null;//Default Cache File
|
||||
|
||||
public HttpRequest(String url){
|
||||
this.url=url;
|
||||
}
|
||||
public HttpRequest setUserAgent(String userAgent){
|
||||
this.userAgent=userAgent;
|
||||
return this;
|
||||
}
|
||||
public HttpRequest setPayload(String payload){
|
||||
this.payload=payload;
|
||||
return this;
|
||||
}
|
||||
public HttpRequest setLoadContent(boolean loadContent){
|
||||
this.loadContent=loadContent;
|
||||
return this;
|
||||
}
|
||||
public HttpRequest setCacheTime(int cacheTime){
|
||||
this.cacheTime=cacheTime;
|
||||
return this;
|
||||
}
|
||||
public HttpRequest setCacheFile(File cacheFile){
|
||||
this.cacheFile=cacheFile;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public static class HttpResponce{
|
||||
public String content=null;
|
||||
public int responceCode=-1;
|
||||
public boolean success=false;
|
||||
public boolean fromCache=false;
|
||||
}
|
||||
public static class CacheInfo{
|
||||
public String url;
|
||||
public String etag=null;
|
||||
|
||||
public long lastModified=-1;//ms
|
||||
public long expire=-1;//UNIX timestamp
|
||||
}
|
||||
public static final File CACHE_DIR=new File(CustomSkinLoader.DATA_DIR,"caches");
|
||||
|
||||
public static HttpResponce makeHttpRequest(HttpRequest request){
|
||||
try{
|
||||
CustomSkinLoader.logger.info("Try to request '"+request.url+(request.userAgent==null?"'.":"' with user agent '"+request.userAgent+"'."));
|
||||
//Check Cache
|
||||
if(StringUtils.isNotEmpty(request.payload))
|
||||
request.cacheTime=-1;//No Cache
|
||||
File cacheInfoFile=null;
|
||||
CacheInfo cacheInfo=new CacheInfo();
|
||||
if(request.cacheFile==null && request.cacheTime>=0){
|
||||
String hash=DigestUtils.sha1Hex(request.url);
|
||||
request.cacheFile=new File(CACHE_DIR,hash);
|
||||
cacheInfoFile=new File(CACHE_DIR,hash+".json");
|
||||
}
|
||||
if(request.cacheTime==0 && request.cacheFile.isFile())
|
||||
return loadFromCache(request,new HttpResponce());
|
||||
if(cacheInfoFile!=null&&cacheInfoFile.isFile()){
|
||||
String json=FileUtils.readFileToString(cacheInfoFile);
|
||||
if(StringUtils.isNotEmpty(json))
|
||||
cacheInfo=CustomSkinLoader.GSON.fromJson(json, CacheInfo.class);
|
||||
if(cacheInfo==null)
|
||||
cacheInfo=new CacheInfo();
|
||||
if(cacheInfo.expire>=TimeUtil.getCurrentUnixTimestamp())
|
||||
return loadFromCache(request,new HttpResponce());
|
||||
}
|
||||
|
||||
//Init Connection
|
||||
HttpURLConnection c = (HttpURLConnection) (new URL(request.url)).openConnection();
|
||||
c.setReadTimeout(1000 * 10);
|
||||
c.setConnectTimeout(1000 * 10);
|
||||
c.setDoInput(true);
|
||||
c.setUseCaches(false);
|
||||
c.setInstanceFollowRedirects(true);
|
||||
|
||||
//Make Connection
|
||||
if(cacheInfo.lastModified>=0)
|
||||
c.setIfModifiedSince(cacheInfo.lastModified);
|
||||
if(cacheInfo.etag!=null)
|
||||
c.setRequestProperty("If-None-Match",cacheInfo.etag);
|
||||
c.setRequestProperty("Accept-Encoding","gzip");
|
||||
if(request.userAgent!=null)
|
||||
c.setRequestProperty("User-Agent",request.userAgent);
|
||||
if(StringUtils.isNotEmpty(request.payload)){
|
||||
CustomSkinLoader.logger.info("Payload: "+request.payload);
|
||||
c.setDoOutput(true);
|
||||
OutputStream os=c.getOutputStream();
|
||||
IOUtils.write(request.payload, os);
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
c.connect();
|
||||
|
||||
//Check Connection
|
||||
HttpResponce responce=new HttpResponce();
|
||||
responce.responceCode=c.getResponseCode();
|
||||
int res=c.getResponseCode()/100;
|
||||
if(res==4||res==5){//Failed
|
||||
CustomSkinLoader.logger.info("Failed to request (Response Code: "+c.getResponseCode()+")");
|
||||
return responce;
|
||||
}
|
||||
responce.success=true;
|
||||
CustomSkinLoader.logger.info("Successfully request (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")");
|
||||
if(responce.responceCode==HttpURLConnection.HTTP_NOT_MODIFIED)
|
||||
return loadFromCache(request,responce);
|
||||
|
||||
//Load Content
|
||||
InputStream is="gzip".equals(c.getContentEncoding())?new GZIPInputStream(c.getInputStream()):c.getInputStream();
|
||||
byte [] bytes = IOUtils.toByteArray(is);
|
||||
if(request.cacheFile!=null){
|
||||
FileUtils.writeByteArrayToFile(request.cacheFile, bytes);
|
||||
if(cacheInfoFile!=null){
|
||||
cacheInfo.url=request.url;
|
||||
cacheInfo.etag=c.getHeaderField("ETag");
|
||||
cacheInfo.lastModified=c.getLastModified();
|
||||
cacheInfo.expire=getExpire(c,request.cacheTime);
|
||||
FileUtils.write(cacheInfoFile, CustomSkinLoader.GSON.toJson(cacheInfo));
|
||||
}
|
||||
}
|
||||
if(!request.loadContent)
|
||||
return responce;
|
||||
responce.content=new String(bytes,Charsets.UTF_8);
|
||||
CustomSkinLoader.logger.info("Content: "+responce.content);
|
||||
return responce;
|
||||
|
||||
}catch(Exception e){
|
||||
CustomSkinLoader.logger.info("Failed to request (Exception: "+e.toString()+")");
|
||||
return loadFromCache(request,new HttpResponce());
|
||||
}
|
||||
}
|
||||
|
||||
public static File getCacheFile(String hash){
|
||||
return new File(CACHE_DIR,hash);
|
||||
}
|
||||
|
||||
private static HttpResponce loadFromCache(HttpRequest request,HttpResponce responce){
|
||||
if(request.cacheFile==null||!request.cacheFile.isFile())
|
||||
return responce;
|
||||
CustomSkinLoader.logger.info("Cache file found (Length: "+request.cacheFile.length()+" , Path: '"+request.cacheFile.getAbsolutePath()+"')");
|
||||
responce.fromCache=true;
|
||||
responce.success=true;
|
||||
if(!request.loadContent)
|
||||
return responce;
|
||||
|
||||
CustomSkinLoader.logger.info("Try to load from cache '"+request.cacheFile.getAbsolutePath()+"'.");
|
||||
try {
|
||||
responce.content=FileUtils.readFileToString(request.cacheFile);
|
||||
CustomSkinLoader.logger.info("Successfully to load from cache");
|
||||
} catch (IOException e) {
|
||||
CustomSkinLoader.logger.info("Failed to load from cache (Exception: "+e.toString()+")");
|
||||
responce.success=false;
|
||||
}
|
||||
return responce;
|
||||
}
|
||||
private final static Pattern MAX_AGE_PATTERN = Pattern.compile("max-age=(\\d+)");
|
||||
private static long getExpire(HttpURLConnection connection,int cacheTime){
|
||||
String cacheControl=connection.getHeaderField("Cache-Control");
|
||||
if(StringUtils.isNotEmpty(cacheControl)){
|
||||
cacheControl=cacheControl.replaceAll(" ", "");
|
||||
Matcher m=MAX_AGE_PATTERN.matcher(cacheControl);
|
||||
if(m!=null && m.matches())
|
||||
return TimeUtil.getUnixTimestamp(Long.parseLong(m.group(0)));
|
||||
}
|
||||
long expires=connection.getExpiration();
|
||||
if(expires>0)
|
||||
return expires/1000;
|
||||
return TimeUtil.getUnixTimestampRandomDelay(cacheTime==0 ? 86400 : cacheTime);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -35,11 +35,9 @@ public class HttpTextureUtil {
|
||||
}
|
||||
if(fakeUrl.startsWith(LOCAL_LEGACY_MARK)){
|
||||
fakeUrl=fakeUrl.replaceFirst(LOCAL_LEGACY_MARK, "");
|
||||
String[] t=fakeUrl.split(",",2);
|
||||
if(t.length!=2)
|
||||
return info;
|
||||
info.cacheFile=new File(CustomSkinLoader.DATA_DIR,t[1]);
|
||||
info.hash=t[0];
|
||||
info.url=fakeUrl;
|
||||
info.hash=DigestUtils.sha1Hex(info.url);;
|
||||
info.cacheFile=HttpTextureUtil.getCacheFile(info.hash);
|
||||
return info;
|
||||
}
|
||||
if(fakeUrl.startsWith(LOCAL_MARK)){
|
||||
@@ -61,8 +59,8 @@ public class HttpTextureUtil {
|
||||
return info;
|
||||
}
|
||||
|
||||
public static String getLegacyFakeUrl(String url,String hash){
|
||||
return LEGACY_MARK+hash+","+url;
|
||||
public static String getLegacyFakeUrl(String url){
|
||||
return LEGACY_MARK+url;
|
||||
}
|
||||
public static String getLocalFakeUrl(String path){
|
||||
return LOCAL_MARK+path;
|
||||
|
||||
@@ -5,8 +5,10 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
@@ -25,127 +27,6 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import customskinloader.CustomSkinLoader;
|
||||
|
||||
public class HttpUtil0 {
|
||||
/**
|
||||
* Read all text from http.
|
||||
* @since 13.11
|
||||
*/
|
||||
public static String readHttp(String url,String userAgent){
|
||||
return readHttp(url,userAgent,null);
|
||||
}
|
||||
public static String readHttp(String url,String userAgent,String payload){
|
||||
HttpURLConnection c=null;
|
||||
try {
|
||||
CustomSkinLoader.logger.info("Try to read '"+url+(userAgent==null?"' without user agent.":"' with user agent '"+userAgent+"'."));
|
||||
c=createConnection(url,userAgent,payload);
|
||||
int res = c.getResponseCode()/100;
|
||||
if (res==4||res==5||c.getResponseCode()==HttpURLConnection.HTTP_NO_CONTENT) {
|
||||
CustomSkinLoader.logger.info("Failed to read (Response Code: "+c.getResponseCode()+")");
|
||||
return null;
|
||||
}
|
||||
CustomSkinLoader.logger.info("Successfully read (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")");
|
||||
InputStream is = getStream(c);
|
||||
String text=IOUtils.toString(is, Charsets.UTF_8);
|
||||
CustomSkinLoader.logger.info("Content: "+text);
|
||||
return text;
|
||||
} catch (Exception e) {
|
||||
CustomSkinLoader.logger.info("Failed to read (Exception: "+e.toString()+")");
|
||||
return null;
|
||||
} finally {
|
||||
if(c!=null)
|
||||
c.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean saveHttp(String url,File target){
|
||||
HttpURLConnection c=null;
|
||||
try {
|
||||
CustomSkinLoader.logger.info("Try to save '"+url+"' to '"+target.getAbsolutePath()+"'.");
|
||||
if(target.isFile()){
|
||||
CustomSkinLoader.logger.info("Cache file found (Length: "+target.length()+")");
|
||||
return true;
|
||||
}
|
||||
c=createConnection(url,null);
|
||||
int res = c.getResponseCode()/100;
|
||||
if (res==4||res==5||c.getResponseCode()==HttpURLConnection.HTTP_NO_CONTENT) {
|
||||
CustomSkinLoader.logger.info("Failed to save (Response Code: "+c.getResponseCode()+")");
|
||||
return false;
|
||||
}
|
||||
CustomSkinLoader.logger.info("Successfully save (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")");
|
||||
InputStream is = getStream(c);
|
||||
byte [] bytes = IOUtils.toByteArray(is);
|
||||
FileUtils.writeByteArrayToFile(target,bytes);
|
||||
return target.exists();
|
||||
} catch (Exception e) {
|
||||
CustomSkinLoader.logger.info("Failed to save (Exception: "+e.toString()+")");
|
||||
return false;
|
||||
} finally {
|
||||
if(c!=null)
|
||||
c.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get faked url after redirect.
|
||||
* Using this method, can also check if it is exist.
|
||||
* @param url - before redirect
|
||||
* @return url - faked url after redirect (null for not exist)
|
||||
* @since 14.1
|
||||
*/
|
||||
public static String getFakeUrl(String url,String userAgent){
|
||||
HttpURLConnection c=null;
|
||||
try {
|
||||
CustomSkinLoader.logger.info("Try to get fake url of '"+url+"' with user agent '"+userAgent+"'.");
|
||||
c=createConnection(url,userAgent);
|
||||
int res = c.getResponseCode()/100;
|
||||
if (res==4||res==5) {
|
||||
CustomSkinLoader.logger.info("Failed to get fake url (Response Code: "+c.getResponseCode()+")");
|
||||
return null;
|
||||
}
|
||||
String url1=c.getURL().toString();
|
||||
CustomSkinLoader.logger.info("Successfully get fake url ("
|
||||
+ "Response Code: "+c.getResponseCode()+" , "
|
||||
+ "Content Length: "+c.getContentLength()+" , "
|
||||
+ "Real URL: "+url1+")");
|
||||
return HttpTextureUtil.getLegacyFakeUrl(url1, HttpTextureUtil.getHash(url1, c.getContentLength(), c.getLastModified()));
|
||||
} catch (Exception e) {
|
||||
CustomSkinLoader.logger.info("Failed to get fake url (Exception: "+e.toString()+")");
|
||||
return null;
|
||||
} finally{
|
||||
if(c!=null)
|
||||
c.disconnect();
|
||||
}
|
||||
}
|
||||
private static HttpURLConnection createConnection(String url,String userAgent) throws MalformedURLException, IOException{
|
||||
return createConnection(url,userAgent,null);
|
||||
}
|
||||
private static HttpURLConnection createConnection(String url,String userAgent,String payload) throws MalformedURLException, IOException{
|
||||
HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection();
|
||||
c.setReadTimeout(1000 * 10);
|
||||
c.setConnectTimeout(1000 * 10);
|
||||
c.setDoInput(true);
|
||||
c.setUseCaches(false);
|
||||
c.setInstanceFollowRedirects(true);
|
||||
|
||||
c.setRequestProperty("Accept-Encoding", "gzip");
|
||||
if(userAgent!=null)
|
||||
c.setRequestProperty("User-Agent",userAgent);
|
||||
if(StringUtils.isNotEmpty(payload)){
|
||||
CustomSkinLoader.logger.info("Payload: "+payload);
|
||||
c.setDoOutput(true);
|
||||
OutputStream os=c.getOutputStream();
|
||||
IOUtils.write(payload, os);
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
c.connect();
|
||||
return c;
|
||||
}
|
||||
private static InputStream getStream(HttpURLConnection connection) throws IOException{
|
||||
if ("gzip".equals(connection.getContentEncoding()))
|
||||
return new GZIPInputStream(connection.getInputStream());
|
||||
else
|
||||
return connection.getInputStream();
|
||||
}
|
||||
|
||||
public static boolean isLocal(String url){
|
||||
return url==null? false : !url.startsWith("http");
|
||||
}
|
||||
@@ -175,4 +56,31 @@ public class HttpUtil0 {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLanServer(String address){
|
||||
if(StringUtils.isEmpty(address))
|
||||
return true;
|
||||
String[] addresses=address.split(":");
|
||||
InetAddress add;
|
||||
try {
|
||||
add = InetAddress.getByName(addresses[0]);
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
return true;
|
||||
}
|
||||
int numIp=getNumIp(add.getHostAddress());
|
||||
return numIp==0||numIp==getNumIp("127.0.0.1")||
|
||||
(numIp>=getNumIp("192.168.0.0")&&numIp<=getNumIp("192.168.255.255"))||
|
||||
(numIp>=getNumIp("10.0.0.0")&&numIp<=getNumIp("10.255.255.255"))||
|
||||
(numIp>=getNumIp("172.16.0.0")&&numIp<=getNumIp("172.31.255.255"));
|
||||
}
|
||||
public static int getNumIp(String ip){
|
||||
int num=0;
|
||||
String[] ips=ip.split("\\.");
|
||||
if(ips.length!=4)
|
||||
return 0;
|
||||
for(int i=0;i<4;i++)
|
||||
num+=Integer.parseInt(ips[i])*(256^(3-i));
|
||||
return num;
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,7 @@ public class MinecraftUtil {
|
||||
net.minecraft.client.multiplayer.ServerData data=net.minecraft.client.Minecraft.getMinecraft().getCurrentServerData();
|
||||
if(data==null)//Single Player
|
||||
return true;
|
||||
return data.serverIP==null;
|
||||
return HttpUtil0.isLanServer(data.serverIP);
|
||||
}
|
||||
|
||||
private final static Pattern MINECRAFT_CORE_FILE_PATTERN = Pattern.compile("^(.*?)/versions/([^\\/\\\\]*?)/([^\\/\\\\]*?).jar$");
|
||||
|
||||
Reference in new issue
Block a user