Skip to content

Commit

Permalink
Add proxy support with split modes
Browse files Browse the repository at this point in the history
  • Loading branch information
donhardman committed May 17, 2021
1 parent 9ae499b commit 001fd2a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/NodeAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class NodeAPI {

protected array $useragents = [];
protected array $proxies = [];
protected array $proxy_modes = ['read', 'write'];

use RequestTrait;

Expand All @@ -34,8 +35,8 @@ public function __construct(array $config) {
if (isset($config['proxies'])) {
$this->proxies = array_map(function ($v): array {
if (is_string($v)) {
[$host, $port, $user, $password] = explode(':', $v);
$proxy = compact('host', 'port', 'user', 'password');
[$type, $host, $port, $user, $password] = explode(':', $v);
$proxy = compact('type', 'host', 'port', 'user', 'password');
} else {
$proxy = $v;
}
Expand All @@ -44,6 +45,10 @@ public function __construct(array $config) {
}, $config['proxies']);
}

if (isset($config['proxy_modes'])) {
$this->proxy_modes = $config['proxy_modes'];
}

if (isset($config['useragents'])) {
$this->useragents = $config['useragents'];
}
Expand Down Expand Up @@ -372,18 +377,25 @@ public function getFollowing(string $search, $type = 'pubkey', string $last_pubk
}

public function run(string $path, array $payload, string $method = 'POST'): array {
$url = match ($path) {
'submit-transaction' => $this->write_url,
default => $this->read_url
$mode = match ($path) {
'submit-transaction' => 'write',
default => 'read',
};

$url = match ($mode) {
'write' => $this->write_url,
'read' => $this->read_url,
};

if (!str_starts_with($path, 'api')) {
$path = 'api/v0/' . $path;
}

// If we have set proxies, randomize
if ($this->proxies) {
if ($this->proxies && in_array($mode, $this->proxy_modes)) {
$this->request_proxy = $this->proxies[array_rand($this->proxies)];
} else {
$this->request_proxy = [];
}

// If we have list of user agents randomize it
Expand Down

0 comments on commit 001fd2a

Please sign in to comment.