TLS added
TODO: Translate all comments to english Translate Readme to english Extend Readme with TLS function Create Readme_DE Code cleanup (ltls.pbi, lsocket.pbi)
This commit is contained in:
parent
e7c776a8fd
commit
57f76e95cd
5 changed files with 418 additions and 16 deletions
101
lsocket.pbi
Normal file
101
lsocket.pbi
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
;lsocket.pbi
|
||||||
|
;
|
||||||
|
; Socket Module
|
||||||
|
; Definition who are not available on linux...
|
||||||
|
; Simple PureBasic Spech commands.
|
||||||
|
;
|
||||||
|
DeclareModule lsocket
|
||||||
|
Structure sockaddr
|
||||||
|
sa_family.w
|
||||||
|
sa_data.s{14}
|
||||||
|
EndStructure
|
||||||
|
|
||||||
|
Structure in_addr
|
||||||
|
s_addr.w
|
||||||
|
EndStructure
|
||||||
|
|
||||||
|
Structure sockaddr_in
|
||||||
|
sin_family.w
|
||||||
|
sin_port.w
|
||||||
|
sin_addr.in_addr
|
||||||
|
sin_zero.s{8}
|
||||||
|
EndStructure
|
||||||
|
|
||||||
|
; Socket types:
|
||||||
|
#SOCK_STREAM = 1 ; stream (connection) socket
|
||||||
|
#SOCK_DGRAM = 2 ; datagram (conn.less) socket
|
||||||
|
#SOCK_RAW = 3 ; raw socket
|
||||||
|
#SOCK_RDM = 4 ; reliably-delivered message
|
||||||
|
#SOCK_SEQPACKET = 5 ; sequential packet socket
|
||||||
|
#SOCK_PACKET = 10 ; linux specific way of
|
||||||
|
; getting packets at the dev
|
||||||
|
; level. For writing rarp And
|
||||||
|
; other similar things on the
|
||||||
|
; user level.
|
||||||
|
|
||||||
|
; socket.h
|
||||||
|
#PF_INET = 2
|
||||||
|
#AF_INET = #PF_INET
|
||||||
|
|
||||||
|
Declare CreateSocket(Port.i, Queuelengt.i = 64, Type.i = #SOCK_STREAM, Protocol.i = #AF_INET, BindedIP.s = "127.0.0.1") ; Return Server Socket < 0 Error
|
||||||
|
Declare WaitSocket(ServerSocket.i) ;Return new SocketID
|
||||||
|
Declare ReadSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Read
|
||||||
|
Declare WriteSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Writen
|
||||||
|
|
||||||
|
EndDeclareModule
|
||||||
|
|
||||||
|
Module lsocket
|
||||||
|
Global NewMap lsockets_server.sockaddr_in()
|
||||||
|
Global NewMap lsockets_client.sockaddr_in()
|
||||||
|
|
||||||
|
Procedure CreateSocket(Port.i, Queuelengt.i = 64, Type.i = #SOCK_STREAM, Protocol.i = #AF_INET, BindedIP.s = "127.0.0.1") ; Return Server Socket < 0 Error
|
||||||
|
Protected SocketID.i
|
||||||
|
SocketID = socket_(Protocol, Type, 0)
|
||||||
|
If SocketID = -1 : ProcedureReturn -1 : EndIf
|
||||||
|
lsockets_server(Str(SocketID))\sin_family = Protocol
|
||||||
|
lsockets_server(Str(SocketID))\sin_addr\s_addr = Inet_addr_(BindedIP)
|
||||||
|
lsockets_server(Str(SocketID))\sin_port = htons_(Port)
|
||||||
|
If bind_(SocketID, @lsockets_server(Str(SocketID)), SizeOf(sockaddr_in)) <> 0
|
||||||
|
Close_(SocketID)
|
||||||
|
DeleteMapElement(lsockets_server(), Str(SocketID))
|
||||||
|
ProcedureReturn -1
|
||||||
|
Else
|
||||||
|
If listen_(SocketID, Queuelengt) <> 0
|
||||||
|
Close_(SocketID)
|
||||||
|
DeleteMapElement(lsockets_server(), Str(SocketID))
|
||||||
|
ProcedureReturn -1
|
||||||
|
EndIf
|
||||||
|
EndIf
|
||||||
|
ProcedureReturn SocketID
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure WaitSocket(ServerSocket.i) ;Return new ClientSocketID
|
||||||
|
Protected ClientSID.i, MapID.s, ClientLength
|
||||||
|
Protected Client.sockaddr_in
|
||||||
|
ClientSID = accept_(ServerSocket, @Client, @ClientLength)
|
||||||
|
If ClientSID < 0 : ProcedureReturn ClientSID : EndIf
|
||||||
|
MapID = Str(ClientSID)
|
||||||
|
lsockets_client(MapID)\sin_addr\s_addr = Client\sin_addr\s_addr
|
||||||
|
lsockets_client(MapID)\sin_family = Client\sin_family
|
||||||
|
lsockets_client(MapID)\sin_port = Client\sin_port
|
||||||
|
lsockets_client(MapID)\sin_zero = Client\sin_zero
|
||||||
|
ProcedureReturn ClientSID
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure ReadSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Read
|
||||||
|
Protected Effective.i
|
||||||
|
Effective = read_(ClientSocket, *Buffer, Length)
|
||||||
|
ProcedureReturn Effective
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure WriteSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Writen
|
||||||
|
Protected Effective.i
|
||||||
|
Effective = write_(ClientSocket, *Buffer, Length)
|
||||||
|
ProcedureReturn Effective
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
EndModule
|
175
ltls.pbi
Normal file
175
ltls.pbi
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
;ltls.pbi
|
||||||
|
|
||||||
|
XIncludeFile "lsocket.pbi"
|
||||||
|
|
||||||
|
DeclareModule ltls
|
||||||
|
Structure s_tls_server
|
||||||
|
CA.s
|
||||||
|
Cert.s
|
||||||
|
Key.s
|
||||||
|
Password.s
|
||||||
|
EndStructure
|
||||||
|
|
||||||
|
|
||||||
|
Declare InitTLS(*Settings.s_tls_server, TLSID.i = 0)
|
||||||
|
Declare WaitTLSSocket(ServerSocket.i, TLSID.i = 0)
|
||||||
|
Declare ReadTLSSocket(Connection.i, *Buffer, Length.i)
|
||||||
|
Declare WriteTLSSocket(Connection.i, *Buffer, Length.i)
|
||||||
|
Declare CloseTLSSocket(Connection.i)
|
||||||
|
Declare CloseTLS(TLSID.i = 0)
|
||||||
|
Declare.s ErrorTLSSrv(TLSID.i = 0)
|
||||||
|
Declare.s ErrorTLSCli(Connection)
|
||||||
|
|
||||||
|
EndDeclareModule
|
||||||
|
|
||||||
|
Module ltls
|
||||||
|
PrototypeC.i Pr_tls_init()
|
||||||
|
PrototypeC.i Pr_tls_config_new()
|
||||||
|
PrototypeC.i Pr_tls_load_file_pw(FileName.p-utf8, Length, Password.p-utf8)
|
||||||
|
PrototypeC.i Pr_tls_load_file(FileName.p-utf8, Length, Passord.s = #Null$)
|
||||||
|
PrototypeC.i Pr_tls_config_set_ca_mem(*tls_config , Memory.i, Length.i)
|
||||||
|
PrototypeC.i Pr_tls_config_set_cert_mem(*tls_config , Memory.i, Length.i)
|
||||||
|
PrototypeC.i Pr_tls_config_set_key_mem(*tls_config , Memory.i, Length.i)
|
||||||
|
PrototypeC.i Pr_tls_server()
|
||||||
|
PrototypeC.i Pr_tls_configure(*ctx, *cfg)
|
||||||
|
PrototypeC.i Pr_tls_error(*ctx)
|
||||||
|
PrototypeC.i Pr_tls_accept_socket(*ctx, *cctx, Socket.l)
|
||||||
|
PrototypeC.i Pr_tls_accept_cbs(*ctx, *cctx, read_cb.i, write_cb.i, arg_ch.i = #Null)
|
||||||
|
PrototypeC.i Pr_tls_read(*cctx, *buffer, Size.i)
|
||||||
|
PrototypeC.i Pr_tls_write(*cctx, *buffer, Size.i)
|
||||||
|
PrototypeC.i Pr_tls_handshake(*cctx)
|
||||||
|
PrototypeC.i Pr_tls_close(*cctx)
|
||||||
|
PrototypeC.i Pr_tls_free(*ctx)
|
||||||
|
PrototypeC.i Pr_tls_config_free(*cfg)
|
||||||
|
|
||||||
|
|
||||||
|
#TLS_WANT_POLLIN = -2
|
||||||
|
#TLS_WANT_POLLOUT = -3
|
||||||
|
|
||||||
|
Define libressl_tls = OpenLibrary(#PB_Any, "libtls.so")
|
||||||
|
|
||||||
|
If IsLibrary(libressl_tls)
|
||||||
|
Define Call_tls_init = GetFunction(libressl_tls, "tls_init")
|
||||||
|
Define Call_tls_config_new = GetFunction(libressl_tls, "tls_config_new")
|
||||||
|
Define Call_tls_load_file = GetFunction(libressl_tls, "tls_load_file")
|
||||||
|
Define Call_tls_config_set_ca_mem = GetFunction(libressl_tls, "tls_config_set_ca_mem")
|
||||||
|
Define Call_tls_config_set_cert_mem = GetFunction(libressl_tls, "tls_config_set_cert_mem")
|
||||||
|
Define Call_tls_config_set_key_mem = GetFunction(libressl_tls, "tls_config_set_key_mem")
|
||||||
|
Define Call_tls_server = GetFunction(libressl_tls, "tls_server")
|
||||||
|
Define Call_tls_configure = GetFunction(libressl_tls, "tls_configure")
|
||||||
|
Define Call_tls_error = GetFunction(libressl_tls, "tls_error")
|
||||||
|
Define Call_tls_accept_socket = GetFunction(libressl_tls, "tls_accept_socket")
|
||||||
|
Define Call_tls_accept_cbs = GetFunction(libressl_tls, "tls_accept_cbs")
|
||||||
|
Define Call_tls_read = GetFunction(libressl_tls, "tls_read")
|
||||||
|
Define Call_tls_write = GetFunction(libressl_tls, "tls_write")
|
||||||
|
Define Call_tls_handshake = GetFunction(libressl_tls, "tls_handshake")
|
||||||
|
Define Call_tls_close = GetFunction(libressl_tls, "tls_close")
|
||||||
|
Define Call_tls_free = GetFunction(libressl_tls, "tls_free")
|
||||||
|
Define Call_tls_config_free = GetFunction(libressl_tls, "tls_config_free")
|
||||||
|
Else
|
||||||
|
Debug "libtls.so Library Not found"
|
||||||
|
End
|
||||||
|
EndIf
|
||||||
|
|
||||||
|
|
||||||
|
Global.Pr_tls_init tls_init = Call_tls_init
|
||||||
|
Global.Pr_tls_config_new tls_config_new = Call_tls_config_new
|
||||||
|
Global.Pr_tls_load_file tls_load_file = Call_tls_load_file
|
||||||
|
Global.Pr_tls_load_file_pw tls_load_file_pw = Call_tls_load_file
|
||||||
|
Global.Pr_tls_config_set_ca_mem tls_config_set_ca_mem = Call_tls_config_set_ca_mem
|
||||||
|
Global.Pr_tls_config_set_cert_mem tls_config_set_cert_mem = Call_tls_config_set_cert_mem
|
||||||
|
Global.Pr_tls_config_set_key_mem tls_config_set_key_mem = Call_tls_config_set_key_mem
|
||||||
|
Global.Pr_tls_server tls_server = Call_tls_server
|
||||||
|
Global.Pr_tls_configure tls_configure = Call_tls_configure
|
||||||
|
Global.Pr_tls_error tls_error = Call_tls_error
|
||||||
|
Global.Pr_tls_accept_socket tls_accept_socket = Call_tls_accept_socket
|
||||||
|
Global.Pr_tls_accept_cbs tls_accept_cbs = Call_tls_accept_cbs
|
||||||
|
Global.Pr_tls_read tls_read = Call_tls_read
|
||||||
|
Global.Pr_tls_write tls_write = Call_tls_write
|
||||||
|
Global.Pr_tls_handshake tls_handshake = Call_tls_handshake
|
||||||
|
Global.Pr_tls_close tls_close = Call_tls_close
|
||||||
|
Global.Pr_tls_free tls_free = Call_tls_free
|
||||||
|
Global.Pr_tls_config_free tls_config_free = Call_tls_config_free
|
||||||
|
|
||||||
|
Global *ctx, *cfg
|
||||||
|
Global tls_cfg = #Null
|
||||||
|
Global tls_ctx = #Null
|
||||||
|
|
||||||
|
Procedure InitTLS(*Settings.s_tls_server, TLSID.i = 0)
|
||||||
|
Protected mem_ptr.i, length.i, returns.i
|
||||||
|
If tls_init() <> 0 : ProcedureReturn -1 : EndIf
|
||||||
|
tls_cfg = tls_config_new()
|
||||||
|
Debug "TLS cfg obj:"+Str(tls_cfg)
|
||||||
|
Debug "CA:"+*Settings\CA
|
||||||
|
mem_ptr = tls_load_file(*Settings\CA, @length)
|
||||||
|
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -2 : EndIf
|
||||||
|
tls_config_set_ca_mem(tls_cfg, mem_ptr, length)
|
||||||
|
Debug "Cert:"+*Settings\Cert
|
||||||
|
mem_ptr = tls_load_file(*Settings\Cert, @length)
|
||||||
|
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -3 : EndIf
|
||||||
|
tls_config_set_cert_mem(tls_cfg, mem_ptr, length)
|
||||||
|
If Len(*Settings\Password) > 0
|
||||||
|
Debug "Key:"+*Settings\Key + " Password:"+*Settings\Password
|
||||||
|
mem_ptr = tls_load_file_pw(*Settings\Key, @length, *Settings\Password)
|
||||||
|
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -4 : EndIf
|
||||||
|
tls_config_set_key_mem(tls_cfg, mem_ptr, length)
|
||||||
|
Else
|
||||||
|
Debug "Key:"+*Settings\Key
|
||||||
|
mem_ptr = tls_load_file(*Settings\Key, @length)
|
||||||
|
If Not mem_ptr : tls_config_free(tls_cfg) : ProcedureReturn -5 : EndIf
|
||||||
|
tls_config_set_key_mem(tls_cfg, mem_ptr, length)
|
||||||
|
EndIf
|
||||||
|
tls_ctx = tls_server()
|
||||||
|
Debug "TLS Server obj:"+Str(tls_ctx)
|
||||||
|
If Not tls_ctx : tls_config_free(tls_cfg) : ProcedureReturn -6 : EndIf
|
||||||
|
returns = tls_configure(tls_ctx, tls_cfg)
|
||||||
|
Debug "Configure Returns:"+Str(returns)
|
||||||
|
If returns <> 0 :Debug ErrorTLSSrv(): tls_free(tls_ctx) : tls_config_free(tls_cfg) : ProcedureReturn -7 : EndIf
|
||||||
|
ProcedureReturn 1
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure WaitTLSSocket(ServerSocket.i, TLSID.i = 0)
|
||||||
|
Protected sockID.i , tlssID.i, cctx
|
||||||
|
sockID = lsocket::WaitSocket(ServerSocket)
|
||||||
|
If sockID < 0 : ProcedureReturn -1 : EndIf
|
||||||
|
tlssID = tls_accept_socket(tls_ctx, @cctx, sockID)
|
||||||
|
If tlssID = -1 : ProcedureReturn -2 : EndIf
|
||||||
|
ProcedureReturn cctx
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure ReadTLSSocket(Connection.i, Buffer, Length.i)
|
||||||
|
Protected Received.i
|
||||||
|
|
||||||
|
Received = tls_read(Connection, Buffer, Length)
|
||||||
|
ProcedureReturn Received
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure WriteTLSSocket(Connection.i, Buffer, Length.i)
|
||||||
|
Protected Received.i
|
||||||
|
Received = tls_write(Connection, Buffer, Length)
|
||||||
|
ProcedureReturn Received
|
||||||
|
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure CloseTLSSocket(Connection.i)
|
||||||
|
tls_close(Connection)
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure.s ErrorTLSSrv(TLSID.i = 0)
|
||||||
|
Protected Error.s
|
||||||
|
Error = PeekS(tls_error(tls_ctx),-1,#PB_Ascii)
|
||||||
|
ProcedureReturn Error
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure.s ErrorTLSCli(Connection.i)
|
||||||
|
Protected Error.s
|
||||||
|
Error = PeekS(tls_error(Connection),-1,#PB_Ascii)
|
||||||
|
ProcedureReturn Error
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure CloseTLS(TLSID.i = 0)
|
||||||
|
tls_free(tls_ctx)
|
||||||
|
tls_config_free(tls_cfg)
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
EndModule
|
129
lweb.pbi
129
lweb.pbi
|
@ -8,6 +8,8 @@
|
||||||
;*
|
;*
|
||||||
|
|
||||||
|
|
||||||
|
XIncludeFile "ltls.pbi"
|
||||||
|
|
||||||
Module lhs_web
|
Module lhs_web
|
||||||
;********************************
|
;********************************
|
||||||
;*
|
;*
|
||||||
|
@ -37,7 +39,14 @@ Module lhs_web
|
||||||
Global.i conf_cache_time = 120 ;120Sekunden
|
Global.i conf_cache_time = 120 ;120Sekunden
|
||||||
Global.i conf_cache_maxsize = 1 ;1 Mbyte
|
Global.i conf_cache_maxsize = 1 ;1 Mbyte
|
||||||
Global.i conf_cache_current = 0
|
Global.i conf_cache_current = 0
|
||||||
Global.i conf_cache_enable = 1
|
Global.i conf_cache_enable = 1
|
||||||
|
Global.i conf_TLS_Port = 8443
|
||||||
|
Global.s conf_TLS_binding = "127.0.0.1"
|
||||||
|
Global.i conf_TLS_enable = 0 ;SSL Server
|
||||||
|
Global.s conf_TLS_CA = ""
|
||||||
|
Global.s conf_TLS_Cert = ""
|
||||||
|
Global.s conf_TLS_Key = ""
|
||||||
|
Global.s conf_TLS_Key_Pass = ""
|
||||||
|
|
||||||
Enumeration s_client_do ;client_do_cli
|
Enumeration s_client_do ;client_do_cli
|
||||||
#CLI_DO_NOP ;Keine Arbeit
|
#CLI_DO_NOP ;Keine Arbeit
|
||||||
|
@ -75,13 +84,13 @@ Module lhs_web
|
||||||
;* vergrössert wird nach abgeschlossener Arbeit wieder auf Default gesetzt.
|
;* vergrössert wird nach abgeschlossener Arbeit wieder auf Default gesetzt.
|
||||||
;*
|
;*
|
||||||
Structure s_client_memory
|
Structure s_client_memory
|
||||||
Buffer.i
|
*Buffer
|
||||||
Initialized.b
|
Initialized.b
|
||||||
Size.i
|
Size.i
|
||||||
EndStructure
|
EndStructure
|
||||||
|
|
||||||
Structure s_file_cache
|
Structure s_file_cache
|
||||||
Buffer.i
|
*Buffer
|
||||||
Size.i
|
Size.i
|
||||||
Timer.i
|
Timer.i
|
||||||
Is.i
|
Is.i
|
||||||
|
@ -91,11 +100,13 @@ Module lhs_web
|
||||||
client_id.i
|
client_id.i
|
||||||
client_do_cli.i
|
client_do_cli.i
|
||||||
client_do_srv.i
|
client_do_srv.i
|
||||||
|
client_ssl.i ;Client ist von einem SSL Server
|
||||||
client_mutex.i
|
client_mutex.i
|
||||||
client_thread.i
|
client_thread.i
|
||||||
List datenbuffer.s_client_memory()
|
List datenbuffer.s_client_memory()
|
||||||
client_test_cli.i
|
client_test_cli.i
|
||||||
client_test_srv.i
|
client_test_srv.i
|
||||||
|
client_cctx.i
|
||||||
EndStructure
|
EndStructure
|
||||||
|
|
||||||
Structure s_request_handler
|
Structure s_request_handler
|
||||||
|
@ -138,6 +149,7 @@ Module lhs_web
|
||||||
;*
|
;*
|
||||||
|
|
||||||
Declare server(network_server_id.i)
|
Declare server(network_server_id.i)
|
||||||
|
Declare server_SSL(network_server_id.i)
|
||||||
Declare client(network_client_id.i)
|
Declare client(network_client_id.i)
|
||||||
Declare AddFileToCache(MemoryID.i, FileName.s, Size.i)
|
Declare AddFileToCache(MemoryID.i, FileName.s, Size.i)
|
||||||
Declare FileCache(Information.i)
|
Declare FileCache(Information.i)
|
||||||
|
@ -153,6 +165,8 @@ Module lhs_web
|
||||||
Select parameter
|
Select parameter
|
||||||
Case #conf_port
|
Case #conf_port
|
||||||
conf_port = Val(setting)
|
conf_port = Val(setting)
|
||||||
|
Case #conf_TLS_Port
|
||||||
|
conf_TLS_Port = Val(setting)
|
||||||
Case #conf_binding
|
Case #conf_binding
|
||||||
If IsIPStringValid(setting)
|
If IsIPStringValid(setting)
|
||||||
conf_binding = setting
|
conf_binding = setting
|
||||||
|
@ -160,6 +174,23 @@ Module lhs_web
|
||||||
conf_binding = "127.0.0.1"
|
conf_binding = "127.0.0.1"
|
||||||
ProcedureReturn #False
|
ProcedureReturn #False
|
||||||
EndIf
|
EndIf
|
||||||
|
Case #conf_TLS_Binding
|
||||||
|
If IsIPStringValid(setting)
|
||||||
|
conf_binding = setting
|
||||||
|
Else
|
||||||
|
conf_binding = "127.0.0.1"
|
||||||
|
ProcedureReturn #False
|
||||||
|
EndIf
|
||||||
|
Case #conf_TLS_CA
|
||||||
|
conf_TLS_CA = setting
|
||||||
|
Case #conf_TLS_Cert
|
||||||
|
conf_TLS_Cert = setting
|
||||||
|
Case #conf_TLS_Key
|
||||||
|
conf_TLS_Key = setting
|
||||||
|
Case #conf_TLS_Key_Pass
|
||||||
|
conf_TLS_Key_Pass = setting
|
||||||
|
Case #conf_TLS_Enable
|
||||||
|
conf_TLS_enable = Val(setting)
|
||||||
Case #conf_defaultfile
|
Case #conf_defaultfile
|
||||||
conf_defaultfile = setting
|
conf_defaultfile = setting
|
||||||
Case #conf_basedir
|
Case #conf_basedir
|
||||||
|
@ -197,6 +228,8 @@ Module lhs_web
|
||||||
ProcedureReturn Str(conf_server_type)
|
ProcedureReturn Str(conf_server_type)
|
||||||
Case #conf_port
|
Case #conf_port
|
||||||
ProcedureReturn Str(conf_port)
|
ProcedureReturn Str(conf_port)
|
||||||
|
Case #conf_TLS_Port
|
||||||
|
ProcedureReturn Str(conf_TLS_Port)
|
||||||
Default
|
Default
|
||||||
ProcedureReturn ""
|
ProcedureReturn ""
|
||||||
EndSelect
|
EndSelect
|
||||||
|
@ -204,7 +237,31 @@ Module lhs_web
|
||||||
|
|
||||||
Procedure start_server()
|
Procedure start_server()
|
||||||
Global NewMap m_clients.s_clients(conf_max_clients)
|
Global NewMap m_clients.s_clients(conf_max_clients)
|
||||||
|
Protected settings.ltls::s_tls_server
|
||||||
|
Protected tlsresponse.i
|
||||||
|
|
||||||
|
;TODO: Create SSL Server
|
||||||
|
|
||||||
server_id = CreateNetworkServer(#PB_Any, conf_port, #PB_Network_TCP, conf_binding)
|
server_id = CreateNetworkServer(#PB_Any, conf_port, #PB_Network_TCP, conf_binding)
|
||||||
|
If conf_TLS_enable = 1
|
||||||
|
server_SSL_id = lsocket::CreateSocket(conf_TLS_Port, conf_max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, conf_TLS_binding)
|
||||||
|
If server_SSL_id
|
||||||
|
settings\CA = conf_TLS_CA
|
||||||
|
settings\Cert = conf_TLS_Cert
|
||||||
|
settings\Key = conf_TLS_Key
|
||||||
|
settings\Password = conf_TLS_Key_Pass
|
||||||
|
tlsresponse = ltls::InitTLS(@settings)
|
||||||
|
Debug "TLS Fehler:"+Str(tlsresponse)
|
||||||
|
If tlsresponse > 0
|
||||||
|
CreateThread(@server_SSL(), server_SSL_id)
|
||||||
|
Else
|
||||||
|
ProcedureReturn #False
|
||||||
|
EndIf
|
||||||
|
|
||||||
|
Else
|
||||||
|
ProcedureReturn #False
|
||||||
|
EndIf
|
||||||
|
EndIf
|
||||||
|
|
||||||
If server_id
|
If server_id
|
||||||
CreateThread(@server(), server_id)
|
CreateThread(@server(), server_id)
|
||||||
|
@ -215,6 +272,42 @@ Module lhs_web
|
||||||
|
|
||||||
EndProcedure
|
EndProcedure
|
||||||
|
|
||||||
|
Procedure server_SSL(network_server_id.i)
|
||||||
|
;**************************
|
||||||
|
;*
|
||||||
|
;* Hauptthread welcher den Netzwerkport verwaltet und den Datenfluss.
|
||||||
|
;* Erstellt die Subthreads für Clients
|
||||||
|
;*
|
||||||
|
Protected.i count_client
|
||||||
|
Protected.i client_id
|
||||||
|
|
||||||
|
thread_alive = #True
|
||||||
|
|
||||||
|
Debug "TLS Server Started."
|
||||||
|
Repeat
|
||||||
|
client_id = ltls::WaitTLSSocket(network_server_id)
|
||||||
|
Debug "New TLS Client:"+Str(client_id)
|
||||||
|
If client_id > 0
|
||||||
|
If count_client <= conf_max_clients
|
||||||
|
m_clients(Str(client_id))\client_id = client_id
|
||||||
|
m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
|
||||||
|
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
|
||||||
|
m_clients(Str(client_id))\client_ssl = 1
|
||||||
|
;Thread erstellen
|
||||||
|
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
|
||||||
|
count_client + 1
|
||||||
|
Else
|
||||||
|
Debug "Max TLS Clients reached..."
|
||||||
|
CloseNetworkConnection(client_id)
|
||||||
|
EndIf
|
||||||
|
Else
|
||||||
|
Break
|
||||||
|
EndIf
|
||||||
|
|
||||||
|
ForEver
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
|
||||||
Procedure server(network_server_id.i)
|
Procedure server(network_server_id.i)
|
||||||
;**************************
|
;**************************
|
||||||
;*
|
;*
|
||||||
|
@ -230,7 +323,7 @@ Module lhs_web
|
||||||
Debug "AllOk"
|
Debug "AllOk"
|
||||||
Repeat
|
Repeat
|
||||||
;Ist etwas passiert ?
|
;Ist etwas passiert ?
|
||||||
|
;TODO: SSL Server
|
||||||
network_event = NetworkServerEvent(network_server_id)
|
network_event = NetworkServerEvent(network_server_id)
|
||||||
Select network_event
|
Select network_event
|
||||||
Case #PB_NetworkEvent_None
|
Case #PB_NetworkEvent_None
|
||||||
|
@ -331,7 +424,7 @@ Module lhs_web
|
||||||
EndProcedure
|
EndProcedure
|
||||||
|
|
||||||
Procedure client(network_client_id.i)
|
Procedure client(network_client_id.i)
|
||||||
Protected thread_cli_id = network_client_id
|
Protected thread_cli_id = network_client_id, sent
|
||||||
Protected MyThreadJSON, ToCall, ToCallType
|
Protected MyThreadJSON, ToCall, ToCallType
|
||||||
Protected thread_temp_cache.s, thread_temp_cache_memory, temp_receivelength, thread_temp_decode_memory
|
Protected thread_temp_cache.s, thread_temp_cache_memory, temp_receivelength, thread_temp_decode_memory
|
||||||
Protected thread_reasign
|
Protected thread_reasign
|
||||||
|
@ -361,7 +454,13 @@ Module lhs_web
|
||||||
Debug "Buffer Initialisierung fehlgeschlagen."
|
Debug "Buffer Initialisierung fehlgeschlagen."
|
||||||
Break
|
Break
|
||||||
EndIf
|
EndIf
|
||||||
temp_receivelength = ReceiveNetworkData(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
|
;TODO: SSL Read
|
||||||
|
If m_clients(Str(thread_cli_id))\client_ssl = 1
|
||||||
|
temp_receivelength = ltls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
|
||||||
|
Else
|
||||||
|
temp_receivelength = ReceiveNetworkData(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
|
||||||
|
EndIf
|
||||||
|
|
||||||
If temp_receivelength = -1
|
If temp_receivelength = -1
|
||||||
Debug "Empfangsfehler."
|
Debug "Empfangsfehler."
|
||||||
Break
|
Break
|
||||||
|
@ -377,7 +476,13 @@ Module lhs_web
|
||||||
Debug "Buffer Initialisierung fehlgeschlagen."
|
Debug "Buffer Initialisierung fehlgeschlagen."
|
||||||
Break 2
|
Break 2
|
||||||
EndIf
|
EndIf
|
||||||
temp_receivelength = ReceiveNetworkData(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
|
;TODO: SSL Read 2
|
||||||
|
If m_clients(Str(thread_cli_id))\client_ssl = 1
|
||||||
|
temp_receivelength = ltls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
|
||||||
|
Else
|
||||||
|
temp_receivelength = ReceiveNetworkData(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
|
||||||
|
EndIf
|
||||||
|
|
||||||
m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength
|
m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength
|
||||||
counter_mem_buffers + 1
|
counter_mem_buffers + 1
|
||||||
If temp_receivelength = -1
|
If temp_receivelength = -1
|
||||||
|
@ -747,8 +852,14 @@ Module lhs_web
|
||||||
EndIf
|
EndIf
|
||||||
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
|
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
|
||||||
;Daten Senden...
|
;Daten Senden...
|
||||||
|
;TODO: SSL Send
|
||||||
|
If m_clients(Str(thread_cli_id))\client_ssl = 1
|
||||||
|
sent = ltls::WriteTLSSocket(thread_cli_id, thread_buffer , thread_data_size+(thread_buffer_offset-thread_buffer))
|
||||||
|
Else
|
||||||
|
sent = SendNetworkData(thread_cli_id, thread_buffer , thread_data_size+(thread_buffer_offset-thread_buffer))
|
||||||
|
EndIf
|
||||||
|
|
||||||
If SendNetworkData(thread_cli_id, thread_buffer , thread_data_size+(thread_buffer_offset-thread_buffer)) = thread_data_size+(thread_buffer_offset-thread_buffer)
|
If sent = thread_data_size+(thread_buffer_offset-thread_buffer)
|
||||||
;Ok
|
;Ok
|
||||||
;Debug "Gesendet:" + PeekS(thread_buffer,thread_buffer_length, #PB_Ascii)
|
;Debug "Gesendet:" + PeekS(thread_buffer,thread_buffer_length, #PB_Ascii)
|
||||||
FreeMemory(thread_buffer)
|
FreeMemory(thread_buffer)
|
||||||
|
@ -1000,7 +1111,7 @@ Module lhs_web
|
||||||
ProcedureReturn 0
|
ProcedureReturn 0
|
||||||
EndProcedure
|
EndProcedure
|
||||||
|
|
||||||
Procedure.s MapToJSONString(Map ConvertMap.s())
|
Procedure.s MapStringToJSONString(Map ConvertMap.s())
|
||||||
Protected MyJSON
|
Protected MyJSON
|
||||||
Protected.s Response
|
Protected.s Response
|
||||||
MyJSON = CreateJSON(#PB_Any)
|
MyJSON = CreateJSON(#PB_Any)
|
||||||
|
|
|
@ -29,6 +29,13 @@ DeclareModule lhs_web
|
||||||
#conf_max_clients
|
#conf_max_clients
|
||||||
#conf_server_type
|
#conf_server_type
|
||||||
#conf_cache_enable
|
#conf_cache_enable
|
||||||
|
#conf_TLS_CA
|
||||||
|
#conf_TLS_Cert
|
||||||
|
#conf_TLS_Key
|
||||||
|
#conf_TLS_Key_Pass
|
||||||
|
#conf_TLS_Port
|
||||||
|
#conf_TLS_Binding
|
||||||
|
#conf_TLS_Enable
|
||||||
EndEnumeration
|
EndEnumeration
|
||||||
|
|
||||||
Enumeration cli_handler 1
|
Enumeration cli_handler 1
|
||||||
|
@ -83,7 +90,7 @@ DeclareModule lhs_web
|
||||||
Declare start_server()
|
Declare start_server()
|
||||||
Declare.s register_client_handler(Route.s, Callback.i, AppPrototype.i = #handler_proto_get, RouteType.i = #handler_sub)
|
Declare.s register_client_handler(Route.s, Callback.i, AppPrototype.i = #handler_proto_get, RouteType.i = #handler_sub)
|
||||||
Declare.s mimetype(file.s)
|
Declare.s mimetype(file.s)
|
||||||
Declare.s MapToJSONString(Map ConvertMap.s())
|
Declare.s MapStringToJSONString(Map ConvertMap.s())
|
||||||
Declare IsIPStringValid(Adress.s)
|
Declare IsIPStringValid(Adress.s)
|
||||||
|
|
||||||
EndDeclareModule
|
EndDeclareModule
|
|
@ -20,10 +20,16 @@ XIncludeFile "lweb.pbi"
|
||||||
|
|
||||||
Define counter, v_lweb_srv_stop
|
Define counter, v_lweb_srv_stop
|
||||||
|
|
||||||
lhs_web::set_config(lhs_web::#conf_port, "8096")
|
lhs_web::set_config(lhs_web::#conf_port, "8081")
|
||||||
;lhs_web::set_config(lhs_web::#conf_binding, "127.0.0.1")
|
;lhs_web::set_config(lhs_web::#conf_binding, "127.0.0.1")
|
||||||
lhs_web::set_config(lhs_web::#conf_binding, "0.0.0.0")
|
lhs_web::set_config(lhs_web::#conf_binding, "0.0.0.0")
|
||||||
|
lhs_web::set_config(lhs_web::#conf_TLS_CA, "/home/renlin/dev/libressl/sample/test_ca.pem")
|
||||||
|
lhs_web::set_config(lhs_web::#conf_TLS_Cert, "/home/renlin/dev/libressl/sample/test_server_cert.pem")
|
||||||
|
lhs_web::set_config(lhs_web::#conf_TLS_Key, "/home/renlin/dev/libressl/sample/test_server_key.pem")
|
||||||
|
lhs_web::set_config(lhs_web::#conf_TLS_Key_Pass, "test-server-pass")
|
||||||
|
lhs_web::set_config(lhs_web::#conf_TLS_Binding, "127.0.0.1")
|
||||||
|
lhs_web::set_config(lhs_web::#conf_TLS_Port, "8444")
|
||||||
|
lhs_web::set_config(lhs_web::#conf_TLS_Enable, "1")
|
||||||
lhs_web::set_config(lhs_web::#conf_defaultfile, "/index.html")
|
lhs_web::set_config(lhs_web::#conf_defaultfile, "/index.html")
|
||||||
lhs_web::set_config(lhs_web::#conf_basedir, "/home/renlin/testweb/")
|
lhs_web::set_config(lhs_web::#conf_basedir, "/home/renlin/testweb/")
|
||||||
lhs_web::set_config(lhs_web::#conf_error400, "integrated")
|
lhs_web::set_config(lhs_web::#conf_error400, "integrated")
|
||||||
|
@ -50,7 +56,7 @@ Procedure.s Sample_Header(Map Header.s())
|
||||||
Response(lhs_web::#cha_R_StringBase64) = Encoded
|
Response(lhs_web::#cha_R_StringBase64) = Encoded
|
||||||
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
|
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
|
||||||
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
|
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
|
||||||
ZumSenden = lhs_web::MapToJSONString(Response())
|
ZumSenden = lhs_web::MapStringToJSONString(Response())
|
||||||
Debug "ZumSenden: " + ZumSenden
|
Debug "ZumSenden: " + ZumSenden
|
||||||
ProcedureReturn ZumSenden
|
ProcedureReturn ZumSenden
|
||||||
EndProcedure
|
EndProcedure
|
||||||
|
@ -74,7 +80,7 @@ Procedure.s Formular_Test(Map Header.s(), ContentData.s)
|
||||||
Response(lhs_web::#cha_R_StringBase64) = Encoded
|
Response(lhs_web::#cha_R_StringBase64) = Encoded
|
||||||
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
|
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
|
||||||
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
|
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
|
||||||
ZumSenden = lhs_web::MapToJSONString(Response())
|
ZumSenden = lhs_web::MapStringToJSONString(Response())
|
||||||
Debug "ZumSenden: " + ZumSenden
|
Debug "ZumSenden: " + ZumSenden
|
||||||
ProcedureReturn ZumSenden
|
ProcedureReturn ZumSenden
|
||||||
|
|
||||||
|
@ -105,7 +111,7 @@ Procedure.s Php_Test(Map Header.s(), ContentData.s)
|
||||||
Response(lhs_web::#cha_R_StringBase64) = Encoded
|
Response(lhs_web::#cha_R_StringBase64) = Encoded
|
||||||
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
|
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
|
||||||
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
|
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
|
||||||
ZumSenden = lhs_web::MapToJSONString(Response())
|
ZumSenden = lhs_web::MapStringToJSONString(Response())
|
||||||
Debug "ZumSenden: " + ZumSenden
|
Debug "ZumSenden: " + ZumSenden
|
||||||
ProcedureReturn ZumSenden
|
ProcedureReturn ZumSenden
|
||||||
EndProcedure
|
EndProcedure
|
||||||
|
@ -139,7 +145,9 @@ EndIf
|
||||||
|
|
||||||
counter = 0
|
counter = 0
|
||||||
OpenConsole("Test")
|
OpenConsole("Test")
|
||||||
PrintN("Webserver an Port:"+lhs_web::get_config(lhs_web::#conf_port))
|
PrintN("Webserver")
|
||||||
|
PrintN("HTTP an Port:"+lhs_web::get_config(lhs_web::#conf_port))
|
||||||
|
PrintN("HTTPS an Port:"+lhs_web::get_config(lhs_web::#conf_TLS_Port))
|
||||||
PrintN("Press Enter to Exit")
|
PrintN("Press Enter to Exit")
|
||||||
Input()
|
Input()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue