UNPKG

3.34 kBJavaScriptView Raw
1#!/usr/bin/env node
2/**
3 * Created by tanxiangyuan on 2016/6/26.
4 */
5var program = require('commander'),
6 color = require('colorful'),
7 fs = require("fs"),
8 path = require("path"),
9 mock = require("./mock"),
10 util = require('./lib/util');
11
12program
13 .option('-u, --host [value]', 'hostname for https proxy, localhost for default')
14 .option('-t, --type [value]', 'http|https, http for default')
15 .option('-p, --port [value]', 'proxy port, 8001 for default')
16 .option('-w, --web [value]', 'web GUI port, 8002 for default')
17 .option('-f, --file [value]', 'save request data to a specified file, will use in-memory db if not specified')
18 .option('-r, --rule [value]', 'path for rule file,')
19 // .option('-g, --root [value]', 'generate root CA')
20 .option('-l, --throttle [value]', 'throttle speed in kb/s (kbyte / sec)')
21 .option('-i, --intercept', 'intercept(decrypt) https requests when root CA exists')
22 .option('-s, --silent', 'do not print anything into terminal')
23 // .option('-c, --clear', 'clear all the tmp certificates')
24 .option('-o, --global', 'set as global proxy for system')
25 .parse(process.argv);
26
27console.log(color.red('here is any mock!'));
28
29var ruleModule;
30
31if (program.rule) {
32 var ruleFilePath = path.resolve(process.cwd(), program.rule);
33 try {
34 if (fs.existsSync(ruleFilePath)) {
35 ruleModule = require(ruleFilePath);
36 console.log(color.green("rule file loaded :" + ruleFilePath));
37 } else {
38 var logText = color.red("can not find rule file at " + ruleFilePath);
39 console(logText);
40 }
41 } catch (e) {
42 console(color.red("failed to load rule file :" + e.toString()));
43 }
44} else {
45 //a feature for donghua.yan
46 //read rule file from a specific position
47 (function () {
48 try {
49 var anymockHome = path.join(util.getAnyMockHome());
50 if (fs.existsSync(path.join(anymockHome, "rule_default.js"))) {
51 ruleModule = require(path.join(anymockHome, "rule_default"));
52 }
53 if (fs.existsSync(path.join(process.cwd(), 'rule.js'))) {
54 ruleModule = require(path.join(process.cwd(), 'rule'));
55 }
56 console.info('use system default rule.');
57 } catch (e) {
58 console.info(e);
59 }
60 })();
61}
62
63
64
65new mock.proxyServer({
66 type: "http" || program.type,
67 port: 8001 || program.port,
68 hostname: "localhost" || program.host,
69 rule: ruleModule,
70 dbFile: null || program.file, // optional, save request data to a specified file, will use in-memory db if not specified
71 webPort: 8002 || program.web, // optional, port for web interface
72 socketPort: 8003, // optional, internal port for web socket, replace this when it is conflict with your own service
73 throttle: 1000 || program.throttle, // optional, speed limit in kb/s
74 disableWebInterface: false, //optional, set it when you don't want to use the web interface
75 setAsGlobalProxy: false || program.global, //set anyproxy as your system proxy
76 silent: false || program.silent, //optional, do not print anything into terminal. do not set it when you are still debugging.
77 interceptHttps: program.intercept,
78});
\No newline at end of file