Skip to content

Conversation

@polesye
Copy link
Contributor

@polesye polesye commented Feb 17, 2025

This PR extends api with tap and download endpoints that allows us to use export object functionality (see attached screenshot), conversations and endpoint stat.

Concerns: the tap endpoint returns different response type based on the passed tap type. I haven't found a better solution to properly bind it in emsc except adding nlohmann/json to the project (is it a good approach to add it that way?) and stringifying each tap result. Hence, js shoud iterate over each tap item and use JSON.parse (See https://github.com/DARTSG/wiregasm/blob/50e9163c0ce00b6ffec3340c3427ad2a9041ccfe/src/index.ts#L118).

Screenshot 2025-02-17 at 12 46 00

@polesye
Copy link
Contributor Author

polesye commented Feb 18, 2025

@dehydr8 for some reasons I cannot assign reviewers, so pinging you here.

@dehydr8
Copy link
Member

dehydr8 commented Feb 19, 2025

Hi @polesye,

I have reviewed the code, thank you for taking the time to contribute it.

There is a way to make the different types in a vector work in emscripten. If you want to give it a go, we can leverage polymorphism. This is what the changes might look like:

// base struct
struct TapValue {
  string tap;
  string type;
  string proto;
  virtual ~TapValue() = default;
}

// derived structs
struct TapConvResponse: TapValue {
  bool geoip;
  vector<Conversation> convs;
  vector<Host> hosts;
};

struct ExportObjectTap: TapValue {
  vector<ExportObject> objects;
};

// response struct
struct TapResponse {
  vector<shared_ptr<TapValue>> taps;
  string error;
};


// pushing to the vector
buf.taps.push_back(make_shared<TapConvResponse>(conv))
buf.taps.push_back(make_shared<ExportObjectTap>(eo))


// bindings
EMSCRIPTEN_BINDINGS(TapValue) {
    class_<TapValue>("TapValue")
        .smart_ptr<shared_ptr<TapValue>>("TapValue")
        .property("tap", &TapValue::tap)
        .property("type", &TapValue::type)
        .property("proto", &TapValue::proto);
}

EMSCRIPTEN_BINDINGS(TapConvResponse) {
    class_<TapConvResponse, base<TapValue>>("TapConvResponse")
        .property("geoip", &TapConvResponse::geoip)
        .property("convs", &TapConvResponse::convs)
        .property("hosts", &TapConvResponse::hosts);
}

EMSCRIPTEN_BINDINGS(ExportObjectTap) {
    class_<ExportObjectTap, base<TapValue>>("ExportObjectTap")
        .property("objects", &ExportObjectTap::objects);
}

EMSCRIPTEN_BINDINGS(TapResponse) {
    class_<TapResponse>("TapResponse")
        .property("taps", &TapResponse::taps)
        .property("error", &TapResponse::error);

    register_vector<shared_ptr<TapValue>>("VectorTapValue");
}
    for (let i = 0; i < response.taps.size(); i++) {
        let tap = response.taps.get(i);

        console.log("Tap:", tap.tap, "Type:", tap.type, "Proto:", tap.proto);

        if (tap instanceof Module.TapConvResponse) {
            console.log("GeoIP:", tap.geoip);
            console.log("Convs:", tap.convs);
            console.log("Hosts:", tap.hosts);
        } else if (tap instanceof Module.ExportObjectTap) {
            console.log("Objects:", tap.objects);
        }
    }

This would ensure proper RTTI in JS. We would be able to check instanceof Module.TapConvResponse, etc.

Let me know if this is something you would like to do.

@polesye
Copy link
Contributor Author

polesye commented Feb 19, 2025

@dehydr8 let me try it.

@polesye
Copy link
Contributor Author

polesye commented Feb 19, 2025

@dehydr8 your solution seems to be working well. The only inconvenience is that we have to manully destroy shared_ptr instances (See e5dff43#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R156). It raises warnign message otherwise.
Screenshot 2025-02-19 at 15 23 47

@dehydr8
Copy link
Member

dehydr8 commented Feb 19, 2025

@polesye this looks really good!

It raises warnign message otherwise.

This is expected. Since we explicitly pass on the pointers, we're responsible for memory management.

Let me know if you're happy with the changes and I'll merge them.

@polesye
Copy link
Contributor Author

polesye commented Feb 20, 2025

@dehydr8 yes, this solution is much better. Thanks.

@dehydr8 dehydr8 merged commit 671d668 into good-tools:master Feb 20, 2025
1 check passed
@github-actions
Copy link

🎉 This PR is included in version 1.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants