8
8
var url = require ( 'url' ) ;
9
9
var request = require ( 'request' ) ;
10
10
11
+ var yargs = require ( 'yargs' ) . options ( {
12
+ 'port' : {
13
+ 'default' : 8080 ,
14
+ 'description' : 'Port to listen on.'
15
+ } ,
16
+ 'public' : {
17
+ 'type' : 'boolean' ,
18
+ 'description' : 'Run a public server that listens on all interfaces.'
19
+ } ,
20
+ 'upstream-proxy' : {
21
+ 'description' : 'A standard proxy server that will be used to retrieve data. Specify a URL including port, e.g. "http://proxy:8000".'
22
+ } ,
23
+ 'bypass-upstream-proxy-hosts' : {
24
+ 'description' : 'A comma separated list of hosts that will bypass the specified upstream_proxy, e.g. "lanhost1,lanhost2"'
25
+ } ,
26
+ 'help' : {
27
+ 'alias' : 'h' ,
28
+ 'type' : 'boolean' ,
29
+ 'description' : 'Show this help.'
30
+ }
31
+ } ) ;
32
+ var argv = yargs . argv ;
33
+
34
+ if ( argv . help ) {
35
+ return yargs . showHelp ( ) ;
36
+ }
37
+
11
38
// eventually this mime type configuration will need to change
12
39
// https://github.com/visionmedia/send/commit/d2cb54658ce65948b0ed6e5fb5de69d022bef941
13
40
var mime = express . static . mime ;
47
74
return result ;
48
75
}
49
76
77
+ var upstreamProxy = argv [ 'upstream-proxy' ] ;
78
+ var bypassUpstreamProxyHosts = { } ;
79
+ if ( argv [ 'bypass-upstream-proxy-hosts' ] ) {
80
+ argv [ 'bypass-upstream-proxy-hosts' ] . split ( ',' ) . forEach ( function ( host ) {
81
+ bypassUpstreamProxyHosts [ host . toLowerCase ( ) ] = true ;
82
+ } ) ;
83
+ }
84
+
50
85
app . get ( '/proxy/*' , function ( req , res , next ) {
51
86
// look for request like http://localhost:8080/proxy/http://example.com/file?query=1
52
87
var remoteUrl = getRemoteUrlFromParam ( req ) ;
66
101
remoteUrl . protocol = 'http:' ;
67
102
}
68
103
69
- remoteUrl = url . format ( remoteUrl ) ;
104
+ var proxy ;
105
+ if ( upstreamProxy && ! ( remoteUrl . host in bypassUpstreamProxyHosts ) ) {
106
+ proxy = upstreamProxy ;
107
+ }
70
108
71
109
// encoding : null means "body" passed to the callback will be raw bytes
72
110
73
111
request . get ( {
74
- url : remoteUrl ,
112
+ url : url . format ( remoteUrl ) ,
75
113
headers : filterHeaders ( req , req . headers ) ,
76
- encoding : null
114
+ encoding : null ,
115
+ proxy : proxy
77
116
} , function ( error , response , body ) {
78
117
var code = 500 ;
79
118
86
125
} ) ;
87
126
} ) ;
88
127
89
- app . listen ( 8080 ) ;
128
+ app . listen ( argv . port , argv . public ? undefined : 'localhost' ) ;
90
129
91
- console . log ( 'Cesium development server running. Connect to http://localhost:8080.' ) ;
130
+ console . log ( 'Cesium development server running. Connect to http://localhost:%d.' , argv . port ) ;
92
131
} ) ( ) ;
0 commit comments