Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/http/testenv/dante.py
2653 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
#***************************************************************************
4
# _ _ ____ _
5
# Project ___| | | | _ \| |
6
# / __| | | | |_) | |
7
# | (__| |_| | _ <| |___
8
# \___|\___/|_| \_\_____|
9
#
10
# Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11
#
12
# This software is licensed as described in the file COPYING, which
13
# you should have received as part of this distribution. The terms
14
# are also available at https://curl.se/docs/copyright.html.
15
#
16
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
# copies of the Software, and permit persons to whom the Software is
18
# furnished to do so, under the terms of the COPYING file.
19
#
20
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
# KIND, either express or implied.
22
#
23
# SPDX-License-Identifier: curl
24
#
25
###########################################################################
26
#
27
import logging
28
import os
29
import socket
30
import subprocess
31
import time
32
from datetime import timedelta, datetime
33
34
from typing import Dict
35
36
from . import CurlClient
37
from .env import Env
38
from .ports import alloc_ports_and_do
39
40
log = logging.getLogger(__name__)
41
42
43
class Dante:
44
45
def __init__(self, env: Env):
46
self.env = env
47
self._cmd = env.danted
48
self._port = 0
49
self.name = 'danted'
50
self._port_skey = 'danted'
51
self._port_specs = {
52
'danted': socket.SOCK_STREAM,
53
}
54
self._dante_dir = os.path.join(env.gen_dir, self.name)
55
self._run_dir = os.path.join(self._dante_dir, 'run')
56
self._tmp_dir = os.path.join(self._dante_dir, 'tmp')
57
self._conf_file = os.path.join(self._dante_dir, 'test.conf')
58
self._dante_log = os.path.join(self._dante_dir, 'dante.log')
59
self._error_log = os.path.join(self._dante_dir, 'error.log')
60
self._pid_file = os.path.join(self._dante_dir, 'dante.pid')
61
self._process = None
62
63
self.clear_logs()
64
65
@property
66
def port(self) -> int:
67
return self._port
68
69
def clear_logs(self):
70
self._rmf(self._error_log)
71
self._rmf(self._dante_log)
72
73
def exists(self):
74
return os.path.exists(self._cmd)
75
76
def is_running(self):
77
if self._process:
78
self._process.poll()
79
return self._process.returncode is None
80
return False
81
82
def start_if_needed(self):
83
if not self.is_running():
84
return self.start()
85
return True
86
87
def stop(self, wait_dead=True):
88
self._mkpath(self._tmp_dir)
89
if self._process:
90
self._process.terminate()
91
self._process.wait(timeout=2)
92
self._process = None
93
return not wait_dead or True
94
return True
95
96
def restart(self):
97
self.stop()
98
return self.start()
99
100
def initial_start(self):
101
102
def startup(ports: Dict[str, int]) -> bool:
103
self._port = ports[self._port_skey]
104
if self.start():
105
self.env.update_ports(ports)
106
return True
107
self.stop()
108
self._port = 0
109
return False
110
111
return alloc_ports_and_do(self._port_specs, startup,
112
self.env.gen_root, max_tries=3)
113
114
def start(self, wait_live=True):
115
assert self._port > 0
116
self._mkpath(self._tmp_dir)
117
if self._process:
118
self.stop()
119
self._write_config()
120
args = [
121
self._cmd,
122
'-f', f'{self._conf_file}',
123
'-p', f'{self._pid_file}',
124
'-d', '0',
125
]
126
procerr = open(self._error_log, 'a')
127
self._process = subprocess.Popen(args=args, stderr=procerr)
128
if self._process.returncode is not None:
129
return False
130
return self.wait_live(timeout=timedelta(seconds=Env.SERVER_TIMEOUT))
131
132
def wait_live(self, timeout: timedelta):
133
curl = CurlClient(env=self.env, run_dir=self._tmp_dir,
134
timeout=timeout.total_seconds(), socks_args=[
135
'--socks5', f'127.0.0.1:{self._port}'
136
])
137
try_until = datetime.now() + timeout
138
while datetime.now() < try_until:
139
r = curl.http_get(url=f'http://{self.env.domain1}:{self.env.http_port}/')
140
if r.exit_code == 0:
141
return True
142
time.sleep(.1)
143
log.error(f"Server still not responding after {timeout}")
144
return False
145
146
def _rmf(self, path):
147
if os.path.exists(path):
148
return os.remove(path)
149
150
def _mkpath(self, path):
151
if not os.path.exists(path):
152
return os.makedirs(path)
153
154
def _write_config(self):
155
conf = [
156
f'errorlog: {self._error_log}',
157
f'logoutput: {self._dante_log}',
158
f'internal: 127.0.0.1 port = {self._port}',
159
'external: 127.0.0.1',
160
'clientmethod: none',
161
'socksmethod: none',
162
'client pass {',
163
' from: 127.0.0.0/24 to: 0.0.0.0/0',
164
' log: error',
165
'}',
166
'socks pass {',
167
' from: 0.0.0.0/0 to: 0.0.0.0/0',
168
' command: bindreply connect udpreply',
169
' log: error',
170
'}',
171
'\n',
172
]
173
with open(self._conf_file, 'w') as fd:
174
fd.write("\n".join(conf))
175
176