-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
177 lines (159 loc) · 5.44 KB
/
__main__.py
File metadata and controls
177 lines (159 loc) · 5.44 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'''
Opens a hotspot on linux.
Deprecated in favor of linux-wifi-hotspot.
'''
import os
from contextlib import contextmanager
from subprocess import Popen
from functools import lru_cache
import typing as tp
import re
import uuid
import argparse
import psutil
DEFAULT_GATEWAY_IP = '192.168.1.1'
HOSTAPD = 'hostapd'
DNSMASQ = 'dnsmasq'
@lru_cache(1)
def interface():
interfaces = [*psutil.net_if_addrs().keys()]
guesses = [x for x in interfaces if 'wlan' in x or 'wlp' in x]
if not guesses:
raise EnvironmentError('No wireless interface found.')
if len(guesses) == 1:
return guesses[0]
while True:
print('Please select an interface:')
for i, x in enumerate(interfaces):
print(f'{i}: {x}')
try:
i = int(input('> '))
return interfaces[i]
except (ValueError, IndexError):
print('Invalid input. Try again.')
@lru_cache(1)
def workingDir():
return os.path.dirname(os.path.realpath(__file__))
def tempFileName(identifier: str):
return os.path.join(workingDir(), f'temp_{identifier}.conf')
def newSecureTempFile(identifier: str):
fn = tempFileName(identifier)
with open(fn, 'w') as f:
f.write('dummy')
os.chmod(fn, 0o600)
return open(fn, 'w')
def writeHostapdConf(
ssid: str, wpa_passphrase: str, conf: dict = {},
):
DEFAULT = {
'interface': interface(),
'ssid': ssid,
'wpa_passphrase': wpa_passphrase,
'driver': 'nl80211',
'hw_mode': 'g',
'channel': 6,
'wmm_enabled': 0,
'auth_algs': 1,
'wpa': 2,
'wpa_key_mgmt': 'WPA-PSK',
'rsn_pairwise': 'CCMP',
}
c = {**DEFAULT, **conf}
with newSecureTempFile(HOSTAPD) as f:
for k, v in c.items():
print(f'{k}={v}', file=f)
def writeDnsmasqConf(conf: dict = {}):
DEFAULT = {
'interface': interface(),
'dhcp-range': '192.168.1.2,192.168.1.20,12h',
}
c = {**DEFAULT, **conf}
with newSecureTempFile(DNSMASQ) as f:
for k, v in c.items():
print(f'{k}={v}', file=f)
@contextmanager
def UnmanageNetwork():
CONF_FILE = '/etc/NetworkManager/NetworkManager.conf'
tag_open = '# >>> the following line(s) are automated by ' + os.path.realpath(__file__)
tag_close = '# <<< end of automation by ' + os.path.realpath(__file__)
block_content: tp.Dict[bool, str] = {}
block_content[True] = '\n'.join([
tag_open,
'[keyfile]',
'unmanaged-devices=interface-name:' + interface(),
tag_close,
])
block_content[False] = '\n'.join([
tag_open,
'# ',
tag_close,
])
block_pattern = re.compile(rf"{re.escape(tag_open)}.*?{re.escape(tag_close)}", re.DOTALL)
def setConf(from_old: bool, to_new: bool):
uuid_ = str(uuid.uuid4())
with open(CONF_FILE, 'r') as f:
content = f.read()
if re.search(block_pattern, content):
if block_content[from_old] not in content:
if block_content[not from_old] not in content:
raise ValueError('Conf file contains correct tags but its enclosed content is unrecognized.')
if not from_old:
print('Hint: maybe another hotspot instance is running?')
raise ValueError('Unexpected state of conf file.')
content = re.sub(block_pattern, uuid_, content)
assert not re.search(block_pattern, content), 'Multiple blocks found.'
content = content.replace(uuid_, block_content[to_new])
else:
assert not from_old
content = '\n'.join([
content.strip(),
'',
block_content[to_new],
'',
])
with open(CONF_FILE, 'w') as f:
f.write(content)
setConf(False, True)
os.system('sudo systemctl restart NetworkManager')
print('NetworkManager out of the way.')
try:
yield
finally:
setConf(True, False)
os.system('sudo systemctl restart NetworkManager')
print('NetworkManager restored.')
def main(
ssid: str, wpa_passphrase: str,
gateway_ip: str = DEFAULT_GATEWAY_IP,
hostapd_conf: dict = {},
dnsmasq_conf: dict = {},
):
if os.geteuid() != 0:
print('Error: sudo required')
return
writeHostapdConf(ssid, wpa_passphrase, hostapd_conf)
writeDnsmasqConf(dnsmasq_conf)
with UnmanageNetwork():
with Popen(['hostapd', tempFileName(HOSTAPD)]) as hostapd:
os.system(f'sudo ip addr add {gateway_ip}/24 dev {interface()}')
with Popen(['dnsmasq', '-C', tempFileName(DNSMASQ), '-d']) as dnsmasq:
print('Hotspot running. Press ctrl+C to stop.')
try:
dnsmasq.wait()
except KeyboardInterrupt:
print('Quiting...')
dnsmasq.terminate()
dnsmasq.wait()
else:
print('unexpected exit by dnsmasq')
hostapd.terminate()
hostapd.wait()
def parseArgs():
parser = argparse.ArgumentParser()
parser.add_argument('ssid', type=str)
parser.add_argument('wpa_passphrase', type=str)
parser.add_argument('gateway_ip', type=str, nargs='?', default=DEFAULT_GATEWAY_IP)
args = parser.parse_args()
return args.ssid, args.wpa_passphrase, args.gateway_ip
if __name__ == '__main__':
main(*parseArgs())