No subject


Mon Oct 15 08:52:25 EDT 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&#39;s over TOR (This part of=
 the &#39;competition&#39; 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&#39;s 3301). =A0I&#39;=
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] : &quot;</div>
<div style><span style=3D"color:rgb(0,0,0);font-family:Consolas,&#39;Lucida=
 Console&#39;,&#39;DejaVu Sans Mono&#39;,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,&#39;Lucida Console&#39;,&#39;DejaVu Sans Mono&#3=
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,&#39;Lucida Console&#3=
9;,&#39;DejaVu Sans Mono&#39;,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 &lt;CRLF&gt; (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 &quot;S:&quot; and lines sent by the client will be preceded by &qu=
ot;C:&quot;

   Each message sent by the server MUST conform to the format:

       [CODE] [RESPONSE NAME] [RESPONSE (optional)]&lt;CRLF&gt;

   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]&lt;CRLF&gt;


   b. RAND [n]
  =20
   Upon receiving a &quot;RAND&quot; 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 &lt;CRLF&gt;.  After the last=20
   number has been sent, the server MUST send a dot (.) on a line=20
   by itself.

   Example:

       C: RAND 3&lt;CRLF&gt;
       S: 01 OK&lt;CRLF&gt;
       S: [first random number]&lt;CRLF&gt;
       S: [second random number]&lt;CRLF&gt;
       S: [third random number]&lt;CRLF&gt;
       S: .&lt;CRLF&gt;


   c. QUINE

   Upon receiving a &quot;QUINE&quot; 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&lt;CRLF&gt;
       S: 01 OK&lt;CRLF&gt;
       S: [quine code]&lt;CRLF&gt;
       S: .&lt;CRLF&gt;
  =20

   d. BASE29 [n]

   Upon receiving a &quot;BASE29&quot; 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&lt;CRLF&gt;
       S: 01 OK 3QO&lt;CRLF&gt;


   e. CODE

   Upon receiving a &quot;CODE&quot; 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&lt;CRLF&gt;
       S: 01 OK&lt;CRLF&gt;
       S: [Server Source Code]&lt;CRLF&gt;
       s: .&lt;CRLF&gt;


   f. KOAN

   Upon receiving a &quot;KOAN&quot; 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&lt;CRLF&gt;
       S: 01 OK&lt;CRLF&gt;
       S: A master who lived as a hermit on a mountain was asked by a&lt;CR=
LF&gt;
       S: monk, &quot;What is the Way?&lt;CRLF&gt;
       S: &quot;What a fine mountain this is,&quot; the master said in repl=
y&lt;CRLF&gt;
       S: &quot;I am not asking you about the mountain, but about the Way.&=
lt;CRLF&gt;
       S: &quot;So long as you cannot go beyond the mountain, my son, you&l=
t;CRLF&gt;
       S: cannot reach the Way,&quot; replied the master&lt;CRLF&gt;
       S: .


   g. DH [p]

   Upon receiving a &quot;DH&quot; 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&lt;CRLF&gt;
       S: 01 OK 5 8&lt;CRLF&gt;
       C: 19&lt;CRLF&gt;
       S: 03 DATA 2&lt;CRLF&gt;


   j. NEXT

   Upon receiving a &quot;NEXT&quot; 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&lt;CRLF&gt;
       S: 01 OK&lt;CRLF&gt;
       C: -----BEGIN PGP SIGNED MESSAGE-----&lt;CRLF&gt;
       C: [MESSAGE CONTENTS]&lt;CRLF&gt;
       C: -----END PGP SIGNATURE-----&lt;CRLF&gt;
       C: .&lt;CRLF&gt;
       S: 01 OK&lt;CRLF&gt;


   i. GOODBYE

   Upon receiving a &quot;DH&quot; request by the client, the server MUST r=
espond with
   99 GOODBYE and then gracefully close the connection.

   Example:

       C: GOODBYE&lt;CRLF&gt;
       S: 99 GOODBYE&lt;CRLF&gt;


=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&quot;</span><div><sp=
an style=3D"color:rgb(0,0,0);font-family:Consolas,&#39;Lucida Console&#39;,=
&#39;DejaVu Sans Mono&#39;,Monaco,monospace;font-size:12px;white-space:pre-=
wrap"><br>
</span></div><div><span style=3D"color:rgb(0,0,0);font-family:Consolas,&#39=
;Lucida Console&#39;,&#39;DejaVu Sans Mono&#39;,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,&#39;Lucida Console&#39;,&#39;DejaVu Sans =
Mono&#39;,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">&lt;<a href=3D"mailto:gl=
yph at twistedmatrix.com" target=3D"_blank">glyph at twistedmatrix.com</a>&gt;</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 &lt;<a hre=
f=3D"mailto:sk82712 at gmail.com" target=3D"_blank">sk82712 at gmail.com</a>&gt; =
wrote:</div>
<br><blockquote type=3D"cite"><div dir=3D"ltr">Guys,<div>I&#39;ll try to be=
 as exact as I can be with my question, but it may end up being pretty broa=
d. (I&#39;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 &quot;y=
ou can&#39;t parse TCP like this&quot; :). =A0You&#39;ll have this problem:=
 &lt;<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>&gt;</div>
<div class=3D"im"><br><blockquote type=3D"cite"><div dir=3D"ltr"><div>The i=
ssue I&#39;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&#39;m simply c=
atching the &#39;data&#39; 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&#39;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&#39;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&#39;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&#39;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&#39;t take too much out of context=
)</div>
<div><br></div><div>You can see I had trouble with next() and dh(). =A0I&#3=
9;m now trying to move this TCP &#39;framework&#39; over to another project=
 (the above was already submitted), and I&#39;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 &quot;other, higher-level object&quot; in your case=
 might be a state machine of some kind, or you might just have an object re=
presenting the mode that you&#39;re currently in and switch that out for di=
fferent modes.</div>
<div><br></div><div>This isn&#39;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&#39;s a general program architectur=
e issue.</div>
<div><br></div><div>This is very general advice, obviously, because I&#39;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 &#39;dh()&#39; 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