This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

TCP over SSL
#5
It's possible, but you cannot run it without a certificate. You can reuse web-server's self-signed certificate though. Here's a short example:
Code:
if not tcpserver then
  require('copas')

  sslparams = {
    mode = 'server',
    protocol = 'tlsv12',
    key = '/etc/nginx_sha256.key',
    certificate = '/etc/nginx_sha256.crt',
    options = 'all',
  }

  -- connection handler
  function connhandler(sock)
    local ip, port = sock:getpeername()

    sock = copas.wrap(sock, sslparams)
    sock:dohandshake()

    alert('[server] connection from %s:%d', ip, port)

    -- main reader loop
    while true do
      -- wait for single line of data (until \n, \r is ignored)
      local data, err = copas.receive(sock, '*l')

      -- error while receiving
      if err then
        alert('[server] closed connection from %s:%d', ip, port)
        return
      end

      -- handle data frame
      if data == 'HELLO' then
        sock:send('HELLO TO YOU\r\n')
        sock:close()
      end
    end
  end

  -- bind to port 12345
  tcpserver = socket.bind('*', 12345)

  -- error while binding, try again later
  if not tcpserver then
    os.sleep(5)
    error('[server] error: cannot bind')
  end

  -- set server connection handler
  copas.addserver(tcpserver, connhandler)
end

copas.step(1)
Reply


Messages In This Thread
TCP over SSL - by oyvindi - 31.10.2016, 15:56
RE: TCP over SSL - by admin - 01.11.2016, 09:30
RE: TCP over SSL - by oyvindi - 01.11.2016, 09:44
RE: TCP over SSL - by rocfusion - 16.10.2017, 07:47
RE: TCP over SSL - by admin - 16.10.2017, 11:52
RE: TCP over SSL - by rocfusion - 18.10.2017, 04:43
RE: TCP over SSL - by rocfusion - 19.10.2017, 09:03
RE: TCP over SSL - by admin - 19.10.2017, 09:19

Forum Jump: