We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Mitmweb can't run
When I run mitmweb,then open 121.0.0.1:8081, the error happened.

```
Traceback (most recent call last):
  File "/Users/venv.mitmproxy/lib/python2.7/site-packages/tornado/ioloop.py", line 1020, in _run
    return self.callback()
  File "/Users/mitmproxy/libmproxy/web/__init__.py", line 141, in tick
    flow.FlowMaster.tick(self, self.masterq, timeout=0)
  File "/Users/mitmproxy/libmproxy/flow.py", line 812, in tick
    return super(FlowMaster, self).tick(q, timeout)
  File "/Users/mitmproxy/libmproxy/controller.py", line 108, in tick
    self.handle(*msg)
  File "/Users/mitmproxy/libmproxy/controller.py", line 129, in handle
    m(obj)
  File "/Users/mitmproxy/libmproxy/web/__init__.py", line 167, in handle_request
    super(WebMaster, self).handle_request(f)
  File "/Users/mitmproxy/libmproxy/flow.py", line 990, in handle_request
    self.state.add_flow(f)
  File "/Users/mitmproxy/libmproxy/flow.py", line 561, in add_flow
    self.flows._add(f)
  File "/Users/mitmproxy/libmproxy/flow.py", line 469, in _add
    view._add(f)
  File "/Users/mitmproxy/libmproxy/web/__init__.py", line 23, in _add
    data=f.get_state(short=True)
  File "/Users/mitmproxy/libmproxy/models/flow.py", line 88, in get_state
    d = super(Flow, self).get_state(short)
  File "/Users/mitmproxy/libmproxy/stateobject.py", line 32, in get_state
    state[attr] = val.get_state(short)
  File "/Users/mitmproxy/libmproxy/models/connections.py", line 138, in get_state
    address={"address": self.address(),
TypeError: 'NoneType' object is not callable
ERROR:tornado.application:Exception in callback <bound method WebMaster.tick of <libmproxy.web.WebMaster object at 0x10cfe5a90>>
```

Mitmweb can't run
When I run mitmweb,then open 121.0.0.1:8081, the error happened.

