Software /
code /
prosody
Comparison
net/server.lua @ 66:018705d57f09 tls
Working TLS!
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 05 Oct 2008 19:10:21 +0100 |
parent | 65:9c471840acb9 |
child | 74:09bda2782837 |
comparison
equal
deleted
inserted
replaced
65:9c471840acb9 | 66:018705d57f09 |
---|---|
444 if not err or ( err == "timeout" or err == "wantread" ) then -- received something | 444 if not err or ( err == "timeout" or err == "wantread" ) then -- received something |
445 local data = data or part or "" | 445 local data = data or part or "" |
446 local count = #data * STAT_UNIT | 446 local count = #data * STAT_UNIT |
447 rstat = rstat + count | 447 rstat = rstat + count |
448 receivestat = receivestat + count | 448 receivestat = receivestat + count |
449 out_put( "server.lua: read data '", data, "', error: ", err ) | 449 --out_put( "server.lua: read data '", data, "', error: ", err ) |
450 return dispatch( handler, data, err ) | 450 return dispatch( handler, data, err ) |
451 else -- connections was closed or fatal error | 451 else -- connections was closed or fatal error |
452 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | 452 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
453 handler.close( ) | 453 handler.close( ) |
454 disconnect( handler, err ) | 454 disconnect( handler, err ) |
468 --writequeue = { } | 468 --writequeue = { } |
469 eol = nil | 469 eol = nil |
470 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist | 470 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist |
471 if handler.need_tls then | 471 if handler.need_tls then |
472 out_put("server.lua: connection is ready for tls handshake"); | 472 out_put("server.lua: connection is ready for tls handshake"); |
473 handler.need_tls = not handler.starttls(true); | 473 handler.starttls(true); |
474 if handler.need_tls then | |
475 out_put("server.lua: uh-oh... we still want tls, something must be wrong"); | |
476 end | |
474 end | 477 end |
475 return true | 478 return true |
476 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write | 479 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write |
477 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer | 480 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer |
478 writequeue[ 1 ] = buffer -- insert new buffer in queue | 481 writequeue[ 1 ] = buffer -- insert new buffer in queue |
497 --// handshake //-- | 500 --// handshake //-- |
498 | 501 |
499 local wrote, read | 502 local wrote, read |
500 | 503 |
501 handler.starttls = function (now) | 504 handler.starttls = function (now) |
502 if not now then handler.need_tls = true; return; end | 505 if not now then out_put("server.lua: we need to do tls, but delaying until later"); handler.need_tls = true; return; end |
503 out_put( "server.lua: attempting to start tls on "..tostring(socket) ) | 506 out_put( "server.lua: attempting to start tls on "..tostring(socket) ) |
504 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket | 507 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket |
505 out_put("sslwrapped socket is "..tostring(socket)); | 508 out_put("sslwrapped socket is "..tostring(socket)); |
506 if err then | 509 if err then |
507 out_put( "server.lua: ssl error: ", err ) | 510 out_put( "server.lua: ssl error: ", err ) |
509 end | 512 end |
510 socket:settimeout( 1 ) | 513 socket:settimeout( 1 ) |
511 send = socket.send | 514 send = socket.send |
512 receive = socket.receive | 515 receive = socket.receive |
513 close = socket.close | 516 close = socket.close |
514 print(readlen, writelen) | |
515 for _, s in ipairs(readlist) do print("R:", tostring(s)) end | |
516 for _, s in ipairs(writelist) do print("W:", tostring(s)) end | |
517 handler.ssl = function( ) | 517 handler.ssl = function( ) |
518 return true | 518 return true |
519 end | 519 end |
520 handler.send = function( _, data, i, j ) | 520 handler.send = function( _, data, i, j ) |
521 return send( socket, data, i, j ) | 521 return send( socket, data, i, j ) |
522 end | 522 end |
523 handler.receive = function( pattern, prefix ) | 523 handler.receive = function( pattern, prefix ) |
524 return receive( socket, pattern, prefix ) | 524 return receive( socket, pattern, prefix ) |
525 end | 525 end |
526 | 526 |
527 handler.handshake = function (conn) | 527 handler.handshake = coroutine_wrap( function( client ) |
528 local succ, msg | 528 local err |
529 out_put("ssl handshaking on socket "..tostring(conn)) | 529 for i = 1, 10 do -- 10 handshake attemps |
530 conn:settimeout() | 530 _, err = client:dohandshake( ) |
531 while not succ do | 531 if not err then |
532 succ, msg = conn:dohandshake() | 532 out_put( "server.lua: ssl handshake done" ) |
533 out_put("msg: "..tostring(msg)) | 533 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen |
534 if msg == 'wantread' then | 534 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions |
535 socket_select({conn}, nil) | 535 handler.dispatchdata = handler._dispatchdata |
536 elseif msg == 'wantwrite' then | 536 handler.need_tls = nil |
537 socket_select(nil, {conn}) | 537 socketlist[ client ] = handler |
538 elseif not succ then | 538 readlen = readlen + 1 |
539 -- other error | 539 readlist[ readlen ] = client |
540 _ = err ~= "closed" and close( socket ) | 540 return true; |
541 handler.close( ) | 541 else |
542 disconnect( handler, err ) | 542 out_put( "server.lua: error during ssl handshake: ", err ) |
543 writequeue = nil | 543 if err == "wantwrite" then |
544 handler = nil | 544 if wrote == nil then |
545 out_error("server.lua: ssl handshake failed"); | 545 writelen = writelen + 1 |
546 return false -- handshake failed | 546 writelist[ writelen ] = client |
547 wrote = true | |
547 end | 548 end |
548 | |
549 end | 549 end |
550 out_put("server.lua: ssl handshake succeeded!"); | 550 coroutine_yield( handler, nil, err ) -- handshake not finished |
551 handler.receivedata = handler._receivedata; | |
552 handler.dispatchdata = handler._dispatchdata; | |
553 return true; | |
554 end | 551 end |
555 | 552 end |
556 handler.receivedata = handler.handshake | 553 _ = err ~= "closed" and close( socket ) |
557 handler.dispatchdata = handler.handshake | 554 handler.close( ) |
558 | 555 disconnect( handler, err ) |
559 return handler.handshake( socket ) -- do handshake | 556 writequeue = nil |
560 end | 557 handler = nil |
561 | 558 return false -- handshake failed |
559 end | |
560 ) | |
561 handler.receivedata = handler.handshake | |
562 handler.dispatchdata = handler.handshake | |
563 | |
564 handler.handshake( socket ) -- do handshake | |
565 end | |
562 socketlist[ socket ] = handler | 566 socketlist[ socket ] = handler |
563 readlen = readlen + 1 | 567 readlen = readlen + 1 |
564 readlist[ readlen ] = socket | 568 readlist[ readlen ] = socket |
565 | 569 |
566 return handler, socket | 570 return handler, socket |
783 end | 787 end |
784 | 788 |
785 loop = function( ) -- this is the main loop of the program | 789 loop = function( ) -- this is the main loop of the program |
786 --signal_set( "hub", "run" ) | 790 --signal_set( "hub", "run" ) |
787 repeat | 791 repeat |
788 out_put("select()") | 792 --[[print(readlen, writelen) |
793 for _, s in ipairs(readlist) do print("R:", tostring(s)) end | |
794 for _, s in ipairs(writelist) do print("W:", tostring(s)) end | |
795 out_put("select()"..os.time())]] | |
789 local read, write, err = socket_select( readlist, writelist, 1 ) -- 1 sec timeout, nice for timers | 796 local read, write, err = socket_select( readlist, writelist, 1 ) -- 1 sec timeout, nice for timers |
790 for i, socket in ipairs( write ) do -- send data waiting in writequeues | 797 for i, socket in ipairs( write ) do -- send data waiting in writequeues |
791 local handler = socketlist[ socket ] | 798 local handler = socketlist[ socket ] |
792 if handler then | 799 if handler then |
793 handler.dispatchdata( ) | 800 handler.dispatchdata( ) |