作者 邓超

proxy

@@ -34,12 +34,18 @@ class ServerProxy extends Base { @@ -34,12 +34,18 @@ class ServerProxy extends Base {
34 $email = app()->request('email'); 34 $email = app()->request('email');
35 $smtp = app()->request('smtp'); 35 $smtp = app()->request('smtp');
36 $imap = app()->request('imap'); 36 $imap = app()->request('imap');
37 - $proxy = new MailProxy($email,$imap,$smtp);  
38 - $proxy->proxySuccess();  
39 - app()->_json([  
40 - 'imap_proxy' => $proxy->getImapProxy(),  
41 - 'smtp_proxy' => $proxy->getSmtpProxy()  
42 - ]); 37 + $default = app()->request('default');
  38 +
  39 + if($email&&$imap&&$smtp&&Verify::sEmail($email)){
  40 + $proxy = new MailProxy($email,$imap,$smtp, $default);
  41 + $proxy->proxySuccess();
  42 + app()->_json([
  43 + 'imap_proxy' => $proxy->getImapProxy(),
  44 + 'smtp_proxy' => $proxy->getSmtpProxy()
  45 + ]);
  46 + }else{
  47 + app()->e('异常');
  48 + }
43 } 49 }
44 50
45 51
@@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
3 namespace Service; 3 namespace Service;
4 4
5 use Lib\Mail\MailFun; 5 use Lib\Mail\MailFun;
  6 +use Model\emailSql;
  7 +use Model\listsSql;
6 8
7 /** 9 /**
8 * @author:dc 10 * @author:dc
@@ -60,7 +62,7 @@ class MailProxy { @@ -60,7 +62,7 @@ class MailProxy {
60 */ 62 */
61 protected $proxyService = [ 63 protected $proxyService = [
62 '43.134.162.250', // 这个是新加坡服务器 代理 64 '43.134.162.250', // 这个是新加坡服务器 代理
63 - '43.154.117.107', // 这个是 shopk的那台服务器 65 +// '43.154.117.107', // 这个是 shopk的那台服务器
64 ]; 66 ];
65 67
66 /** 68 /**
@@ -75,26 +77,44 @@ class MailProxy { @@ -75,26 +77,44 @@ class MailProxy {
75 */ 77 */
76 protected $email = ''; 78 protected $email = '';
77 79
  80 + protected $defaultIp = '';
  81 +
