parent
1a9c4a5698
commit
6f4a00b079
@ -0,0 +1,2 @@
|
||||
test.dat
|
||||
socks/__pycache__/
|
@ -0,0 +1,66 @@
|
||||
import sys
|
||||
import os
|
||||
import os.path
|
||||
import traceback
|
||||
import json
|
||||
import socket
|
||||
import struct
|
||||
import pprint
|
||||
import time
|
||||
import datetime
|
||||
import threading
|
||||
import json
|
||||
import re
|
||||
import time
|
||||
from threading import Thread, Lock
|
||||
import binascii
|
||||
import pylzma
|
||||
import select
|
||||
from subprocess import Popen, PIPE
|
||||
import argparse
|
||||
|
||||
from socks.transmission import SocketOverlay
|
||||
from socks.transmission import Command
|
||||
|
||||
def parsecmd(s):
|
||||
if s=="get": return Command.CMD_GET
|
||||
elif s=="stop": return Command.CMD_SHUTDOWN
|
||||
elif s=="clear": return Command.CMD_CLEAR
|
||||
else: return None
|
||||
|
||||
parser = argparse.ArgumentParser(description="rtbwpy daemon control.")
|
||||
parser.add_argument("sock", help="AF_UNIX socket")
|
||||
parser.add_argument("command", help="Command")
|
||||
parser.add_argument("--data", help="Additional data to send", default=None)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
cmd = parsecmd(args.command)
|
||||
|
||||
if cmd==None:
|
||||
print("Invalid command.")
|
||||
os._exit(1)
|
||||
|
||||
data = args.data
|
||||
|
||||
if cmd == Command.CMD_GET:
|
||||
if data== None:
|
||||
data = struct.pack("L", 0)
|
||||
else:
|
||||
data = struct.pack("L", int(data))
|
||||
|
||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
sock.connect(args.sock)
|
||||
|
||||
con = SocketOverlay(sock)
|
||||
|
||||
cmd = Command.build(cmd, data)
|
||||
|
||||
con.send(cmd.serialise())
|
||||
|
||||
if cmd.uCommand == Command.CMD_GET:
|
||||
json = con.recv().decode("utf-8")
|
||||
print(json)
|
||||
|
||||
sock.close()
|
||||
|
@ -0,0 +1,56 @@
|
||||
import os
|
||||
#import json
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import binascii
|
||||
import struct
|
||||
|
||||
class SocketOverlay(object):
|
||||
def __init__(self, socket):
|
||||
self.socket = socket
|
||||
|
||||
def recv(self):
|
||||
li = self.socket.recv(4)
|
||||
l = struct.unpack("I", li)[0]
|
||||
return self.socket.recv(l)
|
||||
|
||||
def send(self, data):
|
||||
l = len(data)
|
||||
self.socket.send(struct.pack("I", l))
|
||||
self.socket.send(data)
|
||||
|
||||
def close(self):
|
||||
self.socket.close()
|
||||
|
||||
class Command(object):
|
||||
CMD_GET = 1
|
||||
CMD_CLEAR = 2
|
||||
CMD_SHUTDOWN = 3
|
||||
|
||||
def __init__(self):
|
||||
self.uCommand = 0
|
||||
self.uData = None
|
||||
|
||||
def serialise(self):
|
||||
if self.uData==None:
|
||||
return struct.pack("I", self.uCommand)+struct.pack("I", 0)
|
||||
else:
|
||||
return struct.pack("I", self.uCommand)+struct.pack("I", len(self.uData))+self.uData
|
||||
|
||||
@classmethod
|
||||
def build(cls, command, data=None):
|
||||
c = Command()
|
||||
c.uCommand = command
|
||||
c.uData = data
|
||||
return c
|
||||
|
||||
@classmethod
|
||||
def unserialise(cls, data):
|
||||
cmd = struct.unpack("I", data[:4])[0]
|
||||
ds = struct.unpack("I", data[4:8])[0]
|
||||
if ds<1:
|
||||
return Command.build(cmd)
|
||||
else:
|
||||
return Command.build(cmd, data[8:8+ds])
|
||||
|
Loading…
Reference in new issue