2018-07-29 20:18:06 +02:00
|
|
|
// +build js
|
|
|
|
|
2018-07-27 03:07:23 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gopherjs/gopherjs/js"
|
|
|
|
"time"
|
2018-07-29 02:42:35 +02:00
|
|
|
"github.com/mame82/mvuex"
|
2018-07-27 03:07:23 +02:00
|
|
|
)
|
|
|
|
|
2018-08-05 16:04:27 +02:00
|
|
|
var globalState *GlobalState
|
|
|
|
|
2018-07-27 03:07:23 +02:00
|
|
|
const (
|
2018-07-27 17:21:37 +02:00
|
|
|
maxLogEntries = 500
|
2018-07-28 04:49:25 +02:00
|
|
|
|
2018-08-05 16:04:27 +02:00
|
|
|
VUEX_ACTION_UPDATE_RUNNING_HID_JOBS = "updateRunningHidJobs"
|
2018-07-28 04:49:25 +02:00
|
|
|
VUEX_ACTION_DEPLOY_CURRENT_GADGET_SETTINGS = "deployCurrentGadgetSettings"
|
|
|
|
VUEX_ACTION_UPDATE_GADGET_SETTINGS_FROM_DEPLOYED = "updateCurrentGadgetSettingsFromDeployed"
|
|
|
|
VUEX_MUTATION_SET_CURRENT_GADGET_SETTINGS_TO = "setCurrentGadgetSettings"
|
|
|
|
VUEX_MUTATION_SET_CURRENT_HID_SCRIPT_SOURCE_TO = "setCurrentHIDScriptSource"
|
|
|
|
|
2018-07-27 03:07:23 +02:00
|
|
|
initHIDScript = `layout('us'); // US keyboard layout
|
|
|
|
typingSpeed(100,150) // Wait 100ms between key strokes + an additional random value between 0ms and 150ms (natural)
|
|
|
|
|
|
|
|
waitLEDRepeat(NUM); // Wait till NUM LED of target changes frequently multiple times (doesn't work on OSX)
|
|
|
|
press("GUI r");
|
|
|
|
delay(500);
|
|
|
|
type("notepad\n")
|
|
|
|
delay(1000);
|
|
|
|
for (var i = 0; i < 3; i++) {
|
|
|
|
type("Hello from P4wnP1 run " + i + " !\n");
|
|
|
|
type("Moving mouse right ...");
|
|
|
|
moveStepped(500,0);
|
|
|
|
type("and left\n");
|
|
|
|
moveStepped(-500,0);
|
|
|
|
}
|
|
|
|
type("Let's type fast !!!!!!!!!!!!!!!\n")
|
|
|
|
typingSpeed(0,0);
|
|
|
|
for (var i = 3; i < 10; i++) {
|
|
|
|
type("Hello from P4wnP1 run " + i + " !\n");
|
|
|
|
type("Moving mouse right ...");
|
|
|
|
moveStepped(500,0);
|
|
|
|
type("and left\n");
|
|
|
|
moveStepped(-500,0);
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
|
|
|
|
type GlobalState struct {
|
|
|
|
*js.Object
|
2018-08-05 16:04:27 +02:00
|
|
|
Title string `js:"title"`
|
|
|
|
CurrentHIDScriptSource string `js:"currentHIDScriptSource"`
|
|
|
|
CurrentGadgetSettings *jsGadgetSettings `js:"currentGadgetSettings"`
|
|
|
|
EventReceiver *jsEventReceiver `js:"eventReceiver"`
|
|
|
|
HidJobList *jsHidJobStateList `js:"hidJobList"`
|
|
|
|
IsModalEnabled bool `js:"isModalEnabled"`
|
|
|
|
IsConnected bool `js:"isConnected"`
|
|
|
|
FailedConnectionAttempts int `js:"failedConnectionAttempts"`
|
2018-08-06 02:44:28 +02:00
|
|
|
InterfaceSettings *jsEthernetSettingsList `js:"InterfaceSettings"`
|
2018-08-05 16:04:27 +02:00
|
|
|
|
2018-07-27 03:07:23 +02:00
|
|
|
|
|
|
|
Counter int `js:"count"`
|
|
|
|
Text string `js:"text"`
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 17:21:37 +02:00
|
|
|
func createGlobalStateStruct() GlobalState {
|
2018-07-27 03:07:23 +02:00
|
|
|
state := GlobalState{Object:O()}
|
|
|
|
state.Title = "P4wnP1 by MaMe82"
|
|
|
|
state.CurrentHIDScriptSource = initHIDScript
|
|
|
|
state.CurrentGadgetSettings = NewUSBGadgetSettings()
|
2018-07-28 04:49:25 +02:00
|
|
|
//UpdateGadgetSettingsFromDeployed(state.CurrentGadgetSettings)
|
2018-08-01 18:09:50 +02:00
|
|
|
state.HidJobList = NewHIDJobStateList()
|
2018-08-05 16:04:27 +02:00
|
|
|
state.EventReceiver = NewEventReceiver(maxLogEntries, state.HidJobList)
|
|
|
|
state.IsConnected = false
|
2018-07-31 21:58:57 +02:00
|
|
|
state.IsModalEnabled = false
|
2018-08-05 16:04:27 +02:00
|
|
|
state.FailedConnectionAttempts = 0
|
2018-08-06 02:44:28 +02:00
|
|
|
//Retrieve Interface settings
|
|
|
|
ifSettings,err := RpcClient.GetAllDeployedEthernetInterfaceSettings(time.Second*5)
|
|
|
|
if err != nil { panic("Couldn't retrieveinterface settings") }
|
|
|
|
state.InterfaceSettings = ifSettings
|
2018-07-27 03:07:23 +02:00
|
|
|
|
|
|
|
state.Counter = 1337
|
|
|
|
state.Text = "Hi there says MaMe82"
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:49:25 +02:00
|
|
|
func actionUpdateGadgetSettingsFromDeployed(store *mvuex.Store, context *mvuex.ActionContext, state *GlobalState) {
|
|
|
|
go func() {
|
|
|
|
//fetch deployed gadget settings
|
2018-08-06 02:44:28 +02:00
|
|
|
dGS,err := RpcClient.RpcGetDeployedGadgetSettings(time.Second * 5)
|
2018-07-28 04:49:25 +02:00
|
|
|
if err != nil {
|
|
|
|
println("Couldn't retrieve deployed gadget settings")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//convert to JS version
|
|
|
|
jsGS := &jsGadgetSettings{Object:O()}
|
|
|
|
jsGS.fromGS(dGS)
|
|
|
|
|
|
|
|
//commit to current
|
|
|
|
context.Commit("setCurrentGadgetSettings", jsGS)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-05 16:04:27 +02:00
|
|
|
func actionUpdateRunningHidJobs(store *mvuex.Store, context *mvuex.ActionContext, state *GlobalState) {
|
|
|
|
go func() {
|
|
|
|
//fetch deployed gadget settings
|
2018-08-06 02:44:28 +02:00
|
|
|
jobstates,err := RpcClient.RpcGetRunningHidJobStates(time.Second * 10)
|
2018-08-05 16:04:27 +02:00
|
|
|
if err != nil {
|
|
|
|
println("Couldn't retrieve stateof running HID jobs", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _,jobstate := range jobstates {
|
|
|
|
println("updateing jobstate", jobstate)
|
|
|
|
state.HidJobList.UpdateEntry(jobstate.Id, jobstate.VmId, false,false, "initial job state", "",time.Now().String(),jobstate.Source)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:49:25 +02:00
|
|
|
func actionDeployCurrentGadgetSettings(store *mvuex.Store, context *mvuex.ActionContext, state *GlobalState) {
|
|
|
|
go func() {
|
|
|
|
// ToDo: Indicate deployment process via global state
|
|
|
|
|
|
|
|
//get current GadgetSettings
|
|
|
|
curGS := state.CurrentGadgetSettings.toGS()
|
|
|
|
|
|
|
|
//try to set them via gRPC (the server holds an internal state, setting != deploying)
|
2018-08-06 02:44:28 +02:00
|
|
|
err := RpcClient.RpcSetRemoteGadgetSettings(curGS, time.Second)
|
2018-07-28 04:49:25 +02:00
|
|
|
if err != nil {
|
|
|
|
//ToDo: use global store to return something, or allow actions to return promises (latter is too much JavaScript)
|
2018-07-28 04:51:38 +02:00
|
|
|
Alert(err.Error())
|
2018-07-28 04:49:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//try to deploy the, now set, remote GadgetSettings via gRPC
|
2018-08-06 02:44:28 +02:00
|
|
|
_,err = RpcClient.RpcDeployRemoteGadgetSettings(time.Second*10)
|
2018-07-28 04:49:25 +02:00
|
|
|
if err != nil {
|
|
|
|
//ToDo: use global store to return something, or allow actions to return promises (latter is too much JavaScript)
|
2018-07-28 04:51:38 +02:00
|
|
|
Alert(err.Error())
|
2018-07-28 04:49:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//ToDo: If we're here, we succeeded and should indicate this via global state
|
|
|
|
Alert("GadgetSettings deployed successfully")
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-05 16:04:27 +02:00
|
|
|
func initMVuex() GlobalState {
|
2018-07-27 03:07:23 +02:00
|
|
|
state := createGlobalStateStruct()
|
2018-08-05 16:04:27 +02:00
|
|
|
globalState = &state //make accessible through global var
|
2018-07-27 03:07:23 +02:00
|
|
|
store := mvuex.NewStore(
|
|
|
|
mvuex.State(state),
|
2018-07-27 17:21:37 +02:00
|
|
|
mvuex.Action("actiontest", func(store *mvuex.Store, context *mvuex.ActionContext, state *GlobalState) {
|
|
|
|
go func() {
|
|
|
|
for i:=0; i<10; i++ {
|
|
|
|
println(state.Counter)
|
|
|
|
time.Sleep(1*time.Second)
|
|
|
|
context.Commit("increment",5)
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
}),
|
2018-07-28 04:49:25 +02:00
|
|
|
mvuex.Mutation("setModalEnabled", func (store *mvuex.Store, state *GlobalState, enabled bool) {
|
|
|
|
state.IsModalEnabled = enabled
|
|
|
|
return
|
|
|
|
}),
|
2018-07-27 03:07:23 +02:00
|
|
|
mvuex.Mutation("increment", func (store *mvuex.Store, state *GlobalState, add int) {
|
|
|
|
state.Counter += add
|
|
|
|
return
|
|
|
|
}),
|
|
|
|
mvuex.Mutation("decrement", func (store *mvuex.Store, state *GlobalState) {
|
|
|
|
state.Counter--
|
|
|
|
return
|
|
|
|
}),
|
|
|
|
mvuex.Mutation("setText", func (store *mvuex.Store, state *GlobalState, newText string) {
|
|
|
|
state.Text = newText
|
|
|
|
return
|
|
|
|
}),
|
2018-07-28 04:49:25 +02:00
|
|
|
mvuex.Mutation(VUEX_MUTATION_SET_CURRENT_HID_SCRIPT_SOURCE_TO, func (store *mvuex.Store, state *GlobalState, newText string) {
|
2018-07-27 03:07:23 +02:00
|
|
|
state.CurrentHIDScriptSource = newText
|
|
|
|
return
|
|
|
|
}),
|
2018-07-28 04:49:25 +02:00
|
|
|
mvuex.Mutation(VUEX_MUTATION_SET_CURRENT_GADGET_SETTINGS_TO, func (store *mvuex.Store, state *GlobalState, settings *jsGadgetSettings) {
|
|
|
|
state.CurrentGadgetSettings = settings
|
2018-07-27 03:07:23 +02:00
|
|
|
return
|
|
|
|
}),
|
2018-08-06 02:44:28 +02:00
|
|
|
/*
|
2018-07-27 17:21:37 +02:00
|
|
|
mvuex.Mutation("startLogListening", func (store *mvuex.Store, state *GlobalState) {
|
2018-08-05 16:04:27 +02:00
|
|
|
state.EventReceiver.StartListening()
|
2018-07-27 17:21:37 +02:00
|
|
|
return
|
|
|
|
}),
|
|
|
|
mvuex.Mutation("stopLogListening", func (store *mvuex.Store, state *GlobalState) {
|
2018-08-05 16:04:27 +02:00
|
|
|
state.EventReceiver.StopListening()
|
2018-07-27 17:21:37 +02:00
|
|
|
return
|
|
|
|
}),
|
2018-08-06 02:44:28 +02:00
|
|
|
*/
|
2018-07-28 04:49:25 +02:00
|
|
|
mvuex.Action(VUEX_ACTION_UPDATE_GADGET_SETTINGS_FROM_DEPLOYED, actionUpdateGadgetSettingsFromDeployed),
|
|
|
|
mvuex.Action(VUEX_ACTION_DEPLOY_CURRENT_GADGET_SETTINGS, actionDeployCurrentGadgetSettings),
|
2018-08-05 16:04:27 +02:00
|
|
|
mvuex.Action(VUEX_ACTION_UPDATE_RUNNING_HID_JOBS, actionUpdateRunningHidJobs),
|
2018-07-27 03:07:23 +02:00
|
|
|
)
|
|
|
|
|
2018-07-28 04:49:25 +02:00
|
|
|
// fetch deployed gadget settings
|
2018-08-05 16:04:27 +02:00
|
|
|
store.Dispatch(VUEX_ACTION_UPDATE_GADGET_SETTINGS_FROM_DEPLOYED)
|
|
|
|
|
|
|
|
// Update already running HID jobs
|
|
|
|
store.Dispatch(VUEX_ACTION_UPDATE_RUNNING_HID_JOBS)
|
2018-07-27 17:21:37 +02:00
|
|
|
|
2018-07-27 03:07:23 +02:00
|
|
|
// propagate Vuex store to global scope to allow injecting it to Vue by setting the "store" option
|
|
|
|
js.Global.Set("store", store)
|
2018-07-27 17:21:37 +02:00
|
|
|
|
2018-08-06 02:44:28 +02:00
|
|
|
/*
|
2018-07-27 17:21:37 +02:00
|
|
|
// Start Event Listening
|
2018-08-05 16:04:27 +02:00
|
|
|
state.EventReceiver.StartListening()
|
2018-08-06 02:44:28 +02:00
|
|
|
*/
|
2018-07-27 17:21:37 +02:00
|
|
|
|
2018-08-05 16:04:27 +02:00
|
|
|
return state
|
2018-07-27 03:07:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 16:04:27 +02:00
|
|
|
func InitGlobalState() GlobalState {
|
|
|
|
return initMVuex()
|
2018-07-27 03:07:23 +02:00
|
|
|
}
|