Skip to content
Permalink
Browse files

translate the mk(1) client directly into python to form the basis for…

… mk(2)
  • Loading branch information...
Ximin Luo
Ximin Luo committed Aug 1, 2013
1 parent f490c7e commit 41b7608aa4ea79dd805558d7f377a51182c537b0
Showing with 105 additions and 0 deletions.
  1. +104 −0 mkii/obfs-flash-client
  2. +1 −0 mkii/torrc
@@ -0,0 +1,104 @@
#!/usr/bin/python

import atexit
import os
import signal
import subprocess
import sys
import time

from subprocess import Popen

ob_client = os.getenv("OBFSPROXY", "obfsproxy")
fp_client = os.getenv("FLASHPROXY_CLIENT", "flashproxy-client")
fp_local_port = int(os.getenv("FLASHPROXY_LOCAL_PORT", 9001))
fp_remote_port = int(os.getenv("FLASHPROXY_REMOTE_PORT", 9000))

mid_port = 2334

managed_ver = os.getenv("TOR_PT_MANAGED_TRANSPORT_VER")
if managed_ver != "1":
print "VERSION-ERROR no-version"
sys.exit(1)
print "VERSION " + managed_ver

transports = os.getenv("TOR_PT_CLIENT_TRANSPORTS")

if transports not in ["obfs3_flash", "*"]:
print "CMETHODS DONE"
sys.exit(1)

child_procs = []
def termChildren(signum=0, sframe=None):
# terminate all
for proc in child_procs:
if proc.poll() is None:
proc.terminate()
# wait and make sure they're dead
for i in xrange(16):
for proc in child_procs:
if proc.poll() is not None:
child_procs.remove(proc)
if not child_procs: break
time.sleep(1)
# if still existing, kill them
if child_procs:
for proc in child_procs:
if proc.poll() is None:
proc.kill()

atexit.register(termChildren)
signal.signal(signal.SIGINT, termChildren)
signal.signal(signal.SIGTERM, termChildren)

cur_env = os.environ.items()

def writeSink():
return open("/dev/null", "w", 0)

fp_proc = Popen(
[fp_client, ":%s" % fp_local_port, ":%s" % fp_remote_port],
stdout = writeSink(),
env = dict(cur_env + {
"TOR_PT_MANAGED_TRANSPORT_VER": managed_ver,
#"TOR_PT_STATE_LOCATION": same,
"TOR_PT_CLIENT_TRANSPORTS": "websocket",
}.items()),
)
child_procs.append(fp_proc)

mid_proc = Popen(
["socat",
"TCP-LISTEN:%s,reuseaddr,fork,bind=127.0.0.1" % mid_port,
"SOCKS4A:127.0.0.1:0.0.1.0:1,socksport=%s" % fp_local_port],
stdout = writeSink(),
)
child_procs.append(mid_proc)

ob_proc = Popen(
[ob_client, "managed"],
stdout = subprocess.PIPE,
env = dict(cur_env + {
"TOR_PT_MANAGED_TRANSPORT_VER": managed_ver,
#"TOR_PT_STATE_LOCATION": same,
"TOR_PT_CLIENT_TRANSPORTS": "obfs3",
}.items()),
)
child_procs.append(ob_proc)

while 1:
# readlines() has a stupid non-bypassable buffer which hangs everything
line = ob_proc.stdout.readline()
if not line: break
# probably should import pyptlib for anything more complex
(kw, args) = line.split(" ", 1)
if kw == "VERSION":
pass
elif kw == "CMETHOD" and args.startswith("obfs3 "):
sys.stdout.write(line.replace("CMETHOD obfs3 ", "CMETHOD obfs3_flash ", 1))
else:
sys.stdout.write(line)
sys.stdout.flush()

# TODO(infinity0): bail when any one child dies, not just obfsproxy
ob_proc.wait()

0 comments on commit 41b7608

Please sign in to comment.
You can’t perform that action at this time.