11var request = require ( 'request' ) ;
22import { Channel } from './channel' ;
33import { Log } from './../log' ;
4+ var url = require ( 'url' ) ;
45
56export class PrivateChannel {
67 /**
@@ -26,7 +27,7 @@ export class PrivateChannel {
2627 */
2728 authenticate ( socket : any , data : any ) : Promise < any > {
2829 let options = {
29- url : this . authHost ( ) + this . options . authEndpoint ,
30+ url : this . authHost ( socket ) + this . options . authEndpoint ,
3031 form : { channel_name : data . channel } ,
3132 headers : ( data . auth && data . auth . headers ) ? data . auth . headers : { } ,
3233 rejectUnauthorized : false
@@ -40,9 +41,26 @@ export class PrivateChannel {
4041 *
4142 * @return {string }
4243 */
43- protected authHost ( ) : string {
44- return ( this . options . authHost ) ?
44+ protected authHost ( socket : any ) : string {
45+ let referer : Object = url . parse ( socket . request . headers . referer ) ;
46+ let authHostSelected : string = 'http://localhost' ;
47+ let authHosts : any = ( this . options . authHost ) ?
4548 this . options . authHost : this . options . host ;
49+
50+ if ( typeof authHosts === "string" )
51+ authHosts = [ authHosts ] ;
52+
53+ for ( let authHost of authHosts )
54+ {
55+ authHostSelected = authHost ;
56+ if ( referer . hostname . substr ( referer . hostname . indexOf ( '.' ) ) === authHostSelected || referer . protocol + "//" + referer . host === authHostSelected || referer . host === authHostSelected )
57+ {
58+ authHostSelected = referer . protocol + "//" + referer . host ;
59+ break ;
60+ }
61+ }
62+
63+ return authHostSelected ;
4664 }
4765
4866 /**
0 commit comments