Soreepeong / XivMitmLatencyMitigator

Double weave on high latency man-in-the-middle packet modification script running on Linux for Final Fantasy XIV.
Apache License 2.0
60 stars 13 forks source link

Cleanup named "<stdin>.cleanup.sh" and oodle_helper placed in / instead of working directory #66

Closed ghost closed 1 year ago

ghost commented 1 year ago

When I manually execute run.sh the cleanup happens without problems on Ctrl+C. When started as a systemd service, the following happens:

I can only assume the lack of cleanup is due to a name mismatch but I don't quite get why oodle_helper is placed in /. This happens if I manually execute run.sh and if I manually pipe the curl output in python. It should be mentioned that I'm running this in a live CD, similarly to how Arch is run the documentation example. I did install and use this script on a normal Debian installation in the past so I know that it works for sure on Debian.

root@0:~# ls
 definitions.json   ffxiv.exe   run.sh  '<stdin>.cleanup.sh'
[Unit]
Description=XivAlexander script

[Service]
Type=simple
User=root
WorkingDirectory=/root
ExecStart=/bin/bash run.sh
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
#!/bin/bash

curl https://raw.githubusercontent.com/Soreepeong/XivMitmLatencyMitigator/main/mitigate.py | python3
Soreepeong commented 1 year ago

https://github.com/Soreepeong/XivMitmLatencyMitigator/blob/a32f36233077da9bcff51e9f6101c915fb954664/mitigate.py#L817-L832

In fact, if mitigate.py has been run via pipe, then __file__ would be empty, and this oodle_helper_path would end up becoming /oodle_helper. This path probably can just be os.path.join(os.getcwd(), "oodle_helper") instead.

ghost commented 1 year ago

I replaced os.path.join() both for cleanup.sh and oodle_helper and they are correctly placed in the working directory but the PREROUTING rules are still there after the service stops meaning the cleanup script isn't being run

root@0:~# iptables -t nat -F PREROUTING
root@0:~# iptables -t nat -L PREROUTING
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
root@0:~# service xivalexander start
root@0:~# ls -A xivalexander/
.cleanup.sh  definitions.json  ffxiv.exe  oodle_helper  run.sh
root@0:~# service xivalexander stop
root@0:~# iptables -t nat -L PREROUTING
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
REDIRECT   tcp  --  anywhere             27.221.0.0/16        multiport dports tcpmux:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             202.67.52.0/24       multiport dports 1025:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             162.14.0.0/16        multiport dports tcpmux:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             80-239-145-0.teliacarrier-cust.com/24  multiport dports 1025:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             153.254.80.0/24      multiport dports 1025:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             204.2.229.0/24       multiport dports 1025:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             124.150.157.0/24     multiport dports 1025:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             109.244.0.0/16       multiport dports tcpmux:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             183.111.189.0/24     multiport dports 1025:65535 redir ports 45244
REDIRECT   tcp  --  anywhere             119.97.0.0/16        multiport dports tcpmux:65535 redir ports 45244
ghost commented 1 year ago

As a workaround I added ExecStop=/bin/bash .cleanup.sh to my service but the file keeps staying in the working directory even when mitigate.py isn't running

https://github.com/Soreepeong/XivMitmLatencyMitigator/blob/a32f36233077da9bcff51e9f6101c915fb954664/mitigate.py#L1657

ghost commented 1 year ago

The journalctl logs for the xivalexander service

Oct 22 13:10:48 finnix systemd[1]: Started XivAlexander script.
Oct 22 13:10:48 finnix bash[5795]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Oct 22 13:10:48 finnix bash[5795]:                                  Dload  Upload   Total   Spent    Left  Speed
Oct 22 13:10:48 finnix bash[5795]: [158B blob data]
Oct 22 13:10:48 finnix bash[5796]: 2022-10-22 13:10:48,544        5796(main)        INFO        Region filter: (None)
Oct 22 13:10:48 finnix bash[5796]: 2022-10-22 13:10:48,544        5796(main)        INFO        Extra delay: 0.075s
Oct 22 13:10:48 finnix bash[5796]: 2022-10-22 13:10:48,544        5796(main)        INFO        Use measured socket latency: no
Oct 22 13:10:49 finnix bash[5803]: Base: 0xf3d40000
Oct 22 13:10:49 finnix bash[5803]: Oodle encode test: 256 -> 100
Oct 22 13:10:49 finnix bash[5803]: Oodle helper running: state=2eb400 shared=400018 window=8000
Oct 22 13:10:49 finnix bash[5806]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5807]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5808]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5809]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5810]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5811]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5812]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5813]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5814]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5815]: iptables: Bad rule (does a matching rule exist in that chain?).
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,592        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1025:65535 -d 202.67.52.0/24 -j REDIRECT --t>
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,596        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1025:65535 -d 204.2.229.0/24 -j REDIRECT --t>
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,599        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1025:65535 -d 124.150.157.0/24 -j REDIRECT ->
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,603        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1025:65535 -d 183.111.189.0/24 -j REDIRECT ->
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,607        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1:65535 -d 162.14.0.0/16 -j REDIRECT --to 24>
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,611        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1:65535 -d 109.244.0.0/16 -j REDIRECT --to 2>
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,615        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1025:65535 -d 80.239.145.0/24 -j REDIRECT -->
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,619        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1:65535 -d 27.221.0.0/16 -j REDIRECT --to 24>
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,623        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1:65535 -d 119.97.0.0/16 -j REDIRECT --to 24>
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,627        5796(main)        INFO        Running: iptables -t nat -I PREROUTING -p tcp -m multiport --dports 1025:65535 -d 153.254.80.0/24 -j REDIRECT -->
Oct 22 13:10:49 finnix bash[5837]: net.ipv4.ip_forward = 1
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,634        5796(main)        INFO        Listening on ('0.0.0.0', 24121)...
Oct 22 13:10:49 finnix bash[5796]: 2022-10-22 13:10:49,634        5796(main)        INFO        Press Ctrl+C to quit.

The iptables: Bad rule (does a matching rule exist in that chain?). error was caused by me running with nftables active. Switching to legacy mode solved that error

update-alternatives --set iptables /usr/sbin/iptables-legacy

but the rules yet remain in the iptables

ghost commented 1 year ago

Running the run.sh script manually correctly removes the iptables rules and the cleanup script. I basically never touched python before but I think it's due to is_child boolean not being True

https://github.com/Soreepeong/XivMitmLatencyMitigator/blob/a32f36233077da9bcff51e9f6101c915fb954664/mitigate.py#L1617-L1657

ghost commented 1 year ago

This issue has been resolved thanks to https://github.com/Soreepeong/XivMitmLatencyMitigator/issues/66#issuecomment-1287760129 since .cleanup.sh is being now named properly and oodle_helper is being placed in the working directory so I'll just close this issue and maybe open a new one for the systemd problem.