CodeboxIDE / codebox

Open source cloud & desktop IDE
https://www.codebox.io
Apache License 2.0
4.12k stars 588 forks source link

Transparent Proxy Support #334

Closed ttahmouch closed 10 years ago

ttahmouch commented 10 years ago

Hey guys,

Your product rocks. Keep up the great work!

I'm trying to host my own Codebox instances in Docker containers behind a transparent routing proxy, and I'm having trouble getting the application to render completely after websocket connection upgrades happen.

The socket streams are opaque to the proxy.

I don't think this is a problem with Codebox itself, but I'd like to know your thoughts. Any help would be greatly appreciated.

Thanks!

Here's a source snippet of the proxy:

http
        .createServer()
        .on('request', function (req, res) {
            /**
             * Host = "Host" ":" host [ ":" port ] ; Section 3.2.2
             *
             * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
             */
            var headers = req.headers,
                host = headers['host'],
                hasTarget = proxyTable.hasTarget(host);

            if (debug) {
                console.log('------------------------------');
                console.log('Host:');
                console.log(host);
                console.log('Has Target?');
                console.log(hasTarget);
                console.log('------------------------------');
            }

            if (hasTarget) {
                var targetUrl = proxyTable.getTarget(host),
                    target = uri.parse(targetUrl),
                    request =
                        http
                            .request({
                                host: target.hostname,
                                port: target.port,
                                method: req.method,
                                path: req.url,
                                headers: req.headers
                            }, function (response) {
                                switch (response.headers['content-encoding']) {
                                    case 'gzip':
                                    case 'deflate':
                                        response.pipe(zlib.createUnzip()).pipe(res);
                                        break;
                                    default:
                                        response.pipe(res);
                                        break;
                                }
                            })
                            .on('error', function (e) {
                                if (debug) {
                                    console.log('------------------------------');
                                    console.log('Error:');
                                    console.error(e);
                                    console.log('------------------------------');
                                }
                                request.end();
                                request.destroy();
                                res.statusCode = 504;
                                res.end();
                            });
                req.pipe(request);
                if (debug) {
                    console.log('------------------------------');
                    console.log('Target:');
                    console.log(targetUrl);
                    console.log('------------------------------');
                }
            } else {
                res.statusCode = 400;
                res.end();
            }
        })
        .on('upgrade', function (req, clientSocket, head) {
            var headers = req.headers,
                host = headers['host'],
                badRequest = 'HTTP/1.1 400 Bad Request',
                gatewayTimeout = '504 Gateway Timeout',
                proxyAgent = 'Proxy-agent: Example-Proxy/1.0',
                hasTarget = proxyTable.hasTarget(host);

            if (debug) {
                console.log('------------------------------');
                console.log('Host:');
                console.log(host);
                console.log('Has Target?');
                console.log(hasTarget);
                console.log('------------------------------');
            }

            if (hasTarget) {
                var targetUrl = proxyTable.getTarget(host),
                    target = uri.parse(targetUrl),
                    serverSocket =
                        net
                            .connect(target.port, target.hostname, function () {
                                serverSocket.write(head);
                                serverSocket.pipe(clientSocket);
                                clientSocket.pipe(serverSocket);
                            })
                            .on('error', function (e) {
                                if (debug) {
                                    console.log('------------------------------');
                                    console.log('Error:');
                                    console.error(e);
                                    console.log('------------------------------');
                                }
                                clientSocket.write(gatewayTimeout + '\r\n' + proxyAgent + '\r\n\r\n');
                                clientSocket.end();
                                clientSocket.destroy();
                                serverSocket.end();
                                serverSocket.destroy();
                            });
                if (debug) {
                    console.log('------------------------------');
                    console.log('Target:');
                    console.log(targetUrl);
                    console.log('------------------------------');
                }
            } else {
                clientSocket.write(badRequest + '\r\n' + proxyAgent + '\r\n\r\n');
                clientSocket.end();
                clientSocket.destroy();
            }
        })

This is how it renders: screen shot 2014-04-08 at 2 18 43 pm

