diff --git a/API.go b/API.go index 9536228..8619785 100644 --- a/API.go +++ b/API.go @@ -8,19 +8,31 @@ package main import ( "bytes" + "flag" "net/http" + "strings" "time" "github.com/PeernetOfficial/core/webapi" "github.com/gorilla/websocket" ) +// startAPI starts the API if enabled via command line parameter or if the settings are set in the config file. +// Using the command line option always ignores any API settings from the config (including timeout settings). func startAPI() { - if len(config.APIListen) == 0 { + if apiListen := parseCmdParamWebapi(); len(apiListen) > 0 { + // API listen parameter via command line argument. + // Note that read and write timeouts are set to 0 which means they are not used. SSL is not enabled. + webapi.Start(apiListen, false, "", "", 0, 0) + + } else if len(config.APIListen) != 0 { + // API settings via config file. + webapi.Start(config.APIListen, config.APIUseSSL, config.APICertificateFile, config.APICertificateKey, parseDuration(config.APITimeoutRead), parseDuration(config.APITimeoutWrite)) + return + } else { return } - webapi.Start(config.APIListen, config.APIUseSSL, config.APICertificateFile, config.APICertificateKey, parseDuration(config.APITimeoutRead), parseDuration(config.APITimeoutWrite)) webapi.Router.HandleFunc("/console", apiConsole).Methods("GET") } @@ -87,3 +99,16 @@ func apiConsole(w http.ResponseWriter, r *http.Request) { bufferR.Write(message) } } + +// parseCmdParamWebapi parses a "-webapi=" command line parameter +func parseCmdParamWebapi() (apiListen []string) { + var param string + flag.StringVar(¶m, "webapi", "", "Specify the list of IP:Ports for the webapi to listen. Example: -webapi=127.0.0.1:1234") + flag.Parse() + + if len(param) == 0 { + return nil + } + + return strings.Split(param, ",") +} diff --git a/Main.go b/Main.go index 510c4ff..a42656c 100644 --- a/Main.go +++ b/Main.go @@ -25,8 +25,8 @@ var config struct { APIUseSSL bool `yaml:"APIUseSSL"` // Enables SSL. APICertificateFile string `yaml:"APICertificateFile"` // This is the certificate received from the CA. This can also include the intermediate certificate from the CA. APICertificateKey string `yaml:"APICertificateKey"` // This is the private key. - APITimeoutRead string `yaml:"HTTPTimeoutRead"` // The maximum duration for reading the entire request, including the body. - APITimeoutWrite string `yaml:"HTTPTimeoutWrite"` // The maximum duration before timing out writes of the response. This includes processing time and is therefore the max time any HTTP function may take. + APITimeoutRead string `yaml:"APITimeoutRead"` // The maximum duration for reading the entire request, including the body. + APITimeoutWrite string `yaml:"APITimeoutWrite"` // The maximum duration before timing out writes of the response. This includes processing time and is therefore the max time any HTTP function may take. } func init() { diff --git a/README.md b/README.md index b7d3fb4..11692ad 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,42 @@ peer list List current peers debug key create Create Public-Private Key pair debug key self List current Public-Private Key pair ``` + +## Web API + +The web API described in the [core library](https://github.com/PeernetOfficial/core/tree/master/webapi) is only available if the listen parameter is specified either via command line parameter or via the settings file. + +As described in the linked specification, do not expose this API on the internet or local network, it allows sensitive operations such as deleting the private key and access to local files. It shall only be used by local clients on the same machine. Set the listen parameter only to a loopback IP address such as `::1`. + +### Option 1: Command Line Parameter + +Specify the `webapi` parameter with an IP:Port to listen: + +``` +Cmd -webapi=[::1]:1234 +``` + +Multiple addresses can be specified by separating them with a comma: + +``` +Cmd -webapi=127.0.0.1:1337,[::1]:1234 +``` + +Note that the command line parameter does not support the SSL and timeout settings. The API settings in the config file are ignored in case the command line parameter is specified. + +### Option 2: Config File + +In the `Config.yaml` specify the below line. The `APIListen` is a list of IP:Port pairs. IPv4 and IPv6 are supported. The SSL and timeout settings are optional. If the timeouts are not specified, they are not used. Valid units for the timeout settings are ms, s, m, h. + +```yaml +APIListen: ["127.0.0.1:112","[::1]:112"] + +# optional enable SSL +APIUseSSL: true +APICertificateFile: "certificate.crt" # Certificate received from the CA. This can also include the intermediate certificate from the CA. +APICertificateKey: "certificate.key" # Private Key + +# optional timeouts +APITimeoutRead: "10m" # The maximum duration for reading the entire request, including the body. In this example 10 minutes. +APITimeoutWrite: "10m" # The maximum duration before timing out writes of the response. This includes processing time and is therefore the max time any HTTP function may take. +```