8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
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
..
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
|
; simple nonce authentication is used but the
; clipboard is sent IN THE CLEAR
do read/text %ansi.b
verbose: no
readonly: no
port: "42069"
key: #{981A7879}
proto: context [
writecb: #{3E}
readcb: #{C5}
]
debug: func [x] [
if verbose [
x: either block? x [rejoin x] [x]
write stderr fmt [
................................................................................
debug "parsing arguments"
usage: does [
write stderr fmt [[(style hl)"usage:"]
" ./clip.b get <hostname> | send <hostname>" ]
]
flag: func [f /extern verbose readonly] [
debug ["found flag " i]
do select [
"-v" [ verbose: yes ]
"-q" [ verbose: no ]
"-r" [ readonly: yes ]
] f
]
set-clipboard: func[c] [ execute/in "xsel -bi" c ]
get-clipboard: func[/local] [
clip-out: ""
execute/out "xsel -bo" clip-out
clip-out
]
start-server: func[/local] [
debug ["serving clipboard from " hostname none ]
sock: open join "tcp://:" port
forever [
connection: read wait sock
nonce: rand-block 4
expect-key: (key-hash nonce key)
write connection nonce
debug [ "sent nonce " nonce ";"
................................................................................
write connection get-clipboard
]
eq? client-command proto/writecb [
new-clipboard: to-string client-body
debug ["got cb " new-clipboard]
set-clipboard new-clipboard
]
true [ write connection "bad command" ]
]
] [
debug "denied access"
write connection "access denied"
]
close connection
debug [ "closed connection" ]
]
]
send-clip: func[host] [
debug ["sending clipboard to " host ]
sock: open rejoin ["tcp://" host ":" port]
nonce: read wait sock
debug [ "got nonce " nonce ]
client-key: key-hash nonce key
debug [ "sending password " client-key ]
msg: rejoin [
client-key
proto/writecb
get-clipboard #{00}
]
write sock msg
]
get-clip: func[host] [
debug ["getting clipboard from " host ]
sock: open rejoin ["tcp://" host ":" port]
nonce: read wait sock
debug [ "got nonce " nonce ]
client-key: key-hash nonce key
debug [ "sending password " client-key ]
msg: rejoin [ client-key proto/readcb ]
write sock msg
clipboard: read wait sock
print ["server response: " to-string clipboard]
]
main: func [argv checked-flags] [
case [
none? argv [ usage ]
parse argv ["get" host: string!] [ get-clip host ]
parse argv ["send" host: string!] [ send-clip host ]
parse argv ["serve"] [ start-server ]
not checked-flags [
new-args: []
foreach i argv [
either eq? first i '-' [ flag i ] [
new-args: join new-args i ] ]
main new-args yes
]
true [ usage ]
]
]
main args no
|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
|
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
>
>
>
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
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
..
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
...
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
; simple nonce authentication is used but the
; clipboard is sent IN THE CLEAR
do read/text %ansi.b
verbose: no
readonly: no
to-stdout: no
port: "42069"
key: #{981A7879}
setter: case [
getenv "ANDROID_ROOT" "termux-clipboard-set"
true "xsel -bi"
]
getter: case [
getenv "ANDROID_ROOT" "termux-clipboard-get"
true "xsel -bo"
]
proto: context [
writecb: #{3E}
readcb: #{C5}
stopserver: #{69}
]
debug: func [x] [
if verbose [
x: either block? x [rejoin x] [x]
write stderr fmt [
................................................................................
debug "parsing arguments"
usage: does [
write stderr fmt [[(style hl)"usage:"]
" ./clip.b get <hostname> | send <hostname>" ]
]
flag: func [f /extern verbose readonly to-stdout] [
debug ["found flag " i]
do select [
"-v" [ verbose: yes ]
"-q" [ verbose: no ]
"-r" [ readonly: yes ]
"-o" [ to-stdout: yes ]
] f
]
set-clipboard: func[c] [
either to-stdout [ print c ] [
execute/in setter c
]
]
get-clipboard: func[/local] [
clip-out: ""
execute/out getter clip-out
clip-out
]
start-server: func[/local] [
debug ["serving clipboard from " hostname none ]
debug [{using setter "} setter {" and getter "} getter {"}]
sock: open join "tcp://:" port
forever [
connection: read wait sock
nonce: rand-block 4
expect-key: (key-hash nonce key)
write connection nonce
debug [ "sent nonce " nonce ";"
................................................................................
write connection get-clipboard
]
eq? client-command proto/writecb [
new-clipboard: to-string client-body
debug ["got cb " new-clipboard]
set-clipboard new-clipboard
]
eq? client-command proto/stopserver [break]
true [ write connection "bad command" ]
]
] [
debug "denied access"
write connection "access denied"
]
close connection
debug [ "closed connection" ]
]
]
send-clip: func[host] [
catch [
debug [{using getter "} getter {"}]
debug ["sending clipboard to " host ]
sock: open rejoin ["tcp://" host ":" port]
nonce: read wait sock
debug [ "got nonce " nonce ]
client-key: key-hash nonce key
debug [ "sending password " client-key ]
msg: rejoin [
client-key
proto/writecb
get-clipboard #{00}
]
write sock msg
close sock
true
]
]
get-clip: func[host] [
catch [
debug [{using setter "} setter {"}]
debug ["getting clipboard from " host ]
sock: open rejoin ["tcp://" host ":" port]
nonce: read wait sock
debug [ "got nonce " nonce ]
client-key: key-hash nonce key
debug [ "sending password " client-key ]
msg: rejoin [ client-key proto/readcb ]
write sock msg
clipboard: read wait sock
debug ["server response: " to-string clipboard]
set-clipboard to-string clipboard
close sock
true
]
]
stop-clip: func[host] [
catch [
debug ["stopping clipserv on " host ]
sock: open rejoin ["tcp://" host ":" port]
nonce: read wait sock
debug [ "got nonce " nonce ]
client-key: key-hash nonce key
debug [ "sending password " client-key ]
msg: rejoin [ client-key proto/stopserver ]
write sock msg
close sock
true
]
]
main: func [argv checked-flags] [
case [
none? argv [ usage ]
parse argv ["get" host: string!] [ get-clip host ]
parse argv ["send" host: string!] [ send-clip host ]
parse argv ["stop" host: string!] [ stop-clip host ]
parse argv ["serve"] [ start-server ]
not checked-flags [
new-args: []
foreach i argv [
either eq? first i '-' [ flag i ] [
new-args: join new-args i ] ]
main new-args yes
]
true [ usage ]
]
]
quit/return main args no
|