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.

110 lines
2.9 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. const (
  28. flagPort = "port"
  29. flagSocket = "socket"
  30. )
  31. // serveCmd represents the serve command
  32. var serveCmd = &cobra.Command{
  33. Use: "serve",
  34. Short: "Run the key manager as an http server",
  35. Long: `Launch an http server with a rest api to manage the
  36. private keys much more in depth than the cli can perform.
  37. In particular, this will allow you to sign transactions with
  38. the private keys in the store.`,
  39. RunE: runServeCmd,
  40. }
  41. func init() {
  42. serveCmd.Flags().IntP(flagPort, "p", 8118, "TCP Port for listen for http server")
  43. serveCmd.Flags().StringP(flagSocket, "s", "", "UNIX socket for more secure http server")
  44. serveCmd.Flags().StringP(flagType, "t", "ed25519", "Default key type (ed25519|secp256k1)")
  45. }
  46. func runServeCmd(cmd *cobra.Command, args []string) error {
  47. var l net.Listener
  48. var err error
  49. socket := viper.GetString(flagSocket)
  50. if socket != "" {
  51. l, err = createSocket(socket)
  52. if err != nil {
  53. return errors.Wrap(err, "Cannot create socket")
  54. }
  55. } else {
  56. port := viper.GetInt(flagPort)
  57. l, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
  58. if err != nil {
  59. return errors.Errorf("Cannot listen on port %d", port)
  60. }
  61. }
  62. router := mux.NewRouter()
  63. ks := server.New(GetKeyManager(), viper.GetString(flagType))
  64. ks.Register(router)
  65. // only set cors for tcp listener
  66. var h http.Handler
  67. if socket == "" {
  68. allowedHeaders := handlers.AllowedHeaders([]string{"Content-Type"})
  69. h = handlers.CORS(allowedHeaders)(router)
  70. } else {
  71. h = router
  72. }
  73. err = http.Serve(l, h)
  74. fmt.Printf("Server Killed: %+v\n", err)
  75. return nil
  76. }
  77. // createSocket deletes existing socket if there, creates a new one,
  78. // starts a server on the socket, and sets permissions to 0600
  79. func createSocket(socket string) (net.Listener, error) {
  80. err := os.Remove(socket)
  81. if err != nil && !os.IsNotExist(err) {
  82. // only fail if it does exist and cannot be deleted
  83. return nil, err
  84. }
  85. l, err := net.Listen("unix", socket)
  86. if err != nil {
  87. return nil, err
  88. }
  89. mode := os.FileMode(0700) | os.ModeSocket
  90. err = os.Chmod(socket, mode)
  91. if err != nil {
  92. l.Close()
  93. return nil, err
  94. }
  95. return l, nil
  96. }