from pwn import *
import time

r = remote('bytes.satellitesabove.me', 5042)
r.clean()
r.send('THE_TICKET')
time.sleep(0.1)
r.readuntil('tcp:')
[ip, port] = r.readuntil('\n').decode().strip().split(':')
port = int(port)

TEMPL='''
TITLE 'cFS Command and Telemetry Server'

# Can't map same target to two interfaces. Current strategy is to always connect locally
# and allow a switch to a PiSat interface. Another option may be to start both interfaces
# and then remap the targets.
 INTERFACE LOCAL_CFS_INT tcpip_client_interface.rb {ip} {port} {port} 10 nil
  PROTOCOL WRITE ChecksumProtocol

  TARGET SYSTEM
  
  TARGET CFE_ES
  TARGET CFE_EVS
  TARGET CFE_SB
  TARGET CFE_TBL
  TARGET CFE_TIME
  TARGET MM
  TARGET KIT_CI
  TARGET KIT_SCH
  TARGET KIT_TO
  
  
INTERFACE COSMOSINT cmd_tlm_server_interface.rb
  TARGET COSMOS
  DISABLE_DISCONNECT
  

'''

with open('cosmos/config/tools/cmd_tlm_server/cmd_tlm_server.txt', 'w') as f:
    f.write(TEMPL.format(ip=ip, port=port))

r.interactive()
