Software /
code /
prosody
Comparison
net/server_event.lua @ 5962:27eacbb6e71f
net/server_event: pcall require ssl rather than relying on globals
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Wed, 18 Dec 2013 18:11:17 -0500 |
parent | 5958:451b4649fad2 |
child | 5963:3bd90108cc3c |
comparison
equal
deleted
inserted
replaced
5961:5745bb2b6890 | 5962:27eacbb6e71f |
---|---|
42 local setmetatable = use "setmetatable" | 42 local setmetatable = use "setmetatable" |
43 | 43 |
44 local t_insert = table.insert | 44 local t_insert = table.insert |
45 local t_concat = table.concat | 45 local t_concat = table.concat |
46 | 46 |
47 local ssl = use "ssl" | 47 local has_luasec, ssl = pcall ( require , "ssl" ) |
48 local socket = use "socket" or require "socket" | 48 local socket = use "socket" or require "socket" |
49 | 49 |
50 local log = require ("util.logger").init("socket") | 50 local log = require ("util.logger").init("socket") |
51 | 51 |
52 local function debug(...) | 52 local function debug(...) |
134 self.fatalerror = "connection timeout" | 134 self.fatalerror = "connection timeout" |
135 self:ontimeout() -- call timeout listener | 135 self:ontimeout() -- call timeout listener |
136 self:_close() | 136 self:_close() |
137 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) | 137 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) |
138 else | 138 else |
139 if plainssl and ssl then -- start ssl session | 139 if plainssl and has_luasec then -- start ssl session |
140 self:starttls(self._sslctx, true) | 140 self:starttls(self._sslctx, true) |
141 else -- normal connection | 141 else -- normal connection |
142 self:_start_session(true) | 142 self:_start_session(true) |
143 end | 143 end |
144 debug( "new connection established. id:", self.id ) | 144 debug( "new connection established. id:", self.id ) |
504 _ip = ip, _port = port, _server = server, _pattern = pattern, | 504 _ip = ip, _port = port, _server = server, _pattern = pattern, |
505 _serverport = (server and server:port() or nil), | 505 _serverport = (server and server:port() or nil), |
506 _sslctx = sslctx; -- parameters | 506 _sslctx = sslctx; -- parameters |
507 _usingssl = false; -- client is using ssl; | 507 _usingssl = false; -- client is using ssl; |
508 } | 508 } |
509 if not ssl then interface.starttls = false; end | 509 if not has_luasec then interface.starttls = false; end |
510 interface.id = tostring(interface):match("%x+$"); | 510 interface.id = tostring(interface):match("%x+$"); |
511 interface.writecallback = function( event ) -- called on write events | 511 interface.writecallback = function( event ) -- called on write events |
512 --vdebug( "new client write event, id/ip/port:", interface, ip, port ) | 512 --vdebug( "new client write event, id/ip/port:", interface, ip, port ) |
513 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event | 513 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event |
514 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror ) | 514 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror ) |
687 end | 687 end |
688 local client_ip, client_port = client:getpeername( ) | 688 local client_ip, client_port = client:getpeername( ) |
689 interface._connections = interface._connections + 1 -- increase connection count | 689 interface._connections = interface._connections + 1 -- increase connection count |
690 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, sslctx ) | 690 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, sslctx ) |
691 --vdebug( "client id:", clientinterface, "startssl:", startssl ) | 691 --vdebug( "client id:", clientinterface, "startssl:", startssl ) |
692 if ssl and sslctx then | 692 if has_luasec and sslctx then |
693 clientinterface:starttls(sslctx, true) | 693 clientinterface:starttls(sslctx, true) |
694 else | 694 else |
695 clientinterface:_start_session( true ) | 695 clientinterface:_start_session( true ) |
696 end | 696 end |
697 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); | 697 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); |
708 return interface | 708 return interface |
709 end | 709 end |
710 end | 710 end |
711 | 711 |
712 local addserver = ( function( ) | 712 local addserver = ( function( ) |
713 return function( addr, port, listener, pattern, sslcfg, startssl ) -- TODO: check arguments | 713 return function( addr, port, listener, pattern, sslctx, startssl ) -- TODO: check arguments |
714 --vdebug( "creating new tcp server with following parameters:", addr or "nil", port or "nil", sslcfg or "nil", startssl or "nil") | 714 --vdebug( "creating new tcp server with following parameters:", addr or "nil", port or "nil", sslctx or "nil", startssl or "nil") |
715 if sslctx and not has_luasec then | |
716 debug "fatal error: luasec not found" | |
717 return nil, "luasec not found" | |
718 end | |
715 local server, err = socket.bind( addr, port, cfg.ACCEPT_QUEUE ) -- create server socket | 719 local server, err = socket.bind( addr, port, cfg.ACCEPT_QUEUE ) -- create server socket |
716 if not server then | 720 if not server then |
717 debug( "creating server socket on "..addr.." port "..port.." failed:", err ) | 721 debug( "creating server socket on "..addr.." port "..port.." failed:", err ) |
718 return nil, err | 722 return nil, err |
719 end | |
720 local sslctx | |
721 if sslcfg then | |
722 if not ssl then | |
723 debug "fatal error: luasec not found" | |
724 return nil, "luasec not found" | |
725 end | |
726 sslctx, err = sslcfg | |
727 if err then | |
728 debug( "error while creating new ssl context for server socket:", err ) | |
729 return nil, err | |
730 end | |
731 end | 723 end |
732 local interface = handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- new server handler | 724 local interface = handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- new server handler |
733 debug( "new server created with id:", tostring(interface)) | 725 debug( "new server created with id:", tostring(interface)) |
734 return interface | 726 return interface |
735 end | 727 end |
743 return interface, client | 735 return interface, client |
744 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface | 736 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface |
745 end | 737 end |
746 | 738 |
747 function addclient( addr, serverport, listener, pattern, sslctx ) | 739 function addclient( addr, serverport, listener, pattern, sslctx ) |
748 if sslctx and not ssl then | 740 if sslctx and not has_luasec then |
749 debug "need luasec, but not available" | 741 debug "need luasec, but not available" |
750 return nil, "luasec not found" | 742 return nil, "luasec not found" |
751 end | 743 end |
752 local client, err = socket.tcp() -- creating new socket | 744 local client, err = socket.tcp() -- creating new socket |
753 if not client then | 745 if not client then |