2016-12-18 20:36:17 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
2017-08-04 16:14:17 +00:00
|
|
|
"os"
|
|
|
|
|
2016-12-18 20:36:17 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
2017-09-02 17:04:23 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-08-04 16:14:17 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail/console"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/node"
|
2017-09-25 18:22:57 +00:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2016-12-18 20:36:17 +00:00
|
|
|
)
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// signals
|
2016-12-18 20:36:17 +00:00
|
|
|
const (
|
2017-09-18 12:13:32 +00:00
|
|
|
EventSignal = "jail.signal"
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
// EventConsoleLog defines the event type for the console.log call.
|
|
|
|
eventConsoleLog = "vm.console.log"
|
2016-12-18 20:36:17 +00:00
|
|
|
)
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// registerHandlers augments and transforms a given jail cell's underlying VM,
|
|
|
|
// by adding and replacing method handlers.
|
2017-09-02 17:04:23 +00:00
|
|
|
func registerHandlers(jail *Jail, cell common.JailCell, chatID string) error {
|
2017-08-04 16:14:17 +00:00
|
|
|
jeth, err := cell.Get("jeth")
|
2017-05-03 14:24:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
registerHandler := jeth.Object().Set
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
if err = registerHandler("console", map[string]interface{}{
|
|
|
|
"log": func(fn otto.FunctionCall) otto.Value {
|
|
|
|
return console.Write(fn, os.Stdout, eventConsoleLog)
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// register send handler
|
2017-09-18 12:13:32 +00:00
|
|
|
if err = registerHandler("send", makeSendHandler(jail, cell)); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// register sendAsync handler
|
2017-09-18 12:13:32 +00:00
|
|
|
if err = registerHandler("sendAsync", makeAsyncSendHandler(jail, cell)); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// register isConnected handler
|
2017-09-18 12:13:32 +00:00
|
|
|
if err = registerHandler("isConnected", makeJethIsConnectedHandler(jail, cell)); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// register sendMessage/showSuggestions handlers
|
|
|
|
if err = cell.Set("statusSignals", struct{}{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
statusSignals, err := cell.Get("statusSignals")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registerHandler = statusSignals.Object().Set
|
2017-09-14 10:43:01 +00:00
|
|
|
if err = registerHandler("sendSignal", makeSignalHandler(chatID)); err != nil {
|
2017-08-04 16:14:17 +00:00
|
|
|
return err
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 12:13:32 +00:00
|
|
|
// makeAsyncSendHandler returns jeth.sendAsync() handler.
|
|
|
|
func makeAsyncSendHandler(jail *Jail, cellInt common.JailCell) func(call otto.FunctionCall) otto.Value {
|
|
|
|
// FIXME(tiabc): Get rid of this.
|
|
|
|
cell := cellInt.(*Cell)
|
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
|
|
|
go func() {
|
2017-10-06 16:52:26 +00:00
|
|
|
response := jail.Send(call)
|
2017-09-18 12:13:32 +00:00
|
|
|
|
2017-10-06 16:52:26 +00:00
|
|
|
callback := call.Argument(1)
|
2017-10-12 09:15:07 +00:00
|
|
|
if callback.Class() == "Function" {
|
|
|
|
// run callback asyncronously with args (error, response)
|
|
|
|
err := otto.NullValue()
|
|
|
|
cell.CallAsync(callback, err, response)
|
|
|
|
}
|
2017-09-18 12:13:32 +00:00
|
|
|
}()
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:36:17 +00:00
|
|
|
// makeSendHandler returns jeth.send() and jeth.sendAsync() handler
|
2017-10-06 16:52:26 +00:00
|
|
|
// TODO(tiabc): get rid of an extra parameter.
|
2017-09-18 12:13:32 +00:00
|
|
|
func makeSendHandler(jail *Jail, cellInt common.JailCell) func(call otto.FunctionCall) otto.Value {
|
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
2017-10-06 16:52:26 +00:00
|
|
|
return jail.Send(call)
|
2017-09-18 12:13:32 +00:00
|
|
|
}
|
2016-12-18 20:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// makeJethIsConnectedHandler returns jeth.isConnected() handler
|
2017-09-18 12:13:32 +00:00
|
|
|
func makeJethIsConnectedHandler(jail *Jail, cellInt common.JailCell) func(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
// FIXME(tiabc): Get rid of this.
|
|
|
|
cell := cellInt.(*Cell)
|
2016-12-18 20:36:17 +00:00
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
2017-09-14 20:14:31 +00:00
|
|
|
client := jail.nodeManager.RPCClient()
|
2016-12-18 20:36:17 +00:00
|
|
|
|
|
|
|
var netListeningResult bool
|
|
|
|
if err := client.Call(&netListeningResult, "net_listening"); err != nil {
|
2017-09-18 12:13:32 +00:00
|
|
|
return newErrorResponseOtto(cell.VM, err.Error(), nil)
|
2016-12-18 20:36:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if !netListeningResult {
|
2017-09-18 12:13:32 +00:00
|
|
|
return newErrorResponseOtto(cell.VM, node.ErrNoRunningNode.Error(), nil)
|
2016-12-18 20:36:17 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
return newResultResponse(call.Otto, true)
|
2016-12-18 20:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 10:43:01 +00:00
|
|
|
// SignalEvent wraps Jail send signals
|
|
|
|
type SignalEvent struct {
|
2017-05-16 12:09:52 +00:00
|
|
|
ChatID string `json:"chat_id"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
2017-09-14 10:43:01 +00:00
|
|
|
func makeSignalHandler(chatID string) func(call otto.FunctionCall) otto.Value {
|
2017-07-18 14:26:24 +00:00
|
|
|
return func(call otto.FunctionCall) otto.Value {
|
|
|
|
message := call.Argument(0).String()
|
|
|
|
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.Send(signal.Envelope{
|
2017-09-14 10:43:01 +00:00
|
|
|
Type: EventSignal,
|
|
|
|
Event: SignalEvent{
|
2017-07-18 14:26:24 +00:00
|
|
|
ChatID: chatID,
|
2017-09-14 10:43:01 +00:00
|
|
|
Data: message,
|
2017-07-18 14:26:24 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
return newResultResponse(call.Otto, true)
|
2017-07-18 14:26:24 +00:00
|
|
|
}
|
|
|
|
}
|