Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Aug 1, 2020
0 parents commit 1b15ba7
Show file tree
Hide file tree
Showing 22 changed files with 1,667 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.swp
*.zip

npm-debug.log
/node_modules/
/out/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Cheng Zhao

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# CrossClip

Sync clipboard's content across macOS/Linux/Windows.

Written in Node.js, with native UI powered by [the Yue library](https://github.com/yue/yue).

## How to use

* Download the software from [Releases page](https://github.com/yue/crossclip/releases).
* Run it, you will be asked to fill some information on first run, you should
at least change "key" to a unique string.
* Run the software on your other computers, and make sure the port/channel/key
are same.

<img width="685" src="https://user-images.githubusercontent.com/639601/89036101-0c59c480-d377-11ea-9e2c-43f58f3f45ff.png">

## Notes

* Only plain text are synchronized, there is currently no plan to implement file
copy/paste.
* The network part is implemented by broadcasting messages, so large text in
clipboard would fail to be sent. There is plan to rewrite the network code
with a proper P2P library to support sending large text.

## Contributions

I do not plan to spend too much time maintaining this project, so if you want to
add a major new feature, I would suggest forking this project instead of sending
a pull request, and I would be very happy to add a link to your fork here.

Bug reports and fixes would still be very much appreciated.

## Icon

The [application's icon](https://www.iconfinder.com/icons/2530830)
is designed by [BomSymbols](https://www.iconfinder.com/korawan_m).
Binary file added assets/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions azure-pipelines-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
steps:
- task: NodeTool@0
inputs:
versionSpec: 12.x

- bash: |
npm install
npm run dist
displayName: Build

- bash: |
BRANCH=$(Build.SourceBranch)
TAG=${BRANCH:10}
echo "##vso[task.setvariable variable=Name;isOutput=true]$TAG"
displayName: Get Tag Name
name: Tag
condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/')

- task: GithubRelease@0
displayName: Create GitHub Release
condition: startsWith(variables['Tag.Name'], 'v')
inputs:
gitHubConnection: GitHub Yue
repositoryName: yue/crossclip
action: Edit
tagSource: auto
tag: $(Tag.Name)
title: CrossClip $(Tag.name)
releaseNotesSource: input
releaseNotes: (placeholder)
assets: '*.zip'
assetUploadMode: replace
isDraft: true
addChangelog: false
30 changes: 30 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
trigger:
- refs/heads/*
- refs/pull/*/merge
- refs/tags/*

jobs:
- job: windows_build
displayName: 'Build for Windows'
pool:
vmImage: 'VS2017-Win2016'
steps:
- template: azure-pipelines-template.yml

- job: macos_build
displayName: 'Build for macOS'
pool:
vmImage: 'macOS-10.14'
steps:
- template: azure-pipelines-template.yml

- job: linux_build
displayName: 'Build for Linux'
pool:
vmImage: 'ubuntu-18.04'
steps:
- script: |
sudo apt-get update
sudo apt-get install -y libgtk-3-dev
displayName: Install dependencies
- template: azure-pipelines-template.yml
Binary file added build/icon.icns
Binary file not shown.
Binary file added build/icon.ico
Binary file not shown.
36 changes: 36 additions & 0 deletions lib/app-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const gui = require('gui')

const packageJson = require('../package.json')
const text = require('./text')

function setup(window) {
const menu = gui.MenuBar.create([
{
label: packageJson.build.productName,
submenu: [
{
label: text.quit,
onClick: () => global.app.quit()
}
]
},
{
label: text.edit,
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'select-all' },
]
}
])
if (process.platform == 'darwin')
gui.app.setApplicationMenu(menu)
else
window.setMenuBar(menu)
}

module.exports = {setup}
82 changes: 82 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const gui = require('gui')

const Broadcast = require('./broadcast')
const ClipboardSync = require('./clipboard-sync')
const Settings = require('./settings')
const SettingsUI = require('./settings-ui')
const Tray = require('./tray')

const log = require('./log')
const text = require('./text')
const appMenu = require('./app-menu')

class App {
constructor() {
this.tray = new Tray()
this.tray.setStatus(text.notStarted)
this.settings = new Settings()
this.clipboardSync = new ClipboardSync()
if (process.platform == 'darwin')
appMenu.setup()
}

async start() {
if (this.settings.config.firstRun)
await this.configForFirstTime()
this.broadcast = new Broadcast(this.settings.config)
try {
this.tray.setStatus(text.starting + '...')
await this.broadcast.start()
this.clipboardSync.setBroadcast(this.broadcast)
this.tray.setStatus(`${text.listening} ${this.broadcast.ip}:${this.broadcast.port}`)
} catch (error) {
log.write('Failed to start broadcast service:', error)
this.tray.setStatus('✗')
}
}

async quit() {
if (this.broadcast)
await this.broadcast.close()
this.tray.remove()
gui.MessageLoop.quit()
}

async editSettings() {
// Only one settings window allowed.
if (this.settingsUI) {
this.settingsUI.show()
return
}
// Read settings.
this.settingsUI = new SettingsUI(this.settings)
const result = await this.settingsUI.run()
this.settingsUI = null
if (!result)
return
// Destroy current broadcast service.
if (this.broadcast) {
this.tray.setStatus(text.closing + '...')
await this.broadcast.close()
}
// Apply new settings and start.
this.settings.set(result)
await this.start()
}

async configForFirstTime() {
this.settingsUI = new SettingsUI(this.settings)
const result = await this.settingsUI.runForFirstTime()
this.settingsUI = null
const config = {firstRun: false}
if (result)
Object.assign(config, result)
this.settings.set(config)
}

async showSettings() {
return result;
}
}

module.exports = App
104 changes: 104 additions & 0 deletions lib/broadcast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const events = require('events')
const crypto = require('crypto')
const dgram = require('dgram')
const ip = require('ip')
const Bobolink = require('bobolink')

const packageJson = require('../package.json')
const log = require('./log')

class Broadcast extends events.EventEmitter {
constructor({port, channel, key}) {
super()
this.testMode = false
this.port = port
this.channel = channel
this.queue = new Bobolink({concurrency: 1})

this.key = crypto.createHmac('sha256', key).digest('hex').substr(0, 32)
this.iv = 'valar morghulis!'

this.ip = ip.address()
this.subnetMask = '255.255.255.0'
this.broadcastAddress = ip.subnet(this.ip, this.subnetMask).broadcastAddress

this.server = dgram.createSocket('udp4')
this.server.on('message', this.handleMessage.bind(this))

this.client = dgram.createSocket('udp4')
this.client.bind(() => this.client.setBroadcast(true))
}

async start() {
return new Promise((resolve, reject) => {
this.server.bind(this.port, (error) => {
if (error)
reject(error)
else
resolve()
})
})
}

async close() {
return new Promise((resolve) => {
this.server.close(() => {
this.client.close(() => resolve())
})
})
}

async send(message) {
const m = {
version: packageJson.version,
channel: this.channel,
message: this.encrypt(message),
}
return await this.queue.put(this._doSend.bind(this, JSON.stringify(m)))
}

async _doSend(message) {
return new Promise((resolve, reject) => {
this.client.send(message, this.port, this.broadcastAddress, (error) => {
if (error)
reject(error)
else
resolve()
})
})
}

handleMessage(msg, rinfo) {
if (!this.testMode && rinfo.address == this.ip)
return
try {
const json = JSON.parse(msg)
if (json.version != packageJson.version) {
log.write('Ignoring message from mismatched version:', json.version)
return
}
if (json.channel != this.channel) {
log.write('Ignoring message from mismatched channel:', json.channel)
return
}
this.emit('message', this.decrypt(json.message))
} catch (error) {
log.write('Error when handling message:', error)
}
}

encrypt(text) {
const cipher = crypto.createCipheriv('aes-256-cbc', this.key, this.iv)
const encrypted = cipher.update(text)
return Buffer.concat([encrypted, cipher.final()]).toString('hex')
}

decrypt(text) {
const encryptedText = Buffer.from(text, 'hex')
const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, this.iv)
const decrypted = decipher.update(encryptedText)
return Buffer.concat([decrypted, decipher.final()]).toString()
}
}

module.exports = Broadcast
Loading

0 comments on commit 1b15ba7

Please sign in to comment.