```
Traceback (most recent call last):
  File "/Users/venv.mitmproxy/lib/python2.7/site-packages/tornado/ioloop.py", line 1020, in _run
    return self.callback()
  File "/Users/mitmproxy/libmproxy/web/__init__.py", line 141, in tick
    flow.FlowMaster.tick(self, self.masterq, timeout=0)
  File "/Users/mitmproxy/libmproxy/flow.py", line 812, in tick
    return super(FlowMaster, self).tick(q, timeout)
  File "/Users/mitmproxy/libmproxy/controller.py", line 108, in tick
    self.handle(*msg)
  File "/Users/mitmproxy/libmproxy/controller.py", line 129, in handle
    m(obj)
  File "/Users/mitmproxy/libmproxy/web/__init__.py", line 167, in handle_request
    super(WebMaster, self).handle_request(f)
  File "/Users/mitmproxy/libmproxy/flow.py", line 990, in handle_request
    self.state.add_flow(f)
  File "/Users/mitmproxy/libmproxy/flow.py", line 561, in add_flow
    self.flows._add(f)
  File "/Users/mitmproxy/libmproxy/flow.py", line 469, in _add
    view._add(f)
  File "/Users/mitmproxy/libmproxy/web/__init__.py", line 23, in _add
    data=f.get_state(short=True)
  File "/Users/mitmproxy/libmproxy/models/flow.py", line 88, in get_state
    d = super(Flow, self).get_state(short)
  File "/Users/mitmproxy/libmproxy/stateobject.py", line 32, in get_state
    state[attr] = val.get_state(short)
  File "/Users/mitmproxy/libmproxy/models/connections.py", line 138, in get_state
    address={"address": self.address(),
TypeError: 'NoneType' object is not callable
ERROR:tornado.application:Exception in callback <bound method WebMaster.tick of <libmproxy.web.WebMaster object at 0x10cfe5a90>>
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `libmproxy/models/connections.py`
Content:
```
1 from __future__ import (absolute_import, print_function, division)
2 
3 import copy
4 import os
5 
6 from netlib import tcp, certutils
7 from .. import stateobject, utils
8 
9 
10 class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
11     def __init__(self, client_connection, address, server):
12         # Eventually, this object is restored from state. We don't have a
13         # connection then.
14         if client_connection:
15             super(ClientConnection, self).__init__(client_connection, address, server)
16         else:
17             self.connection = None
18             self.server = None
19             self.wfile = None
20             self.rfile = None
21             self.address = None
22             self.clientcert = None
23             self.ssl_established = None
24 
25         self.timestamp_start = utils.timestamp()
26         self.timestamp_end = None
27         self.timestamp_ssl_setup = None
28         self.protocol = None
29 
30     def __nonzero__(self):
31         return bool(self.connection) and not self.finished
32 
33     def __repr__(self):
34         return "<ClientConnection: {ssl}{host}:{port}>".format(
35             ssl="[ssl] " if self.ssl_established else "",
36             host=self.address.host,
37             port=self.address.port
38         )
39 
40     @property
41     def tls_established(self):
42         return self.ssl_established
43 
44     _stateobject_attributes = dict(
45         ssl_established=bool,
46         timestamp_start=float,
47         timestamp_end=float,
48         timestamp_ssl_setup=float
49     )
50 
51     def get_state(self, short=False):
52         d = super(ClientConnection, self).get_state(short)
53         d.update(
54             address={
55                 "address": self.address(),
56                 "use_ipv6": self.address.use_ipv6},
57             clientcert=self.cert.to_pem() if self.clientcert else None)
58         return d
59 
60     def load_state(self, state):
61         super(ClientConnection, self).load_state(state)
62         self.address = tcp.Address(
63             **state["address"]) if state["address"] else None
64         self.clientcert = certutils.SSLCert.from_pem(
65             state["clientcert"]) if state["clientcert"] else None
66 
67     def copy(self):
68         return copy.copy(self)
69 
70     def send(self, message):
71         if isinstance(message, list):
72             message = b''.join(message)
73         self.wfile.write(message)
74         self.wfile.flush()
75 
76     @classmethod
77     def from_state(cls, state):
78         f = cls(None, tuple(), None)
79         f.load_state(state)
80         return f
81 
82     def convert_to_ssl(self, *args, **kwargs):
83         super(ClientConnection, self).convert_to_ssl(*args, **kwargs)
84         self.timestamp_ssl_setup = utils.timestamp()
85 
86     def finish(self):
87         super(ClientConnection, self).finish()
88         self.timestamp_end = utils.timestamp()
89 
90 
91 class ServerConnection(tcp.TCPClient, stateobject.StateObject):
92     def __init__(self, address):
93         tcp.TCPClient.__init__(self, address)
94 
95         self.via = None
96         self.timestamp_start = None
97         self.timestamp_end = None
98         self.timestamp_tcp_setup = None
99         self.timestamp_ssl_setup = None
100         self.protocol = None
101 
102     def __nonzero__(self):
103         return bool(self.connection) and not self.finished
104 
105     def __repr__(self):
106         if self.ssl_established and self.sni:
107             ssl = "[ssl: {0}] ".format(self.sni)
108         elif self.ssl_established:
109             ssl = "[ssl] "
110         else:
111             ssl = ""
112         return "<ServerConnection: {ssl}{host}:{port}>".format(
113             ssl=ssl,
114             host=self.address.host,
115             port=self.address.port
116         )
117 
118     @property
119     def tls_established(self):
120         return self.ssl_established
121 
122     _stateobject_attributes = dict(
123         timestamp_start=float,
124         timestamp_end=float,
125         timestamp_tcp_setup=float,
126         timestamp_ssl_setup=float,
127         address=tcp.Address,
128         source_address=tcp.Address,
129         cert=certutils.SSLCert,
130         ssl_established=bool,
131         sni=str
132     )
133     _stateobject_long_attributes = {"cert"}
134 
135     def get_state(self, short=False):
136         d = super(ServerConnection, self).get_state(short)
137         d.update(
138             address={"address": self.address(),
139                      "use_ipv6": self.address.use_ipv6},
140             source_address=({"address": self.source_address(),
141                              "use_ipv6": self.source_address.use_ipv6} if self.source_address else None),
142             cert=self.cert.to_pem() if self.cert else None
143         )
144         return d
145 
146     def load_state(self, state):
147         super(ServerConnection, self).load_state(state)
148 
149         self.address = tcp.Address(
150             **state["address"]) if state["address"] else None
151         self.source_address = tcp.Address(
152             **state["source_address"]) if state["source_address"] else None
153         self.cert = certutils.SSLCert.from_pem(
154             state["cert"]) if state["cert"] else None
155 
156     @classmethod
157     def from_state(cls, state):
158         f = cls(tuple())
159         f.load_state(state)
160         return f
161 
162     def copy(self):
163         return copy.copy(self)
164 
165     def connect(self):
166         self.timestamp_start = utils.timestamp()
167         tcp.TCPClient.connect(self)
168         self.timestamp_tcp_setup = utils.timestamp()
169 
170     def send(self, message):
171         if isinstance(message, list):
172             message = b''.join(message)
173         self.wfile.write(message)
174         self.wfile.flush()
175 
176     def establish_ssl(self, clientcerts, sni, **kwargs):
177         clientcert = None
178         if clientcerts:
179             path = os.path.join(
180                 clientcerts,
181                 self.address.host.encode("idna")) + ".pem"
182             if os.path.exists(path):
183                 clientcert = path
184 
185         self.convert_to_ssl(cert=clientcert, sni=sni, **kwargs)
186         self.sni = sni
187         self.timestamp_ssl_setup = utils.timestamp()
188 
189     def finish(self):
190         tcp.TCPClient.finish(self)
191         self.timestamp_end = utils.timestamp()
192 
193 
194 ServerConnection._stateobject_attributes["via"] = ServerConnection
195
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/libmproxy/models/connections.py b/libmproxy/models/connections.py
--- a/libmproxy/models/connections.py
+++ b/libmproxy/models/connections.py
@@ -135,8 +135,8 @@
     def get_state(self, short=False):
         d = super(ServerConnection, self).get_state(short)
         d.update(
-            address={"address": self.address(),
-                     "use_ipv6": self.address.use_ipv6},
+            address=({"address": self.address(),
+                     "use_ipv6": self.address.use_ipv6} if self.address else {}), 
             source_address=({"address": self.source_address(),
                              "use_ipv6": self.source_address.use_ipv6} if self.source_address else None),
             cert=self.cert.to_pem() if self.cert else None

{"golden_diff": "diff --git a/libmproxy/models/connections.py b/libmproxy/models/connections.py\n--- a/libmproxy/models/connections.py\n+++ b/libmproxy/models/connections.py\n@@ -135,8 +135,8 @@\n     def get_state(self, short=False):\n         d = super(ServerConnection, self).get_state(short)\n         d.update(\n-            address={\"address\": self.address(),\n-                     \"use_ipv6\": self.address.use_ipv6},\n+            address=({\"address\": self.address(),\n+                     \"use_ipv6\": self.address.use_ipv6} if self.address else {}), \n             source_address=({\"address\": self.source_address(),\n                              \"use_ipv6\": self.source_address.use_ipv6} if self.source_address else None),\n             cert=self.cert.to_pem() if self.cert else None\n", "issue": "Mitmweb can't run\nWhen I run mitmweb,then open 121.0.0.1:8081, the error happened.\n\n```\nTraceback (most recent call last):\n  File \"/Users/venv.mitmproxy/lib/python2.7/site-packages/tornado/ioloop.py\", line 1020, in _run\n    return self.callback()\n  File \"/Users/mitmproxy/libmproxy/web/__init__.py\", line 141, in tick\n    flow.FlowMaster.tick(self, self.masterq, timeout=0)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 812, in tick\n    return super(FlowMaster, self).tick(q, timeout)\n  File \"/Users/mitmproxy/libmproxy/controller.py\", line 108, in tick\n    self.handle(*msg)\n  File \"/Users/mitmproxy/libmproxy/controller.py\", line 129, in handle\n    m(obj)\n  File \"/Users/mitmproxy/libmproxy/web/__init__.py\", line 167, in handle_request\n    super(WebMaster, self).handle_request(f)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 990, in handle_request\n    self.state.add_flow(f)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 561, in add_flow\n    self.flows._add(f)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 469, in _add\n    view._add(f)\n  File \"/Users/mitmproxy/libmproxy/web/__init__.py\", line 23, in _add\n    data=f.get_state(short=True)\n  File \"/Users/mitmproxy/libmproxy/models/flow.py\", line 88, in get_state\n    d = super(Flow, self).get_state(short)\n  File \"/Users/mitmproxy/libmproxy/stateobject.py\", line 32, in get_state\n    state[attr] = val.get_state(short)\n  File \"/Users/mitmproxy/libmproxy/models/connections.py\", line 138, in get_state\n    address={\"address\": self.address(),\nTypeError: 'NoneType' object is not callable\nERROR:tornado.application:Exception in callback <bound method WebMaster.tick of <libmproxy.web.WebMaster object at 0x10cfe5a90>>\n```\n\nMitmweb can't run\nWhen I run mitmweb,then open 121.0.0.1:8081, the error happened.\n\n```\nTraceback (most recent call last):\n  File \"/Users/venv.mitmproxy/lib/python2.7/site-packages/tornado/ioloop.py\", line 1020, in _run\n    return self.callback()\n  File \"/Users/mitmproxy/libmproxy/web/__init__.py\", line 141, in tick\n    flow.FlowMaster.tick(self, self.masterq, timeout=0)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 812, in tick\n    return super(FlowMaster, self).tick(q, timeout)\n  File \"/Users/mitmproxy/libmproxy/controller.py\", line 108, in tick\n    self.handle(*msg)\n  File \"/Users/mitmproxy/libmproxy/controller.py\", line 129, in handle\n    m(obj)\n  File \"/Users/mitmproxy/libmproxy/web/__init__.py\", line 167, in handle_request\n    super(WebMaster, self).handle_request(f)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 990, in handle_request\n    self.state.add_flow(f)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 561, in add_flow\n    self.flows._add(f)\n  File \"/Users/mitmproxy/libmproxy/flow.py\", line 469, in _add\n    view._add(f)\n  File \"/Users/mitmproxy/libmproxy/web/__init__.py\", line 23, in _add\n    data=f.get_state(short=True)\n  File \"/Users/mitmproxy/libmproxy/models/flow.py\", line 88, in get_state\n    d = super(Flow, self).get_state(short)\n  File \"/Users/mitmproxy/libmproxy/stateobject.py\", line 32, in get_state\n    state[attr] = val.get_state(short)\n  File \"/Users/mitmproxy/libmproxy/models/connections.py\", line 138, in get_state\n    address={\"address\": self.address(),\nTypeError: 'NoneType' object is not callable\nERROR:tornado.application:Exception in callback <bound method WebMaster.tick of <libmproxy.web.WebMaster object at 0x10cfe5a90>>\n```\n\n", "before_files": [{"content": "from __future__ import (absolute_import, print_function, division)\n\nimport copy\nimport os\n\nfrom netlib import tcp, certutils\nfrom .. import stateobject, utils\n\n\nclass ClientConnection(tcp.BaseHandler, stateobject.StateObject):\n    def __init__(self, client_connection, address, server):\n        # Eventually, this object is restored from state. We don't have a\n        # connection then.\n        if client_connection:\n            super(ClientConnection, self).__init__(client_connection, address, server)\n        else:\n            self.connection = None\n            self.server = None\n            self.wfile = None\n            self.rfile = None\n            self.address = None\n            self.clientcert = None\n            self.ssl_established = None\n\n        self.timestamp_start = utils.timestamp()\n        self.timestamp_end = None\n        self.timestamp_ssl_setup = None\n        self.protocol = None\n\n    def __nonzero__(self):\n        return bool(self.connection) and not self.finished\n\n    def __repr__(self):\n        return \"<ClientConnection: {ssl}{host}:{port}>\".format(\n            ssl=\"[ssl] \" if self.ssl_established else \"\",\n            host=self.address.host,\n            port=self.address.port\n        )\n\n    @property\n    def tls_established(self):\n        return self.ssl_established\n\n    _stateobject_attributes = dict(\n        ssl_established=bool,\n        timestamp_start=float,\n        timestamp_end=float,\n        timestamp_ssl_setup=float\n    )\n\n    def get_state(self, short=False):\n        d = super(ClientConnection, self).get_state(short)\n        d.update(\n            address={\n                \"address\": self.address(),\n                \"use_ipv6\": self.address.use_ipv6},\n            clientcert=self.cert.to_pem() if self.clientcert else None)\n        return d\n\n    def load_state(self, state):\n        super(ClientConnection, self).load_state(state)\n        self.address = tcp.Address(\n            **state[\"address\"]) if state[\"address\"] else None\n        self.clientcert = certutils.SSLCert.from_pem(\n            state[\"clientcert\"]) if state[\"clientcert\"] else None\n\n    def copy(self):\n        return copy.copy(self)\n\n    def send(self, message):\n        if isinstance(message, list):\n            message = b''.join(message)\n        self.wfile.write(message)\n        self.wfile.flush()\n\n    @classmethod\n    def from_state(cls, state):\n        f = cls(None, tuple(), None)\n        f.load_state(state)\n        return f\n\n    def convert_to_ssl(self, *args, **kwargs):\n        super(ClientConnection, self).convert_to_ssl(*args, **kwargs)\n        self.timestamp_ssl_setup = utils.timestamp()\n\n    def finish(self):\n        super(ClientConnection, self).finish()\n        self.timestamp_end = utils.timestamp()\n\n\nclass ServerConnection(tcp.TCPClient, stateobject.StateObject):\n    def __init__(self, address):\n        tcp.TCPClient.__init__(self, address)\n\n        self.via = None\n        self.timestamp_start = None\n        self.timestamp_end = None\n        self.timestamp_tcp_setup = None\n        self.timestamp_ssl_setup = None\n        self.protocol = None\n\n    def __nonzero__(self):\n        return bool(self.connection) and not self.finished\n\n    def __repr__(self):\n        if self.ssl_established and self.sni:\n            ssl = \"[ssl: {0}] \".format(self.sni)\n        elif self.ssl_established:\n            ssl = \"[ssl] \"\n        else:\n            ssl = \"\"\n        return \"<ServerConnection: {ssl}{host}:{port}>\".format(\n            ssl=ssl,\n            host=self.address.host,\n            port=self.address.port\n        )\n\n    @property\n    def tls_established(self):\n        return self.ssl_established\n\n    _stateobject_attributes = dict(\n        timestamp_start=float,\n        timestamp_end=float,\n        timestamp_tcp_setup=float,\n        timestamp_ssl_setup=float,\n        address=tcp.Address,\n        source_address=tcp.Address,\n        cert=certutils.SSLCert,\n        ssl_established=bool,\n        sni=str\n    )\n    _stateobject_long_attributes = {\"cert\"}\n\n    def get_state(self, short=False):\n        d = super(ServerConnection, self).get_state(short)\n        d.update(\n            address={\"address\": self.address(),\n                     \"use_ipv6\": self.address.use_ipv6},\n            source_address=({\"address\": self.source_address(),\n                             \"use_ipv6\": self.source_address.use_ipv6} if self.source_address else None),\n            cert=self.cert.to_pem() if self.cert else None\n        )\n        return d\n\n    def load_state(self, state):\n        super(ServerConnection, self).load_state(state)\n\n        self.address = tcp.Address(\n            **state[\"address\"]) if state[\"address\"] else None\n        self.source_address = tcp.Address(\n            **state[\"source_address\"]) if state[\"source_address\"] else None\n        self.cert = certutils.SSLCert.from_pem(\n            state[\"cert\"]) if state[\"cert\"] else None\n\n    @classmethod\n    def from_state(cls, state):\n        f = cls(tuple())\n        f.load_state(state)\n        return f\n\n    def copy(self):\n        return copy.copy(self)\n\n    def connect(self):\n        self.timestamp_start = utils.timestamp()\n        tcp.TCPClient.connect(self)\n        self.timestamp_tcp_setup = utils.timestamp()\n\n    def send(self, message):\n        if isinstance(message, list):\n            message = b''.join(message)\n        self.wfile.write(message)\n        self.wfile.flush()\n\n    def establish_ssl(self, clientcerts, sni, **kwargs):\n        clientcert = None\n        if clientcerts:\n            path = os.path.join(\n                clientcerts,\n                self.address.host.encode(\"idna\")) + \".pem\"\n            if os.path.exists(path):\n                clientcert = path\n\n        self.convert_to_ssl(cert=clientcert, sni=sni, **kwargs)\n        self.sni = sni\n        self.timestamp_ssl_setup = utils.timestamp()\n\n    def finish(self):\n        tcp.TCPClient.finish(self)\n        self.timestamp_end = utils.timestamp()\n\n\nServerConnection._stateobject_attributes[\"via\"] = ServerConnection\n", "path": "libmproxy/models/connections.py"}], "after_files": [{"content": "from __future__ import (absolute_import, print_function, division)\n\nimport copy\nimport os\n\nfrom netlib import tcp, certutils\nfrom .. import stateobject, utils\n\n\nclass ClientConnection(tcp.BaseHandler, stateobject.StateObject):\n    def __init__(self, client_connection, address, server):\n        # Eventually, this object is restored from state. We don't have a\n        # connection then.\n        if client_connection:\n            super(ClientConnection, self).__init__(client_connection, address, server)\n        else:\n            self.connection = None\n            self.server = None\n            self.wfile = None\n            self.rfile = None\n            self.address = None\n            self.clientcert = None\n            self.ssl_established = None\n\n        self.timestamp_start = utils.timestamp()\n        self.timestamp_end = None\n        self.timestamp_ssl_setup = None\n        self.protocol = None\n\n    def __nonzero__(self):\n        return bool(self.connection) and not self.finished\n\n    def __repr__(self):\n        return \"<ClientConnection: {ssl}{host}:{port}>\".format(\n            ssl=\"[ssl] \" if self.ssl_established else \"\",\n            host=self.address.host,\n            port=self.address.port\n        )\n\n    @property\n    def tls_established(self):\n        return self.ssl_established\n\n    _stateobject_attributes = dict(\n        ssl_established=bool,\n        timestamp_start=float,\n        timestamp_end=float,\n        timestamp_ssl_setup=float\n    )\n\n    def get_state(self, short=False):\n        d = super(ClientConnection, self).get_state(short)\n        d.update(\n            address={\n                \"address\": self.address(),\n                \"use_ipv6\": self.address.use_ipv6},\n            clientcert=self.cert.to_pem() if self.clientcert else None)\n        return d\n\n    def load_state(self, state):\n        super(ClientConnection, self).load_state(state)\n        self.address = tcp.Address(\n            **state[\"address\"]) if state[\"address\"] else None\n        self.clientcert = certutils.SSLCert.from_pem(\n            state[\"clientcert\"]) if state[\"clientcert\"] else None\n\n    def copy(self):\n        return copy.copy(self)\n\n    def send(self, message):\n        if isinstance(message, list):\n            message = b''.join(message)\n        self.wfile.write(message)\n        self.wfile.flush()\n\n    @classmethod\n    def from_state(cls, state):\n        f = cls(None, tuple(), None)\n        f.load_state(state)\n        return f\n\n    def convert_to_ssl(self, *args, **kwargs):\n        super(ClientConnection, self).convert_to_ssl(*args, **kwargs)\n        self.timestamp_ssl_setup = utils.timestamp()\n\n    def finish(self):\n        super(ClientConnection, self).finish()\n        self.timestamp_end = utils.timestamp()\n\n\nclass ServerConnection(tcp.TCPClient, stateobject.StateObject):\n    def __init__(self, address):\n        tcp.TCPClient.__init__(self, address)\n\n        self.via = None\n        self.timestamp_start = None\n        self.timestamp_end = None\n        self.timestamp_tcp_setup = None\n        self.timestamp_ssl_setup = None\n        self.protocol = None\n\n    def __nonzero__(self):\n        return bool(self.connection) and not self.finished\n\n    def __repr__(self):\n        if self.ssl_established and self.sni:\n            ssl = \"[ssl: {0}] \".format(self.sni)\n        elif self.ssl_established:\n            ssl = \"[ssl] \"\n        else:\n            ssl = \"\"\n        return \"<ServerConnection: {ssl}{host}:{port}>\".format(\n            ssl=ssl,\n            host=self.address.host,\n            port=self.address.port\n        )\n\n    @property\n    def tls_established(self):\n        return self.ssl_established\n\n    _stateobject_attributes = dict(\n        timestamp_start=float,\n        timestamp_end=float,\n        timestamp_tcp_setup=float,\n        timestamp_ssl_setup=float,\n        address=tcp.Address,\n        source_address=tcp.Address,\n        cert=certutils.SSLCert,\n        ssl_established=bool,\n        sni=str\n    )\n    _stateobject_long_attributes = {\"cert\"}\n\n    def get_state(self, short=False):\n        d = super(ServerConnection, self).get_state(short)\n        d.update(\n            address=({\"address\": self.address(),\n                     \"use_ipv6\": self.address.use_ipv6} if self.address else {}), \n            source_address=({\"address\": self.source_address(),\n                             \"use_ipv6\": self.source_address.use_ipv6} if self.source_address else None),\n            cert=self.cert.to_pem() if self.cert else None\n        )\n        return d\n\n    def load_state(self, state):\n        super(ServerConnection, self).load_state(state)\n\n        self.address = tcp.Address(\n            **state[\"address\"]) if state[\"address\"] else None\n        self.source_address = tcp.Address(\n            **state[\"source_address\"]) if state[\"source_address\"] else None\n        self.cert = certutils.SSLCert.from_pem(\n            state[\"cert\"]) if state[\"cert\"] else None\n\n    @classmethod\n    def from_state(cls, state):\n        f = cls(tuple())\n        f.load_state(state)\n        return f\n\n    def copy(self):\n        return copy.copy(self)\n\n    def connect(self):\n        self.timestamp_start = utils.timestamp()\n        tcp.TCPClient.connect(self)\n        self.timestamp_tcp_setup = utils.timestamp()\n\n    def send(self, message):\n        if isinstance(message, list):\n            message = b''.join(message)\n        self.wfile.write(message)\n        self.wfile.flush()\n\n    def establish_ssl(self, clientcerts, sni, **kwargs):\n        clientcert = None\n        if clientcerts:\n            path = os.path.join(\n                clientcerts,\n                self.address.host.encode(\"idna\")) + \".pem\"\n            if os.path.exists(path):\n                clientcert = path\n\n        self.convert_to_ssl(cert=clientcert, sni=sni, **kwargs)\n        self.sni = sni\n        self.timestamp_ssl_setup = utils.timestamp()\n\n    def finish(self):\n        tcp.TCPClient.finish(self)\n        self.timestamp_end = utils.timestamp()\n\n\nServerConnection._stateobject_attributes[\"via\"] = ServerConnection\n", "path": "libmproxy/models/connections.py"}]}