-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-stub.sh
executable file
·128 lines (105 loc) · 3.11 KB
/
wp-stub.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
#!/bin/sh
#
# This is a self-contained shell script that can be copied to a remote machine.
# It has the minimum necessary stuff to interface with a local webpipe server.
# This way you can view remote files without installing stuff.
#
# NOTE: Uses /bin/sh, not /bin/bash. In theory this could run on BSD, although
#
# Copyright 2014 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd
# it hasn't been tested at all.
#
# TODO:
# init: create ~/webpipe/input and fifo?
# send: send a specific file
# sendloop: send files from ~/webpipe fifo
# print-events? Move it here they have inotify-tools installed
#
# How to get it to the remote machine:
#
# $ scp $(wp stub-path) user@example.org:bin
#
# Usage:
# ./wp-stub.sh <function name>
set -o nounset
log() {
echo 1>&2 "$@"
}
# minimal custom TNET encoder. Avoid dependencies on Python, C, etc.
tnetEncodeFile() {
local path="$1"
local hostname="$2"
local size="$3"
# only send filename, not full path. Later we could send it as a "comment".
local filename=$(basename $path)
# { file: example.txt, body: foobar }
local k1='8:filename,'
# $#s gets the length of s; should be portable
local v1="${#filename}:${filename},"
local meta="$k1$v1"
echo -n "${#meta}:$meta}"
echo -n "$size:"
cat $path
echo -n ,
}
sendHeader() {
# Send empty dictionary for now. Send other stream options here in the
# future.
echo -n '0:}'
}
# TODO:
# - send filename, optional hostname, optional file type as fields
# - send body as separate message
send() {
local base_dir=$1 # base dir for relative filenames
cd $base_dir
log "send: changed working directory to $base_dir"
# Add hostname prefix to every filename.
# TODO: make this a tag instead in the metadata message?
local hostname
hostname=$(hostname) # could be empty if this fails
sendHeader
while read path; do
local size
size=$(stat --printf '%s' $path) # stat should be portable?
local exit_code=$?
if test $exit_code -eq 0; then
tnetEncodeFile "$path" "$hostname" "$size"
else
log "stat error, ignoring $path"
fi
done
}
# TODO: This should be:
# $ ls | wps sink
# $ wps show foo.txt
sendfile() {
local path=$1
local size=$(stat --printf '%s' $path) # stat should be portable?
local hostname=$(hostname)
filename=$
tnetEncodeFile "$path" "$hostname" "$size"
}
show() {
local path=$1
sendfile "$path" | nc localhost 8987
}
# Example:
# ls | wp sink # txt file
# ps aux | wp sink ps # file type
sink() {
local ext=${1:-txt}
local basename=$$ # use PID for now.
# NOTE: weird sh quirk: ~ isn't expended if you do this all on one line?
local tempfile
tempfile=~/webpipe/input/$basename.$ext
cat > $tempfile
sendfile $tempfile | nc localhost 8987
# TODO: later, write filename FIFO ~/webpipe/input.
# That can go in ~/webpipe/sink or something.
# This is better because file system events are unreliable. > in bash seems
# to generate both close and close_write events.
}
"$@"