先把代码放出来,其他的有时间再更新(Doge脸)
PHP版服务端脚本
不推荐使用这种方式,推荐使用以后更新的Python脚本。。。
下载地址:http://pan.baidu.com/s/1o6YlCHs
下载后传到服务端,后面自己领悟吧
也可以复制下面代码,都是一样的:
<?php
/*
*******************************
* 树洞外链远程上传服务端(1.0) *
* 树洞主页:https://aoaoao.me *
* E-mail:abslant@126.com *
*******************************
*/
error_reporting(0);
if($_SERVER['REQUEST_METHOD'] == "OPTIONS"){
header("Access-Control-Allow-Origin: *");
exit();
}
//重要!:请在下方填写您的Token
$token = "QTwESDhxb36SiyPszNxQZ8DHEMPFMej4";
$fileAction = $_POST['action'];
switch ($fileAction) {
case '':
header("Access-Control-Allow-Origin: *");
$uploadToken = explode("|",$_POST['token']);
$uploadConfig = base64_decode($uploadToken[0]);
if(md5($token.$uploadConfig) != $uploadToken[1]){
header('HTTP/1.1 400 Bad Request');
$result["error"] = urlencode("非法Token");
echo urldecode(json_encode($result));
exit();
}
$uploadOpinion = json_decode($uploadConfig);
if(!empty($_FILES["file"]["name"])){
$fileEnd = explode(",",$uploadOpinion -> fileEnd);
$fileType = explode(",",$uploadOpinion -> fileType);
$fileExplode = explode(".",$_FILES["file"]["name"]);
if($fileType[0] == "*"){
$fileType = array($_FILES["file"]["type"],"");
}
if(!(in_array($_FILES["file"]["type"],$fileType) & in_array(end($fileExplode),$fileEnd))){
header('HTTP/1.1 400 Bad Request');
$result["error"] = urlencode("不支持此文件类型");
echo urldecode(json_encode($result));
exit();
}else if($_FILES["file"]["size"] > $uploadOpinion -> fileSize){
header('HTTP/1.1 400 Bad Request');
$result["error"] = urlencode("文件尺寸超出限制");
echo urldecode(json_encode($result));
exit();
}else if($_FILES["file"]["error"] > 0){
header('HTTP/1.1 400 Bad Request');
$result["error"] = $_FILES["file"]["error"];
echo json_encode($result);
exit();
}
if ($uploadOpinion -> autoName == "true"){
$arrayName = array('{date}' => date('YmjGis'),'{rand4}' => rand(1000,9999),'{rand8}' => rand(10000000,99999999) ,'{time}' => time());
if(end($fileExplode)=="php"){
exit();
}
$fileNmae = strtr($uploadOpinion -> nameRule,$arrayName).".".end($fileExplode);
}else{
$fileNmae = $_FILES["file"]["name"];
}
if(file_exists(dirname(__FILE__)."/".$uploadOpinion -> fileDir."/".$fileNmae)){
header('HTTP/1.1 614 Bad Request');
$result["error"] = urlencode("文件重名");
echo urldecode(json_encode($result));
exit();
}else{
move_uploaded_file($_FILES["file"]["tmp_name"],dirname(__FILE__)."/".$uploadOpinion -> fileDir."/".$fileNmae);
$result["hash"]="";
$result["key"]=urlencode($fileNmae);
header('HTTP/1.1 200 OK');
echo urldecode(json_encode($result));
}
}
break;
case 'delete':
if(md5($_POST['filename']."delete".$token) != $_POST['token']){
exit();
}
$deleteAction = @unlink (dirname(__FILE__)."/".$_POST['filedir']."/".$_POST['filename']);
if($deleteAction){
echo "ok";
} else {
echo "bad";
}
case 'info':
if(md5($_POST['filename']."info".$token) != $_POST['token']){
exit();
}
$fileAction = fopen(dirname(__FILE__)."/".$_POST['filedir']."/".$_POST['filename'],"r");
$fileInfo = fstat($fileAction);
echo $fileInfo['size']."."."暂不提供";
fclose($fileAction);
default:
# code...
break;
}
?>`</pre>
## Python版服务端脚本
Python版脚本更稳定,理论上可上传无限大的文件(PHP文件大小有限制),所以我更推崇这种服务端。
**安装说明:**
我将以举例的方式说明Python版服务端安装.
1.首先要在服务器上安装Python,推荐2.7版本,其他版本未做兼容性测试。教程大家可以百度
2.安装pip(已安装可以跳过)
[右键这里另存为](https://bootstrap.pypa.io/get-pip.py) 并再终端中运行即可自动下载安装。
3.安装Flask(已安装可以跳过)
执行以下命令自动安装:
<pre class="lang:sh decode:true">sudo pip install Flask</pre>
4.上传、配置远程脚本
比如我有个剩余空间很大的服务器,已经装好了Nginx或其他服务器软件(这是必须的),绑定的域名是f.aoaoao.me,
网站目录位于/home/wwwroot/default/下。而我想把文件都传到网站的data目录下,并以http://f.aoaoao.me/data/文件名 的形式外链出去。下面就以这种情形说明服务端的配置
先在/home/wwwroot/default/目录下创建data目录。
然后把下面的脚本保存为fileServer.py
<pre title="服务端脚本 python">`#!/usr/bin/python
'''
SDshare Server Script For Python
Ver:1.0
E-mail:abslant@126.com
'''
from flask import Flask,request,make_response,abort
from werkzeug import secure_filename
import json,base64,hashlib,time,random,os
from os.path import join, getsize
app = Flask(__name__)
#please enter your token here:
token = 'YOUR TOKEN';
def getdirsize(dir):
size = 0L
for root, dirs, files in os.walk(dir):
size += sum([getsize(join(root, name)) for name in files])
return size
def decode_base64(data):
missing_padding = 4 - len(data) % 4
if missing_padding:
data += b'='* missing_padding
return base64.decodestring(data)
@app.route('/',methods=['GET', 'POST', 'OPTIONS'])
def fileServer():
if request.method == 'OPTIONS':
response = make_response("OPTIONS")
response.headers['Access-Control-Allow-Origin'] = '*'
return response
elif request.method == 'GET':
return 'Shudong File Server For Python <br>Ver:1.0'
elif request.method == 'POST':
fileAction = request.form.get('action','')
if fileAction == '':
upToken = request.form.get('token','').split('|')
uploadConfig = json.loads(decode_base64(upToken[0]))
if hashlib.md5(token+decode_base64(upToken[0])).hexdigest().lower() <> upToken[1]:
resContent = "{\"error\":\"bad token\"}"
code = 400
else:
upFlie = request.files['file']
if uploadConfig['autoName'] == 'true':
fileName = uploadConfig['nameRule'].replace("{date}", time.strftime("%Y%m%d%H%M%S",time.localtime(time.time())))
fileName = fileName.replace("{time}", str(time.time()).replace(".",""))
fileName = fileName.replace("{rand4}", str(random.randint(1000, 9999)))
fileName = fileName.replace("{rand8}", str(random.randint(10000000, 99999999)))
filrType = secure_filename(upFlie.filename).split(".")
fileName = fileName+"."+filrType[-1]
else:
fileName = secure_filename(upFlie.filename)
if os.path.exists(uploadConfig['fileDir']+"/"+fileName):
resContent = "{\"error\":\"file exists\"}"
code = 400
else:
ALLOWED_EXTENSIONS = uploadConfig['fileEnd'].split(",")
fileCheck = '.' in fileName and fileName.split('.')[1] in ALLOWED_EXTENSIONS
if fileCheck:
upFlie.save(uploadConfig['fileDir']+"/"+fileName)
filrSizeNow = os.path.getsize(uploadConfig['fileDir']+"/"+fileName)
if filrSizeNow>uploadConfig['fileSize']:
os.remove(uploadConfig['fileDir']+"/"+fileName)
resContent = "{\"error\":\" The file is too big\"}"
code = 400
else:
resContent = "{\"hash\":\" no\",\"key\":\""+fileName+"\"}"
code = 200
else:
resContent = "{\"error\":\" bad request\"}"
code = 400
response = make_response(resContent,code)
response.headers['Access-Control-Allow-Origin'] = '*'
return response
elif fileAction == "info":
if hashlib.md5(request.form.get('filename','')+"info"+token).hexdigest().lower() <> request.form.get('token',''):
resContent = "{\"error\":\"bad token\"}"
response = make_response("badtoken",200)
response.headers['Access-Control-Allow-Origin'] = '*'
return response
else:
fileSizeGet = os.path.getsize(request.form.get('filedir','')+"/"+request.form.get('filename',''))
response = make_response(str(fileSizeGet)+"."+"None",200)
response.headers['Access-Control-Allow-Origin'] = '*'
return response
elif fileAction == "delete":
if hashlib.md5(request.form.get('filename','')+"delete"+token).hexdigest().lower() <> request.form.get('token',''):
resContent = "{\"error\":\"bad token\"}"
response = make_response("badtoken",200)
response.headers['Access-Control-Allow-Origin'] = '*'
return response
else:
os.remove(request.form.get('filedir','')+"/"+request.form.get('filename',''))
response = make_response("ok",200)
response.headers['Access-Control-Allow-Origin'] = '*'
return response
if __name__ == '__main__':
#change port here:
app.run(port=8000,host='0.0.0.0')
或者点击这里直接下载脚本,打开树洞外链的后台,添加上传方案,把自动生成的Token复制下来并填到脚本第13行,保存,然后使用其他方式比如ftp,上传到/home/wwwroot/目录下(注意不要传到default目录下,否则有安全隐患)
然后把终端切换到wwwroot目录,并运行脚本:
cd /home/wwwroot
python fileServer.py
如果看到下面提示,说明脚本运行成功了
其中的8000是默认的服务端端口,如果被占用可以在第97行更改。
验证外网是否能访问,打开f.aoaoao.me:8000如果出现下面提示,就表示远程端口部署成功:
然后上传方案的配置例子: