summaryrefslogtreecommitdiff
path: root/ui/app/components/main_panel/vpn_section.js
blob: 78146933617b1e4a37b255ccdddcd63b62be9035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import React from 'react'
import { Button, ButtonToolbar, Glyphicon, Alert } from 'react-bootstrap'
import SectionLayout from './section_layout'

import Spinner from 'components/spinner'
import bitmask from 'lib/bitmask'

export default class vpnSection extends React.Component {

  static get defaultProps() {return{
    account: null
  }}

  constructor(props) {
    super(props)
    this.state = {
      interval: null,  // timer callback
      error: null,     // error message
      message: null,   // info message
      expanded: false, // show vpn section expanded or compact
      ready: false,    // true if vpn can start
      vpn: "unknown",  // current state of vpn
      up: null,        // \ throughput
      down: null       // / labels
    }
    this.expand     = this.expand.bind(this)
    this.connect    = this.connect.bind(this)
    this.disconnect = this.disconnect.bind(this)
    this.retry      = this.retry.bind(this)
    this.enable     = this.enable.bind(this)
    this.installHelper = this.installHelper.bind(this)

    this.statusEvent = this.statusEvent.bind(this)
    this.loginEvent  = this.loginEvent.bind(this)
  }

  // called whenever a new account is selected
  componentWillReceiveProps(nextProps) {
    this.stopWatchingStatus()
    if (this.props.account.domain != nextProps.account.domain) {
      this.checkReadiness(nextProps.account.domain)
    }
  }

  // called only once
  componentWillMount() {
    this.checkReadiness()
    bitmask.events.register("VPN_STATUS_CHANGED", 'vpn section update', this.statusEvent)
    bitmask.events.register("BONAFIDE_AUTH_DONE", 'vpn section auth', this.loginEvent)
  }

  // not sure if this is ever called
  componentWillUnmount() {
    bitmask.events.unregister("VPN_STATUS_CHANGED", 'vpn section update')
    bitmask.events.unregister("BONAFIDE_AUTH_DONE", 'vpn section auth')
    this.stopWatchingStatus()
  }

  updateStatus(domain = null) {
    domain = domain || this.props.account.domain
    bitmask.vpn.status().then(
      vpn => {
        this.stopWatchingStatus()
        // If the current active VPN does not match this domain, ignore the status info.
        if (vpn.domain !== domain || vpn.status === "off") {
          this.setState({vpn: "down"})
        } else if (vpn.status === "on") {
          this.setState({
            vpn: "up",
            up: vpn.up,
            down: vpn.down
          })
          this.startWatchingStatus()
        } else if (vpn.status === "disabled") {
          this.setState({vpn: "disabled"})
        } else if (vpn.status === "starting") {
          this.setState({vpn: "connecting"})
        } else {
          console.log("UNKNOWN STATUS", vpn.status)
          // it should not get here...
          this.setState({vpn: "waiting"})
        }
      },
      error => {
        this.setState({vpn: "failed", error: error})
      }
    )
  }

  //
  // Check if all the prerequisites have been met.
  // This is called whenever the widget is shown, and also whenever a user
  // authenticates
  //
  checkReadiness(domain = null) {
    domain = domain || this.props.account.domain
    bitmask.vpn.check(domain).then(
      status => {
        console.log('check()', status)
        if (status.vpn == 'disabled') {
          this.setState({vpn: "disabled"})
        } else if (!status.installed) {
          this.setState({vpn: "nohelpers"})
        } else if (!status.vpn_ready) {
          this.renewCert()
        } else {
          this.setState({
            message: null,
            error: null,
            ready: true
          })
          this.updateStatus(domain)
        }
      },
      error => {
        console.log('check()', error)
        if (error == "Missing VPN certificate") {
          this.renewCert()
        }
        if (error == 'nopolkit') {
          this.setState({vpn: "nopolkit"})
        } else {
          this.setState({vpn: "failed", error: error})
        }
      }
    )
  }

  //
  // install the necessary helper files
  //
  installHelper() {
    bitmask.vpn.install().then(
      ok => {
        this.checkReadiness()
      },
      error => {
        console.log('install()', error)
        this.setState({vpn: "failed", error: error})
      }
    )
  }


  //
  // event callback: something new has happened, time to re-poll
  //
  statusEvent() {
    console.log('statusEvent')
    this.updateStatus()
  }

  //
  // event callback: the user successfully logged in
  //
  loginEvent(event, user) {
    let address = user[1]
    this.checkReadiness(address.split('@')[1])
  }

