11const http = require ( 'http' ) ;
22const express = require ( 'express' ) ;
3- const Server = require ( 'socket.io' ) ;
3+ const { Server } = require ( 'socket.io' ) ;
44const path = require ( 'path' ) ;
55const app = express ( ) ;
66const os = require ( 'os' ) ;
7- const { get } = require ( 'https' ) ;
87let interfaces = os . networkInterfaces ( ) ;
98let mainserver = true ;
109let addresses = [ ] ;
@@ -14,6 +13,7 @@ let password;
1413let dev ;
1514let appserver ;
1615let server ;
16+ let io ;
1717
1818function checkAuth ( authorization , passwordforserver ) {
1919 if ( ! authorization ) return false ;
@@ -91,14 +91,51 @@ function startserver() {
9191 }
9292 res . end ( '{ "users": ' + nofusers + " }" ) ;
9393 } ) ;
94+ app . post ( '/startstop' , ( req , res ) => {
95+ const reject = ( ) => {
96+ res . setHeader ( 'www-authenticate' , 'Basic' ) ;
97+ res . sendStatus ( 401 ) ;
98+ }
99+ if ( ! checkAuth ( req . headers . authorization , password ) ) {
100+ return reject ( ) ;
101+ }
102+ if ( req . body . function === "stop" ) {
103+ mainserver = false ;
104+ res . end ( 'true' ) ;
105+ stopnetplay ( ) ;
106+ } else {
107+ mainserver = true ;
108+ res . end ( 'true' ) ;
109+ startnetplay ( ) ;
110+ }
111+ } ) ;
94112 server . listen ( port || 3000 , '0.0.0.0' , ( ) => {
95- consolelog ( "Starting server on port " + ( port || 3000 ) + " with password " + password ) ;
96113 if ( appserver ) {
97114 process . send ( { function : 'url' , url : 'http://localhost:' + port } ) ;
98115 }
116+ if ( mainserver ) {
117+ startnetplay ( ) ;
118+ }
119+ } ) ;
120+ }
121+
122+ function startnetplay ( ) {
123+ consolelog ( "Starting Netplay" ) ;
124+ io = new Server ( server , {
125+ cors : {
126+ origin : "*" ,
127+ methods : [ "GET" , "POST" ] ,
128+ credentials : true
129+ }
99130 } ) ;
100131}
101132
133+ function stopnetplay ( ) {
134+ consolelog ( "Stopping Netplay" ) ;
135+ io . close ( ) ;
136+ startserver ( ) ;
137+ }
138+
102139function consolelog ( message ) {
103140 if ( dev ) {
104141 console . log ( message ) ;
@@ -113,6 +150,7 @@ process.on('message', function(m) {
113150 dev = m . dev ;
114151 appserver = m . app ;
115152 startserver ( ) ;
153+ consolelog ( "Starting server on port " + ( port || 3000 ) + " with password " + password ) ;
116154 } else if ( m . function == 'kill' ) {
117155 process . exit ( ) ;
118156 }
0 commit comments