Set User-Agent or Referrer header

Hi There,

 

I'm currently developing a channel for AnimeFTW which I have now working all the way to the point the videos are requested. The videos are currently restricted by User-Agent or Referrer.

 

The videos play when using the channel in iOS or Android as the User-Agent has a string of "Lavf" and the web server has already been updated to accept that string for user agent, meaning that the videos do not return a 403 when requested.

 

However the user agent is generic when I request from PHT or Web Browser.

 

The following is the output from the web server log files

Web Interface
###.###.###.### - - [29/Dec/2014:15:59:36 -0600] "GET /11eyes/eyes_11_ns.mkv HTTP/1.1" 403 214 "http://192.168.178.38:32400/manage/index.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"

Android
###.###.###.### - - [29/Dec/2014:16:08:38 -0600] “GET /11eyes/eyes_11_ns.mkv HTTP/1.1” 206 137381142 “-” “Lavf/55.44.100”

iPad
###.###.###.### - - [29/Dec/2014:16:10:04 -0600] “GET /11eyes/eyes_11_ns.mkv HTTP/1.1” 206 137381142 “-” “Lavf/55.44.100”

PHT
###.###.###.### - - [29/Dec/2014:16:12:28 -0600] “GET /11eyes/eyes_11_ns.mkv HTTP/1.1” 403 214 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17”

Is there a way to control the user-agent that is sent to the remote AnimeFTW server. I don't think there is anything we can do for the web interface as the request is being initiated from the web browser itself. However, is there a way to set the user-agent for PHT from within my channel?

 

I've tried to use Http.Headers in Start method to set default values but that didn't seem to make a difference. I would also expect this to affect all clients (other than web)

def Start():
HTTP.Headers['User-Agent'] = 'plex-animeftw'

thanks in advance

By using an indirect response, custom user agent can be set, see for example the NBC service:

https://github.com/plexinc-plugins/Services.bundle/blob/master/Contents/Service%20Sets/com.plexapp.plugins.nbc/URL/NBC/ServiceCode.pys#L166

Just replace the cookie parameter with user_agent

Thank you. I took a quick look. Does it mean I will need to create a service for it?

No, a service is not required :)

You could try this in the Start function:

def Start():
    ObjectContainer.user_agent = 'plex-animeftw'

or if that doesn't work you could try adding the user_agent parameter in the corresponding "CreateVideoClipObject" function:

(corresponds to the indirect response)

....
if include_container:
    oc = ObjectContainer(user_agent = 'plex-animeftw')
    oc.add([vco])

    return oc
else:
    return vco

https://dev.plexapp.com/docs/api/objectkit.html#ObjectContainer

The iOS client uses an user agent something like "AppleCoreMedia.." so "Lafv" is actually PMS(when doing transcoding).

The playback might also fail for those clients(iOS + Android) if the direct play settings are set high enough and depending on which streams that are used(i.e. if the streams are direct play compatible) ...  

Hi Meo,

Thanks for the suggestion. I had missed that setting on the ObjectContainer when looking at the APIs.

I finally had a chance to give this a try and it looks like if I use either method, the user agent is being sent when I browse the channel using an android client. But when I use PHT, it is ignoring and still sending the following user agent

xxx.xxx.xxx.xxx - - [11/Jan/2015:15:09:56 -0600] "GET /11eyes/eyes_1_ns.mp4 HTTP/1.1" 403 213 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"

Any ideas? Is this a bug in PHT?

cheers

No, a service is not required :)

You could try this in the Start function:

def Start():
    ObjectContainer.user_agent = 'plex-animeftw'

or if that doesn't work you could try adding the user_agent parameter in the corresponding "CreateVideoClipObject" function:

(corresponds to the indirect response)

....
if include_container:
    oc = ObjectContainer(user_agent = 'plex-animeftw')
    oc.add([vco])

    return oc
else:
    return vco

https://dev.plexapp.com/docs/api/objectkit.html#ObjectContainer

I don't think this is a bug in PHT, I think it's by design.  When you override the headers you only do it for the actual python code running on PMS.  Once you hand off a URL to a client (any client) the Plex channel framework is completely out of the loop and won't have a way to tell clients to spoof headers.

Thank you for the response Gerk but that doesn’t seem to make sense. the Android client was sending the custom user agent header while pht was not sending it

Thank you for the response Gerk but that doesn't seem to make sense. the Android client was sending the custom user agent header while pht was not sending it

I've found that different clients handle things differently. The only "workaround" that has worked for me is to let the Plex Media Server transcode everything by setting optimized_for_streaming to False (example).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.