Skip to content

Instantly share code, notes, and snippets.

@sciurius
Created May 2, 2014 13:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sciurius/787e99af74132b62b397 to your computer and use it in GitHub Desktop.
Save sciurius/787e99af74132b62b397 to your computer and use it in GitHub Desktop.
Perl script to generate secrets and config data for BTsync
#!/usr/bin/perl -w
# Key generation for BTsync.
# Author : Johan Vromans
# Created On : Sun Apr 27 20:31:57 2014
# Last Modified By: Johan Vromans
# Last Modified On: Fri May 2 15:37:24 2014
# Update Count : 47
# Status : Unknown, Use with caution!
################ Common stuff ################
use strict;
# Package name.
my $my_package = 'Sciurix';
# Program name and version.
my ($my_name, $my_version) = qw( keygen-btsync 0.01 );
################ Command line parameters ################
use Getopt::Long 2.13;
# Command line options.
my $verbose = 0; # verbose processing
# Development options (not shown with -help).
my $debug = 0; # debugging
my $trace = 0; # trace (show process)
my $test = 0; # test mode.
# Process command line options.
app_options();
# Post-processing.
$trace |= ($debug || $test);
################ Presets ################
################ The Process ################
use Cwd qw( getcwd abs_path );
@ARGV = getcwd unless @ARGV;
foreach ( @ARGV ) {
my $cwd = abs_path($_);
# Asecret is the normal RW access key.
chomp( my $Asecret = `btsync --generate-secret` );
# Bsecret is the normal RO access key.
chomp( my $Bsecret = `btsync --get-ro-secret $Asecret` );
# Dsecret is like Asecret, but turns on encryption.
# http://forum.bittorrent.com/topic/25823-generate-encrypted-read-only-secret-without-api-key/
( my $Dsecret = $Asecret ) =~ s/^A/D/;
# Esecret is the RO access key for an encrypted share. It includes the encryption key.
chomp( my $Esecret = `btsync --get-ro-secret $Dsecret` );
# Fsecret is the RO access key for an encryptes share without decryption.
# You can share the files but you can not read the contents.
( my $Fsecret = substr( $Esecret, 0, 33 ) ) =~ s/^E/F/;
# Dump secrets.
print STDOUT ( " // RW, normal $Asecret\n",
" // RO, normal $Bsecret\n",
" // RW, encrypt $Dsecret\n",
" // RO, decrypt $Esecret\n",
" // RO, secret $Fsecret\n",
);
die("Keygen error!\n")
if $Asecret eq $Dsecret
|| $Fsecret eq substr( $Esecret, 0, 33 );
# Dump a sample entry for btsync.conf.
print STDOUT <<" EOD";
{
"secret" : "$Asecret",
"dir" : "$cwd",
"use_relay_server" : false,
"use_tracker" : false,
"use_dht" : false,
"search_lan" : true,
"use_sync_trash" : true,
"known_hosts" :
[
"192.168.1.16:58291"
]
},
EOD
}
################ Subroutines ################
sub app_options {
my $help = 0; # handled locally
my $ident = 0; # handled locally
my $man = 0; # handled locally
my $pod2usage = sub {
# Load Pod::Usage only if needed.
require Pod::Usage;
Pod::Usage->import;
&pod2usage;
};
# Process options.
if ( @ARGV > 0 ) {
GetOptions('ident' => \$ident,
'verbose' => \$verbose,
'trace' => \$trace,
'help|?' => \$help,
'man' => \$man,
'debug' => \$debug)
or $pod2usage->(2);
}
if ( $ident or $help or $man ) {
print STDERR ("This is $my_package [$my_name $my_version]\n");
}
if ( $man or $help ) {
$pod2usage->(1) if $help;
$pod2usage->(VERBOSE => 2) if $man;
}
}
__END__
################ Documentation ################
=head1 NAME
keygen-btsync - generate secrets and config data for BTsync
=head1 SYNOPSIS
keygen-btsync [options] [ path ... ]
Options:
--ident show identification
--help brief help message
--man full documentation
--verbose verbose information
=head1 OPTIONS
=over 8
=item B<--help>
Print a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=item B<--ident>
Prints program identification.
=item B<--verbose>
More verbose information.
=item I<path>
The path(s) to generate keys for.
=back
=head1 DESCRIPTION
B<keygen-btsync> will generate keys (secrets) and a sample config
entry for each of the paths passed on the command line. If no paths
are supplied it uses the current directory.
The output is intended to be cut, modified and pasted into your btsync
configuration file. It will be similar to:
// RW, normal ASOKXDMYPKQPEOZTLQSQSIRNSJZUS6AYC
// RO, normal BA7K2Y6WR4NQDHBJ5DJNXCTNKMU6SDLN3
// RW, encrypt DSOKXDMYPKQPEOZTLQSQSIRNSJZUS6AYC
// RO, decrypt EO4VWGPVX2EZ37L275ZBOF3RTJG4OCJYOWFUKWJUDUVDA7RHT7DEFC4D5H4
// RO, secret FO4VWGPVX2EZ37L275ZBOF3RTJG4OCJYO
{
"secret" : "ASOKXDMYPKQPEOZTLQSQSIRNSJZUS6AYC",
"dir" : "/home/jv/src/videotools",
"use_relay_server" : false,
"use_tracker" : false,
"use_dht" : false,
"search_lan" : true,
"use_sync_trash" : true,
"known_hosts" :
[
"192.168.1.16:58291"
]
},
The first two secrets are the normal read-write and read-only secrets.
For the meaning of the encrypted secrets, see
http://forum.bittorrent.com/topic/25823-generate-encrypted-read-only-secret-without-api-key/
=head1 AUTHOR
Johan Vromans <jvromans@squirrel.nl>
=head1 COPYRIGHT
This programs is Copyright 2014, Squirrel Consultancy.
This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License or the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment