-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmiddleware.ts
More file actions
127 lines (112 loc) · 4.95 KB
/
middleware.ts
File metadata and controls
127 lines (112 loc) · 4.95 KB
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
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export const getValidSubdomain = (host?: string | null) => {
let subdomain: string | null = null;
if (!host && typeof window !== 'undefined') {
// On client side, get the host from window
host = window.location.host;
}
if (host && host.includes('.')) {
const candidate = host.split('.')[0];
if (candidate && !candidate.includes('localhost')) {
// Valid candidate
subdomain = candidate;
}
}
return subdomain;
};
// RegExp for public files
const PUBLIC_FILE = /\.(.*)$/; // Files
export async function middleware(req: NextRequest) {
// Clone the URL
if(process.env.NEXT_PUBLIC_DISABLE_MIDDLEWARE != "true") {
const url = req.nextUrl.clone();
// Skip public files
// it skips .well-known
if (!url.pathname.includes("/.well-known/nostr.json") && !url.pathname.includes('/api/86')) {
if (PUBLIC_FILE.test(url.pathname) || url.pathname.includes('_next') || url.pathname.includes('/api/')) return;
}
const host = req.headers.get('host')?.toLowerCase();
let skipThis = "nostr1.com"
if( process.env.NEXT_PUBLIC_CREATOR_DOMAIN ) {
skipThis = process.env.NEXT_PUBLIC_CREATOR_DOMAIN.toLowerCase()
}
// Extract domain from NEXT_PUBLIC_ROOT_DOMAIN (removing https:// or http:// prefix)
let rootDomain = "relay.tools"
if( process.env.NEXT_PUBLIC_ROOT_DOMAIN ) {
rootDomain = process.env.NEXT_PUBLIC_ROOT_DOMAIN.replace(/^https?:\/\//, '').toLowerCase()
}
// Skip root domains and local IPs
if (host == rootDomain
|| host == skipThis
|| host?.includes("10.0")
|| host?.includes("192.168")
|| host?.includes("127.0")
|| host?.includes("localhost")) {
// Handle .well-known/nostr.json for root domains
if (url.pathname.includes('/.well-known/nostr.json')) {
console.log(`>>> Rewriting for nip05 rootDM: ${url.pathname} to /api/nip05/${host}`);
url.pathname = `/api/nip05/${host}`;
const requestHeaders = new Headers(req.headers)
requestHeaders.set('middleware-rewritten', host || "true")
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
})
return NextResponse.rewrite(url, response);
}
const pathHeaders = new Headers(req.headers)
pathHeaders.set('next-url', url.pathname)
return NextResponse.next({
request: {
headers: pathHeaders,
},
});
}
const subdomain = getValidSubdomain(host);
if (subdomain) {
if(url.pathname.includes('.well-known/nostr.json')) {
// nip05 for subdomains
console.log(`>>> Rewriting for nip05: ${url.pathname} to ->`)
url.pathname = `/api/nip05/${host}`;
} else if(url.pathname.includes('api/86')) {
// NIP-86 for subdomains
// DONT REWRITE TO RELAYS DOWN BELOW HERE..
} else if(url.pathname.includes('clientinvoices')) {
// DONT REWRITE TO RELAYS DOWN BELOW HERE..
console.log(`>>> Rewriting for clientinvoices: ${url.pathname} to /clientinvoices`);
url.pathname = `/clientinvoices`;
} else if(url.pathname.includes('trex')) {
// DONT REWRITE TO RELAYS DOWN BELOW HERE..
console.log(`>>> Rewriting for trex: ${url.pathname} to /trex/${subdomain}`);
url.pathname = `/trex/${subdomain}`;
} else if(url.pathname.includes('/nip05')) {
// DONT REWRITE TO RELAYS DOWN BELOW HERE..
console.log(`>>> Rewriting for nip05: ${url.pathname} to /nip05`);
url.pathname = `/nip05`;
} else {
// Subdomain available, rewriting
console.log(`>>> Rewriting: ${url.pathname} to /relays/${subdomain}/${url.pathname}`);
url.pathname = `/relays/${subdomain}/${url.pathname}`;
}
} else {
if(url.pathname.includes('/.well-known/nostr.json')) {
// root domains nip05
console.log(`>>> Rewriting for nip05 rootDM: ${url.pathname} to ->`)
url.pathname = `/api/nip05/${host}`;
}
}
const requestHeaders = new Headers(req.headers)
requestHeaders.set('middleware-rewritten', host || "true")
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
})
return NextResponse.rewrite(url, response);
} else {
return NextResponse.next();
}
}