2018-04-24 21:01:51 +00:00
package main
import (
"flag"
"fmt"
2018-07-24 13:31:38 +00:00
"git.xdrm.io/logauth/schastsp/client"
2018-07-25 12:19:44 +00:00
"git.xdrm.io/logauth/schastsp/context"
"os"
"time"
2018-04-24 21:01:51 +00:00
)
2018-07-25 12:19:44 +00:00
func main ( ) {
2018-04-24 21:01:51 +00:00
executionStart := time . Now ( ) . UnixNano ( )
/ * ( 1 ) Flag management ( cli arguments )
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Secret folder */
2018-07-25 12:19:44 +00:00
clientConfigPath := flag . String ( "file" , "/tmp/schastsp_keys" , "Configuration folder, it will contain sensitive data (keys), make sure to control it properly." )
2018-04-24 21:01:51 +00:00
/* (2) request | response */
isRequest := flag . Bool ( "req" , false , "Will generate a request into standard output." )
isResponse := flag . Bool ( "res" , false , "Will proceed a response management from standard input." )
/* (3) Context window size */
2018-07-25 12:19:44 +00:00
winSize := flag . Uint ( "win" , 2000 , "Time window value in ms." )
2018-04-24 21:01:51 +00:00
/* (4) Context minimum depth value */
2018-07-25 12:19:44 +00:00
minDepth := flag . Uint ( "min" , 0x0f0 , "Minimum depth value." )
2018-04-24 21:01:51 +00:00
/* (5) Context maximum depth value */
2018-07-25 12:19:44 +00:00
maxDepth := flag . Uint ( "max" , 0xfff , "Maximum depth value." )
2018-04-24 21:01:51 +00:00
/* (6) Context depth threshold */
2018-07-25 12:19:44 +00:00
thrDepth := flag . Uint ( "thr" , 0x00a , "Depth threshold protecting minimum depth to be reached." )
2018-04-24 21:01:51 +00:00
/* (7) Synchronisation request (special order) */
syncRequest := flag . Bool ( "sync" , false , "If set, proceeds a synchronisation request and outputs the synchronisation key." )
/* (8) Parse flags */
flag . Parse ( )
/ * ( 2 ) Create context + client
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Create context */
2018-07-25 12:19:44 +00:00
ctx , err := context . Create ( float64 ( * winSize ) / 1e3 , uint16 ( * minDepth ) , uint16 ( * thrDepth ) , uint16 ( * maxDepth ) )
if err != nil {
os . Stderr . WriteString ( fmt . Sprintf ( "[CLIENT_ERROR:context] %s\n" , err ) )
return
}
2018-04-24 21:01:51 +00:00
/* (2) Create client */
cli , err := client . New ( ctx , * clientConfigPath )
2018-07-25 12:19:44 +00:00
if err != nil {
os . Stderr . WriteString ( fmt . Sprintf ( "[CLIENT_ERROR:client] %s\n" , err ) )
return
}
2018-04-24 21:01:51 +00:00
/ * ( 3 ) Dispatch execution
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) If synchronisation request */
if * syncRequest {
2018-07-25 12:19:44 +00:00
synchronisationRequest ( cli )
2018-04-24 21:01:51 +00:00
2018-07-25 12:19:44 +00:00
/* (2) If request */
2018-04-24 21:01:51 +00:00
} else if * isRequest {
2018-04-24 21:42:42 +00:00
err = cli . Send ( os . Stdout )
2018-07-25 12:19:44 +00:00
if err != nil {
os . Stderr . WriteString ( fmt . Sprintf ( "[CLIENT_ERROR:request] %s\n" , err ) )
}
2018-04-24 21:01:51 +00:00
2018-07-25 12:19:44 +00:00
/* (3) If response */
2018-04-24 21:01:51 +00:00
} else if * isResponse {
2018-04-24 21:42:42 +00:00
err = cli . Receive ( os . Stdin )
2018-07-25 12:19:44 +00:00
if err != nil {
os . Stderr . WriteString ( fmt . Sprintf ( "[CLIENT_ERROR:response] %s\n" , err ) )
return
}
/* (4) Else -> nothing */
} else {
os . Stderr . WriteString ( fmt . Sprintf ( "Missing argument.\n\nYou must give one of the 3 available actions :\n -req to manage a request\n -res to manage a response\n -sync to get a synchronisation key\n" ) )
2018-04-24 21:01:51 +00:00
}
executionStop := time . Now ( ) . UnixNano ( )
2018-07-25 12:19:44 +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 ) )
}
2018-04-24 21:01:51 +00:00
return
}
2018-07-25 12:19:44 +00:00
func synchronisationRequest ( cli * client . T ) {
2018-04-24 21:01:51 +00:00
/* (1) Get synchronisation key */
syncKey , err := cli . SynchronisationKey ( )
2018-07-25 12:19:44 +00:00
if err != nil {
fmt . Errorf ( "[CLIENT_ERROR:syncKey] %s\n" , err )
return
}
2018-04-24 21:01:51 +00:00
/* (2) Print synchronisation key */
os . Stdout . Write ( syncKey )
2018-07-25 12:19:44 +00:00
return
2018-04-24 21:01:51 +00:00
2018-07-25 12:19:44 +00:00
}