Skip to content

Commit 94781ca

Browse files
committed
Add media mirroring
1 parent bd70668 commit 94781ca

11 files changed

Lines changed: 97 additions & 33 deletions

File tree

_config.i2p.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ theme_settings:
44
p2p_player:
55
media_urls_overwrite:
66
- http://codnaft43k7ncna2hfsxrzi2nqoxieu22vbyjkmhkwdrrta2ghlq.b32.i2p
7+
- http://codonfttnvztewul3qgathdxjr2fnv34udrqyc6quw3zptqojnea.b32.i2p
78
nostr:
89
spam_api_overwrite: http://codnaft43k7ncna2hfsxrzi2nqoxieu22vbyjkmhkwdrrta2ghlq.b32.i2p/nostr/spam.nostr.band/spam_api
910
profile_relays_overwrite:

_config.tor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ theme_settings:
44
p2p_player:
55
media_urls_overwrite:
66
- http://codonaftct3jsouvfyrjq4yumyngzv3el2msndf5oddccktgghnw7eyd.onion
7+
- http://codonafte3ygy3szn5qwsdje4vp6mwobvzd75jl4ytyrb262cpkaegid.onion
78
nostr:
89
spam_api_overwrite: http://codonaftct3jsouvfyrjq4yumyngzv3el2msndf5oddccktgghnw7eyd.onion/nostr/spam.nostr.band/spam_api
910
profile_relays_overwrite:

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ theme_settings:
160160
post_navigation: true
161161

162162
p2p_player:
163-
swarmPrefix: https://media.codonaft.com
163+
swarm_prefix: https://media.codonaft.com
164164
media_urls:
165165
- https://media.codonaft.com
166166
- https://media-cached.codonaft.com

_do.cr

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ CACHE_DIR = BUILD_DIR.join(".cache")
6060
BANLISTS = CACHE_DIR.join("banlists.txt")
6161
BROKEN_POST_URLS = CACHE_DIR.join("broken_post_urls.txt")
6262
COMMON_ROOT = Path["_ohmyvps/alpine/alpine-root"]
63-
MEDIA_BUILD_DIR = BUILD_DIR.join(MEDIA_HOST, "/var/www", PUBLIC_MEDIA_HOST)
63+
64+
SERVER_MEDIA_DIR = Path["var/www"].join(PUBLIC_MEDIA_HOST)
65+
MEDIA_BUILD_DIR = BUILD_DIR.join(MEDIA_HOST, SERVER_MEDIA_DIR)
66+
67+
MIRROR_FROM_TO = [
68+
{MEDIA_HOST, [{MIRROR_HOST, SERVER_MEDIA_DIR}]},
69+
]
6470

6571
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
6672

@@ -317,6 +323,10 @@ def sync
317323
hosts = all_hosts()
318324
puts("hosts: #{hosts}")
319325