ttahmouch commented 10 years ago

Looks like Socket.IO is falling back gracefully when websockets aren't available based on my logging. Example:

GET /socket.io/1/xhr-polling/7OGLhIQDmwm4U4a9uvTn?t=1397003483042 1.1

However, it's still not rendering everything. I have no clue why. Perhaps I'm not starting the application properly. Command:

/usr/bin/codebox run /home/git/app --port 80
AaronO commented 10 years ago

@ttahmouch It seems like Codebox has a problem loading your addons. What's your console output ?

ttahmouch commented 10 years ago

I'm running it using supervisord. I hope this is the right output:

root@b2d6cb55a90d:/home/git/app# cat /var/log/supervisor/codebox.log                                                                        
[log][web] disable auth for /static                                                                                                         
[log][project] project is ready                                                                                                             
[log][settings] Settings load for C775E7D8                                                                                                  
[log][rpc] register service /users                                                                                                          
[log][rpc] add method list to /users                                                                                                        
[log][rpc] register service /box                                                                                                            
[log][rpc] add method changes to /box                                                                                                       
[log][web] disable auth for /rpc/box/changes                                                                                                
[log][rpc] add method ping to /box                                                                                                          
[log][web] disable auth for /rpc/box/ping                                                                                                   
[log][rpc] add method status to /box                                                                                                        
[log][web] disable auth for /rpc/box/status                                                                                                 
[log][rpc] register service shells                                                                                                          
[log][rpc] add method destroy to shells                                                                                                     
[log][rpc] add method list to shells                                                                                                        
[log][rpc] add method resize to shells                                                                                                      
[log][rpc] add method status to shells                                                                                                      
[log][rpc] register service debug                                                                                                           
[log][rpc] add method backtrace to debug                                                                                                    
[log][rpc] add method breakpoint_add to debug                                                                                               
[log][rpc] add method breakpoint_clear to debug                                                                                             
[log][rpc] add method breakpoints to debug                                                                                                  
[log][rpc] add method close to debug                                                                                                        
[log][rpc] add method cont to debug                                                                                                         
[log][rpc] add method eval to debug                                                                                                         
[log][rpc] add method init to debug                                                                                                         
[log][rpc] add method locals to debug                                                                                                       
[log][rpc] add method next to debug                                                                                                         
[log][rpc] add method restart to debug                                                                                                      
[log][rpc] add method start to debug                                                                                                        
[log][rpc] add method stop to debug                                                                                                         
[log][rpc] register service search                                                                                                          
[log][rpc] add method code to search                                                                                                        
[log][rpc] add method files to search
[log][rpc] register service deploy                                                                                                          
[log][rpc] add method run to deploy                                                                                                         
[log][rpc] add method solutions to deploy                                                                                                   
[log][rpc] register service proc                                                                                                            
[log][rpc] add method http to proc                                                                                                          
[log][rpc] register service project                                                                                                         
[log][rpc] add method detect to project                                                                                                     
[log][rpc] add method supported to project                                                                                                  
[log][rpc] add method useSample to project                                                                                                  
[log][rpc] register service /run                                                                                                            
[log][rpc] add method file to /run                                                                                                          
[log][rpc] add method list to /run                                                                                                          
[log][rpc] add method ports to /run                                                                                                         
[log][rpc] add method project to /run                                                                                                       
[log][rpc] register service /codecomplete                                                                                                   
[log][rpc] add method get to /codecomplete                                                                                                  
[log][reporting] events reporting with timeout of 180000 ms                                                                                 
[log][addons] Adding default addon cb.debug                                                                                                 
[log][addons] Adding default addon cb.deploy                                                                                                
[log][addons] Adding default addon cb.files.editor                                                                                          
[log][addons] Adding default addon cb.files.image                                                                                           
[log][addons] Adding default addon cb.git                                                                                                   
[log][addons] Adding default addon cb.help                                                                                                  
[log][addons] Adding default addon cb.offline                                                                                               
[log][addons] Adding default addon cb.panel.files                                                                                           
[log][addons] Adding default addon cb.panel.outline                                                                                         
[log][addons] Adding default addon cb.project                                                                                               
[log][addons] Adding default addon cb.settings                                                                                              
[log][addons] Adding default addon cb.terminal                                                                                              
[log][addons] Adding default addon cb.theme.dark                                                                                            
[log][addons] start addon /.codebox/addons/cb.git
[log][rpc] register service git                                                                                                             
[log][rpc] add method branch_create to git                                                                                                  
[log][rpc] add method branch_delete to git                                                                                                  
[log][rpc] add method branches to git                                                                                                       
[log][rpc] add method checkout to git                                                                                                       
[log][rpc] add method clone to git                                                                                                          
[log][rpc] add method commit to git                                                                                                         
[log][rpc] add method commits to git                                                                                                        
[log][rpc] add method commits_pending to git                                                                                                
[log][rpc] add method diff to git                                                                                                           
[log][rpc] add method init to git                                                                                                           
[log][rpc] add method pull to git                                                                                                           
[log][rpc] add method push to git                                                                                                           
[log][rpc] add method status to git                                                                                                         
[log][rpc] add method sync to git                                                                                                           
[log][addons] Addons are ready                                                                                                              
[log][web] disable auth for /manifest.appcache                                                                                              
[log][rpc] register service /auth                                                                                                           
[log][rpc] add method join to /auth                                                                                                         
[log][web] disable auth for /rpc/auth/join                                                                                                  
[log][rpc] add method settings to /auth                                                                                                     
[log][web] disable auth for /rpc/auth/settings                                                                                              
[log][rpc] register service /addons                                                                                                         
[log][rpc] add method install to /addons                                                                                                    
[log][rpc] add method list to /addons                                                                                                       
[log][rpc] add method registry to /addons                                                                                                   
[log][rpc] add method uninstall to /addons                                                                                                  
[log][watch] Starting Watch                                                                                                                 
[log][events] watch.watching.success : { watching: true, state: 'active', path: '/', error: null }

