-
Notifications
You must be signed in to change notification settings - Fork 2
/
irs
executable file
·306 lines (241 loc) · 7.67 KB
/
irs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/ruby
if !File.exist? "config.rb"
puts "ERROR: You must copy config.sample.rb to config.rb and enter your ssh details.\n"
exit
end
require 'rubygems'
require 'dnssd'
require 'timeout'
require 'config.rb'
class RemoteSync
def initialize(mode=nil)
if $ssh_hostname == nil
puts "ERROR: You must set $ssh_hostname in config.rb.\n"
exit
end
if $ssh_username == nil
puts "ERROR: You must set $ssh_username in config.rb.\n"
exit
end
if $ssh_port == nil
puts "ERROR: You must set $ssh_port in config.rb.\n"
exit
end
end
def usage
puts "\n==========================================================="
puts "iTunes Remote Sync v1.0 by m4rkw - http://twitter.com/m4rkw"
puts "==========================================================="
puts "\nUsage:\n\n"
puts "irs dump\n\n"
puts " - dumps out idevices and itunes libraries detected on the local network. use this to find the names"
puts " - of your idevice and itunes library that you want to sync.\n\n"
puts "irs sync\n\n"
puts " - establishes Bonjour proxies allowing your iDevice to appear in iTunes.\n\n"
puts "irs off\n\n"
puts " - removes local and remote Bonjour proxies.\n\n"
end
def dump
puts "\nLooking for iDevices on local network ... \n\n"
self.browse("_apple-mobdev._tcp").each { |i|
puts "#{i[:name]} (#{i[:ip]}:#{i[:port]})"
}
puts "\nLooking for iTunes libraries on local network ... \n\n"
self.browse("_atc._tcp").each { |i|
puts "#{i[:name]} (#{i[:ip]}:#{i[:port]})"
}
puts "\n"
end
def print(msg)
printf("#{msg}")
STDOUT.flush
end
def rcmd(cmd)
`ssh -p #{$ssh_port} #{$ssh_username}@#{$ssh_hostname} -o PasswordAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "#{cmd}" 2>/dev/null`
end
def rkill(ps)
resp = self.rcmd "ps axww |grep '#{ps}' |grep -v grep"
tokill = '';
resp.lines.to_a.each { |line|
if m = line.match(/^[\s\t]*?([0-9]+)[\s\t]/)
tokill += "#{m[1]} "
end
}
self.rcmd "kill -9 #{tokill}"
end
def sync
res = `ps auxww |grep 'SCREEN -dmS -tirs_dnssd dns-sd -P ' |grep -v grep`
if res.length != 0
puts "It looks like you already have Bonjour proxies set up.\n\n"
puts "You should run \"irs off\" to clean these up before trying to establish new ones.\n"
exit
end
self.print "Querying remote iTunes server ... "
if $itunes_library_name != nil
name = $itunes_library_name
else
self.rcmd "dns-sd -B _atc._tcp >/tmp/.irs_itunes_lookup\&"
sleep $dns_sd_delay
self.rkill "dns-sd -B _atc._tcp"
resp = self.rcmd "cat /tmp/.irs_itunes_lookup; rm -f /tmp/.irs_itunes_lookup"
if m = resp.match(/^[0-9]+:[0-9]+:[0-9]+\.[0-9]+[\s\t]+[a-zA-Z]+[\s\t]+[0-9]+[\s\t]+[0-9]+[\s\t]+([a-zA-Z0-9\._]+)[\s\t]+([a-zA-Z0-9\._\-]+)[\s\t]+([a-zA-Z0-9\._\-]+)$/)
name = m[3]
else
puts "failed\n\nUnable to find remote iTunes library (are you sure iTunes is running?)\n"
exit
end
end
self.rcmd "dns-sd -L #{name} _atc._tcp. >/tmp/.irs_itunes_lookup\&"
sleep $dns_sd_delay
self.rkill "dns-sd -L #{name} _atc._tcp."
resp = self.rcmd "cat /tmp/.irs_itunes_lookup; rm -f /tmp/.irs_itunes_lookup"
if m = resp.match(/can be reached at (.*?)\.:([0-9]+) /)
host = m[1]
port = m[2]
else
puts "failed\n\nUnable to resolve remote iTunes library via dns-sd -L.\n"
exit
end
if m = resp.match(/libid=([0-9A-F]+)/)
libid = m[1]
else
puts "failed\n\nUnable to discover remote itunes library-id.\n"
exit
end
resp = self.rcmd "/sbin/ping -c1 #{host} |head -n1"
if m = resp.match(/^PING .*? \(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\)/)
ip = m[1]
else
puts "failed\n\nUnable to resolve #{host} at the remote host.\n"
exit
end
resp = self.rcmd "/sbin/ifconfig |egrep -A1 '^ppp[0-9]+:' |head -n2 |tail -n1"
if m = resp.match(/^[\s\t]+inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) --> ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) netmask/)
remote_endpoint = m[1]
local_endpoint = m[2]
else
puts "failed\n\nVPN not established, please connect your iDevice to the VPN before attempting to sync.\n"
exit
end
puts "OK\n\nLibrary: #{name} @ #{ip}:#{port} [#{libid}]\n"
puts "\s\s\s\sVPN: #{remote_endpoint} --> #{local_endpoint}\n\n"
self.print "Looking for local iDevices ... "
devices = self.browse("_apple-mobdev._tcp")
if devices.empty?
puts "failed\n\nNo iDevices were found on the local network.\n"
exit
end
if devices.length >1
if $iphone_name != nil
device = nil
devices.each { |i|
if i[:name] == $iphone_name
device = i
break
end
}
puts "error\n\nThe configured device (#{$iphone_name}) was not found on the local network.\n"
exit
else
puts "OK\n\nMore than one device was found, please select:\n\n"
puts "(To avoid having to do this in future, set the name of your device in config.rb)\n\n"
while 1 do
i = 1
devices.each { |d|
puts "#{i}. #{d[:name]}, #{d[:host]} (#{d[:ip]}:#{d[:port]})"
i += 1
}
self.print "\nChoice: "
op = STDIN.gets.chomp
if op.match(/^[0-9]+$/) and (op.to_i-1) >= 0 and (op.to_i-1) < devices.length
puts "\n"
break
end
puts "\n"
end
device = devices[op.to_i-1]
end
else
device = devices[0]
if $iphone_name != nil and device[:name] != $iphone_name
puts "error\n\nThe configured device (#{$iphone_name}) was not found on the local network.\n"
exit
end
puts "OK\n\nDevice: #{device[:name]} (#{device[:ip]} #{device[:port]})\n\n"
end
self.print "Establishing local Bonjour proxy ... "
`screen -dmS -tirs_dnssd dns-sd -P #{name} _atc._tcp local. #{port} #{name}.local. #{remote_endpoint} "libid=#{libid}"`
self.print "OK\nEstablishing remote Bonjour proxy ... "
`ssh -p #{$ssh_port} #{$ssh_username}@#{$ssh_hostname} "screen -dmS -tirs_dnssd dns-sd -P '#{device[:name]}' _apple-mobdev._tcp local #{device[:port]} iphone.local. #{local_endpoint}" 2>/dev/null`
puts "OK\n\nThe proxies are up, your iDevice should appear in iTunes within 30-60 seconds.\n"
puts "When you're done syncing, type \"irs off\" to clear the proxies.\n\n"
end
def off
self.print "Killing local proxy ... "
`ps axww |grep 'dns-sd -P ' |grep -v grep`.lines.to_a.each { |line|
m = line.match(/^[\s\t]*?([0-9]+)[\s\t]/)
`kill -9 #{m[1]} 2>/dev/null`
}
self.print "OK\nKilling remote proxy ... "
self.rkill "dns-sd -P"
puts "OK\n"
end
def browse(type, find_name=nil)
browser = DNSSD::Service.new
host = port = ip = nil
services = {}
instances = []
begin
timeout $browse_timeout do
browser.browse type do |reply|
services[reply.fullname] = reply
next if reply.flags.more_coming?
services.sort_by do |_, service|
[(service.flags.add? ? 0 : 1), service.fullname]
end.each do |_, service|
next unless service.flags.add?
DNSSD::Service.new.resolve service do |r|
host = r.target
port = r.port
break
end
DNSSD::Service.new.getaddrinfo host do |a|
if a.address.match(/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/)
ip = a.address
break
end
end
if host != nil
if find_name != nil
if find_name == reply.name
return {:host => host, :ip => ip, :port => port}
end
else
instances.push({:host => host, :port => port, :ip => ip, :name => reply.name})
host = port = ip = nil
end
end
end
services.clear
end
end
rescue Timeout::Error
end
if find_name != nil then return false end
instances
end
end
if ARGV.length == 0
RemoteSync.new.usage
else
case ARGV[0]
when "dump"
RemoteSync.new.dump
when "sync"
RemoteSync.new.sync
when "off"
RemoteSync.new.off
else
RemoteSync.new.usage
end
end