2018-06-29 16:08:08 +04:00
|
|
|
(*****************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Open Source License *)
|
|
|
|
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* Permission is hereby granted, free of charge, to any person obtaining a *)
|
|
|
|
(* copy of this software and associated documentation files (the "Software"),*)
|
|
|
|
(* to deal in the Software without restriction, including without limitation *)
|
|
|
|
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
|
|
|
|
(* and/or sell copies of the Software, and to permit persons to whom the *)
|
|
|
|
(* Software is furnished to do so, subject to the following conditions: *)
|
|
|
|
(* *)
|
|
|
|
(* The above copyright notice and this permission notice shall be included *)
|
|
|
|
(* in all copies or substantial portions of the Software. *)
|
|
|
|
(* *)
|
|
|
|
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
|
|
|
|
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
|
|
|
|
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
|
|
|
|
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
|
|
|
|
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
|
|
|
|
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
|
|
|
|
(* DEALINGS IN THE SOFTWARE. *)
|
|
|
|
(* *)
|
|
|
|
(*****************************************************************************)
|
2018-05-26 14:03:12 +04:00
|
|
|
|
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-07-04 23:34:45 +04:00
|
|
|
let handle_client ?magic_bytes ~check_high_watermark ~require_auth cctxt fd =
|
2018-07-16 00:02:34 +04:00
|
|
|
Lwt_utils_unix.Socket.recv fd Request.encoding >>=? function
|
|
|
|
| Sign req ->
|
|
|
|
let encoding = result_encoding Sign.Response.encoding in
|
2018-07-04 23:34:45 +04:00
|
|
|
Handler.sign cctxt req ?magic_bytes ~check_high_watermark ~require_auth >>= fun res ->
|
2018-07-16 00:02:34 +04:00
|
|
|
Lwt_utils_unix.Socket.send fd encoding res >>= fun _ ->
|
|
|
|
Lwt_unix.close fd >>= fun () ->
|
|
|
|
return_unit
|
|
|
|
| Public_key pkh ->
|
|
|
|
let encoding = result_encoding Public_key.Response.encoding in
|
|
|
|
Handler.public_key cctxt pkh >>= fun res ->
|
|
|
|
Lwt_utils_unix.Socket.send fd encoding res >>= fun _ ->
|
|
|
|
Lwt_unix.close fd >>= fun () ->
|
|
|
|
return_unit
|
|
|
|
| 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 () ->
|
|
|
|
return_unit
|
|
|
|
|
2018-07-04 23:34:45 +04:00
|
|
|
let run (cctxt : #Client_context.wallet) path ?magic_bytes ~check_high_watermark ~require_auth =
|
2018-07-16 00:02:34 +04:00
|
|
|
let open Lwt_utils_unix.Socket in
|
2018-05-26 14:03:12 +04:00
|
|
|
begin
|
|
|
|
match path with
|
2018-07-16 00:02:34 +04:00
|
|
|
| Tcp (host, service, _opts) ->
|
2018-06-12 23:07:50 +04:00
|
|
|
log Tag.DSL.(fun f ->
|
2018-07-16 00:02:34 +04:00
|
|
|
f "Accepting TCP requests on %s:%s"
|
2018-06-12 23:07:50 +04:00
|
|
|
-% t event "accepting_tcp_requests"
|
|
|
|
-% s host_name host
|
2018-07-16 00:02:34 +04:00
|
|
|
-% s service_name service)
|
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 () ->
|
2018-07-16 00:02:34 +04:00
|
|
|
bind path >>=? fun fds ->
|
|
|
|
let rec loop fd =
|
|
|
|
Lwt_unix.accept fd >>= fun (cfd, _) ->
|
|
|
|
Lwt.async begin fun () ->
|
|
|
|
protect
|
|
|
|
~on_error:(function
|
|
|
|
| [Exn End_of_file] -> return_unit
|
|
|
|
| errs -> Lwt.return (Error errs))
|
|
|
|
(fun () ->
|
2018-07-04 23:34:45 +04:00
|
|
|
handle_client ?magic_bytes ~check_high_watermark ~require_auth cctxt cfd)
|
2018-07-16 00:02:34 +04:00
|
|
|
end ;
|
|
|
|
loop fd
|
|
|
|
in
|
|
|
|
Lwt_list.map_p loop fds >>=
|
|
|
|
return
|