#!/usr/bin/perl -w # # (c) 2022 Christian Birchinger # # Scan a barcode with Binary Eye on a mobile phone and send the result to a system # running this tiny embedded webserver. No additional apps on the phone required. # The desktop part only requires this script to run and basic tools like xclip, # xdotool and notify-send to be available. # # Version: 0.3 (Experimental / Development) # # WARNING: This script is currently highly insecure! Everyone that can connect to # port 5188 on your desktop will be able to trigger it. There is currently no auth # mechanism in place. # # Requirements: # - Desktop that can run this Perl script, xdotool, notify-send and xclip (tested: Linux) # - A phone with Binary Eye installed # - Mobile Web Browser (tested with Android Firefox) # # Usage: # - Start this script on desktop # - Connect to http://this_host:5188/ with the phone's browser (tested with Firefox) # - A large "Click to Scan" should appear, which will launch Binary Eye when clicked # - Binary Eye will send the result to this scripts own tiny webserver. use 5.005; use strict; use HTTP::Daemon; use HTTP::Status; use Net::Domain qw (hostfqdn); use URI::Escape qw (uri_unescape); my $do_clipboard = 1; my $do_typing = 0; my $do_stdout = 1; my $do_notify = 1; my $print_qrcode_url = 1; my $print_config = 1; my $http_port = 5188; my $http_servername = hostfqdn(); my $http_bindaddr = '0.0.0.0'; if ($do_typing && !check_exists_command('xdotool')) { print STDERR "xdotool not found\n"; exit 1; } if ($do_clipboard && !check_exists_command('xclip')) { print STDERR "xclip not found\n"; exit 1; } if ($do_notify && !check_exists_command('notify-send')) { print STDERR "notify-send not found\n"; exit 1; } if ($print_qrcode_url && !check_exists_command('qrencode')) { print STDERR "qrencode not found\n"; exit 1; } *STDOUT->autoflush(); my $daemon = HTTP::Daemon->new( LocalAddr => $http_servername, LocalPort => $http_port, ReuseAddr => 1 ) or die "Internal error: $!"; print "Connect to \033[1mhttp://${http_servername}:${http_port}/\033[0m with a browser on a device with Binary Eye installed.\n"; if ($print_qrcode_url) { print "\nThe following QR Code will also open the URL from above:\n\n"; system("qrencode -t ansiutf8 'http://${http_servername}:${http_port}/'"); print "\n"; } if ($print_config) { print_config() }; print "Press \033[1mCtrl+C\033[0m anytime to terminate this server.\n"; my $html = <<__EOF__; Scan with Binary Eye __EOF__ my $html_close = <<__EOF__; Closing... __EOF__ while (my $client = $daemon->accept) { $client->autoflush(); print "\nConnection from " . $client->peerhost . " established\n"; REQUEST: while (my $request = $client->get_request) { if ($request->method ne 'GET' ) { $client->send_error(RC_NOT_IMPLEMENTED); print "DEBUG: Wrong method\n"; next REQUEST; } (my $rawstr) = $request->uri =~ /^\/\?result=(.*)/; if ($rawstr) { $rawstr =~ s/\+/%20/g; my $str = uri_unescape($rawstr); $client->send_basic_header; $client->send_header( 'Cache-Control', 'no-cache' ); $client->send_header( 'Content-Type', 'text/html' ); print {$client} "\n" . $html_close; if ($do_stdout) { print_stdout($str, $client->peerhost); }; if ($do_notify) { notify_send($str); }; if ($do_clipboard) { copy_to_clip($str); }; if ($do_typing) { type_out($str); }; } else { $client->send_basic_header; $client->send_header( 'Cache-Control', 'no-cache' ); $client->send_header( 'Content-Type', 'text/html' ); print {$client} "\n" . $html; } $client->force_last_request; } $client->close; undef $client; } sub copy_to_clip { my ($s) = @_; open(OUT, "|-", "xclip -in"); print OUT "${s}\n"; close OUT; } sub type_out { my ($s) = @_; $s =~ s/\r//g; system("xdotool type '$s'"); system("xdotool key Return"); } sub notify_send { my ($s) = @_; system("notify-send -i 'edit-copy' 'Barcode from Binary Eye' '$s'"); } sub check_exists_command { my $c = `sh -c 'command -v $_[0]'`; return $c; } sub print_stdout { my ($s,$h) = @_; print "\n"; print "---[ Barcode result from ${h} ]"; print "-" x (52 - length($h)) . "\n"; print "${s}\n"; print "-" x 79 . "\n"; } sub print_config { $do_clipboard ? print "[✓]" : print "[ ]"; print " Set clipboard\n"; $do_typing ? print "[✓]" : print "[ ]"; print " Type out the the scan (emulate keyboard)\n"; $do_stdout ? print "[✓]" : print "[ ]"; print " Write scan to STDOUT\n"; $do_notify ? print "[✓]" : print "[ ]"; print " Display desktop notification of scan\n"; $print_qrcode_url ? print "[✓]" : print "[ ]"; print " Show QR code with URL\n"; $print_config ? print "[✓]" : print "[ ]"; print " Display this configuration summary\n"; print "\n"; }