1- var ACME , Duplex , args , createHttpsServer , createServer , cwmp , duckdns , existsSync , file , path , readFileSync , ref , route , statSync ;
1+ var Duplex , args , createHttpsServer , createServer , cwmp , file , path , readFileSync , route ;
22
33Duplex = require ( 'stream' ) . Duplex ;
44
55createServer = require ( 'http' ) . createServer ;
66
77createHttpsServer = require ( 'https' ) . createServer ;
88
9- ref = require ( 'fs' ) , readFileSync = ref . readFileSync , existsSync = ref . existsSync , statSync = ref . statSync ;
10-
11- ACME = require ( '@root/acme' ) ;
12-
13- duckdns = require ( 'acme-dns-01-duckdns' ) ;
9+ readFileSync = require ( 'fs' ) . readFileSync ;
1410
1511path = require ( 'path' ) ;
1612
@@ -23,7 +19,7 @@ args = require('../args');
2319cwmp = require ( './cwmp' ) ;
2420
2521module . exports = function ( ip , port , url ) {
26- var acme , dns01 , domain , e , srv ;
22+ var e , httpsoptions , srv ;
2723 if ( args . file ) {
2824 file . name = path . basename ( args . file ) ;
2925 try {
@@ -58,119 +54,30 @@ module.exports = function(ip, port, url) {
5854 res . writeHead ( 200 ) ;
5955 return res . end ( ) . post ( '/' , cwmp ( url ) ) ;
6056 } ) ;
61- if ( url . indexOf ( "https://" ) !== - 1 ) {
62- domain = new URL ( url ) . hostname ;
63- if ( args . duckdnstoken ) {
64- dns01 = duckdns . create ( {
65- baseUrl : 'https://www.duckdns.org/update' ,
66- token : args . duckdnstoken
67- } ) ;
68- console . log ( "Requesting HTTPS certificate to LE via duckdns..." ) ;
69- require ( 'http-request' ) . get ( 'https://www.duckdns.org/update?domains=' + domain + '&token=' + args . duckdnstoken + '&ip=' + ip , function ( err , res ) {
70- if ( err ) {
71- return console . log ( "Error updating duckdns domain IP!!!" ) ;
72- }
73- } ) ;
74- } else {
75- console . log ( "No duckdns token!!" ) ;
76- }
77- acme = ACME . create ( {
78- maintainerEmail : 'test@gmail.com' ,
79- packageAgent : 'tch-exploit/v1.0'
80- } ) ;
81- acme . init ( 'https://acme-v02.api.letsencrypt.org/directory' ) . then ( function ( r ) {
82- var Keypairs ;
83- Keypairs = require ( '@root/keypairs' ) ;
84- return Keypairs . generate ( {
85- kty : 'EC' ,
86- format : 'jwk'
87- } ) . then ( function ( accountKeypair ) {
88- return acme . accounts . create ( {
89- subscriberEmail : 'test1@gmail.com' ,
90- agreeToTerm : true ,
91- accountKey : accountKeypair [ "private" ]
92- } ) . then ( function ( account ) {
93- return Keypairs . generate ( {
94- kty : 'RSA' ,
95- format : 'jwk'
96- } ) . then ( function ( serverKeypair ) {
97- return Keypairs [ "export" ] ( {
98- jwk : serverKeypair [ "private" ]
99- } ) . then ( function ( privateKey ) {
100- var CSR , PEM , punycode ;
101- CSR = require ( '@root/csr' ) ;
102- PEM = require ( '@root/pem' ) ;
103- punycode = require ( 'punycode' ) ;
104- return CSR . csr ( {
105- jwk : serverKeypair [ "private" ] ,
106- domains : [ punycode . toASCII ( domain ) ] ,
107- encoding : 'der'
108- } ) . then ( function ( csrDer ) {
109- var csr ;
110- csr = PEM . packBlock ( {
111- type : 'CERTIFICATE REQUEST' ,
112- bytes : csrDer
113- } ) ;
114- return acme . certificates . create ( {
115- account : account ,
116- accountKey : accountKeypair [ "private" ] ,
117- csr : csr ,
118- domains : [ domain ] ,
119- challenges : {
120- 'dns-01' : dns01
121- }
122- } ) . then ( function ( pems ) {
123- var httpsoptions , srv ;
124- console . log ( "HTTPS certificate received!" ) ;
125- console . log ( privateKey ) ;
126- console . log ( "Cert:" ) ;
127- console . log ( pems . cert ) ;
128- httpsoptions = {
129- key : privateKey ,
130- cert : pems . cert
131- } ;
132- srv = createHttpsServer ( httpsoptions , route ) ;
133- if ( port === 80 ) {
134- port = 443 ;
135- }
136- srv . keepAliveTimeout = 30000 ;
137- srv . on ( 'error' , function ( e ) {
138- var ref1 ;
139- if ( ( ref1 = e . code ) === 'EADDRINUSE' || ref1 === 'EADDRNOTAVAIL' ) {
140- console . log ( e . code + ', retrying...' ) ;
141- return setTimeout ( function ( ) {
142- srv . close ( ) ;
143- return srv . listen ( port ) ;
144- } , 1000 ) ;
145- } else {
146- return console . error ( e ) ;
147- }
148- } ) ;
149- srv . listen ( port ) ;
150- return console . log ( "Started HTTPS server..." ) ;
151- } ) ;
152- } ) ;
153- } ) ;
154- } ) ;
155- } ) ;
156- } ) ;
157- } ) ;
57+ if ( args . https ) {
58+ console . log ( "Starting HTTPS server..." ) ;
59+ httpsoptions = {
60+ key : readFileSync ( './key.key' ) ,
61+ cert : readFileSync ( './cert.cert' )
62+ } ;
63+ srv = createHttpsServer ( httpsoptions , route ) ;
15864 } else {
65+ console . log ( "Starting HTTP server..." ) ;
15966 srv = createServer ( route ) ;
160- srv . keepAliveTimeout = 30000 ;
161- srv . on ( 'error' , function ( e ) {
162- var ref1 ;
163- if ( ( ref1 = e . code ) === 'EADDRINUSE' || ref1 === 'EADDRNOTAVAIL' ) {
164- console . log ( e . code + ', retrying...' ) ;
165- return setTimeout ( function ( ) {
166- srv . close ( ) ;
167- return srv . listen ( port ) ;
168- } , 1000 ) ;
169- } else {
170- return console . error ( e ) ;
171- }
172- } ) ;
173- srv . listen ( port ) ;
17467 }
68+ srv . keepAliveTimeout = 30000 ;
69+ srv . on ( 'error' , function ( e ) {
70+ var ref ;
71+ if ( ( ref = e . code ) === 'EADDRINUSE' || ref === 'EADDRNOTAVAIL' ) {
72+ console . log ( e . code + ', retrying...' ) ;
73+ return setTimeout ( function ( ) {
74+ srv . close ( ) ;
75+ return srv . listen ( port ) ;
76+ } , 1000 ) ;
77+ } else {
78+ return console . error ( e ) ;
79+ }
80+ } ) ;
81+ srv . listen ( port ) ;
17582 return srv ;
17683} ;
0 commit comments