Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Shortnex
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
jbs
Shortnex
Commits
905ee349
Commit
905ee349
authored
7 years ago
by
lordjbs
Browse files
Options
Downloads
Patches
Plain Diff
wew, its 1.2alpha now
parent
b850abb0
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
conf.json
+8
-2
8 additions, 2 deletions
conf.json
index.js
+8
-0
8 additions, 0 deletions
index.js
lib/ratelimit.js
+45
-0
45 additions, 0 deletions
lib/ratelimit.js
lib/util.js
+5
-0
5 additions, 0 deletions
lib/util.js
with
66 additions
and
2 deletions
conf.json
+
8
−
2
View file @
905ee349
{
"port"
:
3001
,
"db"
:
"tdb.db"
,
"devLog"
:
true
"db"
:
"test/tdb.db"
,
"devLog"
:
true
,
"requestThroughProxy"
:
false
,
"ratelimit"
:
{
"enabled"
:
true
,
"requests"
:
5
,
"interval"
:
10000
}
}
This diff is collapsed.
Click to expand it.
index.js
+
8
−
0
View file @
905ee349
...
...
@@ -17,6 +17,7 @@ const bodyParser = require("body-parser");
const
config
=
require
(
"
./conf.json
"
);
const
database
=
require
(
"
./db/db.js
"
);
const
startup
=
require
(
"
./lib/ascii.js
"
);
const
ratelimit
=
require
(
"
./lib/ratelimit.js
"
);
const
app
=
express
();
...
...
@@ -26,10 +27,16 @@ if(config.devLog) {
app
.
use
(
logger
(
"
default
"
));
}
app
.
use
(
express
.
static
(
path
.
join
(
__dirname
,
"
public
"
)));
app
.
use
(
bodyParser
.
json
());
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
function
(
req
,
res
,
next
)
{
console
.
log
(
"
req!!
"
);
ratelimit
.
request
(
req
,
res
,
next
);
});
app
.
use
(
"
/
"
,
require
(
"
./routes/index.js
"
));
app
.
use
(
"
/shorten
"
,
require
(
"
./routes/shorten.js
"
));
app
.
use
(
"
/jquery.min.js
"
,
require
(
"
./routes/handlerroute.js
"
));
...
...
@@ -51,6 +58,7 @@ app.use(function(err, req, res, next) {
const
port
=
process
.
env
.
PORT
||
config
.
port
||
8080
;
app
.
listen
(
port
,
function
()
{
ratelimit
.
start
();
startup
(
port
,
database
);
});
...
...
This diff is collapsed.
Click to expand it.
lib/ratelimit.js
0 → 100644
+
45
−
0
View file @
905ee349
/**
* Ratelimites. Greetings go to ratelimited.me!
*/
var
ips
=
{};
const
conf
=
require
(
"
../conf.json
"
);
const
util
=
require
(
"
./util.js
"
);
const
modules
=
{
request
:
function
(
req
,
res
,
next
)
{
if
(
!
conf
.
ratelimit
.
enabled
)
{
next
();
}
var
ip
;
if
(
!
conf
.
requestThroughProxy
)
{
ip
=
req
.
connection
.
remoteAddress
;
}
else
{
ip
=
request
.
headers
[
'
x-forwarded-for
'
];
}
if
(
!
ips
[
ip
])
{
ips
[
ip
]
=
1
;
}
else
{
ips
[
ip
]
+=
1
;
}
if
(
ips
[
ip
]
>
conf
.
ratelimit
.
requests
)
{
console
.
log
(
"
WARNING!
"
+
ip
+
"
is sending more requests than allowed. Requests send in interval
\"
"
+
conf
.
ratelimit
.
interval
+
"
\"
:
"
+
ips
[
ip
]);
res
.
status
(
429
);
return
res
.
send
({
error
:
429
,
message
:
"
You have been ratelimited.
"
});
}
else
{
next
();
}
},
start
:
function
()
{
setInterval
(
function
()
{
ips
=
{};
},
conf
.
ratelimit
.
interval
);
}
}
module
.
exports
=
modules
;
This diff is collapsed.
Click to expand it.
lib/util.js
0 → 100644
+
5
−
0
View file @
905ee349
module
.
exports
=
{
isEmpty
:
function
(
obj
)
{
return
!
Object
.
keys
(
obj
).
length
;
}
}
\ No newline at end of file
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