<commit_before>"use strict";

var util = require('util');
var es = require('event-stream');
var stream      = require('stream');
var _ = require('underscore');
var Q = require('q');
let co = require('co');
let ucoin = require('../../index');
let ucp = require('../lib/ucp');
let constants = require('../lib/constants');
let base58 = require('../lib/base58');
let rawer = require('../lib/rawer');
let crypto = require('../lib/crypto');
let http2raw = require('../lib/streams/parsers/http2raw');
let parsers = require('../lib/streams/parsers/doc');
let bma = require('../lib/streams/bma');
let Identity = require('../lib/entity/identity');
let network = require('../lib/network');
let AbstractController = require('../controllers/abstract');
var Synchroniser = require('../lib/sync');
var multicaster = require('../lib/streams/multicaster');
var logger = require('../lib/logger')('webmin');

module.exports = (dbConf, overConf) => new WebAdmin(dbConf, overConf);

function WebAdmin (dbConf, overConf) {

  // Node instance: this is the object to be managed by the web admin
  let server = this.server = ucoin(dbConf, overConf);
  let bmapi;
  let that = this;

  AbstractController.call(this, server);

  stream.Duplex.call(this, { objectMode: true });

  // Unused, but made mandatory by Duplex interface
  this._read = () => null;
  this._write = () => null;

  let startServicesP, stopServicesP;

  let pluggedConfP = plugForConf();

  let pluggedDALP = co(function *() {
    yield pluggedConfP;

    // Routing documents
    server
    // The router asks for multicasting of documents
      .pipe(server.router())
      // The documents get sent to peers
      .pipe(multicaster(server.conf))
      // The multicaster may answer 'unreachable peer'
      .pipe(server.router());

    return plugForDAL();
  });

  this.summary = () => co(function *() {
    yield pluggedDALP;
    let host = server.conf ? [server.conf.ipv4, server.conf.port].join(':') : '';
    let current = yield server.dal.getCurrentBlockOrNull();
    return {
      "host": host,
      "current": current,
      "pubkey": base58.encode(server.pair.publicKey)
    };
  });

  this.startHTTP = () => co(function *() {
    yield pluggedDALP;
    return bmapi.openConnections();
  });

  this.stopHTTP = () => co(function *() {
    yield pluggedDALP;
    return bmapi.closeConnections();
  });

  this.previewNext = () => co(function *() {
    yield pluggedDALP;
    let block = yield server.doMakeNextBlock();
    block.raw = rawer.getBlock(block);
    return block;
  });

  this.sendConf = (req) => co(function *() {
    yield pluggedConfP;
    let conf = http2raw.conf(req);
    let pair = yield Q.nbind(crypto.getKeyPair, crypto)(conf.idty_entropy, conf.idty_password);
    let publicKey = base58.encode(pair.publicKey);
    let secretKey = pair.secretKey;
    yield server.dal.saveConf({
      routing: true,
      createNext: true,
      cpu: 0.5,
      ipv4: conf.local_ipv4,
      ipv6: conf.local_ipv6,
      port: conf.lport,
      remoteipv4: conf.remote_ipv4,
      remoteipv6: conf.remote_ipv6,
      remoteport: conf.rport,
      upnp: conf.upnp,
      salt: conf.idty_entropy,
      passwd: conf.idty_password,
      pair: {
        pub: publicKey,
        sec: base58.encode(secretKey)
      },
      avgGenTime: conf.avgGenTime,
      blocksRot: conf.blocksRot,
      c: conf.c,
      currency: conf.currency,
      dt: conf.dt,
      dtDiffEval: conf.dtDiffEval,
      medianTimeBlocks: conf.medianTimeBlocks,
      msValidity: conf.msValidity,
      percentRot: conf.percentRot,
      sigDelay: conf.sigDelay,
      sigPeriod: conf.sigPeriod,
      sigQty: conf.sigQty,
      sigStock: conf.sigStock,
      sigValidity: conf.sigValidity,
      sigWindow: conf.sigWindow,
      stepMax: conf.stepMax,
      ud0: conf.ud0,
      xpercent: conf.xpercent
    });
    pluggedConfP = co(function *() {
      yield bmapi.closeConnections();
      yield server.loadConf();
      bmapi = yield bma(server, null, true);
      return bmapi.openConnections();
    });
    yield pluggedConfP;
    let buid = '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855';
    let entity = Identity.statics.fromJSON({
      buid: buid,
      uid: conf.idty_uid,
      issuer: publicKey,
      currency: conf.currency
    });
    let found = yield server.dal.getIdentityByHashOrNull(entity.getTargetHash());
    if (!found) {
      let selfCert = rawer.getOfficialIdentity(entity);
      selfCert += crypto.signSync(selfCert, secretKey) + '\n';
      found = yield that.pushEntity({ body: { identity: selfCert }}, http2raw.identity, parsers.parseIdentity);
    }
    yield server.dal.fillInMembershipsOfIdentity(Q(found));
    if (_.filter(found.memberships, { membership: 'IN'}).length == 0) {
      var block = ucp.format.buid(null);
      var join = rawer.getMembershipWithoutSignature({
        "version": constants.DOCUMENTS_VERSION,
        "currency": conf.currency,
        "issuer": publicKey,
        "block": block,
        "membership": "IN",
        "userid": conf.idty_uid,
        "certts": block
      });
      join += crypto.signSync(join, secretKey) + '\n';
      yield that.pushEntity({ body: { membership: join }}, http2raw.membership, parsers.parseMembership);
    }
    //
    return found;
  });

  this.applyNetworkConf = (req) => co(function *() {
    yield pluggedConfP;
    let conf = http2raw.conf(req);
    yield server.dal.saveConf(_.extend(server.conf, {
      ipv4: conf.local_ipv4,
      ipv6: conf.local_ipv6,
      port: conf.lport,
      remoteipv4: conf.remote_ipv4,
      remoteipv6: conf.remote_ipv6,
      remoteport: conf.rport,
      upnp: conf.upnp
    }));
    pluggedConfP = co(function *() {
      yield bmapi.closeConnections();<commit_msg>Generate peering entry on start http server<commit_after>"use strict";

var util = require('util');
var es = require('event-stream');
var stream      = require('stream');
var _ = require('underscore');
var Q = require('q');
let co = require('co');
let ucoin = require('../../index');
let ucp = require('../lib/ucp');
let constants = require('../lib/constants');
let base58 = require('../lib/base58');
let rawer = require('../lib/rawer');
let crypto = require('../lib/crypto');
let http2raw = require('../lib/streams/parsers/http2raw');
let parsers = require('../lib/streams/parsers/doc');
let bma = require('../lib/streams/bma');
let Identity = require('../lib/entity/identity');
let network = require('../lib/network');
let AbstractController = require('../controllers/abstract');
var Synchroniser = require('../lib/sync');
var multicaster = require('../lib/streams/multicaster');
var logger = require('../lib/logger')('webmin');

module.exports = (dbConf, overConf) => new WebAdmin(dbConf, overConf);

function WebAdmin (dbConf, overConf) {

  // Node instance: this is the object to be managed by the web admin
  let server = this.server = ucoin(dbConf, overConf);
  let bmapi;
  let that = this;

  AbstractController.call(this, server);

  stream.Duplex.call(this, { objectMode: true });

  // Unused, but made mandatory by Duplex interface
  this._read = () => null;
  this._write = () => null;

  let startServicesP, stopServicesP;

  let pluggedConfP = plugForConf();

  let pluggedDALP = co(function *() {
    yield pluggedConfP;

    // Routing documents
    server
    // The router asks for multicasting of documents
      .pipe(server.router())
      // The documents get sent to peers
      .pipe(multicaster(server.conf))
      // The multicaster may answer 'unreachable peer'
      .pipe(server.router());

    return plugForDAL();
  });

  this.summary = () => co(function *() {
    yield pluggedDALP;
    let host = server.conf ? [server.conf.ipv4, server.conf.port].join(':') : '';
    let current = yield server.dal.getCurrentBlockOrNull();
    return {
      "host": host,
      "current": current,
      "pubkey": base58.encode(server.pair.publicKey)
    };
  });

  this.startHTTP = () => co(function *() {
    yield pluggedDALP;
    return bmapi.openConnections();
  });

  this.stopHTTP = () => co(function *() {
    yield pluggedDALP;
    return bmapi.closeConnections();
  });

  this.previewNext = () => co(function *() {
    yield pluggedDALP;
    let block = yield server.doMakeNextBlock();
    block.raw = rawer.getBlock(block);
    return block;
  });

  this.sendConf = (req) => co(function *() {
    yield pluggedConfP;
    let conf = http2raw.conf(req);
    let pair = yield Q.nbind(crypto.getKeyPair, crypto)(conf.idty_entropy, conf.idty_password);
    let publicKey = base58.encode(pair.publicKey);
    let secretKey = pair.secretKey;
    yield server.dal.saveConf({
      routing: true,
      createNext: true,
      cpu: 0.5,
      ipv4: conf.local_ipv4,
      ipv6: conf.local_ipv6,
      port: conf.lport,
      remoteipv4: conf.remote_ipv4,
      remoteipv6: conf.remote_ipv6,
      remoteport: conf.rport,
      upnp: conf.upnp,
      salt: conf.idty_entropy,
      passwd: conf.idty_password,
      pair: {
        pub: publicKey,
        sec: base58.encode(secretKey)
      },
      avgGenTime: conf.avgGenTime,
      blocksRot: conf.blocksRot,
      c: conf.c,
      currency: conf.currency,
      dt: conf.dt,
      dtDiffEval: conf.dtDiffEval,
      medianTimeBlocks: conf.medianTimeBlocks,
      msValidity: conf.msValidity,
      percentRot: conf.percentRot,
      sigDelay: conf.sigDelay,
      sigPeriod: conf.sigPeriod,
      sigQty: conf.sigQty,
      sigStock: conf.sigStock,
      sigValidity: conf.sigValidity,
      sigWindow: conf.sigWindow,
      stepMax: conf.stepMax,
      ud0: conf.ud0,
      xpercent: conf.xpercent
    });
    pluggedConfP = co(function *() {
      yield bmapi.closeConnections();
      yield server.loadConf();
      bmapi = yield bma(server, null, true);
      return bmapi.openConnections();
    });
    yield pluggedConfP;
    let buid = '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855';
    let entity = Identity.statics.fromJSON({
      buid: buid,
      uid: conf.idty_uid,
      issuer: publicKey,
      currency: conf.currency
    });
    let found = yield server.dal.getIdentityByHashOrNull(entity.getTargetHash());
    if (!found) {
      let selfCert = rawer.getOfficialIdentity(entity);
      selfCert += crypto.signSync(selfCert, secretKey) + '\n';
      found = yield that.pushEntity({ body: { identity: selfCert }}, http2raw.identity, parsers.parseIdentity);
    }
    yield server.dal.fillInMembershipsOfIdentity(Q(found));
    if (_.filter(found.memberships, { membership: 'IN'}).length == 0) {
      var block = ucp.format.buid(null);
      var join = rawer.getMembershipWithoutSignature({
        "version": constants.DOCUMENTS_VERSION,
        "currency": conf.currency,
        "issuer": publicKey,
        "block": block,
        "membership": "IN",
        "userid": conf.idty_uid,
        "certts": block
      });
      join += crypto.signSync(join, secretKey) + '\n';
      yield that.pushEntity({ body: { membership: join }}, http2raw.membership, parsers.parseMembership);
      yield server.recomputeSelfPeer();
    }
    //
    return found;
  });

  this.applyNetworkConf = (req) => co(function *() {
    yield pluggedConfP;
    let conf = http2raw.conf(req);
    yield server.dal.saveConf(_.extend(server.conf, {
      ipv4: conf.local_ipv4,
      ipv6: conf.local_ipv6,
      port: conf.lport,
      remoteipv4: conf.remote_ipv4,
      remoteipv6: conf.remote_ipv6,
      remoteport: conf.rport,
      upnp: conf.upnp
    }));
    pluggedConfP = co(function *() {
      yield bmapi.closeConnections();
{'fraction_non_alphanumeric': 0.14408824543260945, 'fraction_numerical': 0.014133057566356428, 'mean_word_length': 3.0155709342560555, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4638466', 'n_tokens_mistral': 3894, 'n_tokens_neox': 3793, 'n_words': 807}