1- const fetch = require ( 'node-fetch' ) ;
2- const WebSocket = require ( 'ws' ) ;
3- const { EventEmitter } = require ( 'events' )
4-
5- // https://dashflo.net/docs/api/pterodactyl/v1/
6-
7- class pterosocket extends EventEmitter {
8- constructor ( origin , api_key , server_no , auto_connect = true ) {
9- super ( ) ;
10- this . origin = origin ;
11- this . api_key = api_key ;
12- this . server_no = server_no ;
13- if ( auto_connect )
14- this . connect ( ) ;
15- }
16-
17- async get_new_login ( ) {
18- const response = await fetch ( `${ this . origin } /api/client/servers/${ this . server_no } /websocket` , {
19- "method" : "GET" ,
20- "headers" : {
21- "Accept" : "application/json" ,
22- "Content-Type" : "application/json" ,
23- "Authorization" : `Bearer ${ this . api_key } `
24- }
25- } ) . then ( ( data ) => { return data . json ( ) } ) ;
26- if ( "data" in response ) {
27- return response . data ;
28- }
29- }
30-
31- async #authLogin( token = "" ) {
32- if ( token )
33- this . write ( { "event" :"auth" , "args" :[ token ] } ) ;
34- else {
35- const login = await this . get_new_login ( ) ;
36- if ( ! login )
37- throw "Couldn't connect - server didn't provide credentials." ;
38- const { token} = login ;
39- this . write ( { "event" :"auth" , "args" :[ token ] } ) ;
40- }
41- }
42-
43- async #readPacket( packet ) {
44- const { event, args} = JSON . parse ( packet . toString ( 'utf8' ) ) ;
45- switch ( event ) {
46- //'auth success', 'status', 'console output', 'token expired'
47- case 'stats' :
48- if ( args [ 0 ] . startsWith ( "{" ) )
49- this . emit ( 'stats' , JSON . parse ( args [ 0 ] ) ) ; //avoinding parse error with args=["running"]
50- break ;
51- case 'token expiring' :
52- this . #authLogin( ) ;
53- this . emit ( event . replace ( " " , "_" ) ) ;
54- break ;
55- default :
56- if ( args )
57- this . emit ( event . replace ( " " , "_" ) , args [ 0 ] ) ;
58- else
59- this . emit ( event . replace ( " " , "_" ) ) ;
60- }
61- }
62-
63- async connect ( ) {
64- const login = await this . get_new_login ( ) ;
65- if ( ! login )
66- throw "Couldn't connect - server didn't provide credentials."
67- const { token, socket} = login ;
68-
69- this . ws = new WebSocket ( socket , {
70- origin : this . origin
71- } ) ;
72- this . ws . on ( 'open' , ( ) => {
73- this . #authLogin( token ) ;
74- this . emit ( 'start' ) ;
75- } ) ;
76- this . ws . on ( 'message' , ( data ) => {
77- this . #readPacket( data ) ;
78- } ) ;
79- this . ws . on ( 'error' , ( data ) => {
80- this . emit ( "error" , data ) ;
81- } ) ;
82- this . ws . on ( 'close' , ( data ) => {
83- this . emit ( "close" , data ) ;
84- this . ws = undefined ;
85- } ) ;
86- }
87-
88- close ( ) {
89- this . ws . close ( ) ;
90- this . ws = undefined ;
91- this . emit ( "close" , "Connection closed by user" ) ;
92- }
93-
94- write ( packet ) {
95- this . ws . send ( JSON . stringify ( packet ) ) ;
96- }
97-
98- writeCommand ( command_text ) {
99- this . write ( { "event" :"send command" , "args" :[ command_text ] } ) ;
100- }
101-
102- writePower ( command_text ) {
103- this . write ( { "event" :"set state" , "args" :[ command_text ] } )
104- }
105-
106-
107- }
108-
109- module . exports = {
110- pterosocket : pterosocket
1+ const fetch = require ( 'node-fetch' ) ;
2+ const WebSocket = require ( 'ws' ) ;
3+ const { EventEmitter } = require ( 'events' )
4+
5+ // https://dashflo.net/docs/api/pterodactyl/v1/
6+
7+ class pterosocket extends EventEmitter {
8+ constructor ( origin , api_key , server_no , auto_connect = true ) {
9+ super ( ) ;
10+ this . origin = origin ;
11+ this . api_key = api_key ;
12+ this . server_no = server_no ;
13+ if ( auto_connect )
14+ this . connect ( ) ;
15+ }
16+
17+ async get_new_login ( ) {
18+ const response = await fetch ( `${ this . origin } /api/client/servers/${ this . server_no } /websocket` , {
19+ "method" : "GET" ,
20+ "headers" : {
21+ "Accept" : "application/json" ,
22+ "Content-Type" : "application/json" ,
23+ "Authorization" : `Bearer ${ this . api_key } `
24+ }
25+ } ) . then ( ( data ) => { return data . json ( ) } ) ;
26+ if ( "data" in response ) {
27+ return response . data ;
28+ }
29+ }
30+
31+ async #authLogin( token = "" ) {
32+ if ( token )
33+ this . write ( { "event" :"auth" , "args" :[ token ] } ) ;
34+ else {
35+ const login = await this . get_new_login ( ) ;
36+ if ( ! login )
37+ throw "Couldn't connect - server didn't provide credentials." ;
38+ const { token} = login ;
39+ this . write ( { "event" :"auth" , "args" :[ token ] } ) ;
40+ }
41+ }
42+
43+ async #readPacket( packet ) {
44+ const { event, args} = JSON . parse ( packet . toString ( 'utf8' ) ) ;
45+ switch ( event ) {
46+ //'auth success', 'status', 'console output', 'token expired'
47+ case 'stats' :
48+ if ( args [ 0 ] . startsWith ( "{" ) )
49+ this . emit ( 'stats' , JSON . parse ( args [ 0 ] ) ) ; //avoinding parse error with args=["running"]
50+ break ;
51+ case 'token expiring' :
52+ this . #authLogin( ) ;
53+ this . emit ( event . replace ( " " , "_" ) ) ;
54+ break ;
55+ default :
56+ if ( args )
57+ this . emit ( event . replace ( " " , "_" ) , args [ 0 ] ) ;
58+ else
59+ this . emit ( event . replace ( " " , "_" ) ) ;
60+ }
61+ }
62+
63+ async connect ( ) {
64+ const login = await this . get_new_login ( ) ;
65+ if ( ! login )
66+ throw "Couldn't connect - server didn't provide credentials."
67+ const { token, socket} = login ;
68+
69+ this . ws = new WebSocket ( socket , {
70+ origin : this . origin
71+ } ) ;
72+ this . ws . on ( 'open' , ( ) => {
73+ this . #authLogin( token ) ;
74+ this . emit ( 'start' ) ;
75+ } ) ;
76+ this . ws . on ( 'message' , ( data ) => {
77+ this . #readPacket( data ) ;
78+ } ) ;
79+ this . ws . on ( 'error' , ( data ) => {
80+ this . emit ( "error" , data ) ;
81+ } ) ;
82+ this . ws . on ( 'close' , ( data ) => {
83+ this . emit ( "close" , data ) ;
84+ this . ws = undefined ;
85+ } ) ;
86+ }
87+
88+ close ( ) {
89+ this . ws . close ( ) ;
90+ this . ws = undefined ;
91+ this . emit ( "close" , "Connection closed by user" ) ;
92+ }
93+
94+ write ( packet ) {
95+ this . ws . send ( JSON . stringify ( packet ) ) ;
96+ }
97+
98+ writeCommand ( command_text ) {
99+ this . write ( { "event" :"send command" , "args" :[ command_text ] } ) ;
100+ }
101+
102+ writePower ( command_text ) {
103+ this . write ( { "event" :"set state" , "args" :[ command_text ] } )
104+ }
105+
106+
107+ }
108+
109+ module . exports = {
110+ pterosocket : pterosocket
111111}
0 commit comments