326+
mirror = MIRROR_FROM_TO.map { |source_host, destination_and_files|
327+
{source_host, destination_and_files.select { |destination_host, _| hosts.includes?(destination_host) }}
328+
}.to_h
329+
320330
common_files = git_ls(COMMON_ROOT)
321331
hosts_files : Hash(String, Set(Path)) = hosts.map { |host|
322332
host_dir = HOSTS_DIR.join(host)
@@ -327,7 +337,7 @@ def sync
327337
hosts.map { |host|
328338
done = Channel(Nil).new
329339
spawn do
330-
sync_host(host, hosts_files, common_files)
340+
sync_host(host, hosts_files: hosts_files, common_files: common_files, mirror_to: mirror[host]?)
331341
done.send(nil)
332342
end
333343
done
@@ -336,7 +346,7 @@ def sync
336346
ok("sync finished\n")
337347
end
338348

339-
def sync_host(host : String, hosts_files : Hash(String, Set(Path)), common_files : Set(Path))
349+
def sync_host(host : String, *, hosts_files : Hash(String, Set(Path)), common_files : Set(Path), mirror_to : Array({String, Path}) | Nil)
340350
host_files : Set(Path) = hosts_files[host]
341351
all_hosts_files : Set(Path) = hosts_files
342352
.flat_map { |_, files| files.to_a }
@@ -352,6 +362,15 @@ def sync_host(host : String, hosts_files : Hash(String, Set(Path)), common_files
352362
host_build_files = children_recursive(host_build_dir).to_set
353363
filtered_sync(host, host_build_dir, upload: host_build_files) # TODO: with --delete specifically for jekyll ?
354364

365+
unless mirror_to.nil?
366+
mirror_to.each { |destination_host, dir|
367+
mirror_host_files = host_files.select { |i| i.to_s.starts_with?(dir.to_s) }
368+
mirror_host_build_files = host_build_files.select { |i| i.to_s.starts_with?(dir.to_s) }
369+
filtered_sync(destination_host, HOSTS_DIR.join(host), upload: mirror_host_files.to_set)
370+
filtered_sync(destination_host, BUILD_DIR.join(host), upload: mirror_host_build_files.to_set)
371+
}
372+
end
373+
355374
ssh(host, ["sudo etckeeper commit sync 2>>/dev/null"])
356375
end
357376

@@ -1066,23 +1085,25 @@ end
10661085

10671086
def check_i2p_host(host : String)
10681087
puts("checking i2p configuration at #{host}")
1069-
private_key = File.read_lines(HOSTS_DIR.join(host).join("etc/i2pd/tunnels.conf"))
1070-
.select { |i| i.starts_with?("keys =") }
1071-
.map { |i| i.split(" = ")[1] }[0]
10721088
service_dir = Path["/var/lib/i2pd"]
1089+
private_keys = File.read_lines(HOSTS_DIR.join(host).join("etc/i2pd/tunnels.conf"))
1090+
.select { |i| i.starts_with?("keys =") }
1091+
.map { |i| i.split(" = ")[1] }
10731092
check_manual_upload(host, owner: "i2pd", group: "i2pd", mode: 700, path: service_dir)
1074-
check_manual_upload(host, owner: "i2pd", group: "i2pd", mode: 440, path: service_dir.join(private_key))
1093+
private_keys.each { |i| check_manual_upload(host, owner: "i2pd", group: "i2pd", mode: 440, path: service_dir.join(i)) }
10751094
end
10761095

10771096
def check_tor_host(host : String)
10781097
puts("checking tor configuration at #{host}")
1079-
service_dir = Path[File.read_lines(HOSTS_DIR.join(host).join("etc/tor/torrc"))
1098+
service_dirs = File.read_lines(HOSTS_DIR.join(host).join("etc/tor/torrc"))
10801099
.select { |i| i.starts_with?("HiddenServiceDir") }
1081-
.map { |i| i.split(" ")[1] }[0]]
1082-
check_manual_upload(host, owner: "tor", group: "tor", mode: 700, path: service_dir)
1083-
check_manual_upload(host, owner: "tor", group: "tor", mode: 400, path: service_dir.join("hs_ed25519_secret_key"))
1084-
check_manual_upload(host, owner: "tor", group: "tor", mode: 400, path: service_dir.join("hs_ed25519_public_key"))
1085-
check_manual_upload(host, owner: "tor", group: "tor", mode: 400, path: service_dir.join("hostname"), data: service_dir.basename)
1100+
.map { |i| Path[i.split(" ")[1]] }
1101+
service_dirs.map { |i|
1102+
check_manual_upload(host, owner: "tor", group: "tor", mode: 700, path: i)
1103+
check_manual_upload(host, owner: "tor", group: "tor", mode: 400, path: i.join("hs_ed25519_secret_key"))
1104+
check_manual_upload(host, owner: "tor", group: "tor", mode: 400, path: i.join("hs_ed25519_public_key"))
1105+
check_manual_upload(host, owner: "tor", group: "tor", mode: 400, path: i.join("hostname"), data: i.basename)
1106+
}
10861107
end
10871108

10881109
def check_ssh_hosts(ps : Array(Tuple(Int64, String)))
@@ -1132,12 +1153,15 @@ def check_existing_ssh_connection(host : String, ps : Array(Tuple(Int64, String)
11321153
end
11331154

11341155
def check_manual_upload(host : String, *, owner : String, group : String, mode : UInt16, path : Path, data : String? = nil)
1135-
raise "#{path}: unexpected owner" unless ssh(host, ["sudo stat -c %U #{path}"]) == owner
1136-
raise "#{path}: unexpected group" unless ssh(host, ["sudo stat -c %G #{path}"]) == group
1137-
raise "#{path}: unexpected mode" unless ssh(host, ["sudo stat -c %a #{path}"]).to_i == mode
1138-
unless data.nil?
1139-
raise "#{path}: unexpected data" unless ssh(host, ["sudo cat #{path}"]).strip == data
1140-
end
1156+
commands = ["stat -c %U,%G,%a"]
1157+
commands += ["cat"] unless data.nil?
1158+
output = ssh(host, commands.map { |i| "sudo #{i} #{path}" }).strip.split('\n')
1159+
stat = output[0].split(',')
1160+
1161+
raise "#{path}: unexpected owner" unless stat[0] == owner
1162+
raise "#{path}: unexpected group" unless stat[1] == group
1163+
raise "#{path}: unexpected mode" unless stat[2].to_i == mode
1164+
raise "#{path}: unexpected data" unless data.nil? || output[1] == data
11411165
end
11421166

11431167
def sync_nostr(config, *, profiles : Bool, output_relays : Array(String))

_hosts/media.codonaft/etc/i2pd/tunnels.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[anon-website]
1+
[codonaft-media]
22
type = http
33
host = 127.0.0.1
44
port = 80

_hosts/media.codonaft/etc/nginx/http.d/media.conf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ server {
6464
}
6565

6666
location ~ \.(m3u8|mp4|ts|vtt|webp)$ {
67-
if ($http_origin != $origin) {
68-
return 404;
69-
}
70-
7167
add_header "Access-Control-Allow-Origin" $origin always;
7268

7369
add_header Cache-Control "public, max-age=31536000, immutable";
@@ -136,10 +132,6 @@ server {
136132
ignore_invalid_headers on;
137133

138134
location ~ \.(m3u8|mp4|ts|vtt|webp)$ {
139-
if ($http_origin != $origin) {
140-
return 404;
141-
}
142-
143135
add_header "Access-Control-Allow-Origin" $origin always;
144136

145137
add_header Cache-Control "public, max-age=31536000, immutable";

_hosts/mirror.codonaft/etc/i2pd/tunnels.conf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[anon-website]
1+
[codonaft]
22
type = http
33
host = 127.0.0.1
44
port = 80
@@ -8,3 +8,9 @@ keys = codonftbnpdkjwyflssto3iklawhuthbe37l6swigegqkyyfmiqa.b32.i2p.dat
88
#inbound.quantity = 3
99
#outbound.length = 2
1010
#outbound.quantity = 3
11+
12+
[codonaft-media-mirror]
13+
type = http
14+
host = 127.0.0.1
15+
port = 80
16+
keys = codonfttnvztewul3qgathdxjr2fnv34udrqyc6quw3zptqojnea.b32.i2p.dat

_hosts/mirror.codonaft/etc/nginx/http.d/mirror.conf

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ server {
2525

2626
server {
2727
set $i2p_host "codonaft.i2p";
28-
2928
listen 127.0.0.1:80;
3029
server_name codonftbnpdkjwyflssto3iklawhuthbe37l6swigegqkyyfmiqa.b32.i2p;
3130
root /var/www/$i2p_host;
@@ -57,3 +56,40 @@ server {
5756
index index.html;
5857
error_page 404 =200 /404.html;
5958
}
59+
60+
server {
61+
listen 127.0.0.1:80;
62+
server_name codonafte3ygy3szn5qwsdje4vp6mwobvzd75jl4ytyrb262cpkaegid.onion;
63+
root /var/www/media.codonaft.com;
64+
65+
location ~ \.(m3u8|mp4|ts|vtt|webp)$ {
66+
set $origin http://codonaftbvv4j5k7nsrdivbdblycqrng5ls2qkng6lm77svepqjyxgid.onion;
67+
68+
add_header "Access-Control-Allow-Origin" $origin always;
69+
70+
add_header Cache-Control "public, max-age=31536000, immutable";
71+
expires 1y;
72+
73+
limit_rate_after 5M;
74+
limit_rate 5M;
75+
}
76+
}
77+
78+
server {
79+
set $i2p_host "codonaft.i2p";
80+
listen 127.0.0.1:80;
81+
server_name codonfttnvztewul3qgathdxjr2fnv34udrqyc6quw3zptqojnea.b32.i2p;
82+
root /var/www/media.codonaft.com;
83+
84+
location ~ \.(m3u8|mp4|ts|vtt|webp)$ {
85+
set $origin http://$i2p_host;
86+
87+
add_header "Access-Control-Allow-Origin" $origin always;
88+
89+
add_header Cache-Control "public, max-age=31536000, immutable";
90+
expires 1y;
91+
92+
limit_rate_after 5M;
93+
limit_rate 5M;
94+
}
95+
}

_hosts/mirror.codonaft/etc/tor/torrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ ExcludeExitNodes {am}, {az}, {by}, {ee}, {ge}, {kg}, {kz}, {lt}, {ly}, {md}, {ru
44

55
HiddenServiceDir /var/lib/tor/codonaftbvv4j5k7nsrdivbdblycqrng5ls2qkng6lm77svepqjyxgid.onion/
66
HiddenServicePort 80 127.0.0.1:80
7+
8+
HiddenServiceDir /var/lib/tor/codonafte3ygy3szn5qwsdje4vp6mwobvzd75jl4ytyrb262cpkaegid.onion/
9+
HiddenServicePort 80 127.0.0.1:80

_includes/head.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
const getSource = player => new URL(player.getAttribute('src'));
121121
const getSwarmId = player => {
122122
const source = getSource(player);
123-
return '{{ site.theme_settings.p2p_player.swarmPrefix }}' + source.pathname;
123+
return '{{ site.theme_settings.p2p_player.swarm_prefix }}' + source.pathname;
124124
};
125125

126126
const loadStoryboard = player => {

0 commit comments

Comments
 (0)