UNPKG

4.26 kBtext/coffeescriptView Raw
1fs = require "fs"
2os = require "os"
3colors = require "colors"
4path = require "path"
5url = require "url"
6http = require "http"
7https = require "https"
8request = require "request"
9qs = require "querystring"
10utils = require "../lib/utils"
11Controller = require '../lib/controller'
12
13{findFile, guard} = require '../modules/common/helpers'
14
15
16{ask, logger} = utils
17log = logger "register"
18
19module.exports = class Register extends Controller
20 KD_DIR = path.join process.env.HOME, ".kd"
21
22 @set
23 commands:
24 'renew': ['renew', 'Renews your kite public key']
25 'set': ['set', 'Sets your koding key']
26 'register': ['register', 'Registers your computer']
27 'default': 'register'
28
29 constructor: (@configFile)->
30 @privateKeyPath = path.join KD_DIR, "koding.key"
31 @publicKeyPath = path.join KD_DIR, "koding.key.pub"
32 @configFile.config.KODING_ROOT or= "https://koding.com"
33
34 get: ->
35 console.log @configFile.config.publicKey
36
37 renew: ->
38 {argv: {all, silent}} = @options
39 change = (path, name)=>
40 key = utils.keygen 64
41 fs.writeFileSync path, key
42 console.log "#{name}: #{key}"
43
44 unless silent
45 ask "Do you really want to do it? It may affect your works! (yes|no)",
46 format: /yes|no|Y|N/,
47 callback: (answer)=>
48 if answer is "yes"
49 change @publicKeyPath, "Public"
50 if all then change @privateKeyPath, "Private"
51 process.exit()
52 error: (answer)->
53 console.log "Please write a valid answer: yes or no. That's simple."
54 else
55 change @publicKeyPath, "Public"
56 if all then change @privateKeyPath, "Private"
57
58 where: ->
59 console.log @publicKeyPath
60
61 set: (key)->
62 unless key?.length is 64
63 log "Your key's length must be 64.", "red"
64 process.exit 1
65 fs.writeFileSync (path.join process.env.HOME, ".kd", "koding.key.pub"), key
66 log "You've changed your key successfully.", "green"
67
68 register: =>
69 guard not @configFile.config.publicKey, "you need to set your public key"
70
71 Config = require '../modules/config'
72 if not @configFile.config['user.name']
73 # HELLO first timer
74 readpath = path.join( __dirname, '..', 'modules/resources/kd-manifest.json' )
75 rs = fs.createReadStream(readpath)
76 rs.pipe(fs.createWriteStream(path.join(KD_DIR, "kd-manifest.json" )))
77
78 c = new Config(@configFile)
79 c.username (configuration)=>
80 @configFile = configuration
81 @_register()
82 else
83 @_register()
84
85 _register: ->
86 guard not @configFile.config["user.name"], "you need to set your username"
87 guard not @configFile.config.publicKey, "you need to set your public key"
88
89 {argv: {verbose}} = @options
90 .alias("v", "verbose")
91 .alias("t", "timeout")
92
93 link = "#{@configFile.config.KODING_ROOT}/-/KD/Register/#{encodeURIComponent os.hostname()}/#{@configFile.config.publicKey}"
94 log """
95
96
97 Hello #{@configFile.config["user.name"]}!
98
99 Please visit the following URL to connect your
100 host to this computer.
101
102
103 """
104
105 console.log "#{colors.underline link}"
106
107 trycount = 0
108 process.stdout.write "register: "
109 process.stdout.write "Waiting for you to connect the host"
110 waiting = setInterval =>
111 check?.abort?()
112
113 client = require('needle')
114
115 data =
116 key: @configFile.config.publicKey
117 username : @configFile.config["user.name"]
118
119
120 url = "#{@configFile.config.KODING_ROOT}/-/kd/register-check"
121
122 client.post url, data, multipart:true, (err, resp, body)->
123 console.log(err)
124 console.log("Got status code " + resp.statusCode)
125 console.log(body)
126
127 if resp.statusCode isnt 200
128 console.log "An error occured with the status #{resp.statusCode}: #{http.STATUS_CODES[resp.statusCode]}."
129 return
130
131 if body is "OK"
132 console.log "Host connected!"
133 clearInterval waiting
134 else
135 console.log "Registration not found. Retrying."
136 console.log "response was:", body
137 trycount += 1
138
139 if trycount > 10
140 clearInterval waiting
141 console.log "registration timed out please re-run"
142
143 , 5000