Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ Following builtin functions are addionally available within JS scripts:

* `print(val)` - print arbitrary values *val* into dumbproxy log for debugging purposes.
* `readFile(path: string): string` - read file from *path* and return its content as a string.
* `mmdbOpen(path: string): mmdbReaderObject` - opens MaxMind database and returns a reader object which has only one method:
* `lookup(IP: string): mmdbRecordObject` - returns JS object with information about IP address or throws an exception if record was not found. See detailed example in the project's wiki.
* `newStopAddressIteration(): Exception` - create an exception which, once `throw`n, halts further invocations of JS function with different resolved addresses for that request. Useful to cut excess JS calls of access filter scripts which can conclude access denial without looking further into remaining resolved addresses.

Following objects are additionally available in global scope of JS scripts:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/libp2p/go-reuseport v0.4.0
github.com/ncruces/go-dns v1.3.3
github.com/oschwald/maxminddb-golang/v2 v2.3.0
github.com/pires/go-proxyproto v0.12.0
github.com/redis/go-redis/v9 v9.19.0
github.com/refraction-networking/utls v1.8.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQsc
github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
github.com/ncruces/go-dns v1.3.3 h1:59OV7XoJrTCoUMZjWRVs4GOjtntMTZqiQ5Mn+BT13hk=
github.com/ncruces/go-dns v1.3.3/go.mod h1:tuzixNY8PY/M7yUzcvRbUaeLs3ifIdydpi5H2bfRU+s=
github.com/oschwald/maxminddb-golang/v2 v2.3.0 h1:PnXjMGjkSQlwOBSyZ7hk6Fd75t7erkAhJNJgEhA3MQU=
github.com/oschwald/maxminddb-golang/v2 v2.3.0/go.mod h1:NSQvgFwPxODpBTJI5+5Ns1AAucnx7ggW9PSRRifAT1s=
github.com/pires/go-proxyproto v0.12.0 h1:TTCxD66dU898tahivkqc3hoceZp7P44FnorWyo9d5vM=
github.com/pires/go-proxyproto v0.12.0/go.mod h1:qUvfqUMEoX7T8g0q7TQLDnhMjdTrxnG0hvpMn+7ePNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
3 changes: 3 additions & 0 deletions jsext/jsext.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func ConfigureRuntime(vm *goja.Runtime) error {
if err := AddStopAddressIteration(vm); err != nil {
return err
}
if err := AddMMDBReader(vm); err != nil {
return err
}
if err := ExportEnv(vm); err != nil {
return err
}
Expand Down
98 changes: 98 additions & 0 deletions jsext/mmdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package jsext

import (
"net/netip"
"runtime"

"github.com/dop251/goja"
"github.com/oschwald/maxminddb-golang/v2"
)

type mmdbReaderObject struct {
reader *maxminddb.Reader
vm *goja.Runtime
}

func (ro *mmdbReaderObject) Get(key string) goja.Value {
switch key {
case "lookup":
return ro.vm.ToValue(ro.jsLookup)
default:
return goja.Undefined()
}
}

func (ro *mmdbReaderObject) Set(_ string, _ goja.Value) bool {
return false
}

func (ro *mmdbReaderObject) Has(key string) bool {
return key == "lookup"
}

func (ro *mmdbReaderObject) Delete(key string) bool {
return false
}

func (ro *mmdbReaderObject) Keys() []string {
return []string{"lookup"}
}

func (ro *mmdbReaderObject) lookup(ip netip.Addr) (any, error) {
var record any
err := ro.reader.Lookup(ip).Decode(&record)
if err != nil {
return nil, err
}
return record, nil
}

func (ro *mmdbReaderObject) jsLookup(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
if len(call.Arguments) != 1 {
panic(vm.NewTypeError("lookup expects exactly 1 argument, IP address string"))
}
ip, err := netip.ParseAddr(call.Argument(0).String())
if err != nil {
panic(vm.NewGoError(err))
}
record, err := ro.lookup(ip)
if err != nil {
panic(vm.NewGoError(err))
}
return vm.ToValue(record)
}

func newMMDBReaderObject(reader *maxminddb.Reader, vm *goja.Runtime) *mmdbReaderObject {
o := &mmdbReaderObject{
reader: reader,
vm: vm,
}
runtime.AddCleanup(o, func(r *maxminddb.Reader) {
r.Close()
}, reader)
return o
}

func mmdbOpen(filename string, vm *goja.Runtime) (*mmdbReaderObject, error) {
reader, err := maxminddb.Open(filename)
if err != nil {
return nil, err
}
return newMMDBReaderObject(reader, vm), nil
}

func AddMMDBReader(vm *goja.Runtime) error {
return vm.Set("mmdbOpen", func(call goja.FunctionCall) goja.Value {
if len(call.Arguments) != 1 {
panic(vm.NewTypeError("mmdbOpen expects exactly 1 argument, a filename"))
}

filename := call.Argument(0).String()
ro, err := mmdbOpen(filename, vm)
if err != nil {
panic(vm.NewGoError(err))
}

return vm.NewDynamicObject(ro)
})
}