Frist Version

This commit is contained in:
babytomas
2016-05-27 11:18:25 +08:00
parent 9988c60b02
commit 590d54be87
43 changed files with 23215 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
<?php
define('DB_NAME', ''); //数据库名
define('DB_USER', ''); //数据库账户
define('DB_PASS', ''); //数据库密码
define('DB_HOST', 'localhost'); //数据库主机

View File

@@ -0,0 +1,391 @@
<?php
//加载依赖
require_once 'configuration.php';
//获取端口号
function shadowsocks_port(){
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
mysql_select_db(DB_NAME, $mysql);
$port = mysql_query("SELECT port FROM user order by port desc limit 1");
$port = mysql_fetch_assoc($port);
$port = $port['port'] + 1;
if($port > 65535){
$result = "Error";
}else{
$result = $port;
}
}
return $result;
}
//新建账户,传入密码和流量值(MB)
function shadowsocks_create($pid,$passwd,$traffic){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}elseif(empty($passwd)){
$result = array(
'status' => 'Error',
'result' => 'Undefined password value.'
);
}elseif(empty($traffic)){
$result = array(
'status' => 'Error',
'result' => 'Undefined traffic value.'
);
}else{
if(shadowsocks_port() != "Error"){
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
$port = shadowsocks_port();
mysql_select_db(DB_NAME, $mysql);
$check = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$check = mysql_fetch_assoc($check);
if($check != ""){
$result = array(
'status' => 'Error',
'result' => 'Account already exists.'
);
}else{
$traffic = $traffic * 1048576; //按照 GB 为流量单位
$create = mysql_query("INSERT INTO user(pid,passwd,port,transfer_enable) VALUES ('".$pid."','".$passwd."','".$port."','".$traffic."')");
if(!$create){
$result = array(
'status' => 'Error',
'result' => 'MySQL query failed.'
);
}else{
$result = array(
'status' => 'Success',
'result' => $port
);
}
}
}
}else{
$result = array(
'status' => 'Error',
'result' => 'Port exceeds the maximum value.'
);
}
}
return $result;
}
//暂停账户,传入端口号
function shadowsocks_suspend($pid){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}else{
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
mysql_select_db(DB_NAME, $mysql);
$check = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$check = mysql_fetch_assoc($check);
if($check == ""){
$result = array(
'status' => 'Error',
'result' => 'No data found.'
);
}else{
$passwd = md5(time().rand(0,100));
$suspend = mysql_query("UPDATE user SET passwd='".$passwd."' WHERE pid='".$pid."'");
if(!$suspend){
$result = array(
'status' => 'Error',
'result' => 'MySQL query failed.'
);
}else{
$result = array(
'status' => 'Success',
'result' => 'Account successfully suspend.'
);
}
}
}
}
return $result;
}
//解除暂停,传入密码和端口号
function shadowsocks_unsuspend($pid,$passwd){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}elseif(empty($passwd)){
$result = array(
'status' => 'Error',
'result' => 'Undefined password value.'
);
}else{
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
mysql_select_db(DB_NAME, $mysql);
$check = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$check = mysql_fetch_assoc($check);
if($check == ""){
$result = array(
'status' => 'Error',
'result' => 'No data found.'
);
}else{
$unsuspend = mysql_query("UPDATE user SET passwd='".$passwd."' WHERE pid='".$pid."'");
if(!$unsuspend){
$result = array(
'status' => 'Error',
'result' => 'MySQL query failed.'
);
}else{
$result = array(
'status' => 'Success',
'result' => 'Account successfully unsuspend.'
);
}
}
}
}
return $result;
}
//终止账户,传入端口号
function shadowsocks_terminate($pid){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}else{
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
mysql_select_db(DB_NAME, $mysql);
$check = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$check = mysql_fetch_assoc($check);
if($check == ""){
$result = array(
'status' => 'Error',
'result' => 'No data found.'
);
}else{
$terminate = mysql_query("DELETE FROM user WHERE pid='".$pid."'");
if(!$terminate){
$result = array(
'status' => 'Error',
'result' => 'MySQL query failed.'
);
}else{
$result = array(
'status' => 'Success',
'result' => 'Account successfully terminated.'
);
}
}
}
}
return $result;
}
//修改套餐,传入流量值和端口号(MB)
function shadowsocks_changepackage($pid,$traffic){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}elseif(empty($traffic)){
$result = array(
'status' => 'Error',
'result' => 'Undefined traffic value.'
);
}else{
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
$traffic = $traffic * 1048576;
mysql_select_db(DB_NAME, $mysql);
$check = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$check = mysql_fetch_assoc($check);
if($check == ""){
$result = array(
'status' => 'Error',
'result' => 'No data found.'
);
}else{
$changepackage = mysql_query("UPDATE user SET transfer_enable='".$traffic."' WHERE pid='".$pid."'");
if(!$changepackage){
$result = array(
'status' => 'Error',
'result' => 'MySQL query failed.'
);
}else{
$result = array(
'status' => 'Success',
'result' => 'Account successfully modified.'
);
}
}
}
}
return $result;
}
//更改密码,传入密码和端口号
function shadowsocks_changepassword($pid,$passwd){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}elseif(empty($passwd)){
$result = array(
'status' => 'Error',
'result' => 'Undefined password value.'
);
}else{
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
mysql_select_db(DB_NAME, $mysql);
$check = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$check = mysql_fetch_assoc($check);
if($check == ""){
$result = array(
'status' => 'Error',
'result' => 'No data found.'
);
}else{
$changepassword = mysql_query("UPDATE user SET passwd='".$passwd."' WHERE pid='".$pid."'");
if(!$changepassword){
$result = array(
'status' => 'Error',
'result' => 'MySQL query failed.'
);
}else{
$result = array(
'status' => 'Success',
'result' => 'Password reset complete.'
);
}
}
}
}
return $result;
}
//重置流量,传入端口号
function shadowsocks_reset($pid){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}else{
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
mysql_select_db(DB_NAME, $mysql);
$check = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$check = mysql_fetch_array($check);
if($check == ""){
$result = array(
'status' => 'Error',
'result' => 'No data found.'
);
}else{
$reset = mysql_query("UPDATE user SET u='0',d='0' WHERE pid='".$pid."'");
if(!$reset){
$result = array(
'status' => 'Error',
'result' => 'MySQL query failed.'
);
}else{
$result = array(
'status' => 'Success',
'result' => 'Account Reset success.'
);
}
}
}
}
return $result;
}
//查询账户,传入端口号
function shadowsocks_query($pid){
if(empty($pid)){
$result = array(
'status' => 'Error',
'result' => 'Undefined product id value.'
);
}else{
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql){
$result = array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
);
}else{
mysql_select_db(DB_NAME, $mysql);
$query = mysql_query("SELECT * FROM user WHERE pid='".$pid."'");
$query = mysql_fetch_assoc($query);
if($query == ""){
$result = array(
'status' => 'Error',
'result' => 'No data found.'
);
}else{
$result = array(
'port' => $query['port'],
'upload' => $query['u'],
'download' => $query['d'],
'last_time' => $query['t']
);
}
}
}
return $result;
}

