summaryrefslogtreecommitdiff
path: root/ui/app/components/main_panel/vpn_section.js
blob: 400d2133827fdec2cd3f5d9369d9b4d11dee7c1a (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
import React from 'react'
import { Button, 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 = {
      error: null,
      expanded: false,
      vpn: "waiting",
    }
    this.expand     = this.expand.bind(this)
    this.connect    = this.connect.bind(this)
    this.disconnect = this.disconnect.bind(this)
    this.cancel     = this.cancel.bind(this)
    this.unblock    = this.unblock.bind(this)
    this.location   = this.location.bind(this)
  }

  componentWillMount() {
    bitmask.vpn.status().then(
      status => {
        console.log("status: ", status)
        if (status.VPN == "OFF") {
          this.setState({vpn: "down"})
        } else if (status.VPN == "ON") {
          if (status.domain == this.props.account.domain) {
            this.setState({vpn: "up"})
          } else {
            this.setState({vpn: "down"})
          }
        } else {
          // this.setState({vpn: "????"})
        }
      },
      error => {
        this.setState({vpn: "failed", error: error})
      }
    )
  }

  expand() {
    this.setState({expanded: !this.state.expanded})
  }

  // turn on button pressed
  connect() {
    this.setState({vpn: "connecting", error: null})
    bitmask.vpn.check(this.props.account.domain).then(
      status => {
        console.log('check: ', status)
        if (status.vpn_ready == true) {
          this.startVPN()
        } else if (status.vpn == "disabled") {
          this.setState({vpn: "failed", error: "VPN support disabled"})
        } else {
          bitmask.vpn.get_cert(this.props.account.id).then(
            uid => {
              this.startVPN()
            },
            error => {
              this.setState({vpn: "failed", error: error})
            }
          )
        }
      },
      error => {
        this.setState({vpn: "failed", error: error})
      }
    )
  }

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

  cancel() {

  }

  unblock() {

  }

  location() {

  }

  // call startVPN() only when everything is ready
  startVPN() {
    bitmask.vpn.start(this.props.account.domain).then(
      status => {
        console.log('start: ', status)
        if (status.result == "started") {
          this.setState({vpn: "up", error: null})
        } else {
          this.setState({vpn: "failed"})
        }
      },
      error => {
        this.setState({vpn: "failed", error: error})
      }
    )
  }

  render () {
    console.log(this.state)
    let message = null
    let body = null
    let button = null
    let icon = null

    let header = <h1>VPN</h1>
    if (this.state.error) {
      // style may be: success, warning, danger, info
      message = (
        <Alert bsStyle="danger">{this.state.error}</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"
        break
      case "connecting":
        button = <Button onClick={this.cancel}>Cancel</Button>
        icon = "wait"
        break
      case "disconnecting":
        button = <Button onClick={this.cancel}>Cancel</Button>
        icon = "wait"
        break
      case "failed":
        button = <div>
          <Button onClick={this.connect}>Turn ON</Button>
        </div>
        // <Button onClick={this.unblock}>Unblock</Button>
        icon = "off"
        break
      case "disabled":
        button = <div>Disabled</div>
        icon = "disabled"
        break
      case "waiting":
        button = <Spinner />
        icon = "wait"
        break
    }

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

}