Skip to content

Commit

Permalink
Set userAgent override for videoplayer webview
Browse files Browse the repository at this point in the history
This is used to get around websites useragent sniffing to detect compatibility (#66). Hopefully this can be dropped when #1 is resolved.
  • Loading branch information
samuelmaddock committed Feb 7, 2019
1 parent d85446e commit 13405e4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
12 changes: 6 additions & 6 deletions app/browser/extensions.ts
@@ -1,11 +1,12 @@
import fs from 'fs-extra'
import path from 'path'
import { app, session, componentUpdater, ipcMain, BrowserWindow } from 'electron'
import { app, componentUpdater, ipcMain, BrowserWindow } from 'electron'
import * as settings from 'electron-settings'
import log from './log'
import * as widevine from 'constants/widevine'
import { fileUrl } from 'utils/appUrl'
import * as walkdir from 'walkdir'
import { getMediaSession } from './session'

const extVerRegex = /^[\d._]+$/
const isExtVersion = (dirName: string) => !!extVerRegex.exec(dirName)
Expand Down Expand Up @@ -44,7 +45,6 @@ const disableExtension = (session: Electron.session, extId: string) => {
}

const getActiveExtensions = () => Array.from(activeExtensions)
const getSession = () => session.fromPartition('persist:mediaplayer', { cache: true })

const APP_EXTENSIONS = new Set([
'dfmpchfgfkhhkigicpheeacmlkbomihe' /*enhanced-media-viewer*/,
Expand All @@ -63,7 +63,7 @@ export function initExtensions() {
loadComponents()
}

const mediaSession = getSession()
const mediaSession = getMediaSession()
loadMediaExtensions(mediaSession)
loadVendorExtensions(mediaSession)
initIpc(mediaSession)
Expand All @@ -84,7 +84,7 @@ function initProcessListeners() {
})

if (isVendorExtension(info) && !activeExtensions.has(info.id)) {
disableExtension(getSession(), info.id)
disableExtension(getMediaSession(), info.id)
}
})

Expand Down Expand Up @@ -264,7 +264,7 @@ function ipcError(sender: Electron.WebContents, err: Error) {

async function ipcSet(event: Electron.Event, extId: string, enable: boolean) {
log.debug(`[Extension] Setting extension ${extId} to ${enable}`)
const session = getSession()
const session = getMediaSession()
if (enable) {
enableExtension(session, extId)
} else {
Expand All @@ -278,7 +278,7 @@ function ipcStatus(event: Electron.Event) {
}

function ipcReload(event: Electron.Event) {
loadVendorExtensions(getSession())
loadVendorExtensions(getMediaSession())
onExtensionsChange()
}

Expand Down
11 changes: 11 additions & 0 deletions app/browser/session.ts
@@ -0,0 +1,11 @@
import { session } from 'electron'
import { WEBVIEW_PARTITION } from 'constants/http'

let mediaSession: Electron.Session

export const getMediaSession = () => {
if (!mediaSession) {
mediaSession = session.fromPartition(WEBVIEW_PARTITION, { cache: true })
}
return mediaSession
}
7 changes: 7 additions & 0 deletions app/constants/http.ts
Expand Up @@ -9,6 +9,13 @@ const defaultUserAgent =
const userAgent = typeof navigator !== 'undefined' ? navigator.userAgent : defaultUserAgent
export const MEDIA_USER_AGENT = `${userAgent} (Googlebot)`

/**
* User agent override to prevent sniffing from breaking apps until
* issue #1 can be resolved.
*/
export const MEDIA_SESSION_USER_AGENT =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36'

export const APP_WEBSITE = 'https://getmetastream.com/'
export const MEDIA_REFERRER = APP_WEBSITE

Expand Down
2 changes: 0 additions & 2 deletions app/renderer/components/browser/WebBrowser.tsx
Expand Up @@ -11,8 +11,6 @@ import { IReactReduxProps } from 'types/redux-thunk'
const { ipcRenderer, remote } = chrome

const DEFAULT_URL = absoluteUrl('./browser/resources/homescreen.html')
// const DEFAULT_URL = 'https://www.google.com/';
// const DEFAULT_URL = 'data:text/html,<style>html{color:#fff;font-size:36px}</style>B R O W S E R';

interface IProps {
className?: string
Expand Down
7 changes: 5 additions & 2 deletions app/renderer/components/lobby/VideoPlayer.tsx
Expand Up @@ -14,7 +14,7 @@ import { DispatchProp, connect } from 'react-redux'
import { PlaybackControls } from 'renderer/components/media/PlaybackControls'
import { setVolume } from 'renderer/actions/settings'
import { clamp } from 'utils/math'
import { WEBVIEW_PARTITION, MEDIA_REFERRER } from 'constants/http'
import { WEBVIEW_PARTITION, MEDIA_REFERRER, MEDIA_SESSION_USER_AGENT } from 'constants/http'
import { absoluteUrl } from 'utils/appUrl'
import { IAppState } from 'renderer/reducers'
import { getPlaybackTime2 } from 'renderer/lobby/reducers/mediaPlayer.helpers'
Expand Down Expand Up @@ -310,7 +310,10 @@ class _VideoPlayer extends PureComponent<PrivateProps, IState> {
// this.updatePlayback(PlaybackState.Paused);

if (this.webContents) {
this.webContents.loadURL(this.mediaUrl, { httpReferrer: this.httpReferrer })
this.webContents.loadURL(this.mediaUrl, {
httpReferrer: this.httpReferrer,
userAgent: MEDIA_SESSION_USER_AGENT
})
}

ipcRenderer.send('media-cleanup')
Expand Down

0 comments on commit 13405e4

Please sign in to comment.