本文整理汇总了Golang中mig.Action.ValidFrom方法的典型用法代码示例。如果您正苦于以下问题:Golang Action.ValidFrom方法的具体用法?Golang Action.ValidFrom怎么用?Golang Action.ValidFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mig.Action
的用法示例。
在下文中一共展示了Action.ValidFrom方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
var Usage = func() {
fmt.Fprintf(os.Stderr,
"Mozilla InvestiGator Action Generator\n"+
"usage: %s -k=<key id> (-i <input file)\n\n"+
"Command line to generate and sign MIG Actions.\n"+
"The resulting actions are display on stdout.\n\n"+
"Options:\n",
os.Args[0])
flag.PrintDefaults()
}
// command line options
var key = flag.String("k", "key identifier", "Key identifier used to sign the action (ex: B75C2346)")
var pretty = flag.Bool("p", false, "Print signed action in pretty JSON format")
var urlencode = flag.Bool("urlencode", false, "URL Encode marshalled JSON before output")
var posturl = flag.String("posturl", "", "POST action to <url> (enforces urlencode)")
var file = flag.String("i", "/path/to/file", "Load action from file")
var validfrom = flag.String("validfrom", "now", "(optional) set an ISO8601 date the action will be valid from. If unset, use 'now'.")
var expireafter = flag.String("expireafter", "30m", "(optional) set a validity duration for the action. If unset, use '30m'.")
flag.Parse()
// We need a key, if none is set on the command line, fail
if *key == "key identifier" {
Usage()
os.Exit(-1)
}
var a mig.Action
var err error
// if a file is defined, load action from that
if *file != "/path/to/file" {
a, err = mig.ActionFromFile(*file)
} else {
// otherwise, use interactive mode
a, err = getActionFromTerminal()
}
if err != nil {
panic(err)
}
// set the dates
if *validfrom == "now" {
a.ValidFrom = time.Now().UTC()
} else {
a.ValidFrom, err = time.Parse("2014-01-01T00:00:00.0Z", *validfrom)
if err != nil {
panic(err)
}
}
period, err := time.ParseDuration(*expireafter)
if err != nil {
log.Fatal(err)
}
a.ExpireAfter = a.ValidFrom.Add(period)
// compute the signature
str, err := a.String()
if err != nil {
panic(err)
}
a.PGPSignature, err = sign.Sign(str, *key)
if err != nil {
panic(err)
}
a.PGPSignatureDate = time.Now().UTC()
var jsonAction []byte
if *pretty {
jsonAction, err = json.MarshalIndent(a, "", "\t")
} else {
jsonAction, err = json.Marshal(a)
}
if err != nil {
panic(err)
}
// if asked, url encode the action before marshaling it
actionstr := string(jsonAction)
if *urlencode {
strJsonAction := string(jsonAction)
actionstr = url.QueryEscape(strJsonAction)
}
if *posturl != "" {
resp, err := http.PostForm(*posturl, url.Values{"action": {actionstr}})
if err != nil {
panic(err)
}
var buf [512]byte
reader := resp.Body
for {
n, err := reader.Read(buf[0:])
if err != nil {
os.Exit(0)
}
fmt.Print(string(buf[0:n]))
//.........这里部分代码省略.........
示例2: actionLauncher
//.........这里部分代码省略.........
load <path> load an action from a file at <path>
setname <name> set the name of the action
settarget <target> set the target
settimes <start> <stop> set the validity and expiration dates
sign PGP sign the action
times show the various timestamps of the action
`)
case "json":
ajson, err := json.MarshalIndent(a, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", ajson)
case "launch":
follow := true
if len(orders) > 1 {
if orders[1] == "nofollow" {
follow = false
} else {
fmt.Printf("Unknown option '%s'\n", orders[1])
}
}
if a.Name == "" {
fmt.Println("Action has no name. Define one using 'setname <name>'")
break
}
if a.Target == "" {
fmt.Println("Action has no target. Define one using 'settarget <target>'")
break
}
if !hasTimes {
fmt.Printf("Times are not defined. Setting validity from now until +%s\n", defaultExpiration)
// for immediate execution, set validity one minute in the past
a.ValidFrom = time.Now().Add(-60 * time.Second).UTC()
period, err := time.ParseDuration(defaultExpiration)
if err != nil {
panic(err)
}
a.ExpireAfter = a.ValidFrom.Add(period)
a.ExpireAfter = a.ExpireAfter.Add(60 * time.Second).UTC()
hasTimes = true
}
if !hasSignatures {
pgpsig, err := computeSignature(a, ctx)
if err != nil {
panic(err)
}
a.PGPSignatures = append(a.PGPSignatures, pgpsig)
hasSignatures = true
}
a, err = postAction(a, follow, ctx)
if err != nil {
panic(err)
}
fmt.Println("")
_ = actionReader(fmt.Sprintf("action %.0f", a.ID), ctx)
goto exit
case "load":
if len(orders) != 2 {
fmt.Println("Wrong arguments. Expects 'load <path_to_file>'")
break
}
a, err = mig.ActionFromFile(orders[1])
if err != nil {
panic(err)
}
示例3: main
//.........这里部分代码省略.........
fs.SetOutput(os.NewFile(uintptr(87592), os.DevNull))
err = fs.Parse(os.Args[2:])
fs.SetOutput(nil)
if err != nil {
// ignore the flag not defined error, which is expected because
// module parameters are defined in modules and not in main
if len(err.Error()) > 30 && err.Error()[0:29] == "flag provided but not defined" {
// requeue the parameter that failed
modargs = append(modargs, err.Error()[31:])
} else {
// if it's another error, panic
panic(err)
}
}
for _, arg := range fs.Args() {
modargs = append(modargs, arg)
}
modRunner = modules.Available[op.Module].Runner()
if _, ok := modRunner.(modules.HasParamsParser); !ok {
fmt.Fprintf(os.Stderr, "[error] module '%s' does not support command line invocation\n", op.Module)
os.Exit(2)
}
op.Parameters, err = modRunner.(modules.HasParamsParser).ParamsParser(modargs)
if err != nil || op.Parameters == nil {
panic(err)
}
a.Operations = append(a.Operations, op)
for _, arg := range os.Args[1:] {
a.Name += arg + " "
}
a.Target = target
readytolaunch:
// instanciate an API client
conf, err = client.ReadConfiguration(migrc)
if err != nil {
panic(err)
}
cli, err = client.NewClient(conf, "cmd-"+version)
if err != nil {
panic(err)
}
// set the validity 60 second in the past to deal with clock skew
a.ValidFrom = time.Now().Add(-60 * time.Second).UTC()
period, err := time.ParseDuration(expiration)
if err != nil {
panic(err)
}
a.ExpireAfter = a.ValidFrom.Add(period)
// add extra 60 seconds taken for clock skew
a.ExpireAfter = a.ExpireAfter.Add(60 * time.Second).UTC()
asig, err := cli.SignAction(a)
if err != nil {
panic(err)
}
a = asig
// evaluate target before launch, give a change to cancel before going out to agents
agents, err := cli.EvaluateAgentTarget(a.Target)
if err != nil {
panic(err)
}
fmt.Fprintf(os.Stderr, "\x1b[33m%d agents will be targeted. ctrl+c to cancel. launching in \x1b[0m", len(agents))
for i := 5; i > 0; i-- {
time.Sleep(1 * time.Second)
fmt.Fprintf(os.Stderr, "\x1b[33m%d\x1b[0m ", i)
}
fmt.Fprintf(os.Stderr, "\x1b[33mGO\n\x1b[0m")
// launch and follow
a, err = cli.PostAction(a)
if err != nil {
panic(err)
}
c := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(c, os.Interrupt)
go func() {
err = cli.FollowAction(a)
if err != nil {
panic(err)
}
done <- true
}()
select {
case <-c:
fmt.Fprintf(os.Stderr, "stop following action. agents may still be running. printing available results:\n")
goto printresults
case <-done:
goto printresults
}
printresults:
err = cli.PrintActionResults(a, show, render)
if err != nil {
panic(err)
}
}