SyncRadius.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Command;
  3. use App\Models\User;
  4. use App\Models\Node;
  5. use App\Models\RadiusRadPostauth;
  6. use App\Models\RadiusRadAcct;
  7. use App\Models\RadiusNas;
  8. use App\Services\Config;
  9. use App\Services\Mail;
  10. use App\Models\TrafficLog;
  11. use App\Utils\Tools;
  12. use App\Utils\Radius;
  13. use App\Utils\Da;
  14. class SyncRadius
  15. {
  16. public static function synclogin()
  17. {
  18. if (Config::get('enable_radius')=="false") {
  19. return;
  20. }
  21. $tempuserbox=array();
  22. $users = User::all();
  23. foreach ($users as $user) {
  24. $email=$user->email;
  25. $email=Radius::GetUserName($email);
  26. $tempuserbox[$email]=$user->id;
  27. }
  28. /*$tempnodebox=array();
  29. $nodes = Node::all();
  30. foreach($nodes as $node){
  31. if(strpos($node->name,"Shadowsocks")!=FALSE)
  32. {
  33. $ip=gethostbyname($node->server);
  34. $tempnodebox[$ip]=$node->id;
  35. }
  36. }*/
  37. $logs = RadiusRadPostauth::where('authdate', '<', date("Y-m-d H:i:s"))->where('authdate', '>', date("Y-m-d H:i:s", time()-60))->get();
  38. foreach ($logs as $log) {
  39. if (isset($tempuserbox[$log->username])) {
  40. $traffic = new TrafficLog();
  41. $traffic->user_id = $tempuserbox[$log->username];
  42. $traffic->u = 0;
  43. $traffic->d = 10000;
  44. $traffic->node_id = 1;
  45. $traffic->rate = 1;
  46. $traffic->traffic = Tools::flowAutoShow(10000);
  47. $traffic->log_time = time();
  48. $traffic->save();
  49. $user=User::find($tempuserbox[$log->username]);
  50. $user->t = time();
  51. $user->u = $user->u + 0;
  52. $user->d = $user->d + 10000;
  53. $user->save();
  54. }
  55. }
  56. /*$stmt = $db->query("SELECT * FROM `radacct` WHERE `acctstoptime`<'".date("Y-m-d H:i:s")."' AND `acctstoptime`>'".date("Y-m-d H:i:s",time()-60)."'");
  57. $result = $stmt->fetchAll();
  58. foreach($result as $row)
  59. {
  60. $traffic = new TrafficLog();
  61. $traffic->user_id = $tempuserbox[$row["username"]];
  62. $traffic->u = $row["acctinputoctets"];
  63. $traffic->d = $row["acctoutputoctets"];
  64. $traffic->node_id = 150;
  65. $traffic->rate = 1;
  66. $traffic->traffic = Tools::flowAutoShow(($row["acctinputoctets"]+$row["acctoutputoctets"])/1024/1024);
  67. $traffic->log_time = time();
  68. $traffic->save();
  69. $user->t = time();
  70. $user->u = $user->u + $row["acctinputoctets"];
  71. $user->d = $user->d + $row["acctoutputoctets"];
  72. $user->save();
  73. } */
  74. }
  75. public static function syncvpn()
  76. {
  77. if (Config::get('radius_db_host')=="") {
  78. return;
  79. }
  80. $tempuserbox=array();
  81. $users = User::all();
  82. foreach ($users as $user) {
  83. $email=$user->email;
  84. $email=Radius::GetUserName($email);
  85. $tempuserbox[$email]=$user->id;
  86. }
  87. /*$tempnodebox=array();
  88. $nodes = Node::all();
  89. foreach($nodes as $node){
  90. if(strpos($node->name,"Shadowsocks")!=FALSE)
  91. {
  92. $ip=gethostbyname($node->server);
  93. $tempnodebox[$ip]=$node->id;
  94. }
  95. }*/
  96. /*$stmt = $db->query("SELECT * FROM `radpostauth` WHERE `authdate`<'".date("Y-m-d H:i:s")."' AND`authdate`>'".date("Y-m-d H:i:s",time()-60)."'");
  97. $result = $stmt->fetchAll();
  98. foreach($result as $row)
  99. {
  100. //if($row["pass"]!="")
  101. {
  102. $traffic = new TrafficLog();
  103. $traffic->user_id = $tempuserbox[$row["username"]];
  104. $traffic->u = 0;
  105. $traffic->d = 10000;
  106. $traffic->node_id = 149;
  107. $traffic->rate = 1;
  108. $traffic->traffic = Tools::flowAutoShow(10000);
  109. $traffic->log_time = time();
  110. $traffic->save();
  111. $user->t = time();
  112. $user->u = $user->u + 0;
  113. $user->d = $user->d + 10000;
  114. $user->save();
  115. }
  116. }
  117. */
  118. $logs = RadiusRadAcct::where('acctstoptime', '<', date("Y-m-d H:i:s"))->where('acctstoptime', '>', date("Y-m-d H:i:s", time()-60))->get();
  119. foreach ($logs as $log) {
  120. $traffic = new TrafficLog();
  121. $traffic->user_id = $tempuserbox[$log->username];
  122. $traffic->u = $log->acctinputoctets;
  123. $traffic->d = $log->acctoutputoctets;
  124. $traffic->node_id = 2;
  125. $traffic->rate = 1;
  126. $traffic->traffic = Tools::flowAutoShow(($log->acctinputoctets + $log->acctoutputoctets));
  127. $traffic->log_time = time();
  128. $traffic->save();
  129. $user=User::find($tempuserbox[$log->username]);
  130. $user->t = time();
  131. $user->u = $user->u + $log->acctinputoctets;
  132. $user->d = $user->d + $log->acctoutputoctets;
  133. $user->save();
  134. }
  135. }
  136. public static function syncusers()
  137. {
  138. $users = User::all();
  139. foreach ($users as $user) {
  140. Radius::Add($user, $user->passwd);
  141. echo "Send sync mail to user: ".$user->id;
  142. $subject = Config::get('appName')."-密码更新通知";
  143. $to = $user->email;
  144. $text = "您好,为了保证密码系统的统一,刚刚系统已经将您 vpn 等连接方式的用户名已经重置为:".Radius::GetUserName($user->email).",密码自动重置为您 ss 的密码:". $user->passwd." 了,以后您修改 ss 密码就会自动修改 vpn 等连接方式的密码了,感谢您的支持。 " ;
  145. try {
  146. Mail::send($to, $subject, 'password/vpn.tpl', [
  147. "user" => $user,"text" => $text
  148. ], [
  149. ]);
  150. } catch (Exception $e) {
  151. echo $e->getMessage();
  152. }
  153. }
  154. }
  155. public static function syncnas()
  156. {
  157. if (Config::get('radius_db_host')!="") {
  158. $md5txt="";
  159. $nases = RadiusNas::all();
  160. foreach ($nases as $nas) {
  161. //if($row["pass"]!="")
  162. {
  163. $md5txt=$md5txt.$nas->id.$nas->nasname.$nas->shortname.$nas->secret.$nas->description;
  164. }
  165. }
  166. $md5=MD5($md5txt);
  167. $oldmd5=file_get_contents(BASE_PATH."/storage/nas.md5");
  168. if ($oldmd5!=$md5) {
  169. //Restart radius
  170. $myfile = fopen(BASE_PATH."/storage/nas.md5", "w+") or die("Unable to open file!");
  171. echo("Restarting...");
  172. system("/bin/bash /sbin/service radiusd restart", $retval);
  173. echo($retval);
  174. $txt = $md5;
  175. fwrite($myfile, $txt);
  176. fclose($myfile);
  177. }
  178. }
  179. $Nodes = Node::where('sort', 0)->where('node_ip', "<>", "")->where('node_ip', "<>", 'NULL')->get();
  180. foreach ($Nodes as $Node) {
  181. if (file_exists("/usr/local/psionic/portsentry/portsentry.ignore")) {
  182. $content=file_get_contents("/usr/local/psionic/portsentry/portsentry.ignore");
  183. if (strpos($content, $Node->node_ip)===false) {
  184. file_put_contents("/usr/local/psionic/portsentry/portsentry.ignore", "\n".$Node->node_ip."/32", FILE_APPEND);
  185. }
  186. }
  187. }
  188. }
  189. }