Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallenbagel committed Jun 13, 2024
2 parents f735d86 + a9741fa commit a302929
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
30 changes: 18 additions & 12 deletions server/api/jellyfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,31 @@ class JellyfinAPI extends ExternalAPI {
Password?: string,
ClientIP?: string
): Promise<JellyfinLoginResponse> {
try {
const headers = ClientIP
? {
'X-Forwarded-For': ClientIP,
}
: {};
const authenticate = async (useHeaders: boolean) => {
const headers =
useHeaders && ClientIP ? { 'X-Forwarded-For': ClientIP } : {};

const authResponse = await this.post<JellyfinLoginResponse>(
return this.post<JellyfinLoginResponse>(
'/Users/AuthenticateByName',
{
Username: Username,
Username,
Pw: Password,
},
{
headers: headers,
}
{ headers }
);
};

return authResponse;
try {
return await authenticate(true);
} catch (e) {
logger.debug(`Failed to authenticate with headers: ${e.message}`, {
label: 'Jellyfin API',
ip: ClientIP,
});
}

try {
return await authenticate(false);
} catch (e) {
const status = e.response?.status;

Expand Down
14 changes: 14 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type CacheableLookupType from 'cacheable-lookup';
import { TypeormStore } from 'connect-typeorm/out';
import cookieParser from 'cookie-parser';
import csurf from 'csurf';
import { lookup } from 'dns';
import type { NextFunction, Request, Response } from 'express';
import express from 'express';
import * as OpenApiValidator from 'express-openapi-validator';
Expand Down Expand Up @@ -54,6 +55,19 @@ app
const CacheableLookup = (await _importDynamic('cacheable-lookup'))
.default as typeof CacheableLookupType;
const cacheable = new CacheableLookup();

const originalLookup = cacheable.lookup;

// if hostname is localhost use dns.lookup instead of cacheable-lookup
cacheable.lookup = (...args: any) => {
const [hostname] = args;
if (hostname === 'localhost') {
lookup(...(args as Parameters<typeof lookup>));
} else {
originalLookup(...(args as Parameters<typeof originalLookup>));
}
};

cacheable.install(http.globalAgent);
cacheable.install(https.globalAgent);

Expand Down
15 changes: 13 additions & 2 deletions server/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ApiError } from '@server/types/error';
import * as EmailValidator from 'email-validator';
import { Router } from 'express';
import gravatarUrl from 'gravatar-url';
import net from 'net';

const authRoutes = Router();

Expand Down Expand Up @@ -271,11 +272,21 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
? jellyfinHost.slice(0, -1)
: jellyfinHost;

const ip = req.ip ? req.ip.split(':').reverse()[0] : undefined;
const ip = req.ip;
let clientIp;

if (ip) {
if (net.isIPv4(ip)) {
clientIp = ip;
} else if (net.isIPv6(ip)) {
clientIp = ip.startsWith('::ffff:') ? ip.substring(7) : ip;
}
}

const account = await jellyfinserver.login(
body.username,
body.password,
ip
clientIp
);

// Next let's see if the user already exists
Expand Down

0 comments on commit a302929

Please sign in to comment.