78 /** 82 /**
79 * MailProxy constructor. 83 * MailProxy constructor.
80 * @param string $email 84 * @param string $email
81 * @param string $imap 85 * @param string $imap
82 * @param string $smtp 86 * @param string $smtp
  87 + * @param string $default
83 * @throws \Exception 88 * @throws \Exception
84 */ 89 */
85 - public function __construct(string $email, string $imap, string $smtp) 90 + public function __construct(string $email, string $imap, string $smtp,string $default='')
86 { 91 {
87 $this->email = str_replace(['"',"'"],'',$email); 92 $this->email = str_replace(['"',"'"],'',$email);
  93 +
  94 + if($default){
  95 + $this->defaultIp = $default;
  96 + }
  97 +
88 // 先分配 98 // 先分配
89 $this->assignEmail(); 99 $this->assignEmail();
90 100
91 $this->originImapHost = $imap; 101 $this->originImapHost = $imap;
92 102
93 $this->originSmtpHost = $smtp; 103 $this->originSmtpHost = $smtp;
94 - // 解析  
95 - $this->proxyImapHost = $this->parse($imap,993);  
96 - // 解析  
97 - $this->proxySmtpHost = $this->parse($smtp,465); 104 + // 不需要分配
  105 + if($this->assignIp===false){
  106 + // 解析
  107 + $this->proxyImapHost = $imap;
  108 + // 解析
  109 + $this->proxySmtpHost = $smtp;
  110 + }else{
  111 + // 解析
  112 + $this->proxyImapHost = $this->parse($imap,993);
  113 + // 解析
  114 + $this->proxySmtpHost = $this->parse($smtp,465);
  115 + }
  116 +
  117 +
98 } 118 }
99 119
100 /** 120 /**
@@ -153,9 +173,7 @@ class MailProxy { @@ -153,9 +173,7 @@ class MailProxy {
153 if($serve != $this->assignIp){ 173 if($serve != $this->assignIp){
154 $this->assignIp = $serve; 174 $this->assignIp = $serve;
155 // 更新 ip 175 // 更新 ip
156 - db()->throw()->update('mail_proxy',[  
157 - 'ip' => $this->assignIp  
158 - ],dbWhere(['email'=>$this->email]),false); 176 + $this->updateIp();
159 } 177 }
160 178
161 }else{ 179 }else{
@@ -193,6 +211,30 @@ class MailProxy { @@ -193,6 +211,30 @@ class MailProxy {
193 $this->assignIp = $data['ip']; 211 $this->assignIp = $data['ip'];
194 return ; 212 return ;
195 } 213 }
  214 +
  215 + if(!$data){
  216 + // 如果 已经有之前的邮箱了, 就不进行代理了
  217 + if(db()->count(emailSql::first($this->email))){
  218 + $this->assignIp = false;
  219 + return ;
  220 + }
  221 + }
  222 +
  223 + // 默认指定 ip
  224 + if($this->defaultIp){
  225 + if($data){
  226 + $this->assignIp = $this->defaultIp;
  227 + if($data['ip'] == $this->defaultIp){
  228 + return ;
  229 + }else{
  230 + $this->updateIp();
  231 + }
  232 + }else{
  233 + $this->createIp();
  234 + }
  235 + return ;
  236 + }
  237 +
196 // 一个月活跃用户 238 // 一个月活跃用户
197 $t = date('Y-m-d H:i:s',strtotime("-30 day")); 239 $t = date('Y-m-d H:i:s',strtotime("-30 day"));
198 foreach ($this->proxyService as $ip){ 240 foreach ($this->proxyService as $ip){
@@ -202,16 +244,9 @@ class MailProxy { @@ -202,16 +244,9 @@ class MailProxy {
202 $this->assignIp = $ip; 244 $this->assignIp = $ip;
203 // 如果某个服务器 停止了,进来了就要重新分配 245 // 如果某个服务器 停止了,进来了就要重新分配
204 if($data){ 246 if($data){
205 - db()->throw()->update('mail_proxy',[  
206 - 'ip' => $this->assignIp  
207 - ],'id = '.$data['id']); 247 + $this->updateIp();
208 }else{ 248 }else{
209 - db()->throw()->insert('mail_proxy',[  
210 - 'email' => $this->email,  
211 - 'ip' => $this->assignIp,  
212 - 'time' => date('Y-m-d H:i:s'),  
213 - 'status' => 0  
214 - ],false); 249 + $this->createIp();
215 } 250 }
216 251
217 return ; 252 return ;
@@ -221,6 +256,32 @@ class MailProxy { @@ -221,6 +256,32 @@ class MailProxy {
221 throw new \Exception('没有资源可分配'); 256 throw new \Exception('没有资源可分配');
222 } 257 }
223 258
  259 + /**
  260 + * 更新ip
  261 + * @param $ip
  262 + * @author:dc
  263 + * @time 2025/3/14 15:55
  264 + */
  265 + protected function updateIp(){
  266 + db()->throw()->update('mail_proxy',[
  267 + 'ip' => $this->assignIp,
  268 + 'time' => date('Y-m-d H:i:s'),
  269 + ],dbWhere(['email'=>$this->email]),false);
  270 + }
  271 +
  272 + /**
  273 + * 创建
  274 + * @author:dc
  275 + * @time 2025/3/14 15:57
  276 + */
  277 + protected function createIp(){
  278 + db()->throw()->insert('mail_proxy',[
  279 + 'email' => $this->email,
  280 + 'ip' => $this->assignIp,
  281 + 'time' => date('Y-m-d H:i:s'),
  282 + 'status' => 0
  283 + ],false);
  284 + }
224 285
225 /** 286 /**
226 * 标记为正常使用代理 287 * 标记为正常使用代理
@@ -229,7 +290,8 @@ class MailProxy { @@ -229,7 +290,8 @@ class MailProxy {
229 */ 290 */
230 public function proxySuccess(){ 291 public function proxySuccess(){
231 db()->update('mail_proxy',[ 292 db()->update('mail_proxy',[
232 - 'status' => 1 293 + 'status' => 1,
  294 + 'time' => date('Y-m-d H:i:s'),
233 ],dbWhere(['email'=>$this->email]),false); 295 ],dbWhere(['email'=>$this->email]),false);
234 } 296 }
235 297