Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Charlotte
Sharkey
Commits
2234fbcb
Commit
2234fbcb
authored
4 months ago
by
Julia
Browse files
Options
Downloads
Plain Diff
merge: Bump version (!762)
View MR for information:
TransFem-org/Sharkey!762
parents
0fcb23c4
8e90484b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
package.json
+1
-1
1 addition, 1 deletion
package.json
packages/backend/src/server/FileServerService.ts
+0
-89
0 additions, 89 deletions
packages/backend/src/server/FileServerService.ts
with
1 addition
and
90 deletions
package.json
+
1
−
1
View file @
2234fbcb
{
"name"
:
"sharkey"
,
"version"
:
"2024.9.
2
"
,
"version"
:
"2024.9.
3
"
,
"codename"
:
"shonk"
,
"repository"
:
{
"type"
:
"git"
,
...
...
This diff is collapsed.
Click to expand it.
packages/backend/src/server/FileServerService.ts
+
0
−
89
View file @
2234fbcb
...
...
@@ -28,11 +28,7 @@ import { bindThis } from '@/decorators.js';
import
{
isMimeImage
}
from
'
@/misc/is-mime-image.js
'
;
import
{
correctFilename
}
from
'
@/misc/correct-filename.js
'
;
import
{
handleRequestRedirectToOmitSearch
}
from
'
@/misc/fastify-hook-handlers.js
'
;
import
{
RateLimiterService
}
from
'
@/server/api/RateLimiterService.js
'
;
import
{
getIpHash
}
from
'
@/misc/get-ip-hash.js
'
;
import
{
AuthenticateService
}
from
'
@/server/api/AuthenticateService.js
'
;
import
type
{
FastifyInstance
,
FastifyRequest
,
FastifyReply
,
FastifyPluginOptions
}
from
'
fastify
'
;
import
type
Limiter
from
'
ratelimiter
'
;
const
_filename
=
fileURLToPath
(
import
.
meta
.
url
);
const
_dirname
=
dirname
(
_filename
);
...
...
@@ -56,8 +52,6 @@ export class FileServerService {
private
videoProcessingService
:
VideoProcessingService
,
private
internalStorageService
:
InternalStorageService
,
private
loggerService
:
LoggerService
,
private
authenticateService
:
AuthenticateService
,
private
rateLimiterService
:
RateLimiterService
,
)
{
this
.
logger
=
this
.
loggerService
.
getLogger
(
'
server
'
,
'
gray
'
);
...
...
@@ -82,8 +76,6 @@ export class FileServerService {
});
fastify
.
get
<
{
Params
:
{
key
:
string
;
}
}
>
(
'
/files/:key
'
,
async
(
request
,
reply
)
=>
{
if
(
!
await
this
.
checkRateLimit
(
request
,
reply
,
`/files/
${
request
.
params
.
key
}
`
))
return
;
return
await
this
.
sendDriveFile
(
request
,
reply
)
.
catch
(
err
=>
this
.
errorHandler
(
request
,
reply
,
err
));
});
...
...
@@ -97,20 +89,6 @@ export class FileServerService {
Params
:
{
url
:
string
;
};
Querystring
:
{
url
?:
string
;
};
}
>
(
'
/proxy/:url*
'
,
async
(
request
,
reply
)
=>
{
const
url
=
'
url
'
in
request
.
query
?
request
.
query
.
url
:
'
https://
'
+
request
.
params
.
url
;
if
(
!
url
||
!
URL
.
canParse
(
url
))
{
reply
.
code
(
400
);
return
;
}
const
keyUrl
=
new
URL
(
url
);
keyUrl
.
searchParams
.
forEach
(
k
=>
keyUrl
.
searchParams
.
delete
(
k
));
keyUrl
.
hash
=
''
;
keyUrl
.
username
=
''
;
keyUrl
.
password
=
''
;
if
(
!
await
this
.
checkRateLimit
(
request
,
reply
,
`/proxy/
${
keyUrl
}
`
))
return
;
return
await
this
.
proxyHandler
(
request
,
reply
)
.
catch
(
err
=>
this
.
errorHandler
(
request
,
reply
,
err
));
});
...
...
@@ -594,71 +572,4 @@ export class FileServerService {
path
,
};
}
// Based on ApiCallService
private
async
checkRateLimit
(
request
:
FastifyRequest
<
{
Body
?:
Record
<
string
,
unknown
>
|
undefined
,
Querystring
?:
Record
<
string
,
unknown
>
|
undefined
,
Params
?:
Record
<
string
,
unknown
>
|
unknown
,
}
>
,
reply
:
FastifyReply
,
rateLimitKey
:
string
,
):
Promise
<
boolean
>
{
const
body
=
request
.
method
===
'
GET
'
?
request
.
query
:
request
.
body
;
// https://datatracker.ietf.org/doc/html/rfc6750.html#section-2.1 (case sensitive)
const
token
=
request
.
headers
.
authorization
?.
startsWith
(
'
Bearer
'
)
?
request
.
headers
.
authorization
.
slice
(
7
)
:
body
?.[
'
i
'
];
if
(
token
!=
null
&&
typeof
token
!==
'
string
'
)
{
reply
.
code
(
400
);
return
false
;
}
// koa will automatically load the `X-Forwarded-For` header if `proxy: true` is configured in the app.
const
[
user
]
=
await
this
.
authenticateService
.
authenticate
(
token
);
const
actor
=
user
?.
id
??
getIpHash
(
request
.
ip
);
const
limit
=
{
// Group by resource
key
:
rateLimitKey
,
// Maximum of 10 requests / 10 minutes
max
:
10
,
duration
:
1000
*
60
*
10
,
// Minimum of 250 ms between each request
minInterval
:
250
,
};
// Rate limit proxy requests
try
{
await
this
.
rateLimiterService
.
limit
(
limit
,
actor
);
return
true
;
}
catch
(
err
)
{
// errはLimiter.LimiterInfoであることが期待される
reply
.
code
(
429
);
if
(
hasRateLimitInfo
(
err
))
{
const
cooldownInSeconds
=
Math
.
ceil
((
err
.
info
.
resetMs
-
Date
.
now
())
/
1000
);
// もしかするとマイナスになる可能性がなくはないのでマイナスだったら0にしておく
reply
.
header
(
'
Retry-After
'
,
Math
.
max
(
cooldownInSeconds
,
0
).
toString
(
10
));
}
reply
.
send
({
message
:
'
Rate limit exceeded. Please try again later.
'
,
code
:
'
RATE_LIMIT_EXCEEDED
'
,
id
:
'
d5826d14-3982-4d2e-8011-b9e9f02499ef
'
,
});
return
false
;
}
}
}
function
hasRateLimitInfo
(
err
:
unknown
):
err
is
{
info
:
Limiter
.
LimiterInfo
}
{
return
err
!=
null
&&
typeof
(
err
)
===
'
object
'
&&
'
info
'
in
err
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment