""" Parse scheme, host and port from a string. """ import collections import re from typing import Tuple from mitmproxy.net import check ServerSpec = collections.namedtuple("ServerSpec", ["scheme", "address"]) server_spec_re = re.compile( r""" ^ (?:(?P\w+)://)? # scheme is optional (?P[^:/]+|\[.+\]) # hostname can be DNS name, IPv4, or IPv6 address. (?::(?P\d+))? # port is optional /? # we allow a trailing backslash, but no path $ """, re.VERBOSE ) def parse(server_spec: str) -> ServerSpec: """ Parses a server mode specification, e.g.: - http://example.com/ - example.org - example.com:443 Raises: ValueError, if the server specification is invalid. """ m = server_spec_re.match(server_spec) if not m: raise ValueError("Invalid server specification: {}".format(server_spec)) # defaulting to https/port 443 may annoy some folks, but it's secure-by-default. scheme = m.group("scheme") or "https" if scheme not in ("http", "https"): raise ValueError("Invalid server scheme: {}".format(scheme)) host = m.group("host") # IPv6 brackets if host.startswith("[") and host.endswith("]"): host = host[1:-1] if not check.is_valid_host(host.encode("idna")): raise ValueError("Invalid hostname: {}".format(host)) if m.group("port"): port = int(m.group("port")) else: port = { "http": 80, "https": 443 }[scheme] if not check.is_valid_port(port): raise ValueError("Invalid port: {}".format(port)) return ServerSpec(scheme, (host, port)) def parse_with_mode(mode: str) -> Tuple[str, ServerSpec]: """ Parse a proxy mode specification, which is usually just (reverse|upstream):server-spec Returns: A (mode, server_spec) tuple. Raises: ValueError, if the specification is invalid. """ mode, server_spec = mode.split(":", maxsplit=1) return mode, parse(server_spec) og/quantum/process_keycode/process_leader.h'>
blob: da7a3d2ef702a5e1a0f3e8382bdce1840c22b184 (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
/* Copyright 2016 Jack Humbert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PROCESS_LEADER_H
#define PROCESS_LEADER_H

#include "quantum.h"

bool process_leader(uint16_t keycode, keyrecord_t *record);

void leader_start(void);
void leader_end(void);

#ifndef LEADER_TIMEOUT
  #define LEADER_TIMEOUT 200
#endif
#define SEQ_ONE_KEY(key) if (leader_sequence[0] == (key) && leader_sequence[1] == 0 && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0)
#define SEQ_TWO_KEYS(key1, key2) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0)
#define SEQ_THREE_KEYS(key1, key2, key3) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == 0 && leader_sequence[4] == 0)
#define SEQ_FOUR_KEYS(key1, key2, key3, key4) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == (key4) && leader_sequence[4] == 0)
#define SEQ_FIVE_KEYS(key1, key2, key3, key4, key5) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == (key4) && leader_sequence[4] == (key5))

#define LEADER_EXTERNS() extern bool leading; extern uint16_t leader_time; extern uint16_t leader_sequence[5]; extern uint8_t leader_sequence_size
#define LEADER_DICTIONARY() if (leading && timer_elapsed(leader_time) > LEADER_TIMEOUT)

#endif