Giter VIP home page Giter VIP logo

wshook's Introduction

wsHook

Easily intercept and modify WebSocket requests and message events.

ToDo

Figure out if we still need immutable MessageEvent

Usage

1. Download and include wsHook.js in your WebSocket client

<script src='wsHook.js'></script>

2. Define the before and after hooks

Define your custom before and after hooks on the globally exposed wsHook object.

wsHook.before = function(data, url, wsObject) {
    console.log("Sending message to " + url + " : " + data);
}

// Make sure your program calls `wsClient.onmessage` event handler somewhere.
wsHook.after = function(messageEvent, url, wsObject) {
    console.log("Received message from " + url + " : " + messageEvent.data);
    return messageEvent;
}

// if you do not want to propagate the MessageEvent further down, just return null
wsHook.after = function(messageEvent, url, wsObject) {
 console.log("Received message from " + url + " : " + messageEvent.data);
 // This example can ping-pong forever, so maybe use some conditions
 wsObject.send("Intercepted and sent again")
 return null;
}

3. Let your program play with WebSockets

var wsClient = new WebSocket("wss://echo.websocket.org");

wsClient.onopen = function() {
    wsClient.send("Echo this");
}

wsClient.onmessage = function(e){
  console.log(e);
}

API

wsHook.before - function(data, url, wsObject):

Invoked just before calling the actual WebSocket's send() method.

This method must return data which can be modified as well.

wsHook.after - function(event, url, wsObject):

Invoked just after receiving the MessageEvent from the WebSocket server and before calling the WebSocket's onmessage Event Handler.

This method must return event whose properties can be modified as well. You might be interested in modiying, event.data or event.origin usually.

The wsObject refers to the corresponding WebSocket object used. You can use this to send a message to the server. This allows one to fully hijack the WebSocket connection programatically.

If you do not want the user's original onmessage event handler to be called, just return null.

Overview

Example

// Load wsHook.js
// Define the 'before' and 'after' hooks as you wish.

wsHook.before = function(data, url, wsObject){
  data += "_modified";
  console.log("Modifying data to " + data);
  return data;
}

var wsClient = new WebSocket("wss://echo.websocket.org");
wsClient.onopen = function() {
  wsClient.send("Echo this");
}
wsClient.onmessage = function(e){
  console.log(e);
}

Used by

  • Hookish: Hooks in to interesting functions and helps reverse the web app faster.

TODO

  • Test cases for common WebSocket libraries.

License

The MIT License (MIT)

Copyright (c) 2015 Ahamed Nafeez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

wshook's People

Contributors

freehuntx avatar skepticfx avatar zozman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wshook's Issues

WsHook not detecting websocket on skribble.io

Neither Hookish nor WsHook seem to detect WebSockets on skribble.io inside of a userscript

// ==UserScript==
// @name     Skribbl Testing
// @version  1
// @grant    none
// @include https://skribbl.io/*
// @require https://cdn.rawgit.com/skepticfx/wshook/master/wsHook.js
// @run-at document-start
// @grant none
// ==/UserScript==

wsHook.after = function(messageEvent, url, wsObject) {
    console.log("Received message from " + url + " : " + messageEvent.data);
    return messageEvent;
}

I assume they are capturing the WebSocket object somehow, but I have not been able to figure out where at this point.
I will conduct further research on this, but would be very happy about your help, as you probably have more experience.

send data from after()

is there any way to send data to websocket from after method??
like sending x data when i receive y data.

wsHook.before/after not triggering

I have this code to inject the script into the page once loaded. I can access wsHook from the console, however before/after never trigger.
Am I missing a step or is this not a possible implementation?

window.onload = () => {
    // make websocket hook
    let websockethook = document.createElement('script');
    websockethook.setAttribute('src', 'https://cdn.rawgit.com/skepticfx/wshook/master/wsHook.js');
    websockethook.setAttribute('type', 'text/javascript');

    document.body.appendChild(websockethook);

    wsHook.before = function (data, url) {
        console.log("Sending message to " + url + " : " + data);
        console.log(1)
        return data;
    }
    
    // Make sure your program calls `wsClient.onmessage` event handler somewhere.
    wsHook.after = function (messageEvent, url, wsObject) {
        console.log("Received message from " + url + " : " + messageEvent.data);
        return messageEvent;
    }
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.