Codebox is running at http://localhost:80                                                                                                   
[log][events] project.define : [ 'node', 'makefile' ]                                                                                       
[log][hooks] use hook auth                                                                                                                  
[log][events] users.add : { userId: 'ttahmouch@gmail.com',                                                                                  
  token: 'i3eps6ecdi',                                                                                                                      
  name: 'ttahmouch@gmail.com',                                                                                                              
  email: 'ttahmouch@gmail.com',                                                                                                             
  mtime: 1397008238,                                                                                                                        
  settings: {} }                                                                                                                            
[log][shells.stream] new socket connected                                                                                                   
[log][shells.stream] open shell  { shellId: 'term1253-39',                                                                                  
  opts: { rows: 80, columns: 24, id: 'term1253-39', cwd: null } }                                                                           
[log][events] shell.spawn : { shellId: 'term1253-39' }                                                                                      
[log][events] shell.attach : { shellId: 'term1253-39' }                                                                                     
[log][events] shell.open : { shellId: 'term1253-39' }                                                                                       
[log][events] shell.resize : { rows: 18,                                                                                                    
  columns: 140,                                                                                                                             
  shellId: 'term1253-39',                                                                                                                   
  id: 'term1253-39' }  
ttahmouch commented 10 years ago

Codebox works beautifully if I point my browser at the Docker container port directly. It's when I try to route to the origin server through my transparent proxy that it doesn't render all the way.

I checked all the developer console in Chrome, and didn't see anything failing client-side besides websocket upgrade requests.

ttahmouch commented 10 years ago

Chrome developer console:

