Project

General

Profile

Download (6.39 KB) Statistics
| Branch: | Revision:
1
define([
2
'dojo/_base/declare',
3
'dojo/_base/lang',
4
'dojo/_base/window',
5
'dojo/dom-construct',
6
'dijit/Dialog',
7
'dijit/form/Button',
8
'dijit/form/Form',
9
'dijit/form/TextBox',
10
'dojox/NodeList/delegate',
11
'java/applet'
12
], function(declare, lang, win, domConstruct, Dialog, Button, Form, TextBox, delegate, applet){
13

    
14
var Tunnel = declare('ssh.Tunnel', null, {
15
    // applet stuff
16
    callback: 'ssh.Tunnel.javatrigger',
17
    code: 'Tunnel',
18
    archive: dojo.moduleUrl("ssh","resources/ssh.jar"),
19

    
20
    connected: false,
21

    
22
    // tunnel params.
23
    host: null,
24
    local_port: null,
25
    remote_port: null,
26
    remote_ip: null,
27
    local_port: null,
28
    username: null,
29
    id: null,
30

    
31
    // applet inject node
32
    node:null,
33

    
34
    constructor: function(args){
35
        lang.mixin(this, args);
36

    
37
        this.id = 'tunnel-' + Tunnel.id;
38
        Tunnel.id = Tunnel.id + 1;
39
        if(typeof Tunnel.tunnels[this.id] !== 'undefined'){
40
            console.log('Tunnel already present');
41
            domConstruct.destroy(this.id);
42
        }
43
        Tunnel.tunnels[this.id] = this;
44
        Tunnel.local_port = Tunnel.local_port + 1;
45
        this.local_port = Tunnel.local_port;
46

    
47
        if(!this.node){
48
            this.node = win.body();
49
        }
50
        // FIXME: we are incrementing the port number
51
        // for each new tunnel.
52
    },
53

    
54
    start: function(){
55
        console.log('steam2.tunnel.start');
56
        
57
        if(this.connected){
58
            console.log('Tunnel.start', 'already connected');
59

    
60
            // dojo.publish(tunnel.INIT);
61
            // dojo.publish(tunnel.CONNECTED);
62
            return;
63
        }
64
        applet.inject(this.node, {
65
            callback:this.callback,
66
            code:this.code,
67
            archive:this.archive,
68
            cache_archive: this.archive,
69
            cache_version: '1',
70
            host:this.host,
71
            local_port:this.local_port,
72
            remote_port:this.remote_port,
73
            remote_ip:this.remote_ip,
74
            username:this.username,
75
            password:Tunnel.password,
76
            id:this.id
77
        });
78
    },
79

    
80
    stop: function(){
81
        this.onDestroy();
82
    },
83
    
84
    onConnect: function(){
85
        this.connected = true;
86
    },
87
                
88
    onDestroy: function(){
89
        console.log('steam2.tunnel.onDestroy');
90
        this.connected = false;
91
        domConstruct.destroy(this.id);
92
    },
93

    
94
    onPasswordError: function(){
95
        console.log('steam2.tunnel.onPasswordError');
96
        domConstruct.destroy(this.id);
97
        Tunnel.showPasswordDialog();
98
        Tunnel.current = this;
99
    },
100

    
101
    onBindError: function(){
102
        domConstruct.destroy(this.id);
103
        Tunnel.portDialog();
104
    }
105
});
106

    
107
Tunnel.tunnels = {};
108
Tunnel.id = 0;
109
Tunnel.current = null;
110
Tunnel.password = null;
111
Tunnel.local_port = 8240; 
112

    
113
Tunnel.INJECT = 'tunnel:inject';
114
Tunnel.INIT = 'tunnel:init';
115
Tunnel.DESTROY = 'tunnel:destroy';
116
Tunnel.BIND_ERROR = 'tunnel:bind_error';
117
Tunnel.PASSWORD_ERROR = 'tunnel:password_error';
118
Tunnel.CONNECTED = 'tunnel:connected';
119

    
120
Tunnel.javatrigger = function(eventname, id, msg){
121
    console.log('Tunnel.javatrigger', arguments);
122
    var tunnel = this.tunnels[id];    
123
    var self = this;
124

    
125
    function doIt(){
126
        switch('tunnel:' + eventname){
127
        case self.DESTROY:
128
            tunnel.onDestroy();
129
            break;
130
        case self.INIT:
131
            break;
132
        case self.CONNECTED:
133
            tunnel.onConnect();
134
            break;
135
        case self.BIND_ERROR:
136
            tunnel.onBindError();
137
            break;
138
        case self.PASSWORD_ERROR:
139
            tunnel.onPasswordError();
140
            break;
141
        default:
142
            console.log('default case');
143
        }
144
    }
145
    setTimeout(doIt, 0);
146
};
147

    
148
Tunnel.showPasswordDialog = function(){
149
    console.log('evd::Tunnel2::showPasswordDialog');
150
    if(typeof this._passwordDialog === 'undefined'){
151
        this._passwordDialog = this.passwordDialog();
152
    }
153
    this._passwordDialog.show();
154
};
155

    
156
Tunnel.passwordDialog = function(){
157

    
158
    var dialog = new dijit.Dialog({
159
        title: "Enter password",
160
        style: "width: 250px",
161
        onHide: lang.hitch(this, this.stop)
162
    });
163

    
164
    var content = [
165
        '<div id="tunnel2-password-form" dojoType="dijit.form.Form" style="text-align:center">',
166
        '    <script type="dojo/method" data-dojo-event="onSubmit" data-dojo-args="e">',
167
        '    ssh.Tunnel.passwordDialogSubmit(e,this);',
168
        '    </script>',
169
        '    <div>',
170
        '        Password:',
171
        '        <input id="evd-tunnel2-password" type="password" name="password" dojoType="dijit.form.TextBox" style="width:100px"/>',
172
        '    </div>',
173
        '    <div style="margin-top:5px;">',
174
        '        <button type="submit" dojoType="dijit.form.Button">OK</button>',
175
        '    </div>',
176
        '</div>'].join('');
177
    dialog.set('content', content);
178
    return dialog;
179
 };
180

    
181
Tunnel.passwordDialogSubmit = function(e, form){
182
    console.log('evd::Tunnel2::passwordDialogSubmit', arguments);
183

    
184
    e.preventDefault(); 
185
    this.password = form.get('value').password;
186
    this._passwordDialog.hide();
187

    
188
    var tunnel = this.current;
189
    tunnel.start();
190
};
191

    
192
Tunnel.portDialogSubmit = function(form){
193
    console.log(arguments, this);
194

    
195
    this.local_port = parseInt(form.get('value').port, 10);
196
    this._portDialog.hide();
197
    
198
    var tunnel = this.current;
199
    tunnel.local_port = this.local_port;
200
    tunnel.start();
201
};
202

    
203
Tunnel.portDialog = function(){
204
    console.log('evd.Tunne2.portDialog');
205

    
206
    if(typeof this._portDialog == 'undefined'){
207
        this._portDialog = new Dialog({
208
            style: "width: 300px",
209
            onHide: lang.hitch(this, this.stop)
210
        });
211
    }
212

    
213
    var content = [
214
        '<div id="tunnel-port-form" dojoType="dijit.form.Form" style="text-align:center">',
215
        '    <script type="dojo/method" event="onSubmit">',
216
        '    ssh.Tunnel.portDialogSubmit(this);return false;',
217
        '    </script>',
218
        '    <div>',
219
        '        A free local port:',
220
        '        <input id="port" type="port" name="port" dojoType="dijit.form.TextBox" style="width:100px" value="' + this.local_port + '" />',
221
        '    </div>',
222
        '    <div style="margin-top:5px;">',
223
        '        <button type="submit" dojoType="dijit.form.Button">OK</button>',
224
        '    </div>',
225
        '</div>'].join('');
226
    this._portDialog.set('title','local port ' + this.local_port + ' is busy');
227
    this._portDialog.set('content', content);
228
    this._portDialog.show();
229
};
230

    
231
return Tunnel;
232

    
233
});
    (1-1/1)