diff --git a/Blockchain.go b/Blockchain.go index d9324a6..b74434e 100644 --- a/Blockchain.go +++ b/Blockchain.go @@ -26,6 +26,6 @@ func initUserBlockchain() { if err != nil { Filters.LogError("initUserBlockchain", "error: %s\n", err.Error()) - os.Exit(1) + os.Exit(ExitBlockchainCorrupt) } } diff --git a/Config.go b/Config.go index b935997..04dc480 100644 --- a/Config.go +++ b/Config.go @@ -16,7 +16,7 @@ import ( ) // Version is the current core library version -const Version = "Alpha 4/05.11.2021" +const Version = "Alpha 4/15.11.2021" var config struct { LogFile string `yaml:"LogFile"` // Log file diff --git a/Exit.go b/Exit.go new file mode 100644 index 0000000..c3486be --- /dev/null +++ b/Exit.go @@ -0,0 +1,23 @@ +/* +File Name: Exit.go +Copyright: 2021 Peernet s.r.o. +Author: Peter Kleissner +*/ + +package core + +// Exit codes signal why the application exited. These are universal between clients developed by the Peernet organization. +// Clients are encouraged to log additional details in a log file. 3rd party clients may define additional exit codes. +const ( + ExitSuccess = 0 // This is actually never used. + ExitErrorConfigAccess = 1 // Error accessing the config file. + ExitErrorConfigRead = 2 // Error reading the config file. + ExitErrorConfigParse = 3 // Error parsing the config file. + ExitErrorLogInit = 4 // Error initializing log file. + ExitParamWebapiInvalid = 5 // Parameter for webapi is invalid. + ExitPrivateKeyCorrupt = 6 // Private key is corrupt. + ExitPrivateKeyCreate = 7 // Cannot create a new private key. + ExitBlockchainCorrupt = 8 // Blockchain is corrupt. + ExitGraceful = 9 // Graceful shutdown. + STATUS_CONTROL_C_EXIT = 0xC000013A // The application terminated as a result of a CTRL+C. This is a Windows NTSTATUS value. +) diff --git a/Peer ID.go b/Peer ID.go index 53540e7..3f0b099 100644 --- a/Peer ID.go +++ b/Peer ID.go @@ -42,7 +42,7 @@ func initPeerID() { } Filters.LogError("initPeerID", "private key in config is corrupted! Error: %s\n", err.Error()) - os.Exit(1) + os.Exit(ExitPrivateKeyCorrupt) } // if the peer ID is empty, create a new user public-private key pair @@ -50,7 +50,7 @@ func initPeerID() { peerPrivateKey, peerPublicKey, err = Secp256k1NewPrivateKey() if err != nil { Filters.LogError("initPeerID", "generating public-private key pairs: %s\n", err.Error()) - os.Exit(1) + os.Exit(ExitPrivateKeyCreate) } nodeID = protocol.PublicKey2NodeID(peerPublicKey) diff --git a/README.md b/README.md index 6131b4e..c686fee 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ import ( func init() { if status, err := core.LoadConfig("Config.yaml"); err != nil { fmt.Printf("Error loading config file: %s", err.Error()) - os.Exit(1) + os.Exit(core.ExitErrorConfigAccess) } core.InitLog() diff --git a/webapi/API.go b/webapi/API.go index b4ad608..549ffc9 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -10,10 +10,10 @@ import ( "crypto/tls" "encoding/json" "errors" - "log" "net/http" "time" + "github.com/PeernetOfficial/core" "github.com/gorilla/mux" "github.com/gorilla/websocket" ) @@ -73,13 +73,15 @@ func Start(ListenAddresses []string, UseSSL bool, CertificateFile, CertificateKe Router.HandleFunc("/file/view", apiFileView).Methods("GET") for _, listen := range ListenAddresses { - go startWebServer(listen, UseSSL, CertificateFile, CertificateKey, Router, "API", TimeoutRead, TimeoutWrite) + go startWebAPI(listen, UseSSL, CertificateFile, CertificateKey, Router, "API", TimeoutRead, TimeoutWrite) } } -// startWebServer starts a web-server with given parameters and logs the status. If may block forever and only returns if there is an error. +// startWebAPI starts a web-server with given parameters and logs the status. If may block forever and only returns if there is an error. // The certificate file and key are only used if SSL is enabled. The read and write timeout may be 0 for no timeout. -func startWebServer(WebListen string, UseSSL bool, CertificateFile, CertificateKey string, Handler http.Handler, Info string, ReadTimeout, WriteTimeout time.Duration) { +func startWebAPI(WebListen string, UseSSL bool, CertificateFile, CertificateKey string, Handler http.Handler, Info string, ReadTimeout, WriteTimeout time.Duration) { + core.Filters.LogError("startWebAPI", "Start API at '%s'\n", WebListen) + tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12} // for security reasons disable TLS 1.0/1.1 server := &http.Server{ @@ -94,12 +96,12 @@ func startWebServer(WebListen string, UseSSL bool, CertificateFile, CertificateK if UseSSL { // HTTPS if err := server.ListenAndServeTLS(CertificateFile, CertificateKey); err != nil { - log.Printf("Error listening on '%s': %v\n", WebListen, err) + core.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err) } } else { // HTTP if err := server.ListenAndServe(); err != nil { - log.Printf("Error listening on '%s': %v\n", WebListen, err) + core.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err) } } } @@ -110,7 +112,7 @@ func EncodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (err e err = json.NewEncoder(w).Encode(data) if err != nil { - log.Printf("Error writing data for route '%s': %v\n", r.URL.Path, err) + core.Filters.LogError("EncodeJSON", "Error writing data for route '%s': %v\n", r.URL.Path, err) } return err