-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathtx-rx-secure.sh
executable file
·175 lines (143 loc) · 5.21 KB
/
tx-rx-secure.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
# use croc (https://github.com/schollz/croc) to securely send and receive file(s)
# via the use of single-use code phrase using a local-only relay by default
###############################################################################
# script options
set -o pipefail
shopt -s nocasematch
ENCODING="utf-8"
###############################################################################
# script variables
MODE=
SERVER=
PORTS="9009,9010,9011,9012,9013"
TOKEN=
LOCAL_RELAY=yes
RELAY_PID=
RELAY_OUTPUT_REDIR=/dev/null
OUTPUT_ARGS=()
VERBOSE_FLAG=
DEBUG_FLAG=
# don't let the environment variables croc normally uses get in our way
unset CROC_RELAY
unset CROC_RELAY6
unset CROC_PASS
unset SOCKS5_PROXY
###############################################################################
# show script usage
function help() {
echo -e "$(basename $0)\n"
echo -e "-v enable bash verbosity"
echo -e "-d enable croc debugging"
echo -e "-g \"global\" (don't use only local connections)"
echo -e "-p value relay port(s) (default: \"${PORTS}\")"
echo -e "\ntransmit mode:"
echo -e "-t enable transmit mode"
echo -e "-s value bind local IP"
echo -e "<post-options arguments> file(s) to transfer"
echo -e "\nreceive mode:"
echo -e "-s value local server IP or hostname"
echo -e "-r value one-time receive token"
echo -e "-o value output folder for file(s) received (default: .)"
exit 1
}
###############################################################################
# parse command-line parameters
while getopts 'vdgo:r:s:tp:' OPTION; do
case "$OPTION" in
v)
VERBOSE_FLAG="-v"
RELAY_OUTPUT_REDIR=/dev/stderr
set -x
;;
d)
DEBUG_FLAG="--debug"
RELAY_OUTPUT_REDIR=/dev/stderr
;;
g)
LOCAL_RELAY=
;;
o)
OUTPUT_ARGS=(--out "$OPTARG")
;;
r)
MODE=rx
TOKEN="$OPTARG"
;;
s)
SERVER="$OPTARG"
;;
t)
MODE=tx
;;
p)
PORTS="$OPTARG"
;;
?)
help >&2
exit 1;
;;
esac
done
shift "$(($OPTIND -1))"
# without croc what are we even doing here
if ! command -v croc >/dev/null 2>&1; then
echo "$(basename $0) requires croc (https://github.com/schollz/croc)" >&2
exit 1
fi
###############################################################################
# cleanup: kill croc relay process on exit, if we started one
function cleanup {
if [[ -n "${RELAY_PID}" ]] && kill -s 0 "${RELAY_PID}" >/dev/null 2>&1; then
[[ -n "${VERBOSE_FLAG}" ]] && echo "killing relay process ${RELAY_PID}"
kill "${RELAY_PID}" >/dev/null 2>&1
sleep 1
if kill -s 0 "${RELAY_PID}" >/dev/null 2>&1; then
sleep 5
[[ -n "${VERBOSE_FLAG}" ]] && echo "killing (-9) relay process ${RELAY_PID}"
kill -s -9 "${RELAY_PID}" >/dev/null 2>&1
fi
fi
}
###############################################################################
trap "cleanup" EXIT
RELAY_ARGS=()
CURVE_ARGS=(--curve siec)
HASH_ARGS=(--hash xxhash)
FIRST_PORT="$(echo "${PORTS}" | cut -d, -f1)"
if [[ -n "${PORTS}" ]] && [[ "${MODE}" == "tx" ]] && (( $# > 0 )); then
# we have ports defined, have requested transmit mode, and have been given file(s) to transmit
if [[ -n "${LOCAL_RELAY}" ]]; then
# we're using "local-only" mode, which means we need to be the relay ourselves
if [[ -z "${SERVER}" ]]; then
SERVER="0.0.0.0"
command -v ip >/dev/null 2>&1 && SEND_RELAY_IP=$(ip route get 255.255.255.255 2>/dev/null | grep -Po '(?<=src )(\d{1,3}.){4}' | sed "s/ //g")
else
SEND_RELAY_IP="${SERVER}"
fi
croc ${DEBUG_FLAG} "${CURVE_ARGS[@]}" relay --host "${SERVER}" --ports "${PORTS}" >${RELAY_OUTPUT_REDIR} &
RELAY_PID=$!
sleep 5
if [[ -n "${RELAY_PID}" ]] && kill -s 0 "${RELAY_PID}" >/dev/null 2>&1; then
[[ -n "${VERBOSE_FLAG}" ]] && echo "relay running at ${RELAY_PID}"
else
echo "Failed to start relay process" >&2
exit 1;
fi
elif [[ -n "${SERVER}" ]]; then
# we're not using local-only mode, they *can* but don't have to define a relay
SEND_RELAY_IP="${SERVER}"
fi
[[ -n "${SEND_RELAY_IP}" ]] && RELAY_ARGS=(--relay "${SEND_RELAY_IP}:${FIRST_PORT}")
# run croc
croc --yes --ignore-stdin --overwrite ${DEBUG_FLAG} "${CURVE_ARGS[@]}" "${RELAY_ARGS[@]}" send "${HASH_ARGS[@]}" "$@"
elif [[ -n "${PORTS}" ]] && ( [[ "${MODE}" == "rx" ]] && [[ -n "${TOKEN}" ]] && ( [[ -z "${LOCAL_RELAY}" ]] || [[ -n "${SERVER}" ]] ) ); then
# we have ports defined, have requested receive mode, have been given a token, and either have a relay IP or are not using a local relay
[[ -n "${SERVER}" ]] && RELAY_ARGS=(--relay "${SERVER}:${FIRST_PORT}")
# run croc
export CROC_SECRET="${TOKEN}"
croc --yes --ignore-stdin --overwrite ${DEBUG_FLAG} "${CURVE_ARGS[@]}" "${RELAY_ARGS[@]}" "${OUTPUT_ARGS[@]}"
else
help >&2
exit 1;
fi