You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.8 KiB

  1. // Copyright © 2017 Ethan Frey
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cmd
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "os"
  20. "github.com/gorilla/handlers"
  21. "github.com/gorilla/mux"
  22. "github.com/pkg/errors"
  23. "github.com/spf13/cobra"
  24. "github.com/spf13/viper"
  25. "github.com/tendermint/go-crypto/keys/server"
  26. )
  27. // serveCmd represents the serve command
  28. var serveCmd = &cobra.Command{
  29. Use: "serve",
  30. Short: "Run the key manager as an http server",
  31. Long: `Launch an http server with a rest api to manage the
  32. private keys much more in depth than the cli can perform.
  33. In particular, this will allow you to sign transactions with
  34. the private keys in the store.`,
  35. RunE: serveHTTP,
  36. }
  37. func init() {
  38. RootCmd.AddCommand(serveCmd)
  39. serveCmd.Flags().IntP("port", "p", 8118, "TCP Port for listen for http server")
  40. serveCmd.Flags().StringP("socket", "s", "", "UNIX socket for more secure http server")
  41. serveCmd.Flags().StringP("type", "t", "ed25519", "Default key type (ed25519|secp256k1)")
  42. }
  43. func serveHTTP(cmd *cobra.Command, args []string) error {
  44. var l net.Listener
  45. var err error
  46. socket := viper.GetString("socket")
  47. if socket != "" {
  48. l, err = createSocket(socket)
  49. if err != nil {
  50. return errors.Wrap(err, "Cannot create socket")
  51. }
  52. } else {
  53. port := viper.GetInt("port")
  54. l, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
  55. if err != nil {
  56. return errors.Errorf("Cannot listen on port %d", port)
  57. }
  58. }
  59. router := mux.NewRouter()
  60. ks := server.New(GetKeyManager(), viper.GetString("type"))
  61. ks.Register(router)
  62. // only set cors for tcp listener
  63. var h http.Handler
  64. if socket == "" {
  65. allowedHeaders := handlers.AllowedHeaders([]string{"Content-Type"})
  66. h = handlers.CORS(allowedHeaders)(router)
  67. } else {
  68. h = router
  69. }
  70. err = http.Serve(l, h)
  71. fmt.Printf("Server Killed: %+v\n", err)
  72. return nil
  73. }
  74. // createSocket deletes existing socket if there, creates a new one,
  75. // starts a server on the socket, and sets permissions to 0600
  76. func createSocket(socket string) (net.Listener, error) {
  77. err := os.Remove(socket)
  78. if err != nil && !os.IsNotExist(err) {
  79. // only fail if it does exist and cannot be deleted
  80. return nil, err
  81. }
  82. l, err := net.Listen("unix", socket)
  83. if err != nil {
  84. return nil, err
  85. }
  86. mode := os.FileMode(0700) | os.ModeSocket
  87. err = os.Chmod(socket, mode)
  88. if err != nil {
  89. l.Close()
  90. return nil, err
  91. }
  92. return l, nil
  93. }