@@ -2,8 +2,11 @@ var path = require('path');
22var fs = require ( 'fs' ) ;
33var http = require ( 'http' ) ;
44var url = require ( 'url' ) ;
5+ var Buffer = require ( 'safe-buffer' ) . Buffer ;
56var util = require ( './util' ) ;
7+ var importModule = require ( './import' ) ;
68var pkg = require ( '../package.json' ) ;
9+ var getHomedir = require ( '../lib/util/common' ) . getHomedir ;
710
811var isRunning = util . isRunning ;
912var error = util . error ;
@@ -14,24 +17,28 @@ var MAX_RULES_LEN = 1024 * 256;
1417var DEFAULT_OPTIONS = { host : '127.0.0.1' , port : 8899 } ;
1518var options ;
1619
17- function showStartWhistleTips ( storage ) {
18- error ( 'No running whistle, execute `w2 start' + ( storage ? ' -S ' + storage : '' )
19- + '` to start whistle on the cli.' ) ;
20+ function showStartWhistleTips ( storage , isClient ) {
21+ if ( isClient ) {
22+ error ( 'No running whistle client, please install and start the latest whistle client: https://github.com/avwo/whistle-client' ) ;
23+ } else {
24+ error ( 'No running whistle, execute `w2 start' + ( storage ? ' -S ' + storage : '' ) + '` to start whistle on the cli.' ) ;
25+ }
2026}
2127
2228function handleRules ( filepath , callback , port ) {
23- var getRules = require ( filepath ) ;
24- if ( typeof getRules !== 'function' ) {
25- return callback ( getRules ) ;
26- }
27- var opts = {
28- port : port ,
29- existsPlugin : existsPlugin
30- } ;
31- if ( options && options . host ) {
32- opts . host = options . host ;
33- }
34- getRules ( callback , opts ) ;
29+ importModule ( filepath , function ( getRules ) {
30+ if ( typeof getRules !== 'function' ) {
31+ return callback ( getRules ) ;
32+ }
33+ var opts = {
34+ port : port ,
35+ existsPlugin : existsPlugin
36+ } ;
37+ if ( options && options . host ) {
38+ opts . host = options . host ;
39+ }
40+ getRules ( callback , opts ) ;
41+ } ) ;
3542}
3643
3744function getString ( str ) {
@@ -75,10 +82,13 @@ function request(body, callback) {
7582 reqOptions . headers = {
7683 'content-type' : 'application/x-www-form-urlencoded'
7784 } ;
85+ if ( options . specialAuth ) {
86+ reqOptions . headers [ 'x-whistle-special-auth' ] = options . specialAuth ;
87+ }
7888 reqOptions . method = 'POST' ;
7989 if ( options . username || options . password ) {
8090 var auth = [ options . username || '' , options . password || '' ] . join ( ':' ) ;
81- reqOptions . headers . authorization = 'Basic ' + new Buffer ( auth ) . toString ( 'base64' ) ;
91+ reqOptions . headers . authorization = 'Basic ' + new Buffer . from ( auth ) . toString ( 'base64' ) ;
8292 }
8393 }
8494 var req = http . request ( reqOptions , function ( res ) {
@@ -93,10 +103,13 @@ function request(body, callback) {
93103 req . end ( body ) ;
94104}
95105
96- function checkDefault ( running , storage , callback ) {
106+ function checkDefault ( running , storage , isClient , callback ) {
97107 if ( running ) {
98108 return callback ( ) ;
99109 }
110+ if ( isClient ) {
111+ return callback ( true ) ;
112+ }
100113 var execCallback = function ( err ) {
101114 callback && callback ( err ) ;
102115 callback = null ;
@@ -114,20 +127,42 @@ function checkDefault(running, storage, callback) {
114127 req . end ( ) ;
115128}
116129
117- module . exports = function ( filepath , storage , force ) {
118- storage = storage || '' ;
119- var dir = encodeURIComponent ( storage ) ;
120- var config = readConfig ( dir ) || '' ;
130+ function readClientConfig ( ) {
131+ var procPath = path . join ( getHomedir ( ) , '.whistle_client.pid' ) ;
132+ try {
133+ var info = fs . readFileSync ( procPath , { encoding : 'utf-8' } ) . split ( ',' ) ;
134+ if ( info . length === 4 ) {
135+ return {
136+ pid : info [ 0 ] ,
137+ options : {
138+ host : info [ 1 ] ,
139+ port : info [ 2 ] ,
140+ specialAuth : info [ 3 ]
141+ }
142+ } ;
143+ }
144+ } catch ( e ) { }
145+ }
146+
147+ module . exports = function ( filepath , storage , force , isClient ) {
148+ var config ;
149+ var dir = '' ;
150+ if ( isClient ) {
151+ storage = '' ;
152+ config = readClientConfig ( ) || '' ;
153+ } else {
154+ storage = storage || '' ;
155+ dir = encodeURIComponent ( storage ) ;
156+ config = readConfig ( dir ) || '' ;
157+ if ( config . options ) {
158+ delete config . options . specialAuth ;
159+ }
160+ }
121161 options = config . options || '' ;
122- var pid = options && config . pid ;
123- var addon = options && options . addon ;
124- var conf = require ( '../lib/config' ) ;
125- conf . addon = addon && typeof addon === 'string' ? addon . split ( / [ | , ] / ) : null ;
126- conf . noGlobalPlugins = options && options . noGlobalPlugins ;
127- isRunning ( pid , function ( running ) {
128- checkDefault ( running , dir , function ( err , port ) {
162+ isRunning ( options && config . pid , function ( running ) {
163+ checkDefault ( running , dir , isClient , function ( err , port ) {
129164 if ( err ) {
130- return showStartWhistleTips ( storage ) ;
165+ return showStartWhistleTips ( storage , isClient ) ;
131166 }
132167 filepath = path . resolve ( filepath || '.whistle.js' ) ;
133168 if ( port ) {
@@ -158,7 +193,7 @@ module.exports = function(filepath, storage, force) {
158193 'groupName=' + encodeURIComponent ( groupName . trim ( ) )
159194 ] . join ( '&' ) ;
160195 request ( body , function ( ) {
161- info ( 'Setting whistle (' + ( options . host || '127.0.0.1' ) + ':' + port + ') rules successful.' ) ;
196+ info ( 'Setting whistle' + ( isClient ? ' client' : '' ) + ' (' + ( options . host || '127.0.0.1' ) + ':' + port + ') rules successful.' ) ;
162197 } ) ;
163198 } ;
164199 if ( force ) {
0 commit comments