2018-05-26 14:03:12 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-06-12 23:07:50 +04:00
|
|
|
open Signer_logging
|
2018-05-26 14:11:08 +04:00
|
|
|
open Signer_messages
|
2018-05-26 14:03:12 +04:00
|
|
|
|
2018-06-12 23:07:50 +04:00
|
|
|
let log = lwt_log_notice
|
2018-05-26 14:03:12 +04:00
|
|
|
|
2018-06-08 19:56:05 +04:00
|
|
|
let run (cctxt : #Client_context.wallet) path ?magic_bytes ~require_auth =
|
2018-05-26 14:03:12 +04:00
|
|
|
Lwt_utils_unix.Socket.bind path >>=? fun fd ->
|
|
|
|
let rec loop () =
|
|
|
|
Lwt_unix.accept fd >>= fun (fd, _) ->
|
|
|
|
Lwt.async begin fun () ->
|
|
|
|
Lwt_utils_unix.Socket.recv fd Request.encoding >>=? function
|
|
|
|
| Sign req ->
|
|
|
|
let encoding = result_encoding Sign.Response.encoding in
|
2018-06-08 19:56:05 +04:00
|
|
|
Handler.sign cctxt req ?magic_bytes ~require_auth >>= fun res ->
|
2018-05-26 14:03:12 +04:00
|
|
|
Lwt_utils_unix.Socket.send fd encoding res >>= fun _ ->
|
|
|
|
Lwt_unix.close fd >>= fun () ->
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-05-26 14:51:51 +04:00
|
|
|
| Public_key pkh ->
|
2018-05-26 14:03:12 +04:00
|
|
|
let encoding = result_encoding Public_key.Response.encoding in
|
2018-05-26 14:51:51 +04:00
|
|
|
Handler.public_key cctxt pkh >>= fun res ->
|
2018-05-26 14:03:12 +04:00
|
|
|
Lwt_utils_unix.Socket.send fd encoding res >>= fun _ ->
|
|
|
|
Lwt_unix.close fd >>= fun () ->
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-06-06 12:49:53 +04:00
|
|
|
| Authorized_keys ->
|
|
|
|
let encoding = result_encoding Authorized_keys.Response.encoding in
|
|
|
|
begin if require_auth then
|
|
|
|
Handler.Authorized_key.load cctxt >>=? fun keys ->
|
|
|
|
return (Authorized_keys.Response.Authorized_keys
|
|
|
|
(keys |> List.split |> snd |> List.map Signature.Public_key.hash))
|
|
|
|
else return Authorized_keys.Response.No_authentication
|
|
|
|
end >>= fun res ->
|
|
|
|
Lwt_utils_unix.Socket.send fd encoding res >>= fun _ ->
|
|
|
|
Lwt_unix.close fd >>= fun () ->
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-05-26 14:03:12 +04:00
|
|
|
end ;
|
|
|
|
loop ()
|
|
|
|
in
|
|
|
|
begin
|
|
|
|
match path with
|
|
|
|
| Tcp (host, port) ->
|
2018-06-12 23:07:50 +04:00
|
|
|
log Tag.DSL.(fun f ->
|
|
|
|
f "Accepting TCP requests on port %s:%d"
|
|
|
|
-% t event "accepting_tcp_requests"
|
|
|
|
-% s host_name host
|
|
|
|
-% s port_number port)
|
2018-05-26 14:03:12 +04:00
|
|
|
| Unix path ->
|
|
|
|
Sys.set_signal Sys.sigint (Signal_handle begin fun _ ->
|
|
|
|
Format.printf "Removing the local socket file and quitting.@." ;
|
|
|
|
Unix.unlink path ;
|
|
|
|
exit 0
|
|
|
|
end) ;
|
2018-06-12 23:07:50 +04:00
|
|
|
log Tag.DSL.(fun f ->
|
|
|
|
f "Accepting UNIX requests on %s"
|
|
|
|
-% t event "accepting_unix_requests"
|
|
|
|
-% s unix_socket_path path)
|
2018-05-26 14:03:12 +04:00
|
|
|
end >>= fun () ->
|
|
|
|
loop ()
|