14
Server/API/cron.php Normal file
View File

@@ -0,0 +1,14 @@
<?php
//加载依赖
require_once 'config/configuration.php';
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$mysql) {
die(json_encode(array(
'status' => 'Error',
'result' => 'Unable to connect to database.'
)));
} else {
mysql_select_db(DB_NAME, $mysql);
mysql_query("UPDATE `user` SET `u` = '0', `d` = '0';");
}

1
Server/API/crontab.txt Normal file
View File

@@ -0,0 +1 @@
0 0 1 * * php -q /home/wwwroot/api.hhh.com/cron.php

Binary file not shown.

View File

@@ -0,0 +1,53 @@
<?php
require_once 'config/function.php';
if(empty($_POST['action'])){
die(json_encode(array(
'status' => 'Error',
'result' => 'Undefined value.'
)));
}else{
if(!empty($_POST['password'])){
$passwd = $_POST['password'];
}
if(!empty($_POST['traffic'])){
$traffic = $_POST['traffic'];
}
if(!empty($_POST['pid'])){
$pid = $_POST['pid'];
}
$action = $_POST['action'];
if($action == "create"){
$create = @shadowsocks_create($pid, $passwd, $traffic);
echo json_encode($create);
}elseif($action == "terminate"){
$terminate = @shadowsocks_terminate($pid);
echo json_encode($terminate);
}elseif($action == "suspend"){
$suspend = @shadowsocks_suspend($pid);
echo json_encode($suspend);
}elseif($action == "unsuspend"){
$unsuspend = @shadowsocks_unsuspend($pid, $passwd);
echo json_encode($unsuspend);
}elseif($action == "changepassword"){
$changepassword = @shadowsocks_changepassword($pid, $passwd);
echo json_encode($changepassword);
}elseif($action == "changepackage"){
$changepackage = @shadowsocks_changepackage($pid, $traffic);
echo json_encode($changepackage);
}elseif($action == "reset"){
$reset = @shadowsocks_reset($pid);
}elseif ($action == "query"){
$query = @shadowsocks_query($pid);
echo json_encode($query);
}else{
die(json_encode(array(
'status' => 'Error',
'result' => 'Undefined value.'
)));
}
}