chiark / gitweb /
wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Mar 2017 20:05:51 +0000 (20:05 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Mar 2017 20:05:51 +0000 (20:05 +0000)
server
test.cfg [new file with mode: 0644]

diff --git a/server b/server
index a139e6e..fb6bcc5 100755 (executable)
--- a/server
+++ b/server
@@ -2,8 +2,8 @@
 
 import twisted
 
-import twisted.web.server import Site
-from twisted.web.resource import Resource
+#import twisted.web.server import Site
+#from twisted.web.resource import Resource
 from twisted.web.server import NOT_DONE_YET
 from twisted.internet import reactor
 
@@ -46,6 +46,8 @@ mtu = 1500
 
 [server]
 ipif = userv root ipif %(host),%(relay),%(mtu),slip %(network)
+addrs = 127.0.0.1 ::1
+port = 80
 
 [limits]
 max_batch_down = 262144
@@ -53,6 +55,26 @@ max_queue_time = 121
 max_request_time = 121
 '''
 
+#---------- "router" ----------
+
+def route(packet. daddr):
+  try: client = clients[daddr]
+  except KeyError: dclient = None
+  if dclient is not None:
+    dclient.queue_outbound(packet)
+  else if daddr = host or daddr not in network:
+    queue_inbound(packet)
+  else if daddr = relay:
+    log_discard(packet, saddr, daddr, 'relay')
+  else:
+    log_discard(packet, saddr, daddr, 'no client')
+
+def log_discard(packet, saddr, daddr, why):
+  syslog.syslog(syslog.LOG_DEBUG,
+                'discarded packet %s -> %s (%s)' % (saddr, daddr, why))
+
+#---------- ipif (slip subprocess) ----------
+
 class IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
   def __init__(self):
     self._buffer = b''
@@ -64,27 +86,22 @@ class IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
     for packet in packets:
       (saddr, daddr) = packet_addrs(packet)
       route(packet, daddr)
+  def processEnded(self, status):
+    status.raiseException()
 
 def start_ipif():
-  reactor.spawnProcess(IpifProcessProtocol(),
+  global ipif
+  ipif = IpifProcessProtocol()
+  reactor.spawnProcess(ipif,
                        '/bin/sh',['-c', ipif_command],
                        childFDs={0:'w', 1:'r', 2:2})
 
-def log_discard(packet, saddr, daddr, why):
-  syslog.syslog(syslog.LOG_DEBUG,
-                'discarded packet %s -> %s (%s)' % (saddr, daddr, why))
+def queue_inbound(packet):
+  ipif.transport.write(slip_delimiter)
+  ipif.transport.write(slip_encode(packet))
+  ipif.transport.write(slip_delimiter)
 
-def route(packet. daddr):
-  try: client = clients[daddr]
-  except KeyError: dclient = None
-  if dclient is not None:
-    dclient.queue_outbound(packet)
-  else if daddr = host or daddr not in network:
-    queue_inbound(packet)
-  else if daddr = relay:
-    log_discard(packet, saddr, daddr, 'relay')
-  else:
-    log_discard(packet, saddr, daddr, 'no client')
+#---------- client ----------
 
 class Client():
   def __init__(self, ip, cs):
@@ -170,6 +187,44 @@ class Client():
         request.finish()
         # round again, looking for more to do
 
+class IphttpResource(twisted.web.resource.Resource):
+  def render_POST(self, request):
+    # find client, update config, etc.
+    ci = ipaddress(request.args['i'])
+    c = clients[ci]
+    pw = request.args['pw']
+    if pw != c.pw: raise ValueError('bad password')
+
+    # update config
+    for r, w in (('mbd', 'max_batch_down'),
+                 ('mqt', 'max_queue_time'),
+                 ('mrt', 'max_request_time')):
+      try: v = request.args[r]
+      except KeyError: continue
+      v = int(v)
+      c.__dict__[w] = v
+
+    try: d = request.args['d']
+    except KeyError: d = ''
+
+    c.process_arriving_data(d)
+    c.new_request(request)
+
+def start_http():
+  resource = IphttpResource()
+  sitefactory = twisted.web.server.Site(resource)
+  for addrspec in cfg.get('server','addresses').split():
+    try:
+      addr = ipaddress.IPv4Address(addrspec)
+      endpointfactory = twisted.internet.endpoints.TCP4ServerEndpoint
+    except AddressValueError:
+      addr = ipaddress.IPv6Address(addrspec)
+      endpointfactory = twisted.internet.endpoints.TCP6ServerEndpoint
+    ep = endpointfactory(reactor, cfg.getint('server','port'), addr)
+    ep.listen(sitefactory)
+
+#---------- config and setup ----------
+        
 def process_cfg():
   global network
   global host
@@ -208,29 +263,6 @@ def process_cfg():
 
   ipif_command = cfg.get('server','ipif', vars=iic_vars)
 
-class FormPage(Resource):
-  def render_POST(self, request):
-    # find client, update config, etc.
-    ci = ipaddress(request.args['i'])
-    c = clients[ci]
-    pw = request.args['pw']
-    if pw != c.pw: raise ValueError('bad password')
-
-    # update config
-    for r, w in (('mbd', 'max_batch_down'),
-                 ('mqt', 'max_queue_time'),
-                 ('mrt', 'max_request_time')):
-      try: v = request.args[r]
-      except KeyError: continue
-      v = int(v)
-      c.__dict__[w] = v
-
-    try: d = request.args['d']
-    except KeyError: d = ''
-
-    c.process_arriving_data(d)
-    c.new_request(request)
-
 def startup():
   op = OptionParser()
   op.add_option('-c', '--config', dest='configfile',
diff --git a/test.cfg b/test.cfg
new file mode 100644 (file)
index 0000000..fd4cc7f
--- /dev/null
+++ b/test.cfg
@@ -0,0 +1,2 @@
+[server]
+addrs = 127.0.0.1