Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide example for telehash-http #22

Open
Kagami opened this issue Nov 19, 2014 · 5 comments
Open

Provide example for telehash-http #22

Kagami opened this issue Nov 19, 2014 · 5 comments

Comments

@Kagami
Copy link

Kagami commented Nov 19, 2014

Could you please provide example of using telehash-http transport to create a link between two meshes?
API is a bit tricky, it's not very clear when we should bind the http-server and how to use http-client to connect to it.
Thanks.

Over usage examples (udp, tcp) would be also super-helpful.

@quartzjer
Copy link
Member

Yep, some work def needs to be done here, plan to circle back to it by this weekend and add webrtc back in.

If it helps, the transports in telehash-js should be pretty automatic, there's little an app will have to do beyond setting some preferences, I'll try to demo/document that much better.

@Kagami
Copy link
Author

Kagami commented Nov 19, 2014

I almost got it working, http-server listenning on the port, http-client making the connection, but after that I can't setup any channel and .status callback isn't firing.

Code:

var th = require("telehash");
delete th.extensions.udp4;
delete th.extensions.tcp4;
th.log({debug: console.log});

var idA, idB;
var meshA, meshB;
var linkAB;

function initEndpoints() {
  th.generate(function(err, id) {
    idA = id;
    th.generate(function(err, id) {
      idB = id;
      initMesh();
    });
  });
}

function initMesh() {
  th.mesh({id: idA}, function(err, mesh) {
    meshA = mesh;
    th.mesh({
      id: idB,
      http: {host: "127.0.0.1", port: 12345}
    }, function(err, mesh) {
      meshB = mesh;
      link();
    });
  });
}

function link() {
  linkAB = meshA.link({
    keys: idB.keys,
    paths: [{type: "http", url: "http://127.0.0.1:12345/"}]
  });
  linkAB.status(function(err) {
    console.log("CONNECTED");
  });
}

initEndpoints();
$ node 1.js
generating secrets
generated new id 2dvgbyhemt3t7nahuv2wbbhll3s5cdbs26hynmjeureit4tasmna
generating secrets
generated new id 25w4j7uzpkpflkky7zsapu7x25jmmbvdicnx6wbvuddm42n5bsqa
created new mesh 2dvgbyhemt3t7nahuv2wbbhll3s5cdbs26hynmjeureit4tasmna
extending mesh with peer
extending mesh with path
extending mesh with stream
extending mesh with http
extending mesh with http-client
extending mesh with http-server
created new mesh 25w4j7uzpkpflkky7zsapu7x25jmmbvdicnx6wbvuddm42n5bsqa
extending mesh with peer
extending mesh with path
extending mesh with stream
extending mesh with http
extending mesh with http-client
extending mesh with http-server
extending link with path
extending link with stream
adding exchange 25w4j7uzpkpflkky7zsapu7x25jmmbvdicnx6wbvuddm42n5bsqa 8e88fe18dcdcaab373a6789e022632d9
addPath { type: 'http', url: 'http://127.0.0.1:12345/' }
new http connection 127.0.0.1 node-XMLHttpRequest
connected http://127.0.0.1:12345/
25w4j7uzpkpflkky7zsapu7x25jmmbvdicnx6wbvuddm42n5bsqa adding new pipe { type: 'http', url: 'http://127.0.0.1:12345/' }
handshake generated 1416423456
25w4j7uz incoming packet 72 http-server
inner { at: 1416423456 } <Buffer 02 38 15 ea 5b ef f8 49 ec e1 ff 48 61 fc f8 fa c4 19 af 80 98>
untrusted hashname { paths: [],
  hashname: '2dvgbyhemt3t7nahuv2wbbhll3s5cdbs26hynmjeureit4tasmna',
  csid: '1a',
  key: <Buffer 02 38 15 ea 5b ef f8 49 ec e1 ff 48 61 fc f8 fa c4 19 af 80 98> }
failed to create exchange from handshake <Buffer 00 11 7b 22 61 74 22 3a 31 34 31 36 34 32 33 34 35 36 7d 02 38 15 ea 5b ef f8 49 ec e1 ff 48 61 fc f8 fa c4 19 af 80 98>
^C

What am I doing wrong? Example scripts seems to be doing the same.

@quartzjer
Copy link
Member

The error about untrusted hashname is the important part, handshakes will only succeed to hashnames that are trusted, you'll need a linkBA = meshA.link(...) counterpart and then it should succeed :)

@Kagami
Copy link
Author

Kagami commented Nov 19, 2014

Thanks! Get it working. Should it be mentioned in the docs? I was confused by the title «Authorizing links (optional)» and thought that there is no any authorization by default.

Btw, how this example is working? It generates the new endpoint and connects to the one from stdin. How the other side authorize incoming link? Does it use the second argument of .link()?

@quartzjer
Copy link
Member

The other side is here: https://github.com/telehash/telehash-c/blob/master/test/net_link.c#L30 which in JS would be the same as this: https://github.com/telehash/telehash-js/blob/master/bin/router.js#L14

The first step is discovery, recognizing a hashname and trying to establish a link and send whatever is needed to do that, the second step is accepting/denying that link request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants