-
Notifications
You must be signed in to change notification settings - Fork 0
/
stoptls_client.py
60 lines (39 loc) · 1.39 KB
/
stoptls_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from sys import argv
from twisted.python.log import startLogging
from twisted.internet import reactor
from twisted.internet.stdio import StandardIO
from twisted.protocols.basic import LineReceiver
from twisted.internet.ssl import ClientContextFactory
from twisted.internet.endpoints import clientFromString
from twisted.internet.protocol import Factory
from stoptls_server import OnionFactory
class Sender(LineReceiver):
from os import linesep as delimiter
def __init__(self, client):
self.client = client
def lineReceived(self, line):
self.client.sendLine(line)
class OnionClient(LineReceiver):
delimiter = '\n'
def connectionMade(self):
StandardIO(Sender(self))
def sendLine(self, line):
LineReceiver.sendLine(self, line)
if line == "secure":
self.transport.startTLS(self.factory.contextFactory, True)
elif line == "unsecure":
self.transport.stopTLS()
def lineReceived(self, line):
print 'Received:', repr(line)
def connectionLost(self, reason):
reactor.stop()
def main():
startLogging(file('client.log', 'a'), False)
factory = Factory()
factory.contextFactory = ClientContextFactory()
factory.protocol = OnionClient
endpoint = clientFromString(reactor, argv[1])
endpoint.connect(OnionFactory(factory))
reactor.run()
if __name__ == '__main__':
main()