全新更新

仿CDN模式更新
This commit is contained in:
Bigsk 2020-08-09 22:08:44 +08:00 committed by Gitee
parent 9d810e808c
commit 8adccda6c6
2 changed files with 150 additions and 0 deletions

39
download.py Normal file
View File

@ -0,0 +1,39 @@
import os,requests,json,time,sys,argparse,psutil
Log=""
UA={'User-Agent':'GeMCAPI/4.0.0 (GeMC API Sync 1.0.0;Release)'}
parser=argparse.ArgumentParser()
parser.add_argument("--url", help="The online url of the file which need to download.")
parser.add_argument("--file", help="The file path of the file which need to download.")
parser.add_argument("--other", help="Other parameter which help some special file to download correctly.")
parser=parser.parse_args()
proxies={'http':'http://127.0.0.1:4780'}
def log(log_type,logvar):
global Log
if(log_type=="info"):
log_type="INFO"
elif(log_type=="warn"):
log_type="WARN"
elif(log_type=="error"):
log_type="ERROR"
Log=Log + '\n' + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[' + log_type + ']' + logvar
def download(url, dirpath):
global Log
global UA
log("info",'We are downloading the file ' + dirpath + '.')
try:
download=requests.get(url, headers=UA, proxies=proxies)
with open(dirpath,"wb") as code:
code.write(download.content)
except:
log("error",'There has a error in task while downloading the file ' + dirpath + '.')
else:
log("info",'The file ' + dirpath + ' has been downloaded successfully.')
def make_dir_exist(dirpath):
global Log
if os.path.exists(dirpath):
log("info",'The dir ' + dirpath + ' has already existed.Nothing to do.')
else:
log("info",'The dir ' + dirpath + ' does not exist.We will create one.')
os.makedirs(dirpath)
make_dir_exist(parser.file[::-1].split('/', 1)[-1][::-1])
download(parser.url,parser.file)

111
index.php Normal file
View File

@ -0,0 +1,111 @@
<?php
$active=array('mojang/launchermeta/mc/game/version_manifest.json');
$type=array('.com');
//[0]:mojang
function url_exists($url) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code == 200) {
return true;
}
return false;
}
function url_exists_2($url){
$opts = array(
'http'=>array(
'timeout'=>3,
)
);
$context = stream_context_create($opts);
return @file_get_contents($url, false, $context);
}
function get_main_domain($file){
$str=preg_replace('/^[^\/]*\//is','',$file);
$str2=str_replace('/'.$str,'',$file);
return $str2;
}
function get_sub_domain($file){
$str=preg_replace('/^[^\/]*\//is','',$file);
$str2=preg_replace('/^[^\/]*\//is','',$str);
$str3=str_replace('/'.$str2,'',$str);
return $str3;
}
function get_file_path($file){
$str=preg_replace('/^[^\/]*\//is','',$file);
$str2=preg_replace('/^[^\/]*\//is','',$str);
return $str2;
}
function get_online_url($file,$id){
global $type;
$str='http://'.get_sub_domain($file).'.'.get_main_domain($file).$type[$id].'/'.get_file_path($file);
return $str;
}
function check_file_update($file,$dir,$url){
$time=time()-filemtime($file);
if($time<86400){//检查文件是否超出过期期限1天86400秒
echo file_get_contents($file);
}else{
if(url_exists($url.'/'.str_replace($dir,'',$file))){//防止源无法访问而损坏文件
if(file_get_contents($url.'/'.str_replace($dir,'',$file))==file_get_contents($file)){
echo file_get_contents($file);
}else{
echo file_get_contents($url.'/'.str_replace($dir,'',$file));
$jsonwrite = fopen($file, "w") or die('{"wikis":"https://gitee.com/ghink/gemcapi/wikis","code":502,"server_time":"'.date("Y-m-d h:i:s").'"}');
fwrite($jsonwrite,file_get_contents($url.'/'.str_replace($dir,'',$file)));
fclose($jsonwrite);
}
}else{
echo file_get_contents($file);
}
}
}
function download_file($url,$file,$other){//下载文件
system('python download.py --url "'.$url.'" --file "'.$file.'" --other "'.$other.'"');
}
if($_GET['error']!=""){
echo '{"wikis":"https://gitee.com/ghink/gemcapi/wikis","code":'.$_GET['error'].',"server_time":"'.date("Y-m-d h:i:s").'"}';
}elseif($_GET['path']==""){
echo '{"wikis":"https://gitee.com/ghink/gemcapi/wikis","code":200,"server_time":"'.date("Y-m-d h:i:s").'"}';
}else{
if(is_file($_GET['path'])){
//文件存在
if(in_array($_GET['path'],$active)){//判断是否为需要保持最新的文件
if(strstr($_GET['path'],'mojang/')!=False){//目录分组
if(strstr($_GET['path'],'mojang/launchermeta/')!=False){
$dir='mojang/launchermeta/';
check_file_update('mojang/launchermeta/mc/game/version_manifest.json',$dir,'http://launchermeta.mojang.com');
}
}
}else{//无需更新的文件,直接重定向
header('Location: http://1.gemcapi.ghink.net:10000/'.$_GET['path']);
}
}elseif(is_dir($_GET['path'])){
//目录存在
header('Location: http://1.gemcapi.ghink.net:10000/'.$_GET['path']);
}else{
//目录或文件不存在
if(strstr($_GET['path'],'forge/')!=False){//特殊Forge采用二级源
if(url_exists_2('https://download.mcbbs.net/maven/net/minecraftforge/forge/1.7.10-10.13.0.1151/forge-1.7.10-10.13.0.1151-installer.jar')){
download_file('https://download.mcbbs.net/'.$_GET['path'],$_GET['path'],'');
header('Location: http://1.gemcapi.ghink.net:10000/'.$_GET['path']);
}else{
echo '{"wikis":"https://gitee.com/ghink/gemcapi/wikis","code":404,"server_time":"'.date("Y-m-d h:i:s").'"}';
}
}else{
if(get_main_domain($_GET['path'])=='mojang'){//分配顶级域名尾
$id=0;
}
if(url_exists(get_online_url($_GET['path'],$id))){
download_file(get_online_url($_GET['path'],$id),$_GET['path'],'');
header('Location: http://1.gemcapi.ghink.net:10000/'.$_GET['path']);
}else{
echo '{"wikis":"https://gitee.com/ghink/gemcapi/wikis","code":404,"server_time":"'.date("Y-m-d h:i:s").'"}';
}
}
}
}