schastsp/cmd/server/server.go

118 lines
3.3 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"git.xdrm.io/logauth/schastsp/context"
2018-09-06 14:41:03 +00:00
"git.xdrm.io/logauth/schastsp/internal/scha"
"git.xdrm.io/logauth/schastsp/server"
2018-09-06 14:41:03 +00:00
"os"
"time"
)
2018-09-06 14:41:03 +00:00
func main() {
executionStart := time.Now().UnixNano()
/* (1) Flag management (cli arguments)
---------------------------------------------------------*/
/* (1) Secret folder */
serverConfigPath := flag.String("file", "/tmp/schastsp_hash", "Configuration file, it will contain sensitive data (key), make sure to control it properly. (default: /tmp/schastsp_hash)")
/* (2) Context window size */
winSize := flag.Uint("win", 2000, "Time window value in ms. (default: 2000)")
/* (3) Context minimum depth value */
minDepth := flag.Uint("min", 0x0f0, "Minimum depth value. (default: 240)")
/* (4) Context maximum depth value */
maxDepth := flag.Uint("max", 0xfff, "Maximum depth value. (default: 4095)")
/* (5) Context depth threshold */
thrDepth := flag.Uint("thr", 0x00a, "Depth threshold protecting minimum depth to be reached. (default: 10)")
/* (6) Synchronisation request (special order) */
syncRequest := flag.Bool("sync", false, "If set, proceeds a synchronisation from standard input (synchronisation key).")
/* (7) Parse flags */
flag.Parse()
/* (2) Create context + client
---------------------------------------------------------*/
/* (1) Create context */
2018-09-06 14:41:03 +00:00
ctx, err := context.Create(float64(*winSize)/1e3, uint16(*minDepth), uint16(*thrDepth), uint16(*maxDepth))
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("[SERVER_ERROR:context] %s\n", err))
return
}
/* (2) Create server */
ser, err := server.New(ctx, *serverConfigPath)
2018-09-06 14:41:03 +00:00
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("[SERVER_ERROR:server] %s\n", err))
return
}
/* (3) Dispatch execution
---------------------------------------------------------*/
/* (1) If synchronisation request */
if *syncRequest {
2018-09-06 14:41:03 +00:00
synchronisationCommit(ser)
2018-09-06 14:41:03 +00:00
/* (2) If request handling */
} else {
err = ser.HandleRequest(os.Stdin, os.Stdout)
2018-09-06 14:41:03 +00:00
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("[SERVER_ERROR:request] %s\n", err))
}
}
executionStop := time.Now().UnixNano()
2018-09-06 14:41:03 +00:00
nsElapsed := float64(executionStop - executionStart)
usElapsed := float64(nsElapsed / 1e3)
msElapsed := float64(usElapsed / 1e3)
sElapsed := float64(msElapsed / 1e3)
if sElapsed >= 1 {
os.Stderr.WriteString(fmt.Sprintf("executed in %.3f s\n", sElapsed))
} else if msElapsed >= 1 {
os.Stderr.WriteString(fmt.Sprintf("executed in %.3f ms\n", msElapsed))
} else if usElapsed >= 1 {
os.Stderr.WriteString(fmt.Sprintf("executed in %.3f us\n", usElapsed))
} else if nsElapsed >= 1 {
os.Stderr.WriteString(fmt.Sprintf("executed in %.3f ns\n", nsElapsed))
}
return
}
2018-09-06 14:41:03 +00:00
func synchronisationCommit(ser *server.T) {
/* (1) Try to read key from standard input */
syncKey := make([]byte, scha.HSIZE)
read, err := os.Stdin.Read(syncKey)
2018-09-06 14:41:03 +00:00
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("[SERVER_ERROR:syncKey] %s\n", err))
return
}
if uint16(read) != scha.HSIZE {
os.Stderr.WriteString(fmt.Sprintf("[SERVER_ERROR:syncKey] Cannot read proper key size\n"))
return
}
/* (2) Store synchronisation key */
err = ser.SynchronisationKey(syncKey)
2018-09-06 14:41:03 +00:00
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("[SERVER_ERROR:syncKey] %s\n", err))
return
}
/* (3) Debug */
os.Stdout.WriteString("Synchronisation key successfully written\n")
2018-09-06 14:41:03 +00:00
return
2018-09-06 14:41:03 +00:00
}