Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

blockr

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

blockr - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

blockr.json

8

.eslintrc.js
module.exports = {
extends: ['airbnb', 'prettier'],
parser: '@typescript-eslint/parser',
extends: [
'airbnb',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
rules: {

@@ -4,0 +10,0 @@ 'no-console': 'off',

158

index.js
#! /usr/bin/env node
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const meow = require('meow');
const cli = meow(
`
Usage
$ blocko
$ blocko unblock
$ blocko unblock <site>
Options
--hosts-file, -h Path to the hosts file
`,
{
flags: {
'hosts-file': {
type: 'string',
alias: 'h',
default: '/etc/hosts',
},
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var fs = __importStar(require("fs"));
var os = __importStar(require("os"));
var path = __importStar(require("path"));
var lodash_1 = __importDefault(require("lodash"));
var meow_1 = __importDefault(require("meow"));
var execa_1 = __importDefault(require("execa"));
var chalk_1 = __importDefault(require("chalk"));
var cli = meow_1["default"]("\n\tUsage\n $ blockr\n $ blockr unblock\n $ blockr unblock <site>\n\n\tOptions\n --hosts-file, -h Path to the hosts file\n --config-file, -c Path to the config file\n --password, -p Sudo password\n", {
flags: {
'hosts-file': {
type: 'string',
alias: 'h',
"default": '/etc/hosts'
},
},
);
const BEGIN_MARKER = '# BEGIN BLOCKO';
const END_MARKER = '# END BLOCKO';
const makeEntry = host => `0.0.0.0 ${host}\n:: ${host}\n`;
'config-file': {
type: 'string',
alias: 'c',
"default": path.join(os.homedir(), 'blockr.json')
},
password: {
type: 'string',
alias: 'p',
"default": null
}
}
});
var BEGIN_MARKER = '# BEGIN BLOCKR';
var END_MARKER = '# END BLOCKR';
var makeEntry = function (host) { return "0.0.0.0 " + host + "\n:: " + host + "\n"; };
function generateBlockString(hosts) {
let str = `${BEGIN_MARKER}\n`;
hosts.forEach(host => {
var str = BEGIN_MARKER + "\n";
hosts.forEach(function (host) {
str += makeEntry(host);
str += makeEntry(`www.${host}`);
str += makeEntry("www." + host);
});
str += END_MARKER;
return str;
}
function tryWrite(hosts) {
try {
fs.writeFileSync(cli.flags.hostsFile, hosts);
} catch (error) {
if (error.code === 'EACCES') {
console.log('Please re-run with sudo to write to the hosts file');
function tryWrite(hostsString) {
if (cli.flags.password) {
fs.writeFileSync('/tmp/hosts', hostsString);
execa_1["default"].sync("./runner.sh", [cli.flags.password, cli.flags.hostsFile]);
}
else {
try {
fs.writeFileSync(cli.flags.hostsFile, hostsString);
}
process.exit(1);
catch (error) {
if (error.code === 'EACCES') {
console.log('Please re-run with sudo or use the `--password` option to write to the hosts file');
}
process.exit(1);
}
}
}
function updateBlock(hosts) {
function updateBlock(hostsMap) {
if (!fs.existsSync(cli.flags.hostsFile)) {
fs.closeSync(fs.openSync(cli.flags.hostsFile, 'w'));
}
const currentHosts = fs.readFileSync(cli.flags.hostsFile, 'utf-8');
const blockString = generateBlockString(_.values(hosts));
const startLocation = currentHosts.indexOf(BEGIN_MARKER);
const endLocation = currentHosts.indexOf(END_MARKER);
let newHosts;
var currentHosts = fs.readFileSync(cli.flags.hostsFile, 'utf-8');
var blockString = generateBlockString(lodash_1["default"].values(hostsMap));
var startLocation = currentHosts.indexOf(BEGIN_MARKER);
var endLocation = currentHosts.indexOf(END_MARKER);
var newHosts;
if (startLocation > -1 && endLocation > -1) {
newHosts =
currentHosts.substr(0, startLocation) +
blockString +
currentHosts.substr(endLocation + END_MARKER.length);
} else {
newHosts = `${currentHosts}\n${blockString}`;
blockString +
currentHosts.substr(endLocation + END_MARKER.length);
}
else {
newHosts = currentHosts + "\n" + blockString;
}
tryWrite(newHosts);
}
const command = cli.input[0];
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'blocko.json'), 'utf-8'));
function loadConfig() {
try {
return JSON.parse(fs.readFileSync(cli.flags.configFile, 'utf-8'));
}
catch (error) {
console.error("No config file found at " + cli.flags.configFile);
process.exit(1);
}
return null;
}
var command = cli.input[0];
var config = loadConfig();
if (!command || command === 'block') {
updateBlock(config.hosts);
console.log('✅ All sites blocked');
}
if (command === 'unblock') {
if (cli.input[1]) {
updateBlock(_.omit(config.hosts, cli.input[1]));
} else {
var site = cli.input[1];
if (site) {
if (config.hosts[site]) {
updateBlock(lodash_1["default"].omit(config.hosts, site));
console.log("\u2705 '" + chalk_1["default"].yellow(site) + "' unblocked");
}
else {
console.error("\u274C No site called '" + chalk_1["default"].yellow(site) + "'");
}
}
else {
updateBlock({});
console.log('✅ All sites unblocked');
}
}
{
"name": "blockr",
"version": "1.0.2",
"version": "1.0.3",
"main": "index.js",

@@ -8,5 +8,9 @@ "license": "MIT",

"scripts": {
"lint": "eslint ."
"lint": "eslint index.ts",
"build": "tsc --esModuleInterop index.ts",
"release": "yarn lint && yarn build && npm publish"
},
"dependencies": {
"chalk": "^2.4.2",
"execa": "^2.0.3",
"lodash": "^4.17.14",

@@ -16,2 +20,5 @@ "meow": "^5.0.0"

"devDependencies": {
"@types/node": "^12.6.2",
"@typescript-eslint/eslint-plugin": "^1.12.0",
"@typescript-eslint/parser": "^1.12.0",
"eslint": "^6.0.1",

@@ -22,4 +29,5 @@ "eslint-config-airbnb": "^17.1.1",

"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.14.2"
"eslint-plugin-react": "^7.14.2",
"typescript": "^3.5.3"
}
}

@@ -1,3 +0,9 @@

# Blocko
# Blockr
Block distracting sites from the command line
## Install
```
yarn global add blockr
```

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc