No subject
Mon Oct 15 06:52:25 MDT 2012
On Sat, Feb 16, 2013 at 11:53 PM, Glyph <glyph at twistedmatrix.com> wrote:
>
> On Feb 16, 2013, at 1:09 PM, Adam Heller <sk82712 at gmail.com> wrote:
>
> Guys,
> I'll try to be as exact as I can be with my question, but it may end up
> being pretty broad. (I'm also relatively new to python, please go easy on
> my code although suggestions are of course welcome.)
>
>
> The main suggestion is "you can't parse TCP like this" :). You'll have
> this problem: <
> http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writewith
> >
>
> The issue I'm running into:
> When a user connects to the TCP server, they are displayed a welcome menu
> of things they can do. I'm simply catching the 'data' in an if statement to
> determine which menu item was picked. The problem is that some of the menu
> items require further input. I've had to use a series of bools to
> determine if a certain menu option was previously picked that will then
> directly send the data into the function / class. Is there an easier way
> to go about this? My current method seems a bit backwards and over
> complicated. I'm thinking I missed something in the documentation.
>
>
> Anyway, here's the code: https://infotomb.com/q4293 // Pass: twisted
>
>
> You should really attach the code itself to the email, so that future
> readers of the archive have some idea what you're talking about. Pastebins
> in general are not great, since the code will expire before your archived
> message does, and pastebins with passwords (!?) are even worse.
>
> (it was part of a challenge, so to speak, don't take too much out of
> context)
>
> You can see I had trouble with next() and dh(). I'm now trying to move
> this TCP 'framework' over to another project (the above was already
> submitted), and I'm still running into the same issues.
>
>
> You should try separating out your concerns a bit. The protocol should
> just parse the bytes into something meaningful, then call methods on some
> other, higher-level object that isn't just manipulating strings.
>
> The "other, higher-level object" in your case might be a state machine of
> some kind, or you might just have an object representing the mode that
> you're currently in and switch that out for different modes.
>
> This isn't an issue with Twisted though; Twisted just calls some methods
> on your objects in order to tell you what happened; what happens next is up
> to you. So it's a general program architecture issue.
>
> This is very general advice, obviously, because I'm not sure what you are
> specifically asking, nor what your program is really intended to do. What
> kind of client is talking to this system? Did you invent the protocol
> yourself, or did it come from somewhere else? What is 'dh()' even trying
> to accomplish? Etc, etc.
>
>
> -glyph
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
--20cf30334add3b384604d5e14260
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Glyph,<div style><br></div><div style>I will definately lo=
ok into that. =A0As far as the server spec, it's over TOR (This part of=
the 'competition' has since closed, but feel free to catch up: <a =
href=3D"http://uncovering-cicada.wikia.com/wiki/Uncovering_Cicada_Wiki">htt=
p://uncovering-cicada.wikia.com/wiki/Uncovering_Cicada_Wiki</a> and <a href=
=3D"http://mentalfloss.com/article/31932/chasing-cicada-exploring-darkest-c=
orridors-internet">http://mentalfloss.com/article/31932/chasing-cicada-expl=
oring-darkest-corridors-internet</a> (from last year's 3301). =A0I'=
m simply trying to do the python thing and move classes around instead of t=
rying to reinvent the wheel [dh() is a rudimentary Diffie Helman key exchan=
ge] : "</div>
<div style><span style=3D"color:rgb(0,0,0);font-family:Consolas,'Lucida=
Console','DejaVu Sans Mono',Monaco,monospace;font-size:12px;wh=
ite-space:pre-wrap"><br></span></div><div style><span style=3D"color:rgb(0,=
0,0);font-family:Consolas,'Lucida Console','DejaVu Sans Mono=
9;,Monaco,monospace;font-size:12px;white-space:pre-wrap">In the programming=
language of your choice build a TCP server</span></div>
<span style=3D"color:rgb(0,0,0);font-family:Consolas,'Lucida Console=
9;,'DejaVu Sans Mono',Monaco,monospace;font-size:12px;white-space:p=
re-wrap">that implements the protocol below. The server code must be=20
written by you and you alone, although you are free to use any=20
modules or libraries publicly available for the selected=20
programming language.
Once you have done this, make it accessible as a Tor hidden=20
service. Then provide us with the onion address and port
via a GPG-encrypted email to this address.
You have until 0:00 UTC on 3 Feb, 2013. Any emails received
after that time will be ignored.
Good luck.
3301
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
1. INTRODUCTION
The TCP server MUST listen on an arbitrary port, and send and=20
receive plain text with lines separated by <CRLF> (representing
a carriage return followed by a line feed). The TCP server MUST
disregard the case of input.
In the examples below, lines sent by the server will be preceded
with "S:" and lines sent by the client will be preceded by &qu=
ot;C:"
Each message sent by the server MUST conform to the format:
[CODE] [RESPONSE NAME] [RESPONSE (optional)]<CRLF>
Where [CODE] and [RESPONSE NAME] is one of:
CODE RESPONSE NAME
00 Welcome=20
01 Ok=20
02 Error
03 Data
99 Goodbye
2. PROCEDURES
a. Remote Connection
Upon receiving a remote connection, the server MUST greet the=20
client with a 00 WELCOME message. The RESPONSE of a welcome
message MAY contain arbitrary text. The arbitrary text MUST
at the very least contain the name of the programming language
used to implement the server.
Upon receiving a 00 WELCOME message, the client may begin=20
initiating procedures.
Example:
S: 00 WELCOME [ARBITRARY RESPONSE TEXT]<CRLF>
b. RAND [n]
=20
Upon receiving a "RAND" request by the client, the server will
first send a 01 OK response, and will then provide the client=20
with [n] cryptographically random numbers within the range of=20
0-255. Each number MUST be followed by <CRLF>. After the last=20
number has been sent, the server MUST send a dot (.) on a line=20
by itself.
Example:
C: RAND 3<CRLF>
S: 01 OK<CRLF>
S: [first random number]<CRLF>
S: [second random number]<CRLF>
S: [third random number]<CRLF>
S: .<CRLF>
c. QUINE
Upon receiving a "QUINE" request by the client, the server wil=
l
first send a 01 OK response, and will then provide the client
with a quine in the programming language used to implement the
server. This quine does not have to be original. After the last=20
line of code has been sent, the server MUST send a dot (.) on a=20
line by itself.
Example:
C: QUINE<CRLF>
S: 01 OK<CRLF>
S: [quine code]<CRLF>
S: .<CRLF>
=20
d. BASE29 [n]
Upon receiving a "BASE29" request by the client, the server wi=
ll
send a 01 OK response followed by the number [n] converted into=20
its base 29 representation.
Example:
C: BASE29 3301<CRLF>
S: 01 OK 3QO<CRLF>
e. CODE
Upon receiving a "CODE" request by the client, the server will
send a 01 OK response followed by its own source code. After the
last line of code has been sent, the server MUST send a dot(.) on
a line by itself. =20
Example:
C: CODE<CRLF>
S: 01 OK<CRLF>
S: [Server Source Code]<CRLF>
s: .<CRLF>
f. KOAN
Upon receiving a "KOAN" request by the client, the server will
send a 01 OK response followed by a koan. After the last line of
the koan, the server MUST send a dot (.) on a line by itself.
Example:
C: KOAN<CRLF>
S: 01 OK<CRLF>
S: A master who lived as a hermit on a mountain was asked by a<CR=
LF>
S: monk, "What is the Way?<CRLF>
S: "What a fine mountain this is," the master said in repl=
y<CRLF>
S: "I am not asking you about the mountain, but about the Way.&=
lt;CRLF>
S: "So long as you cannot go beyond the mountain, my son, you&l=
t;CRLF>
S: cannot reach the Way," replied the master<CRLF>
S: .
g. DH [p]
Upon receiving a "DH" request by the client, the server will p=
roceed
to perform a Diffie-Hellman key exchange using [p] as the prime modulus.
The server will then select a base [b] to use in the protocol, as well a=
s
its secret integer. The server will then compute its exponent result [e=
]=20
as specified within the Diffie-Hellman key exchange protocol. =20
The server MUST then respond with a 01 OK response followed by the=20
selected base [b] and computed exponent [e] separated by white space.
The client MUST respond with its exponent result [e2], and the client an=
d=20
server will follow the rest of the Diffie-Hellman key exchange protocol.
The server MUST then compute the resulting secret key, and provide it
using 03 DATA [k].
Example:
C: DH 23<CRLF>
S: 01 OK 5 8<CRLF>
C: 19<CRLF>
S: 03 DATA 2<CRLF>
j. NEXT
Upon receiving a "NEXT" request by the client, the server will=
respond
with 01 OK and then listen for text data to be provided by the client. =
=20
The client will send a dot (.) on a line by itself after the last line=
=20
of text. The server MUST record this. This data will be the next set=
=20
of instructions. Once the data is received the server will respond=20
with 01 OK.=20
Example:
C: NEXT<CRLF>
S: 01 OK<CRLF>
C: -----BEGIN PGP SIGNED MESSAGE-----<CRLF>
C: [MESSAGE CONTENTS]<CRLF>
C: -----END PGP SIGNATURE-----<CRLF>
C: .<CRLF>
S: 01 OK<CRLF>
i. GOODBYE
Upon receiving a "DH" request by the client, the server MUST r=
espond with
99 GOODBYE and then gracefully close the connection.
Example:
C: GOODBYE<CRLF>
S: 99 GOODBYE<CRLF>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"</span><div><sp=
an style=3D"color:rgb(0,0,0);font-family:Consolas,'Lucida Console',=
'DejaVu Sans Mono',Monaco,monospace;font-size:12px;white-space:pre-=
wrap"><br>
</span></div><div><span style=3D"color:rgb(0,0,0);font-family:Consolas,'=
;Lucida Console','DejaVu Sans Mono',Monaco,monospace;font-size:=
12px;white-space:pre-wrap"><br></span></div><div style><span style=3D"color=
:rgb(0,0,0);font-family:Consolas,'Lucida Console','DejaVu Sans =
Mono',Monaco,monospace;font-size:12px;white-space:pre-wrap">From a sign=
ed GPG message.</span></div>
</div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_quote">On Sat,=
Feb 16, 2013 at 11:53 PM, Glyph <span dir=3D"ltr"><<a href=3D"mailto:gl=
yph at twistedmatrix.com" target=3D"_blank">glyph at twistedmatrix.com</a>></s=
pan> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div style=3D"word-wrap:break-word"><br><div=
><div class=3D"im"><div>On Feb 16, 2013, at 1:09 PM, Adam Heller <<a hre=
f=3D"mailto:sk82712 at gmail.com" target=3D"_blank">sk82712 at gmail.com</a>> =
wrote:</div>
<br><blockquote type=3D"cite"><div dir=3D"ltr">Guys,<div>I'll try to be=
as exact as I can be with my question, but it may end up being pretty broa=
d. (I'm also relatively new to python, please go easy on my code althou=
gh suggestions are of course welcome.)</div>
</div></blockquote><div><br></div></div><div>The main suggestion is "y=
ou can't parse TCP like this" :). =A0You'll have this problem:=
<<a href=3D"http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions=
#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writ=
ewith" target=3D"_blank">http://twistedmatrix.com/trac/wiki/FrequentlyAsked=
Questions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtrans=
port.writewith</a>></div>
<div class=3D"im"><br><blockquote type=3D"cite"><div dir=3D"ltr"><div>The i=
ssue I'm running into:</div><div>When a user connects to the TCP server=
, they are displayed a welcome menu of things they can do. I'm simply c=
atching the 'data' in an if statement to determine which menu item =
was picked. =A0The problem is that some of the menu items require further i=
nput. =A0I've had to use a series of bools to determine if a certain me=
nu option was previously picked that will then directly send the data into =
the function / class. =A0Is there an easier way to go about this? My curren=
t method seems a bit backwards and over complicated. =A0I'm thinking I =
missed something in the documentation.</div>
</div></blockquote><div><br></div><blockquote type=3D"cite"><div dir=3D"ltr=
"><div>Anyway, here's the code:=A0<a href=3D"https://infotomb.com/q4293=
" target=3D"_blank">https://infotomb.com/q4293</a>=A0// Pass: twisted</div>=
</div></blockquote>
<div><br></div></div><div>You should really attach the code itself to the e=
mail, so that future readers of the archive have some idea what you're =
talking about. =A0Pastebins in general are not great, since the code will e=
xpire before your archived message does, and pastebins with passwords (!?) =
are even worse.</div>
<div class=3D"im"><br><blockquote type=3D"cite"><div dir=3D"ltr"><div>(it w=
as part of a challenge, so to speak, don't take too much out of context=
)</div>
<div><br></div><div>You can see I had trouble with next() and dh(). =A0I=
9;m now trying to move this TCP 'framework' over to another project=
(the above was already submitted), and I'm still running into the same=
issues.</div>
</div></blockquote><br></div></div><div>You should try separating out your =
concerns a bit. =A0The protocol should just parse the bytes into something =
meaningful, then call methods on some other, higher-level object that isn&#=
39;t just manipulating strings.</div>
<div><br></div><div>The "other, higher-level object" in your case=
might be a state machine of some kind, or you might just have an object re=
presenting the mode that you're currently in and switch that out for di=
fferent modes.</div>
<div><br></div><div>This isn't an issue with Twisted though; Twisted ju=
st calls some methods on your objects in order to tell you what happened; w=
hat happens next is up to you. =A0So it's a general program architectur=
e issue.</div>
<div><br></div><div>This is very general advice, obviously, because I'm=
not sure what you are specifically asking, nor what your program is really=
intended to do. =A0What kind of client is talking to this system? =A0Did y=
ou invent the protocol yourself, or did it come from somewhere else? =A0Wha=
t is 'dh()' even trying to accomplish? =A0Etc, etc.</div>
<span class=3D"HOEnZb"><font color=3D"#888888"><div><br></div><div><br></di=
v><div>-glyph</div></font></span></div><br>________________________________=
_______________<br>
Twisted-Python mailing list<br>
<a href=3D"mailto:Twisted-Python at twistedmatrix.com">Twisted-Python at twistedm=
atrix.com</a><br>
<a href=3D"http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python=
" target=3D"_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twist=
ed-python</a><br>
<br></blockquote></div><br></div>
--20cf30334add3b384604d5e14260--
More information about the Twisted-Python
mailing list