  //
  // get new vpn cert from provider
  //
  renewCert() {
    if (!this.props.account.authenticated) {
      this.setState({
        message: 'Please log in to renew VPN credentials.',
        vpn: null
      })
    } else {
      let message = (<div>
        <Spinner/>&nbsp;<span>Renewing VPN credentials...</span>
      </div>)
      this.setState({message: message})
      bitmask.vpn.get_cert(this.props.account.id).then(
        ok => {
          console.log('get_cert()', ok)
          this.checkReadiness()
        }, error => {
          console.log('get_cert()', error)
          this.setState({vpn: "failed", error: error})
        }
      )
    }
  }

  // section expand/collapse button pressed
  expand() {
    this.setState({expanded: !this.state.expanded})
  }

  // turn on button pressed
  connect() {
    this.setState({vpn: "connecting", error: null})
    bitmask.vpn.stop().then(
      wasRunning   => {this.startvpn()},
      wasntRunning => {this.startvpn()}
    )
  }

  // turn off button pressed
  disconnect() {
    this.setState({vpn: "disconnecting", error: null})
    bitmask.vpn.stop().then(
      success => {
        this.setState({vpn: "down"})
      },
      error => {
        this.setState({vpn: "failed", error: error})
      }
    )
  }

  // retry button pressed
  retry() {
    this.setState({error: null, message: null, vpn: 'waiting'})
    this.updateStatus()
    this.checkReadiness()
  }

  // enable button pressed
  enable() {
    this.setState({error: null, message: null, vpn: 'waiting'})
    bitmask.vpn.enable().then(
      ok => {
        console.log('enable()', ok)
        this.retry()
      },
      error => {
        this.setState({error: error, message: null, vpn: 'failed'})
        console.log('enable(error)', error)
      }
    )
  }

  //
  // call startvpn() only when everything is ready, and no vpn is currently
  // running.
  //
  startvpn() {
    bitmask.vpn.start(this.props.account.domain).then(
      status => {
        console.log('start success', status)
      }, error => {
        console.log('start error', error)
        this.setState({vpn: "failed", error: error})
      }
    )
  }

  startWatchingStatus() {
    this.interval = setInterval(this.statusEvent, 1000)
  }

  stopWatchingStatus() {
    clearInterval(this.interval)
  }

  render () {
    let message = null
    let error = null
    let body = null
    let button = null
    let icon = null
    let info = null
    let expand = null // this.expand

    // style may be: success, warning, danger, info
    if (this.state.error) {
      error = (
        <Alert bsStyle="danger">{this.state.error}</Alert>
      )
    }
    if (this.state.message) {
      message = (
        <Alert bsStyle="info">{this.state.message}</Alert>
      )
    }

    if (this.state.expanded) {
      body = <div>traffic details go here</div>
    }

    switch(this.state.vpn) {
      case "down":
        button = <Button onClick={this.connect}>Turn ON</Button>
        icon = "disabled"
        break
      case "up":
        button = <Button onClick={this.disconnect}>Turn OFF</Button>
        icon = "on"
        info = "Connected"
        if (this.state.up) {
          info = <span>
            <Glyphicon glyph="chevron-down" />
            {this.state.down}
            &nbsp;&nbsp;
            <Glyphicon glyph="chevron-up" />
            {this.state.up}
          </span>
        }
        break
      case "connecting":
        button = <Button onClick={this.disconnect}>Cancel</Button>
        icon = "wait"
        info = "Connecting..."
        break
      case "disconnecting":
        button = <Button onClick={this.disconnect}>Cancel</Button>
        icon = "wait"
        break
      case "nopolkit":
        info = "Missing authentication agent."
        body = (
          <div>
            <p>Could not find a polkit authentication agent.
            Please run one and try again.</p>
          </div>
        )
        icon= "disabled"
        break
      case "failed":
        info = "Failed"
        if (this.state.ready) {
          button = <ButtonToolbar>
            <Button onClick={this.connect}>Turn ON</Button>
            <Button onClick={this.disconnect}>Unblock</Button>
          </ButtonToolbar>
        } else {
          button = <Button onClick={this.retry}>Retry</Button>
        }
        icon = "off"
        break
      case "disabled":
        button = <Button onClick={this.enable}>Enable</Button>
        icon = "disabled"
        break
      case "waiting":
        button = <Spinner />
        icon = "wait"
        break
      case "nohelpers":
        body = (
          <div>
            <p>The VPN requires that certain helpers are installed on your system.</p>
            <Button onClick={this.installHelper}>Install Helper Files</Button>
          </div>
        )
        break
    }

    let header = (
      <div>
        <h1>VPN</h1>
        <span className="info">{info}</span>
      </div>
    )

    if (button == null) {
      expand = null
    }

    return (
      <SectionLayout icon="planet" buttons={button} status={icon}
        onExpand={expand} header={header} body={body}
        message={message} error={error} />
    )
  }

}