Creating Application Cache with manifest http://test.example.com/manifest.appcache test.example.com/:1
Application Cache Checking event test.example.com/:1
Application Cache Downloading event test.example.com/:1
Resource interpreted as Script but transferred with MIME type text/plain: "http://test.example.com/static/application.js". test.example.com/:13
Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://test.example.com/static/application.css". test.example.com/:6
Application Cache Progress event (0 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-text.js test.example.com/:1
Application Cache Progress event (1 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-haxe.js test.example.com/:1
Application Cache Progress event (2 of 167) http://test.example.com/static/addons/cb.files.editor/ace/worker-json.js test.example.com/:1
Application Cache Progress event (3 of 167) http://test.example.com/static/addons/cb.deploy/addon-built.js test.example.com/:1
Application Cache Progress event (4 of 167) http://test.example.com/static/fonts/fontawesome/FontAwesome.otf test.example.com/:1
[localfs] [log] sync took  300 seconds application.js:1
[settings] [log] add settings tab search application.js:1
[command] [log] Register command addons.install application.js:1
[command] [log] Register command addons.uninstall application.js:1
[settings] [log] add settings tab files application.js:1
[command] [log] Register command view application.js:1
[command] [log] Register command themes.settings application.js:1
[command] [log] Register command file application.js:1
[command] [log] Register command quit application.js:1
[command] [log] Register command statusbar.sendfeedback application.js:1
[command] [log] Register command palette.toggle application.js:1
[settings] [log] add settings tab themes application.js:1
[themes] [log] add theme default Object {id: "default", title: "White"} application.js:1
[search] [log] add search handler commands application.js:1
[search] [log] add search handler files application.js:1
[search] [log] add search handler recentfiles application.js:1
[search] [log] add search handler tags application.js:1
[settings] [log] add settings tab manager application.js:1
[search] [log] add search handler addons:uninstall application.js:1
[search] [log] add search handler addons:install application.js:1
[command] [log] Register command code.search application.js:1
[command] [log] Register command code.replace application.js:1
[command] [log] Register command find application.js:1
Application Cache Progress event (5 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-sjs.js test.example.com/:1
Application Cache Progress event (6 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-scala.js test.example.com/:1
Application Cache Progress event (7 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-groovy.js test.example.com/:1
Application Cache Progress event (8 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-diff.js test.example.com/:1
Application Cache Progress event (9 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-mel.js test.example.com/:1
Application Cache Progress event (10 of 167) http://test.example.com/static/addons/cb.files.editor/ace/keybinding-emacs.js test.example.com/:1
Application Cache Progress event (11 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/sql.js test.example.com/:1
Application Cache Progress event (12 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/php.js test.example.com/:1
Application Cache Progress event (13 of 167) http://test.example.com/static/addons/cb.project/addon-built.js test.example.com/:1
Application Cache Progress event (14 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/erlang.js test.example.com/:1
Application Cache Progress event (15 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-toml.js test.example.com/:1
Application Cache Progress event (16 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-assembly_x86.js test.example.com/:1
Application Cache Progress event (17 of 167) http://test.example.com/static/addons/cb.files.editor/ace/ext-language_tools.js test.example.com/:1
Application Cache Progress event (18 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/css.js test.example.com/:1
Application Cache Progress event (19 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/markdown.js test.example.com/:1
Application Cache Progress event (20 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-html_ruby.js test.example.com/:1
Application Cache Progress event (21 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/tex.js test.example.com/:1
Application Cache Progress event (22 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-markdown.js test.example.com/:1
Application Cache Progress event (23 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-ini.js test.example.com/:1
Application Cache Progress event (24 of 167) http://test.example.com/static/addons/cb.settings/addon-built.js test.example.com/:1
Application Cache Progress event (25 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-ocaml.js test.example.com/:1
WebSocket connection to 'ws://test.example.com/socket.io/1/websocket/lZNn7IoCg_DbJdUBRlfX' failed: Connection closed before receiving a handshake response application.js:1
Application Cache Progress event (26 of 167) http://test.example.com/static/fonts/helvetica/ultralight.eot test.example.com/:1
Application Cache Progress event (27 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/javascript.js test.example.com/:1
Application Cache Progress event (28 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-csharp.js test.example.com/:1
Application Cache Progress event (29 of 167) http://test.example.com/static/addons/cb.files.editor/ace/keybinding-vim.js test.example.com/:1
Application Cache Progress event (30 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-sass.js test.example.com/:1
Application Cache Progress event (31 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-plain_text.js test.example.com/:1
Application Cache Progress event (32 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-properties.js test.example.com/:1
Application Cache Progress event (33 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-liquid.js test.example.com/:1
Application Cache Progress event (34 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-typescript.js test.example.com/:1
Application Cache Progress event (35 of 167) http://test.example.com/static/addons/cb.files.editor/ace/worker-xquery.js test.example.com/:1
Application Cache Progress event (36 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-python.js test.example.com/:1
Application Cache Progress event (37 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-jsoniq.js test.example.com/:1
Application Cache Progress event (38 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-scad.js test.example.com/:1
Application Cache Progress event (39 of 167) http://test.example.com/static/addons/cb.panel.outline/addon-built.js test.example.com/:1
Application Cache Progress event (40 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/django.js test.example.com/:1
Application Cache Progress event (41 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-yaml.js test.example.com/:1
Application Cache Progress event (42 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-julia.js test.example.com/:1
Application Cache Progress event (43 of 167) http://test.example.com/static/fonts/helvetica/ultralight.ttf test.example.com/:1
Application Cache Progress event (44 of 167) http://test.example.com/static/addons/cb.files.editor/ace/worker-coffee.js test.example.com/:1
Application Cache Progress event (45 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/r.js test.example.com/:1
Application Cache Progress event (46 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-javascript.js test.example.com/:1
Application Cache Progress event (47 of 167) http://test.example.com/static/fonts/fontawesome/fontawesome-webfont.eot test.example.com/:1
Application Cache Progress event (48 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-sh.js test.example.com/:1
Application Cache Progress event (49 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-protobuf.js test.example.com/:1
Application Cache Progress event (50 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-coldfusion.js test.example.com/:1
Application Cache Progress event (51 of 167) http://test.example.com/static/images/icons/128.png test.example.com/:1
Application Cache Progress event (52 of 167) http://test.example.com/static/addons/cb.git/addon-built.js test.example.com/:1
Application Cache Progress event (53 of 167) http://test.example.com/static/images/icons/512.png test.example.com/:1
Application Cache Progress event (54 of 167) http://test.example.com/static/addons/cb.files.editor/ace/worker-lua.js test.example.com/:1
Application Cache Progress event (55 of 167) http://test.example.com/static/addons/cb.files.editor/ace/ext-whitespace.js test.example.com/:1
Application Cache Progress event (56 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/ruby.js test.example.com/:1
Application Cache Progress event (57 of 167) http://test.example.com/static/fonts/helvetica/normal.svg test.example.com/:1
Application Cache Progress event (58 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/haskell.js test.example.com/:1
Application Cache Progress event (59 of 167) http://test.example.com/static/fonts/fontawesome/fontawesome-webfont.ttf test.example.com/:1
Application Cache Progress event (60 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-ftl.js test.example.com/:1
Application Cache Progress event (61 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/c_cpp.js test.example.com/:1
Application Cache Progress event (62 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-lsl.js test.example.com/:1
Application Cache Progress event (63 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-latex.js test.example.com/:1
Application Cache Progress event (64 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-luapage.js test.example.com/:1
Application Cache Progress event (65 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-dot.js test.example.com/:1
Application Cache Progress event (66 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-ruby.js test.example.com/:1
Application Cache Progress event (67 of 167) http://test.example.com/static/fonts/helvetica/normal.woff test.example.com/:1
Application Cache Progress event (68 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-curly.js test.example.com/:1
Application Cache Progress event (69 of 167) http://test.example.com/static/addons/cb.debug/addon-built.js test.example.com/:1
Application Cache Progress event (70 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-jack.js test.example.com/:1
Application Cache Progress event (71 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-stylus.js test.example.com/:1
Application Cache Progress event (72 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-lisp.js test.example.com/:1
Application Cache Progress event (73 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-c_cpp.js test.example.com/:1
Application Cache Progress event (74 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/diff.js test.example.com/:1
Application Cache Progress event (75 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/textile.js test.example.com/:1
Application Cache Progress event (76 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/python.js test.example.com/:1
Application Cache Progress event (77 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-scheme.js test.example.com/:1
Application Cache Progress event (78 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-haskell.js test.example.com/:1
Application Cache Progress event (79 of 167) http://test.example.com/static/fonts/fontawesome/fontawesome-webfont.woff test.example.com/:1
Application Cache Progress event (80 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-xquery.js test.example.com/:1
Application Cache Progress event (81 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-lua.js test.example.com/:1
Application Cache Progress event (82 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-pgsql.js test.example.com/:1
Application Cache Progress event (83 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-jsp.js test.example.com/:1
Application Cache Progress event (84 of 167) http://test.example.com/static/addons/cb.files.image/addon-built.js test.example.com/:1
Application Cache Progress event (85 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/clojure.js test.example.com/:1
Application Cache Progress event (86 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-coffee.js test.example.com/:1
Application Cache Progress event (87 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-mushcode_high_rules.js test.example.com/:1
Application Cache Progress event (88 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-mysql.js test.example.com/:1
Application Cache Progress event (89 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-dart.js test.example.com/:1
Application Cache Progress event (90 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-django.js test.example.com/:1
Application Cache Progress event (91 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/java.js test.example.com/:1
Application Cache Progress event (92 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-textile.js test.example.com/:1
Application Cache Progress event (93 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/snippets.js test.example.com/:1
Application Cache Progress event (94 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-snippets.js test.example.com/:1
Application Cache Progress event (95 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-jsx.js test.example.com/:1
Application Cache Progress event (96 of 167) http://test.example.com/static/addons/cb.files.editor/ace/worker-php.js test.example.com/:1
Application Cache Progress event (97 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-scss.js test.example.com/:1
Application Cache Progress event (98 of 167) http://test.example.com/static/addons/cb.files.editor/ace/worker-css.js test.example.com/:1
Application Cache Progress event (99 of 167) http://test.example.com/static/addons/cb.files.editor/ace/ace.js test.example.com/:1
Application Cache Progress event (100 of 167) http://test.example.com/static/addons/cb.files.editor/ace/ext-searchbox.js test.example.com/:1
Application Cache Progress event (101 of 167) http://test.example.com/static/addons/cb.files.editor/ace/worker-javascript.js test.example.com/:1
Application Cache Progress event (102 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-clojure.js test.example.com/:1
Application Cache Progress event (103 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-java.js test.example.com/:1
Application Cache Progress event (104 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-actionscript.js test.example.com/:1
Application Cache Progress event (105 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-rdoc.js test.example.com/:1
Application Cache Progress event (106 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/lsl.js test.example.com/:1
Application Cache Progress event (107 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-livescript.js test.example.com/:1
Application Cache Progress event (108 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-xml.js test.example.com/:1
Application Cache Progress event (109 of 167) http://test.example.com/static/images/icons/72.png test.example.com/:1
Application Cache Progress event (110 of 167) http://test.example.com/static/addons/cb.offline/addon-built.js test.example.com/:1
Application Cache Progress event (111 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/html.js test.example.com/:1
Application Cache Progress event (112 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-rust.js test.example.com/:1
Application Cache Progress event (113 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-lucene.js test.example.com/:1
Application Cache Progress event (114 of 167) http://test.example.com/static/fonts/helvetica/ultralight.svg test.example.com/:1
Application Cache Progress event (115 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/perl.js test.example.com/:1
Application Cache Progress event (116 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-svg.js test.example.com/:1
Application Cache Progress event (117 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-rhtml.js test.example.com/:1
Application Cache Progress event (118 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-prolog.js test.example.com/:1
Application Cache Progress event (119 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-html.js test.example.com/:1
Application Cache Progress event (120 of 167) http://test.example.com/static/images/icons/ios.png test.example.com/:1
Application Cache Progress event (121 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-glsl.js test.example.com/:1
Application Cache Progress event (122 of 167) http://test.example.com/static/images/icons/32.png test.example.com/:1
Application Cache Progress event (123 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/makefile.js test.example.com/:1
Application Cache Progress event (124 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/jsp.js test.example.com/:1
Application Cache Progress event (125 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/lua.js test.example.com/:1
Application Cache Progress event (126 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/coffee.js test.example.com/:1
Application Cache Progress event (127 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-perl.js test.example.com/:1
Application Cache Progress event (128 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-makefile.js test.example.com/:1
Application Cache Progress event (129 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-sql.js test.example.com/:1
Application Cache Progress event (130 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-nix.js test.example.com/:1
Application Cache Progress event (131 of 167) http://test.example.com/static/fonts/fontawesome/fontawesome-webfont.svg test.example.com/:1
Application Cache Progress event (132 of 167) http://test.example.com/static/addons/cb.files.editor/ace/ext-modelist.js test.example.com/:1
Application Cache Progress event (133 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/actionscript.js test.example.com/:1
Application Cache Progress event (134 of 167) http://test.example.com/static/fonts/helvetica/normal.eot test.example.com/:1
Application Cache Progress event (135 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-php.js test.example.com/:1
Application Cache Progress event (136 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-r.js test.example.com/:1
Application Cache Progress event (137 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-space.js test.example.com/:1
Application Cache Progress event (138 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-json.js test.example.com/:1
Application Cache Progress event (139 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-handlebars.js test.example.com/:1
Application Cache Progress event (140 of 167) http://test.example.com/static/addons/cb.theme.dark/addon-built.js test.example.com/:1
Application Cache Progress event (141 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-objectivec.js test.example.com/:1
Application Cache Progress event (142 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-css.js test.example.com/:1
Application Cache Progress event (143 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-logiql.js test.example.com/:1
Application Cache Progress event (144 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-tex.js test.example.com/:1
Application Cache Progress event (145 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-matlab.js test.example.com/:1
Application Cache Progress event (146 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-d.js test.example.com/:1
Application Cache Progress event (147 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-erlang.js test.example.com/:1
Application Cache Progress event (148 of 167) http://test.example.com/static/addons/cb.terminal/addon-built.js test.example.com/:1
Application Cache Progress event (149 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-golang.js test.example.com/:1
Application Cache Progress event (150 of 167) http://test.example.com/static/fonts/helvetica/normal.ttf test.example.com/:1
Application Cache Progress event (151 of 167) http://test.example.com/static/addons/cb.help/addon-built.js test.example.com/:1
Application Cache Progress event (152 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-ejs.js test.example.com/:1
Application Cache Progress event (153 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/haml.js test.example.com/:1
Application Cache Progress event (154 of 167) http://test.example.com/static/images/icons/48.png test.example.com/:1
Application Cache Progress event (155 of 167) http://test.example.com/static/addons/cb.panel.files/addon-built.js test.example.com/:1
Application Cache Progress event (156 of 167) http://test.example.com/static/fonts/helvetica/ultralight.woff test.example.com/:1
Application Cache Progress event (157 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-twig.js test.example.com/:1
Application Cache Progress event (158 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-jade.js test.example.com/:1
Application Cache Progress event (159 of 167) http://test.example.com/static/application.css test.example.com/:1
Application Cache Progress event (160 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/sh.js test.example.com/:1
Application Cache Progress event (161 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-html_completions.js test.example.com/:1
Application Cache Progress event (162 of 167) http://test.example.com/static/application.js test.example.com/:1
Application Cache Progress event (163 of 167) http://test.example.com/static/addons/cb.files.editor/ace/snippets/dart.js test.example.com/:1
Application Cache Progress event (164 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-less.js test.example.com/:1
Application Cache Progress event (165 of 167) http://test.example.com/static/addons/cb.files.editor/ace/mode-haml.js test.example.com/:1
Application Cache Progress event (166 of 167) http://test.example.com/static/addons/cb.files.editor/addon-built.js test.example.com/:1
Application Cache Progress event (167 of 167)  test.example.com/:1
Application Cache Cached event test.example.com/:1
AaronO commented 10 years ago

It seems like your proxy is faulty.

I recommend that you use a better, more feature-complete and tested reverse proxy. Either :

ttahmouch commented 10 years ago

@AaronO You're probably right. I was hoping to write a really thin proxy to keep dependencies small. I built it originally for routing SSH traffic through HTTP CONNECT, and I was hoping to extend it for Codebox routing.

I'll take a look into the ones you mentioned. Much appreciated.

ttahmouch commented 10 years ago

Yeah, switching proxies worked beautifully. I wonder what I did wrong. :(

AaronO commented 10 years ago

Which one did you switch to ?

Maybe post your full code and I'll take a look at it. I wrote loadfire which is the one we maintain and use, so I'm pretty familiar with writing proxies.

ttahmouch commented 10 years ago

I gave node-http-proxy a shot since their API seemed pretty terse. I think the code is literally identical sans the pieces where I established my own socket streams, and began piping. They seem to handle all of it in the proxy.web() and proxy.ws() methods.

New code:

var proxy = httpProxy.createProxy();
    http
        .createServer(function (req, res) {
            /**
             * Host = "Host" ":" host [ ":" port ] ; Section 3.2.2
             *
             * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
             */
            var headers = req.headers,
                host = headers['host'],
                hasTarget = proxyTable.hasTarget(host);

            if (debug) {
                console.log('------------------------------');
                console.log('Host:');
                console.log(host);
                console.log('Has Target?');
                console.log(hasTarget);
                console.log('------------------------------');
            }

            if (hasTarget) {
                var targetUrl = proxyTable.getTarget(host);

                proxy.web(req, res, {
                    target: targetUrl
                }, function (e) {
                    if (debug) {
                        console.log('------------------------------');
                        console.log('Error:');
                        console.error(e);
                        console.log('------------------------------');
                    }
                    res.statusCode = 504;
                    res.end();
                });

                if (debug) {
                    console.log('------------------------------');
                    console.log('Target:');
                    console.log(targetUrl);
                    console.log('------------------------------');
                }
            } else {
                res.statusCode = 400;
                res.end();
            }
        })
        .on('upgrade', function (req, clientSocket, head) {
            /**
             * Host = "Host" ":" host [ ":" port ] ; Section 3.2.2
             *
             * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
             */
            var headers = req.headers,
                host = headers['host'],
                badRequest = 'HTTP/1.1 400 Bad Request',
                gatewayTimeout = '504 Gateway Timeout',
                proxyAgent = 'Proxy-agent: Hapi-Proxy/1.0',
                hasTarget = proxyTable.hasTarget(host);

            if (debug) {
                console.log('------------------------------');
                console.log('Host:');
                console.log(host);
                console.log('Has Target?');
                console.log(hasTarget);
                console.log('------------------------------');
            }

            if (hasTarget) {
                var targetUrl = proxyTable.getTarget(host);

                proxy.ws(req, clientSocket, head, {
                    target: targetUrl
                }, function (e) {
                    if (debug) {
                        console.log('------------------------------');
                        console.log('Error:');
                        console.error(e);
                        console.log('------------------------------');
                    }
                    clientSocket.write(gatewayTimeout + '\r\n' + proxyAgent + '\r\n\r\n');
                    clientSocket.end();
                    clientSocket.destroy();
                });

                if (debug) {
                    console.log('------------------------------');
                    console.log('Target:');
                    console.log(targetUrl);
                    console.log('------------------------------');
                }
            } else {
                /**
                 * If the host is not a valid host on the server, the response MUST be a 400 (Bad Request)
                 * error message.
                 *
                 * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2
                 */
                clientSocket.write(badRequest + '\r\n' + proxyAgent + '\r\n\r\n');
                clientSocket.end();
                clientSocket.destroy();
            }
        })
ttahmouch commented 10 years ago

I'll have to take a look at loadfire soon for sure. I love the name. :)

I just knew http-proxy relatively well looking around the web the past few days.