Skip to content

Instantly share code, notes, and snippets.

@zachdekoning
Forked from barneygale/throttlepatch.py
Created March 9, 2013 22:55
Show Gist options
  • Select an option

  • Save zachdekoning/5126143 to your computer and use it in GitHub Desktop.

Select an option

Save zachdekoning/5126143 to your computer and use it in GitHub Desktop.
from os import chdir, path
from tempfile import mkdtemp
from shutil import rmtree
from struct import pack
from subprocess import check_output
def jar_contents(j_path):
return check_output(['jar', 'tf', j_path]).split("\n")
def jar_extract(j_path):
return check_output(['jar', 'xf', j_path])
def jar_update(j_path, t_path, c_path):
c_path = c_path[len(t_path)+1:]
return check_output(['jar', 'uf', j_path, '-C', t_path, c_path])
def jlong(v):
return pack('>q', v)
def jstring(v):
return pack('>h', len(v)) + v
def main(j_path, t_path):
print "... checking jar"
j_path = path.realpath(j_path)
if not path.isfile(j_path):
print "### not found: %s" % j_path
return
print "... getting jar contents"
j_contents = jar_contents(j_path)
print "... extracting jar"
chdir(t_path)
jar_extract(j_path)
print "... searching for DedicatedServerConnectionThread"
search = jstring('Listen thread')
replace = (jlong(4000), jlong(0))
c_path = None
c_data = None
for name in j_contents:
name = "%s/%s" % (t_path, name)
if not name.endswith('.class'):
continue
with open(name, 'rb') as f:
data = f.read()
if search in data:
c_path = name
c_data = data
break
if c_path is None:
print "### couldn't find class!"
return
print "... patching"
if not replace[0] in c_data:
print "### already patched!"
return
c_data = c_data.replace(*replace)
print "... saving"
with open(c_path, 'wb') as f:
f.write(c_data)
jar_update(j_path, t_path, c_path)
print "... done!"
if __name__ == '__main__':
import sys
if len(sys.argv) != 2:
print "usage: python throttlepath.py jar_path"
else:
t_path = mkdtemp(prefix='throttle-patch-')
main(sys.argv[1], t_path)
rmtree(t_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment