-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfake_ntp_server.py
executable file
·73 lines (58 loc) · 2.09 KB
/
fake_ntp_server.py
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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import socket
import time
from ntp_packet import (
NTPPacket,
NTPTimestamp,
)
def fake_server(address):
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind( (address, 123) )
drift_start = time.time()
speed = 0.9996
while True:
packet, source = s.recvfrom(100)
print('Received {length} byte packet from {source}'.format(length=len(packet), source=source[0]))
packet = NTPPacket.from_bytes(packet)
if packet.version not in (3, 4):
print('Unsupported NTP version.')
continue
if packet.mode != 3:
print('Not a client request.')
continue
try:
print('Client time:', packet.transmit_timestamp)
except:
pass
time_since_start = time.time() - drift_start
return_time = drift_start + time_since_start * speed
response = NTPPacket()
response.version = packet.version
response.mode = 4
response.stratum = 1
response.reference_identifier = b'XFAK'
response.origin_timestamp = packet.transmit_timestamp
response.reference_timestamp = NTPTimestamp.from_unix_timestamp(return_time)
response.receive_timestamp = NTPTimestamp.from_unix_timestamp(return_time)
response.transmit_timestamp = NTPTimestamp.from_unix_timestamp(return_time)
response = response.to_bytes()
s.sendto(response, source)
def parse_bind_addr(address):
address = socket.getaddrinfo(address, 123, 0, 0, 0, socket.AI_NUMERICHOST)
address = address[0]
if address[0] == socket.AF_INET:
return '::ffff:' + address[4][0]
else:
return address[4][0]
def parse_args():
parser = argparse.ArgumentParser(description='Fake NTP server')
parser.add_argument('--listen', type=parse_bind_addr, default='::')
return parser.parse_args()
def main():
args = parse_args()
fake_server(args.listen)
if __name__ == '__main__':
main()