from pwn import *
import numpy as np
from skyfield.api import load
import astropy.units

satellites = load.tle_file('./stations.txt')

ts = load.timescale()

r = tubes.remote.remote('where.satellitesabove.me', 5021)
r.clean()
r.send('THE_TICKET\n')


r.readuntil('find the correct satellite:')
t = ts.utc(*[float(x) for x in r.readuntil('\n').decode().strip()[1:-1].split(', ')])

r.readuntil('coordinates to find the satellite:')
eci_coords = np.array([float(x) for x in r.readuntil('\n').decode().strip()[1:-1].split(', ')])

match = satellites[np.argmin([np.linalg.norm(s.at(t).position.km-eci_coords) for s in satellites])]

for _ in range(3):
	r.readuntil('X coordinate at the time of:')
	new_t = ts.utc(*[float(x)
		for x in r.readuntil('?\n', drop=True).decode().strip()[1:-1].split(', ')])
	print(new_t.utc_jpl())

	x,y,z = match.at(new_t).position.km
	r.send(f'{x}\n')
	r.send(f'{y}\n')
	r.send(f'{z}\n')

r.interactive()