summaryrefslogtreecommitdiffstats
path: root/vendor/clue/connection-manager-extra/src/Multiple/ConnectionManagerSelective.php
blob: 859ea9053419f3fd694a224f65db47161f701bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

namespace ConnectionManager\Extra\Multiple;

use React\Socket\ConnectorInterface;
use React\Promise;
use UnderflowException;
use InvalidArgumentException;

class ConnectionManagerSelective implements ConnectorInterface
{
    private $managers;

    /**
     *
     * @param ConnectorInterface[] $managers
     */
    public function __construct(array $managers)
    {
        foreach ($managers as $filter => $manager) {
            $host = $filter;
            $portMin = 0;
            $portMax = 65535;

            // search colon (either single one OR preceded by "]" due to IPv6)
            $colon = strrpos($host, ':');
            if ($colon !== false && (strpos($host, ':') === $colon || substr($host, $colon - 1, 1) === ']' )) {
                if (!isset($host[$colon + 1])) {
                    throw new InvalidArgumentException('Entry "' . $filter . '" has no port after colon');
                }

                $minus = strpos($host, '-', $colon);
                if ($minus === false) {
                    $portMin = $portMax = (int)substr($host, $colon + 1);

                    if (substr($host, $colon + 1) !== (string)$portMin) {
                        throw new InvalidArgumentException('Entry "' . $filter . '" has no valid port after colon');
                    }
                } else {
                    $portMin = (int)substr($host, $colon + 1, ($minus - $colon));
                    $portMax = (int)substr($host, $minus + 1);

                    if (substr($host, $colon + 1) !== ($portMin . '-' . $portMax)) {
                        throw new InvalidArgumentException('Entry "' . $filter . '" has no valid port range after colon');
                    }

                    if ($portMin > $portMax) {
                        throw new InvalidArgumentException('Entry "' . $filter . '" has port range mixed up');
                    }
                }
                $host = substr($host, 0, $colon);
            }

            if ($host === '') {
                throw new InvalidArgumentException('Entry "' . $filter . '" has an empty host');
            }

            if (!$manager instanceof ConnectorInterface) {
                throw new InvalidArgumentException('Entry "' . $filter . '" is not a valid connector');
            }
        }

        $this->managers = $managers;
    }

    public function connect($uri)
    {
        $parts = parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri);
        if (!isset($parts) || !isset($parts['scheme'], $parts['host'], $parts['port'])) {
            return Promise\reject(new InvalidArgumentException('Invalid URI'));
        }

        $connector = $this->getConnectorForTarget(
            trim($parts['host'], '[]'),
            $parts['port']
        );

        if ($connector === null) {
            return Promise\reject(new UnderflowException('No connector for given target found'));
        }

        return $connector->connect($uri);
    }

    private function getConnectorForTarget($targetHost, $targetPort)
    {
        foreach ($this->managers as $host => $connector) {
            $portMin = 0;
            $portMax = 65535;

            // search colon (either single one OR preceded by "]" due to IPv6)
            $colon = strrpos($host, ':');
            if ($colon !== false && (strpos($host, ':') === $colon || substr($host, $colon - 1, 1) === ']' )) {
                $minus = strpos($host, '-', $colon);
                if ($minus === false) {
                    $portMin = $portMax = (int)substr($host, $colon + 1);
                } else {
                    $portMin = (int)substr($host, $colon + 1, ($minus - $colon));
                    $portMax = (int)substr($host, $minus + 1);
                }
                $host = trim(substr($host, 0, $colon), '[]');
            }

            if ($targetPort >= $portMin && $targetPort <= $portMax && fnmatch($host, $targetHost)) {
                return $connector;
            }
        }

        return null;
    }
}