Skip to content

Commit

Permalink
add js evaluation job
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Apr 19, 2022
1 parent 2f36255 commit 8051cc3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
16 changes: 16 additions & 0 deletions examples/config/js.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# this scenario copies the one from https://git.gay/a/ddos-guard-bypass/src/branch/master/index.js
# it is here for educational purposes only and is meant to showcase the capabilities of the tool
jobs:
- type: sequence
args:
jobs:
- type: js
name: testscript
args:
script: arg1 + arg2
data:
arg1: 2
arg2: 4
- type: log
args:
text: 'testscript result: {{ .Value (ctx_key "data.testscript") }}'
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ require (
github.com/mitchellh/mapstructure v1.4.3
github.com/mjpitz/go-ga v0.0.7
github.com/prometheus/client_golang v1.12.1
github.com/refraction-networking/utls v1.0.0
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
github.com/valyala/fasthttp v1.34.0
go.uber.org/zap v1.21.0
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
h12.io/socks v1.0.3
)
Expand All @@ -42,10 +43,10 @@ require (
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/refraction-networking/utls v1.0.0 h1:6XQHSjDmeBCF9sPq8p2zMVGq7Ud3rTD2q88Fw8Tz1tA=
github.com/refraction-networking/utls v1.0.0/go.mod h1:tz9gX959MEFfFN5whTIocCLUG57WiILqtdVxI8c6Wj0=
github.com/rhysd/go-github-selfupdate v1.2.3 h1:iaa+J202f+Nc+A8zi75uccC8Wg3omaM7HDeimXA22Ag=
github.com/rhysd/go-github-selfupdate v1.2.3/go.mod h1:mp/N8zj6jFfBQy/XMYoWsmfzxazpPAODuqarmPDe2Rg=
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f h1:a7clxaGmmqtdNTXyvrp/lVO/Gnkzlhc/+dLs5v965GM=
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f/go.mod h1:/mK7FZ3mFYEn9zvNPhpngTyatyehSwte5bJZ4ehL5Xw=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down Expand Up @@ -554,6 +554,9 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375/go.mod h1:lNEQeAhU009zbRxng+XOj5ITVgY24WcbNnQopyfKoYQ=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
2 changes: 2 additions & 0 deletions src/job/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func Get(t string) Job {
return timeoutJob
case "loop":
return loopJob
case "js":
return jsJob
case "encrypted":
return encryptedJob
default:
Expand Down
24 changes: 24 additions & 0 deletions src/job/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/mitchellh/mapstructure"
"github.com/robertkrimen/otto"
"go.uber.org/zap"

"github.com/Arriven/db1000n/src/job/config"
Expand Down Expand Up @@ -182,6 +183,29 @@ func loopJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig,
return nil, nil
}

func jsJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (
data any, err error,
) {
var jobConfig struct {
Script string
Data map[string]any
}

if err := mapstructure.Decode(args, &jobConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}

vm := otto.New()

for key, value := range jobConfig.Data {
if err := vm.Set(key, value); err != nil {
return nil, fmt.Errorf("error setting script data: %w", err)
}
}

return vm.Run(jobConfig.Script)
}

const isEncryptedContextKey = "is_in_encrypted_context"

func EncryptedContext(ctx context.Context) context.Context {
Expand Down

0 comments on commit 8051cc3

Please sign in to comment.