summaryrefslogtreecommitdiffstats
path: root/modules-available/dnbd3/inc/dnbd3rpc.inc.php
blob: f6bbf0ca0f68a793a118a69abcb6c4e3f78dacce (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

class Dnbd3Rpc {

	const ERROR_UNREACHABLE = 1;
	const ERROR_NOT_200 = 2;
	const ERROR_NOT_JSON = 3;

	const QUERY_STATS = 'stats';
	const QUERY_CLIENTS = 'clients';
	const QUERY_IMAGES = 'images';
	const QUERY_SPACE = 'space';
	const QUERY_CONFIG = 'config';
	const QUERY_ALTSERVERS = 'altservers';

	private static function translateServer(string $server): string
	{
		// Special case - local server
		if ($server === '<self>') {
			$server = '127.0.0.1:5003';
		} elseif (($out = Dnbd3Util::matchAddress($server)) !== false) {
			$server = $out['v4'] ?? '[' . $out['v6'] . ']';
			$server .= $out['port'] ?? ':5003';
		}
		return $server;
	}

	/**
	 * Query given DNBD3 server for status information.
	 *
	 * @param string $server server address
	 * @param array $queryOptions Options to query, self::QUERY_*
	 * @return int|array the queried data as an array, or error code (self::ERROR_*) on error
	 */
	public static function query(string $server, array $queryOptions)
	{
		$server = self::translateServer($server);
		$url = 'http://' . $server . '/query?q=version';
		if (!empty($queryOptions)) {
			$url .= '&q=' . implode('&q=', $queryOptions);
		}
		$str = Download::asString($url, 3, $code);
		if ($str === false)
			return self::ERROR_UNREACHABLE;
		if ($code !== 200)
			return self::ERROR_NOT_200;
		$ret = json_decode($str, true);
		if (!is_array($ret))
			return self::ERROR_NOT_JSON;
		return $ret;
	}

	public static function getCacheMap($server, $imgId)
	{
		$server = self::translateServer($server);
		$str = Download::asString('http://' . $server . '/cachemap?id=' . $imgId, 3, $code);
		if ($str === false)
			return self::ERROR_UNREACHABLE;
		if ($code !== 200)
			return self::ERROR_NOT_200;
		return $str;
	}

	/**
	 * Get statistics for multiple servers at once.
	 * @param string[] $servers
	 */
	public static function getStatsMulti(array $servers, array $queryOptions = [], int $timeout = 2): array
	{
		if (empty($servers))
			return [];
		$extra = '';
		if (!empty($queryOptions)) {
			$extra = '&q=' . implode('&q=', $queryOptions);
		}
		$active = [];
		$mh = curl_multi_init();
		curl_multi_setopt($mh, CURLMOPT_MAXCONNECTS, 8);
		curl_multi_setopt($mh, CURLMOPT_MAX_HOST_CONNECTIONS, 2);
		curl_multi_setopt($mh, CURLMOPT_MAX_TOTAL_CONNECTIONS, 8);
		foreach ($servers as $server) {
			$url = 'http://' . self::translateServer($server) . '/query?q=version' . $extra;
			$res = curl_init($url);
			if ($res === false) {
				error_log("curl_init($url) failed");
				continue;
			}
			curl_setopt_array($res, [
				CURLOPT_CONNECTTIMEOUT => $timeout,
				CURLOPT_TIMEOUT => $timeout,
				CURLOPT_FOLLOWLOCATION => 0,
				CURLOPT_ACCEPT_ENCODING => '', // Use everything libcurl supports
				CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
				CURLOPT_RETURNTRANSFER => 1,
			]);
			$err = curl_multi_add_handle($mh, $res);
			if ($err !== 0) {
				error_log("curl_multi_add_handle() failed with $err");
				curl_close($res);
			} else {
				$active[(int)$res] = $server;
			}
		}
		// Wait
		$running = 1;
		$result = [];
		$startTime = microtime(true);
		for (;;) {
			$ret = curl_multi_exec($mh, $running);
			while (($info = curl_multi_info_read($mh)) !== false) {
				if ($info['msg'] === CURLMSG_DONE) {
					if (isset($active[(int)$info['handle']])) {
						$server = $active[(int)$info['handle']];
						unset($active[(int)$info['handle']]);
						if ($info['result'] === CURLE_OK) {
							$data = json_decode(curl_multi_getcontent($info['handle']), true);
							if (is_array($data)) {
								$data['ts'] = microtime(true);
								$result[$server] = $data;
							}
						}
					}
					curl_multi_remove_handle($mh, $info['handle']);
					curl_close($info['handle']);
				}
			}
			$delay = ($startTime + $timeout) - microtime(true);
			if ($ret !== CURLM_OK || !$running || $delay <= 0)
				break;
			$sret = curl_multi_select($mh, $delay);
			if ($sret < 0) {
				error_log("curl_multi_select returned $sret");
				break;
			}
		}
		curl_multi_close($mh);
		return $result;
	}

}