1
0
mirror of https://github.com/danog/Merger.git synced 2024-11-30 04:19:10 +01:00

More fixes

This commit is contained in:
Daniil Gentili 2019-04-03 18:27:07 +02:00
parent 3e0e584512
commit 4d4de73f41
6 changed files with 98 additions and 76 deletions

View File

@ -6,7 +6,7 @@ use danog\Merger\Merger;
use danog\Merger\Settings;
$settings = new Settings();
$settings->setTunnelEndpoint('127.0.0.1', 4444);
$settings->setTunnelEndpoint('manuel.giuseppem99.cf', 4444);
$settings->addConnectAddress('192.168.1.236');
$client = new Merger($settings);
$client->loop();

View File

@ -18,8 +18,8 @@ namespace danog\Merger\Abstr;
use danog\Merger\MergerWorker;
use danog\Merger\SequentialSocket;
use danog\Merger\Settings;
use function Amp\Socket\connect;
use function Amp\asyncCall;
use function Amp\Socket\connect;
/**
* Abstract class shared merger
@ -94,7 +94,7 @@ abstract class SharedMerger
$this->logger->write("Connected to $host:$rport, {$port}\n");
} catch (\Exception $e) {
$this->logger->write("Exception {$e->getMessage()} in $host:$rport, {$port}\n");
$this->writers[key($this->writers)]->write(pack('VnC', 0, $port, Settings::ACTION_DISCONNECT));
$this->writers[0]->write(pack('VnC', 0, $port, Settings::ACTION_DISCONNECT));
}
});
} else {

View File

@ -86,6 +86,7 @@ class Merger extends SharedMerger
public function getReadLoop(): callable
{
return function () {
try {
$this->_logger->write("New {$this->_port}\n");
$socket = $this->_socket;
$socksInit = $socket->getBuffer();
@ -102,7 +103,7 @@ class Merger extends SharedMerger
$type = ord(fread($socksInit, 1));
if ($type === 0) {
$auth = false;
} else if ($type === 2) {
} elseif ($type === 2) {
$auth = true;
}
}
@ -154,7 +155,7 @@ class Merger extends SharedMerger
yield $socket->write(chr(5) . chr(0) . chr(0) . chr(1) . pack('Vn', 0, 0));
$this->_logger->write("================================ SENDING CONNECT ================================\n");
yield $this->_writers[key($this->_writers)]->write(pack('VnCn', 0, $this->_port, Settings::ACTION_CONNECT, $rport) . $payload);
yield $this->_writers[0]->write(pack('VnCn', 0, $this->_port, Settings::ACTION_CONNECT, $rport) . $payload);
if (fstat($socksInit)['size'] - ftell($socksInit)) {
yield $this->commonWrite($socksInit);
@ -162,6 +163,10 @@ class Merger extends SharedMerger
while (yield $socket->read()) {
yield $this->commonWrite($socksInit);
}
} catch (\Exception $e) {
$this->_logger->write($e);
}
$this->close();
};
}

View File

@ -58,6 +58,7 @@ class MergerServer extends SharedMerger
$server = listen($this->settings->getTunnelEndpoint());
while ($socket = yield $server->accept()) {
try {
$socket = new SequentialSocket($socket);
yield $socket->read(2);
$id = unpack('n', fread($socket->getBuffer(), 2))[1];
@ -66,6 +67,9 @@ class MergerServer extends SharedMerger
ksort($this->writers);
asyncCall([$this, 'sharedLoop'], $id);
yield $socket->write(pack('n', $id));
} catch (\Exception $e) {
$this->logger->write($e);
}
};
}

View File

@ -112,6 +112,7 @@ class MergerWorker
public function commonWrite($chunk)
{
try {
$shared_deferred = new Deferred();
$promise = $shared_deferred->promise();
$length = fstat($chunk)['size'] - ftell($chunk);
@ -139,6 +140,9 @@ class MergerWorker
fseek($chunk, 0);
ftruncate($chunk, 0);
return $promise;
} catch (\Exception $e) {
return $this->_logger->write($e);
}
}
public function close()
{
@ -149,7 +153,7 @@ class MergerWorker
$this->_socket = null;
$this->_logger->write("Closing {$this->_port}\n");
$socket->close();
$this->_writers[key($this->_writers)]->write(pack('VnC', 0, $this->_port, Settings::ACTION_DISCONNECT));
$this->_writers[0]->write(pack('VnC', 0, $this->_port, Settings::ACTION_DISCONNECT));
}
public function handleSharedRead($writerId, $buffer, $length)

View File

@ -72,6 +72,15 @@ class Stats
}
private $speeds = [];
public function __construct()
{
Loop::repeat(1000, (function () {
foreach ($this->speeds as $elem) {
$elem->pop();
$elem->unshift(0);
}
})->bindTo($this, get_class($this)));
}
public function allocate($ID)
{
$this->speeds[$ID] = new \Ds\Deque();
@ -81,8 +90,8 @@ class Stats
public function stopSending($ID, $started, $sent)
{
$time = microtime(true) - $started;
$this->speeds[$ID]->unshift(($sent * 8) / $time);
$this->speeds[$ID]->pop();
$this->speeds[$ID]->unshift(($sent * 8) / $time);
}
public function getSpeed($ID, $powerOf = 6)
@ -115,8 +124,8 @@ class Stats
foreach ($result as $key => &$elem) {
$elem = (int) ($elem * $per_bytes);
if (!$elem) {
$this->speeds[$key]->unshift(1000000);
$this->speeds[$key]->pop();
$this->speeds[$key]->unshift(1000000);
$elem += 2;
}
$sum += $elem;