@@ -149,6 +149,7 @@ typedef struct janus_http_msg {
149
149
char * acro ; /* Value of the Origin HTTP header, if any (needed for CORS) */
150
150
char * acrh ; /* Value of the Access-Control-Request-Headers HTTP header, if any (needed for CORS) */
151
151
char * acrm ; /* Value of the Access-Control-Request-Method HTTP header, if any (needed for CORS) */
152
+ char * xff ; /* Value of the X-Forwarded-For HTTP header, if any */
152
153
char * contenttype ; /* Content-Type of the payload */
153
154
char * payload ; /* Payload of the message */
154
155
size_t len ; /* Length of the message in octets */
@@ -173,6 +174,7 @@ static void janus_http_msg_free(const janus_refcount *msg_ref) {
173
174
g_free (request -> acro );
174
175
g_free (request -> acrh );
175
176
g_free (request -> acrm );
177
+ g_free (request -> xff );
176
178
g_free (request -> response );
177
179
g_free (request );
178
180
}
@@ -303,6 +305,7 @@ static gboolean enforce_cors = FALSE;
303
305
304
306
/* REST and Admin/Monitor ACL list */
305
307
static GList * janus_http_access_list = NULL , * janus_http_admin_access_list = NULL ;
308
+ static gboolean janus_http_check_xff = FALSE, janus_http_admin_check_xff = FALSE;
306
309
static janus_mutex access_list_mutex ;
307
310
static void janus_http_allow_address (const char * ip , gboolean admin ) {
308
311
if (ip == NULL )
@@ -736,6 +739,10 @@ int janus_http_init(janus_transport_callbacks *callback, const char *config_path
736
739
}
737
740
g_strfreev (list );
738
741
list = NULL ;
742
+ /* Check if we should use the value of X-Forwarded-For for checks too */
743
+ item = janus_config_get (config , config_general , janus_config_type_item , "acl_forwarded" );
744
+ if (item && item -> value )
745
+ janus_http_check_xff = janus_is_true (item -> value );
739
746
}
740
747
item = janus_config_get (config , config_admin , janus_config_type_item , "admin_acl" );
741
748
if (item && item -> value ) {
@@ -754,6 +761,10 @@ int janus_http_init(janus_transport_callbacks *callback, const char *config_path
754
761
}
755
762
g_strfreev (list );
756
763
list = NULL ;
764
+ /* Check if we should use the value of X-Forwarded-For for checks too */
765
+ item = janus_config_get (config , config_general , janus_config_type_item , "admin_acl_forwarded" );
766
+ if (item && item -> value )
767
+ janus_http_admin_check_xff = janus_is_true (item -> value );
757
768
}
758
769
759
770
/* Any custom value for the Access-Control-Allow-Origin header? */
@@ -1375,6 +1386,13 @@ static MHD_Result janus_http_handler(void *cls, struct MHD_Connection *connectio
1375
1386
janus_mutex_unlock (& messages_mutex );
1376
1387
* ptr = ts ;
1377
1388
MHD_get_connection_values (connection , MHD_HEADER_KIND , & janus_http_headers , msg );
1389
+ if (janus_http_check_xff && msg -> xff ) {
1390
+ /* Any access limitation based on this IP address? */
1391
+ if (!janus_http_is_allowed (msg -> xff , FALSE)) {
1392
+ JANUS_LOG (LOG_ERR , "IP %s is unauthorized to connect to the Janus API interface\n" , msg -> xff );
1393
+ return MHD_NO ;
1394
+ }
1395
+ }
1378
1396
ret = MHD_YES ;
1379
1397
/* Notify handlers about this new transport instance */
1380
1398
if (notify_events && gateway -> events_is_enabled ()) {
@@ -1773,6 +1791,13 @@ static MHD_Result janus_http_admin_handler(void *cls, struct MHD_Connection *con
1773
1791
janus_mutex_unlock (& messages_mutex );
1774
1792
* ptr = ts ;
1775
1793
MHD_get_connection_values (connection , MHD_HEADER_KIND , & janus_http_headers , msg );
1794
+ if (janus_http_admin_check_xff && msg -> xff ) {
1795
+ /* Any access limitation based on this IP address? */
1796
+ if (!janus_http_is_allowed (msg -> xff , TRUE)) {
1797
+ JANUS_LOG (LOG_ERR , "IP %s is unauthorized to connect to the Janus API interface\n" , msg -> xff );
1798
+ return MHD_NO ;
1799
+ }
1800
+ }
1776
1801
ret = MHD_YES ;
1777
1802
/* Notify handlers about this new transport instance */
1778
1803
if (notify_events && gateway -> events_is_enabled ()) {
@@ -2011,6 +2036,8 @@ static MHD_Result janus_http_headers(void *cls, enum MHD_ValueKind kind, const c
2011
2036
request -> acrm = g_strdup (value );
2012
2037
} else if (!strcasecmp (key , "Access-Control-Request-Headers" )) {
2013
2038
request -> acrh = g_strdup (value );
2039
+ } else if (!strcasecmp (key , "X-Forwarded-For" )) {
2040
+ request -> xff = g_strdup (value );
2014
2041
}
2015
2042
janus_refcount_decrease (& request -> ref );
2016
2043
return MHD_YES ;
0 commit comments