Skip to content

Commit

Permalink
New checkbox to allow for connections via SSL (#80)
Browse files Browse the repository at this point in the history
* feat: allow connection through ssl

* refactor: support SSL on http connections

* chore: bump minor version to 4.4.0
  • Loading branch information
Cinzya authored Sep 25, 2024
1 parent c39f0db commit 70d4e64
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion companion/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "getontime-ontime",
"shortname": "ontime",
"description": "Companion module for ontime",
"version": "4.3.0",
"version": "4.4.0",
"license": "MIT",
"repository": "git+https://github.com/bitfocus/companion-module-getontime-ontime.git",
"bugs": "https://github.com/bitfocus/companion-module-getontime-ontime/issues",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "getontime-ontime",
"version": "4.3.0",
"version": "4.4.0",
"main": "/dist/index.js",
"license": "MIT",
"prettier": "@companion-module/tools/.prettierrc.json",
Expand Down
13 changes: 11 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SomeCompanionConfigField, Regex } from '@companion-module/base'
export interface OntimeConfig {
host: string
port: string
ssl: boolean
version: string
refetchEvents: boolean
reconnect: boolean
Expand Down Expand Up @@ -38,20 +39,28 @@ export function GetConfigFields(): SomeCompanionConfigField[] {
regex: Regex.PORT,
tooltip: 'Ontime server port. Default is 4001',
},
{
label: 'Use SSL',
id: 'ssl',
type: 'checkbox',
default: false,
width: 2,
tooltip: 'Use SSL to connect to the Ontime server.',
},
{
label: 'Refetch events',
id: 'refetchEvents',
type: 'checkbox',
default: true,
width: 4,
width: 3,
tooltip: 'Whether Companion should keep the rundown updated with Ontime by refetching on change.',
},
{
label: 'Reconnect',
id: 'reconnect',
type: 'checkbox',
default: true,
width: 4,
width: 3,
tooltip: 'Chose if you want Companion to try to reconnect to ontime when the connection is lost.',
},
{
Expand Down
7 changes: 6 additions & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const defaultTimerObject = {
negative: '',
}

export function sanitizeHost(host: string) {
const pattern = /^((http|https):\/\/)/
return host.replace(pattern, '')
}

type SplitTime = typeof defaultTimerObject

export function msToSplitTime(time: number | null): SplitTime {
Expand Down Expand Up @@ -87,7 +92,7 @@ export function findPreviousPlayableEvent(ontime: OntimeV3): OntimeEvent | null
now = true
continue
}
if (now && !ontime.events[i].skip ) {
if (now && !ontime.events[i].skip) {
return ontime.events[i]
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/v3/connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InputValue, InstanceStatus } from '@companion-module/base'
import { OnTimeInstance } from '..'
import Websocket from 'ws'
import { findPreviousPlayableEvent, msToSplitTime } from '../utilities'
import { findPreviousPlayableEvent, msToSplitTime, sanitizeHost } from '../utilities'
import { feedbackId, variableId } from '../enums'
import {
CurrentBlockState,
Expand All @@ -26,7 +26,7 @@ export function connect(self: OnTimeInstance, ontime: OntimeV3): void {
reconnectInterval = self.config.reconnectInterval * 1000
shouldReconnect = self.config.reconnect

const host = self.config.host
const host = sanitizeHost(self.config.host)
const port = self.config.port

if (!host || !port) {
Expand All @@ -40,13 +40,9 @@ export function connect(self: OnTimeInstance, ontime: OntimeV3): void {
ws.close()
}

const pattern = /^((http|https):\/\/)/
const prefix = self.config.ssl ? 'wss' : 'ws'

if (pattern.test(host)) {
host.replace(pattern, '')
}

ws = new Websocket(`ws://${host}:${port}/ws`)
ws = new Websocket(`${prefix}://${host}:${port}/ws`)

ws.onopen = () => {
clearTimeout(reconnectionTimeout as NodeJS.Timeout)
Expand Down Expand Up @@ -322,9 +318,12 @@ export function socketSendJson(type: string, payload?: InputValue | object): voi
let rundownEtag: string = ''

export async function fetchAllEvents(self: OnTimeInstance, ontime: OntimeV3): Promise<void> {
const prefix = self.config.ssl ? 'https' : 'http'
const host = sanitizeHost(self.config.host)

self.log('debug', 'fetching events from ontime')
try {
const response = await fetch(`http://${self.config.host}:${self.config.port}/data/rundown`, {
const response = await fetch(`${prefix}://${host}:${self.config.port}/data/rundown`, {
method: 'GET',
headers: { Etag: rundownEtag },
})
Expand Down Expand Up @@ -352,13 +351,16 @@ let customFieldsEtag: string = ''
let customFieldsTimeout: NodeJS.Timeout

export async function fetchCustomFields(self: OnTimeInstance, ontime: OntimeV3): Promise<void> {
const prefix = self.config.ssl ? 'https' : 'http'
const host = sanitizeHost(self.config.host)

clearTimeout(customFieldsTimeout)
if (self.config.refetchEvents) {
customFieldsTimeout = setTimeout(() => fetchCustomFields(self, ontime), 60000)
}
self.log('debug', 'fetching custom-fields from ontime')
try {
const response = await fetch(`http://${self.config.host}:${self.config.port}/data/custom-fields`, {
const response = await fetch(`${prefix}://${host}:${self.config.port}/data/custom-fields`, {
method: 'GET',
headers: { Etag: customFieldsEtag },
})
Expand Down

0 comments on commit 70d4e64

Please sign in to comment.