forked from xiaozhenliang/shadowsocks-manage-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwooleTask.php
130 lines (120 loc) · 4.84 KB
/
SwooleTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
$file_path = dirname(__FILE__).'/swoole_config.json';
$json_string = file_get_contents($file_path);
$config = json_decode($json_string);
$service_port = $config->service_port;
$address = $config->address;
$dsn = $config->dsn; //构造数据源,mysql是数据类型,localhost是主机地址,shadow_manage是数据库名称
$db_user = $config->db_user; //数据库用户名
$db_password = $config->db_password; //登录数据库的密码
$interval_time = $config->interval_time; //向SSServer添加端口号的间隔时间
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function(swoole_client $cli) use($dsn,$db_user,$db_password) {
$db = new PDO($dsn,$db_user,$db_password);
$sql = 'select * from members';
$query = $db->query($sql);
$query->setfetchmode(pdo::FETCH_ASSOC); //设置数组关联方式
$result = $query->fetchAll();
$db = null;
// mysql_close($con);
if(!empty($result)){
foreach($result as $array){
$attr = array(
'server_port' => (int)$array['port'],
'password' => $array['password']
);
$jsonAttr = 'add:'.json_encode($attr);
$cli->send($jsonAttr);
}
}
});
$client->on("receive", function(swoole_client $cli, $data){
echo "Receive: $data\n";
// stat: {"8001":11370}
sleep(1);
});
$client->on("error", function(swoole_client $cli){
echo "error\n";
});
$client->on("close", function(swoole_client $cli){
echo "Connection close\n";
});
$client->connect($address, $service_port);
//每50秒遍历一遍数据库,把所有端口都添加到ssserver中,从第50秒开始
swoole_timer_tick($interval_time,function() use($service_port,$address,$dsn,$db_user,$db_password){
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function(swoole_client $cli) use($dsn,$db_user,$db_password){
$db = new PDO($dsn,$db_user,$db_password);
$sql = 'select * from members';
$query = $db->query($sql);
$query->setfetchmode(pdo::FETCH_ASSOC); //设置数组关联方式
$result = $query->fetchAll();
$db = null;
// mysql_close($con);
if(!empty($result)){
foreach($result as $array){
$attr = array(
'server_port' => (int)$array['port'],
'password' => $array['password']
);
$jsonAttr = 'add:'.json_encode($attr);
$cli->send($jsonAttr);
}
}
});
$client->on("receive", function(swoole_client $cli, $data) use($dsn,$db_user,$db_password){
echo "Receive: $data\n";
updateData($data,$dsn,$db_user,$db_password);
sleep(1);
});
$client->on("error", function(swoole_client $cli){
echo "error\n";
});
$client->on("close", function(swoole_client $cli){
echo "Connection close\n";
});
$client->connect($address, $service_port);
});
/**
* 更新端口号的数据流量
*/
function updateData($json,$dsn,$db_user,$db_password){
$stat = 'stat: ';
if(!strstr($json, $stat)){
return;
}
$json = str_replace($stat, '', $json);
$db = new PDO($dsn,$db_user,$db_password);
$array = json_decode($json);
if(empty($array)){
return;
}
foreach ($array as $key => $value) {
try{
//向数据库中记录一条流量
date_default_timezone_set('PRC');
$currentTime = "'".date('Y-m-d H:i:s',time())."'";
$values = $key.','.time().','.$currentTime.','.$value;
$sqlFlow = 'insert into flows(port,time,date_time,flow) values('.$values.')';
$db->exec($sqlFlow);
echo $sqlFlow;
//向数据库中记录总流量
$sql = 'select * from members where port='.$key;
$query = $db->query($sql);
$query->setfetchmode(pdo::FETCH_ASSOC); //设置数组关联方式
$result = $query->fetchAll();
if(!empty($result)){
foreach($result as $data){
echo "数据流量是:$value";
$flow = $data['flow']+$value;
$update = 'update members set flow = '
.$flow.' where port = '.$data['port'];
$db->exec($update);
// echo "\n查询的结果是".$value['port'];
}
}
}catch(PDOException $e){
}
};
$db = null;
}