Skip to content

Instantly share code, notes, and snippets.

@JeK2a
Created June 21, 2019 09:57
Show Gist options
  • Save JeK2a/150daff0aa295a3668d3d558c649aa4d to your computer and use it in GitHub Desktop.
Save JeK2a/150daff0aa295a3668d3d558c649aa4d to your computer and use it in GitHub Desktop.
<?php
class ProxySmtp
{
// PROXY CONNECT
private $proxy;
private $proxy_ip;
private $proxy_port;
private $proxy_login;
private $proxy_password;
// CURL SERVICE
private $curl_requests = [];
private $curl_connection;
private $curl_file_header;
private $curl_file_log;
private $curl_is_debag;
private $curl_type;
// SMTP CONNECT
private $smtp_host;
private $smtp_port;
private $smtp_user;
private $smtp_password;
private $smtp_timeout;
private $smtp_char_set;
private $smtp_secure;
private $file_log_name = '';
// EMAIL HEAD
private $smtp_from;
private $smtp_to = [];
private $i = 0;
public function __construct($smtp_params = [], $proxy_params = [], $curl_is_debag = 0)
{
$this->curl_is_debag = $curl_is_debag;
$this->curl_is_debag = false;
$this->curl_is_logs = true;
if ($this->curl_is_debag) {
echo '<pre>';
print_r($smtp_params);
print_r($proxy_params);
echo '</pre>';
}
$this->smtp_host = $smtp_params['host'];
$this->smtp_port = $smtp_params['port'];
$this->smtp_user = $smtp_params['user'];
$this->smtp_password = $smtp_params['password'];
$this->smtp_timeout = $smtp_params['timeout'];
$this->smtp_from = $smtp_params['user']; // TODO ?
$this->smtp_to = $smtp_params['to'];
$this->smtp_timeout = $smtp_params['timeout'];
$this->smtp_char_set = $smtp_params['char_set'];
$this->smtp_secure = $smtp_params['secure'];
$this->proxy_type = $proxy_params['type'];
$this->proxy_ip = $proxy_params['ip'];
$this->proxy_port = $proxy_params['port'];
$this->proxy_login = $proxy_params['user'] ?? '';
$this->proxy_password = $proxy_params['password'] ?? '';
$this->proxy = $this->proxy_ip.":".$this->proxy_port;
$this->curl_requests = [];
$patch_proxy = '/var/www/admin/data/www/my/public_html/sandbox/proxy/';
if ($this->curl_is_logs) {
$date = date("y.m.d H:i:s");
$this->file_log_name = $patch_proxy . 'log '.$date.'.txt';
$this->curl_file_header = fopen($patch_proxy . 'headers '.$date.'.txt', 'w+');
$this->curl_file_log = fopen($this->file_log_name, 'w+');
}
}
public function getLine()
{
// $out = curl_getinfo($this->curl_connection)['http_code'];
$out = 250;
return $out;
}
public function getNextQuery()
{
$resp = current($this->curl_requests);
next($this->curl_requests);
return $resp;
}
// QUERY
public function curl_telnet($query)
{
$this->curl_requests[] = $query;
if ($query == ".\r\n") {
if (++$this->i > 1) {
die('END');
}
return $this->goQuery() ? -1 : -2; // -1 send and all ok -2 error
}
return strlen($query);
}
public function goQuery()
{
//// $ch = curl_init('http://www.example.com/');
//
// $secure = 'smtp' .(empty($this->smtp_secure) ? '' : 's');
// $ch = curl_init($secure."://".$this->smtp_host.":".$this->smtp_port);
//
//// Запускаем
// curl_exec($ch);
//
//// Проверяем наличие ошибок
// if (!curl_errno($ch)) {
// switch ($http_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE)) {
// case 200: echo $http_code; # OK
// break;
// default:
// echo 'Неожиданный код HTTP: ', $http_code, "\n";
// }
// }
//
//// Закрываем дескриптор
// curl_close($ch);
//
//
// die('END');
if ($this->curl_is_debag) {
print_r($this->curl_requests);
}
foreach ($this->curl_requests as $key => $value) {
if (stripos($value, 'PHPMailer')) {
unset($this->curl_requests[$key]);
}
if (stripos($value, 'essage-ID:')) {
$this->curl_requests[$key] = str_replace('my.tdfort.ru', $this->getRandomString(rand(7, 20)), $value);
}
if ($value == "DATA\r\n") {
$start = $key + 1;
}
if ($value == ".\r\n") {
$this->curl_requests = array_slice($this->curl_requests, $start, $key - $start);
}
}
// echo '<pre>';
// echo __FILE__.' - '.__LINE__."\n";
// print_r($this->curl_requests);
// echo '</pre>';
//
// die();
if ($this->curl_is_debag) {
print_r($this->curl_requests);
}
// $this->curl_connection = curl_init($secure."://".$this->smtp_host.":".$this->smtp_port);
// $this->curl_connection = curl_init('http://lumtest.com/myip.json');
// // LOG
// if ($this->curl_is_logs) {
// curl_setopt($this->curl_connection, CURLOPT_WRITEHEADER, $this->curl_file_header);
// curl_setopt($this->curl_connection, CURLOPT_VERBOSE, 1);
// curl_setopt($this->curl_connection, CURLOPT_STDERR, $this->curl_file_log);
// }
// PROXY
// curl_setopt($this->curl_connection, CURLOPT_PROXY, $this->proxy);
$secure = 'smtp' .(empty($this->smtp_secure) ? '' : 's');
$curl = curl_init($secure."://".$this->smtp_host.":".$this->smtp_port);
switch ($this->proxy_type) {
case 'http' :
case 'HTTP' : $this->proxy_type = CURLPROXY_HTTP; break;
case 'https' :
case 'HTTPS' : $this->proxy_type = 2; break;
case 'socks4':
case 'SOCKS4': $this->proxy_type = CURLPROXY_SOCKS4; break;
case 'socks5':
case 'SOCKS5': $this->proxy_type = CURLPROXY_SOCKS5; break;
default : $this->proxy_type = CURLPROXY_SOCKS5; break;
}
curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($curl, CURLOPT_PROXY, 'proxy.torguard.org:1090');
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxy_login . ':' . $this->proxy_password);
curl_setopt($curl, CURLOPT_URL, $secure."://".$this->smtp_host.":".$this->smtp_port);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// LOG
if ($this->curl_is_logs) {
curl_setopt($curl, CURLOPT_WRITEHEADER, $this->curl_file_header);
curl_setopt($curl, CURLOPT_VERBOSE, True);
curl_setopt($curl, CURLOPT_STDERR, $this->curl_file_log);
}
if (!empty($this->proxy_login) && !empty($this->proxy_password)) {
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxy_login . ':' . $this->proxy_password);
}
//
// curl_setopt($this->curl_connection, CURLOPT_PROXYPORT, $this->proxy_port);
// curl_setopt($this->curl_connection, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
//
curl_setopt($curl, CURLOPT_TIMEOUT, $this->smtp_timeout);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->smtp_timeout);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// BODY
curl_setopt($curl, CURLOPT_MAIL_FROM, "<" . $this->smtp_from . ">");
curl_setopt($curl, CURLOPT_MAIL_RCPT, $this->smtp_to);
curl_setopt($curl, CURLOPT_USERNAME, $this->smtp_user);
curl_setopt($curl, CURLOPT_PASSWORD, $this->smtp_password);
curl_setopt($curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
//
curl_setopt($curl, CURLOPT_READFUNCTION, [$this, 'getNextQuery']);
//
// curl_exec($this->curl_connection);
// echo " IN 3: " . curl_getinfo($curl)['http_code'] . ' ';
echo '<pre>';
print_r(curl_share_errno ($curl));
// print_r(" / ");
// print_r(curl_error($curl));
echo '</pre>';
echo curl_exec($curl);
curl_close($curl);
if ($this->curl_is_logs) {
fclose($this->curl_file_log);
fclose($this->curl_file_header);
}
// echo file_get_contents($this->file_log_name);
die("send to ".$this->smtp_to[0]." with proxy " . $this->proxy_ip . ' from ' . $this->smtp_user); // TODO
return true;
}
private function getRandomString($size = 100)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ......';
$randString = '';
for ($i = 0; $i < $size; $i++) {
$randString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment