Created
October 7, 2025 00:07
-
-
Save danny8376/2299c3a00587cf22dfc939f74bad4a4d to your computer and use it in GitHub Desktop.
Crude Clipboard Syncing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| PORT=42325 | |
| WORKDIR=/dev/shm/clipsync | |
| LOG=true | |
| RUN=true | |
| mkdir -p $WORKDIR | |
| cleanup() { | |
| RUN=false | |
| kill $(jobs -p) 2>/dev/null | |
| } | |
| log() { | |
| [ "$LOG" = true ] && echo $@ >&2 | |
| } | |
| getclip() { | |
| xclip -selection clipboard -o | |
| #wl-paste -n | |
| # wl-paste cause annoying flashing in gnome... | |
| } | |
| putclip() { | |
| #xclip -selection clipboard -i < $1 | |
| #xclip -selection primary -i < $1 | |
| wl-copy < $1 | |
| wl-copy -p < $1 | |
| } | |
| put() { | |
| cat > $WORKDIR/put-new | |
| getclip > $WORKDIR/put-old | |
| #diff $WORKDIR/put-new $WORKDIR/put-old >/dev/null 2>/dev/null | |
| diff $WORKDIR/put-new $WORKDIR/put-old | |
| if [ $? = 1 ]; then | |
| putclip $WORKDIR/put-new | |
| fi | |
| rm $WORKDIR/put-new $WORKDIR/put-old | |
| } | |
| check() { | |
| if [ ! -f $WORKDIR/check-old ]; then | |
| getclip > $WORKDIR/check-old | |
| return 1 | |
| else | |
| getclip > $WORKDIR/check-new | |
| diff $WORKDIR/check-new $WORKDIR/check-old >/dev/null 2>/dev/null | |
| [ $? = 1 ] | |
| res=$? | |
| mv $WORKDIR/check-new $WORKDIR/check-old | |
| return $res | |
| fi | |
| } | |
| case "$1" in | |
| conn) | |
| read req | |
| case "$req" in | |
| put) | |
| log "Clipboard Update received" | |
| put | |
| ;; | |
| esac | |
| ;; | |
| *) | |
| mainpid=$$ | |
| server="$1" | |
| if [ -z "$server" ]; then | |
| echo "please give server to connect." | |
| exit 1 | |
| fi | |
| if echo "$server" | grep -qv '\([0-9a-zA-Z\-_.]\+\|\[[0-9a-fA-F:]\+\]\):[0-9]\+$'; then | |
| server="$server:$PORT" | |
| fi | |
| trap cleanup INT TERM EXIT | |
| echo LISTENING@$PORT >&2 | |
| socat TCP-LISTEN:$PORT,pktinfo,fork EXEC:"$0 conn" & | |
| socatpid=$! | |
| while [ "$RUN" = true ]; do | |
| counter=x$counter | |
| if [ $(echo -n "$counter" | wc -c) -gt 10 ]; then | |
| echo "reg" | socat - "TCP:$server" 2>/dev/null & | |
| counter="" | |
| fi | |
| if check; then | |
| log "Clipboard Changed detected, sending update" | |
| (echo "sync" ; cat $WORKDIR/check-old) | socat - "TCP:$server" & | |
| fi | |
| sleep .5 | |
| done | |
| ;; | |
| esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| PORT=42325 | |
| WORKDIR=/run/clipsyncd | |
| LOG=true | |
| RUN=true | |
| mkdir -p $WORKDIR | |
| mkdir -p $WORKDIR/clients | |
| cleanup() { | |
| RUN=false | |
| kill $(jobs -p) 2>/dev/null | |
| rm $WORKDIR/pid | |
| } | |
| log() { | |
| [ "$LOG" = true ] && echo $@ >&2 | |
| } | |
| diff() { | |
| h1=$(sha256sum $1 | cut -d' ' -f1) | |
| h2=$(sha256sum $2 | cut -d' ' -f1) | |
| if [ -z "$h1" ] || [ -z "$h2" ]; then return 2; fi | |
| if [ "$h1" = "$h2" ]; then return 0 | |
| else return 1 | |
| fi | |
| } | |
| case "$1" in | |
| conn) | |
| read req | |
| case "$req" in | |
| reg) touch "$WORKDIR/clients/$SOCAT_PEERADDR" ;; | |
| sync) | |
| log "SYNC received" | |
| mv $WORKDIR/cliptmp $WORKDIR/cliptmp-old | |
| cat > $WORKDIR/cliptmp | |
| if [ -f $WORKDIR/cliptmp-old ]; then | |
| diff $WORKDIR/cliptmp $WORKDIR/cliptmp-old >/dev/null 2>/dev/null | |
| if [ $? = 0 ]; then | |
| log "Content unchanged, skip" | |
| exit 0 | |
| fi | |
| fi | |
| for cp in $(find $WORKDIR/clients -type f); do | |
| c=$(basename $cp) | |
| if [ "$c" = "$SOCAT_PEERADDR" ]; then continue; fi | |
| (echo "put" ; cat $WORKDIR/cliptmp) | socat - TCP:$c:$PORT & | |
| done | |
| ;; | |
| pull) cat $WORKDIR/cliptmp ;; | |
| esac | |
| ;; | |
| *) | |
| mainpid=$$ | |
| echo -n $mainpid > $WORKDIR/pid | |
| trap cleanup INT TERM EXIT | |
| echo LISTENING@$PORT >&2 | |
| socat TCP-LISTEN:$PORT,pktinfo,fork EXEC:"$0 conn" & | |
| socatpid=$! | |
| while [ "$RUN" = true ]; do | |
| find $WORKDIR/clients -type f -mmin +0 -print0 | xargs -r -0 rm | |
| sleep 1 | |
| done | |
| ;; | |
| esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh /etc/rc.common | |
| START=50 | |
| STOP=50 | |
| NAME=clipsyncd | |
| USER=clipsync | |
| GROUP=clipsync | |
| PIDFILE=/var/run/clipsyncd/pid | |
| WORKDIR=/var/run/clipsyncd | |
| BIN=/usr/bin/clipsyncd | |
| start() { | |
| echo Starting $NAME | |
| mkdir -p $WORKDIR | |
| chown $USER:$GROUP $WORKDIR | |
| start-stop-daemon -S -q -c $USER:$GROUP -p $PIDFILE -b -x $BIN | |
| } | |
| stop() { | |
| start-stop-daemon -K -q -p $PIDFILE | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment