Skip to content

Synology DownloadStation fix #360

@HaverKhrone

Description

@HaverKhrone

Hi,

I updated SynologyWebUI.js to use synotoken instead of sid - according to
https://global.download.synology.com/download/Document/Software/DeveloperGuide/Os/DSM/All/enu/DSM_Login_Web_API_Guide_enu.pdf

Now it is working great!

Tip: you can create multiple accounts on Synology NAS - on every account in DownloadStation selecting different default location for your downloads,
then add accounts to RTA and in a result you will have in a context menu the choice where torrents will be downloaded.

Cheers

function handleResponse(server, data) {
	if (this.readyState == 4 && this.status == 200) {
		var json = JSON.parse(this.responseText)
		if (json.success) {
			RTA.displayResponse('Success', 'Torrent added successfully.')
		} else {
			RTA.displayResponse('Failure', "Server didn't accept data: " + JSON.stringify(this.responseText), true)
		}
	} else if (this.readyState == 4 && this.status != 200) {
		RTA.displayResponse(
			'Failure',
			'Server responded with an irregular HTTP error code:\n' + this.status + ': ' + this.responseText,
			true
		)
	}
}

RTA.clients.synologyAdder = function (server, torrentdata, torrentname) {
	var scheme = server.hostsecure ? 'https://' : 'http://'

	var xhr = new XMLHttpRequest()
	xhr.open(
		'GET',
		scheme +
			server.host +
			':' +
			server.port +
			'/webapi/entry.cgi?api=SYNO.API.Auth&version=6&method=login&account=' +
			server.login +
			'&passwd=' +
			server.password +
			'&enable_syno_token=yes',
		false
	)
	xhr.send(null)
	var synotoken
	var json = JSON.parse(xhr.response)
	if (json && json.data) {
		synotoken = json.data.synotoken
	} else {
		RTA.displayResponse('Failure', 'Problem getting the Synology SID. Is the configuration correct?', true)
	}

	if (torrentdata.substring(0, 7) == 'magnet:') {
		console.log('DATA: ' + torrentdata)
		console.log(
			'GET: ' +
				scheme +
				server.host +
				':' +
				server.port +
				'/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=2&method=create&SynoToken=' +
				synotoken +
				'&uri=' +
				encodeURIComponent(torrentdata)
		)
		var mxhr = new XMLHttpRequest()
		mxhr.open(
			'GET',
			scheme +
				server.host +
				':' +
				server.port +
				'/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=2&method=create&SynoToken=' +
				synotoken +
				'&uri=' +
				encodeURIComponent(torrentdata),
			true
		)
		mxhr.onreadystatechange = handleResponse
		mxhr.send(message)
	} else {
		var xhr = new XMLHttpRequest()
		xhr.open(
			'POST',
			scheme +
				server.host +
				':' +
				server.port +
				'/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=2&method=create&SynoToken=' +
				synotoken,
			true
		)
		xhr.onreadystatechange = handleResponse
		// mostly stolen from https://github.com/igstan/ajax-file-upload/blob/master/complex/uploader.js
		var boundary = 'AJAX-----------------------' + new Date().getTime()
		xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary)
		var message = '--' + boundary + '\r\n'
		message += 'Content-Disposition: form-data; name="api"\r\n\r\n'
		message += 'SYNO.DownloadStation.Task' + '\r\n'

		message += '--' + boundary + '\r\n'
		message += 'Content-Disposition: form-data; name="version"\r\n\r\n'
		message += '2' + '\r\n'

		message += '--' + boundary + '\r\n'
		message += 'Content-Disposition: form-data; name="method"\r\n\r\n'
		message += 'create' + '\r\n'

		message += '--' + boundary + '\r\n'
		message += 'Content-Disposition: form-data; name="_sid"\r\n\r\n'
		message += sid + '\r\n'

		message += '--' + boundary + '\r\n'
		message += 'Content-Disposition: form-data; name="file"; filename="' + torrentname + '"\r\n'
		message += 'Content-Type: application/octet-stream\r\n\r\n'
		message += torrentdata + '\r\n'

		message += '--' + boundary + '--\r\n'

		xhr.sendAsBinary(message)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions