Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
Hetzner-Master
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Hetzner-Master
Commits
293fb21e
Commit
293fb21e
authored
Dec 21, 2019
by
Ahmad Nemati
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
bcba4f10
Pipeline
#22
canceled with stages
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
250 additions
and
22 deletions
+250
-22
vcs.xml
.idea/vcs.xml
+1
-1
Hetzner.js
Hetzner.js
+91
-6
app.js
app.js
+85
-13
constant.js
constant.js
+10
-0
test.js
test.js
+63
-2
No files found.
.idea/vcs.xml
View file @
293fb21e
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<project
version=
"4"
>
<component
name=
"VcsDirectoryMappings"
>
<component
name=
"VcsDirectoryMappings"
>
<mapping
directory=
"
$PROJECT_DIR$
"
vcs=
"Git"
/>
<mapping
directory=
""
vcs=
"Git"
/>
</component>
</component>
</project>
</project>
\ No newline at end of file
Hetzner.js
View file @
293fb21e
...
@@ -26,6 +26,22 @@ class Hetzner {
...
@@ -26,6 +26,22 @@ class Hetzner {
}
}
getAllFloatingIps
()
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
client
.
floatingIPs
.
list
().
then
(
function
(
res
)
{
let
d
=
_
.
values
(
res
.
floatingIPs
);
resolve
(
d
)
})
})
}
async
deleteServer
(
id
)
{
async
deleteServer
(
id
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
...
@@ -35,14 +51,22 @@ class Hetzner {
...
@@ -35,14 +51,22 @@ class Hetzner {
})
})
}
}
async
delFloatingIp
(
id
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
client
.
floatingIPs
.
delete
(
id
).
then
(
function
(
res
)
{
resolve
(
res
)
})
})
}
async
removeByIp
(
ip
)
{
async
removeByIp
(
ip
)
{
try
{
try
{
ip
=
_
.
trim
(
ip
)
ip
=
_
.
trim
(
ip
)
console
.
log
(
'
try to remove ip:
'
+
ip
)
console
.
log
(
'
try to remove ip:
'
+
ip
)
let
servers
=
await
this
.
getAllServers
()
let
servers
=
await
this
.
getAllServers
()
for
(
let
i
=
0
;
i
<
servers
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
servers
.
length
;
i
++
)
{
if
(
ip
===
servers
[
i
].
publicNet
.
server
.
publicNet
.
ipv4
.
ip
)
if
(
ip
===
servers
[
i
].
publicNet
.
server
.
publicNet
.
ipv4
.
ip
)
{
{
await
this
.
deleteServer
(
servers
[
i
].
id
)
await
this
.
deleteServer
(
servers
[
i
].
id
)
}
}
...
@@ -57,16 +81,54 @@ class Hetzner {
...
@@ -57,16 +81,54 @@ class Hetzner {
}
}
async
getAllFloatingIps
()
{
async
removeFloatingIpByIp
(
ip
)
{
try
{
ip
=
_
.
trim
(
ip
)
console
.
log
(
'
try to remove ip:
'
+
ip
)
let
ips
=
await
this
.
getAllFloatingIps
()
for
(
let
i
=
0
;
i
<
ips
.
length
;
i
++
)
{
if
(
ip
===
ips
[
i
].
ip
)
{
await
this
.
delFloatingIp
(
ips
[
i
].
id
)
}
}
}
catch
(
e
)
{
}
return
true
}
}
deleteFloatingIp
(
id
)
{
async
getMasterServerId
()
{
try
{
let
servers
=
await
this
.
getAllServers
()
for
(
let
i
=
0
;
i
<
servers
.
length
;
i
++
)
{
if
(
servers
[
i
].
name
===
'
master
'
)
{
return
servers
[
i
].
id
}
}
}
catch
(
e
)
{
}
return
true
}
}
createServer
(
name
)
{
createServer
(
name
)
{
// nuremberg nbg1
// nuremberg nbg1
...
@@ -90,6 +152,29 @@ class Hetzner {
...
@@ -90,6 +152,29 @@ class Hetzner {
}
}
createFloatingIp
(
location
,
server
)
{
// nuremberg nbg1
//fsn1
return
new
Promise
(
function
(
resolve
,
reject
)
{
client
.
floatingIPs
.
build
()
.
type
(
'
ipv4
'
)
.
location
(
location
)
.
server
(
server
)
.
create
()
.
then
(
function
(
response
)
{
resolve
(
response
.
floatingIP
.
ip
)
})
.
catch
(
function
(
error
)
{
resolve
(
false
)
})
})
}
}
}
...
...
app.js
View file @
293fb21e
...
@@ -10,6 +10,11 @@ const request = require('request');
...
@@ -10,6 +10,11 @@ const request = require('request');
const
fs
=
require
(
'
fs
'
)
const
fs
=
require
(
'
fs
'
)
const
cronSpeed
=
require
(
'
node-cron
'
);
const
cronSpeed
=
require
(
'
node-cron
'
);
const
shellExec
=
require
(
'
shell-exec
'
)
const
shellExec
=
require
(
'
shell-exec
'
)
let
Hetzner
=
require
(
'
./Hetzner
'
)
let
constant
=
require
(
'
./constant
'
)
let
het
var
decimalPointRegex
=
/
\-?\d
+
\.\d
+/g
;
var
decimalPointRegex
=
/
\-?\d
+
\.\d
+/g
;
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
res
.
send
(
'
Hello World!
'
)
res
.
send
(
'
Hello World!
'
)
...
@@ -24,24 +29,51 @@ app.get('/ip/:ip/', (req, res) => {
...
@@ -24,24 +29,51 @@ app.get('/ip/:ip/', (req, res) => {
})
})
app
.
get
(
'
/add/:num/
'
,
(
req
,
res
)
=>
{
// console.log(req.params.acc)
return
newFloatingIpsOrder
(
req
,
res
)
})
app
.
get
(
'
/test
'
,
(
req
,
res
)
=>
{
app
.
get
(
'
/test
'
,
(
req
,
res
)
=>
{
// console.log(req.params.acc)
// console.log(req.params.acc)
res
.
sendfile
(
'
jetmtp.png
'
)
res
.
sendfile
(
'
jetmtp.png
'
)
})
})
async
function
addNewIp
(
req
,
res
)
{
res
.
send
(
'
Hello World!
'
)
console
.
log
(
req
.
params
.
ip
)
addBaship
(
req
.
params
.
ip
)
async
function
newFloatingIpsOrder
(
req
,
res
)
{
let
order
=
parseInt
(
req
.
params
.
num
)
res
.
send
(
'
Floating Ips from
'
+
(
order
===
1
?
'
Nuremberg
'
:
'
Falkenstein
'
)
+
'
Requested
'
)
let
ips
=
await
het
.
getAllFloatingIps
()
if
(
ips
.
length
===
0
)
return
let
masterid
=
await
het
.
getMasterServerId
()
let
floatNum
=
10
-
ips
.
length
let
location
=
order
===
1
?
constant
.
nuremberg
:
constant
.
falkenstein
let
newIps
=
[]
for
(
let
i
=
1
;
i
<=
floatNum
;
i
++
)
{
let
ip
=
await
het
.
createFloatingIp
(
location
,
masterid
)
addBaship
(
ip
)
newIps
.
push
(
ip
)
}
// console.log(req.params.ip)
// addBaship(req.params.ip)
}
}
async
function
addBaship
(
ip
)
{
async
function
addBaship
(
ip
)
{
return
await
shell
.
exec
(
'
ip addr add
'
+
ip
+
'
dev eth0
'
).
stderr
;
return
shell
.
exec
(
'
ip addr add
'
+
ip
+
'
dev eth0
'
).
stderr
;
}
}
//
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
app
.
listen
(
port
,
()
=>
console
.
log
(
`Example app listening on port
${
port
}
!`
))
async
function
run
()
{
async
function
run
()
{
...
@@ -64,21 +96,61 @@ cron.schedule('*/10 * * * * *', () => {
...
@@ -64,21 +96,61 @@ cron.schedule('*/10 * * * * *', () => {
postRequest
()
postRequest
()
cronRemover
.
schedule
(
'
*/1 * * * *
'
,
()
=>
{
cronRemover
.
schedule
(
'
*/1 * * * *
'
,
()
=>
{
init
()
remover
()
postRequest
()
});
});
async
function
init
()
{
await
initToken
()
postRequest
()
}
async
function
initToken
()
{
var
contents
=
await
fs
.
readFileSync
(
'
./token.txt
'
,
'
utf8
'
);
het
=
new
Hetzner
(
contents
)
// remover()
}
async
function
addNewIp
(
req
,
res
)
{
res
.
send
(
'
Hello World!
'
)
console
.
log
(
req
.
params
.
ip
)
addBaship
(
req
.
params
.
ip
)
}
function
postFloatingIps
(
data
)
{
data
=
JSON
.
stringify
(
data
)
data
=
cryptLib
.
encryptPlainTextWithRandomIV
(
data
,
key
)
var
options
=
{
method
:
'
POST
'
,
url
:
'
http://admin.fcfglobal.co:3000/addFloatingIp
'
,
headers
:
{
'
content-type
'
:
'
application/x-www-form-urlencoded
'
},
body
:
'
data=
'
+
Buffer
.
from
(
data
).
toString
(
'
base64
'
)
};
return
new
Promise
(
function
(
resolve
,
reject
)
{
request
(
options
,
function
(
error
,
response
,
body
)
{
if
(
error
)
console
.
log
(
error
)
resolve
(
true
)
});
});
}
async
function
remover
()
{
async
function
remover
()
{
let
work
=
await
getWork
()
let
work
=
await
getWork
()
if
(
work
===
'
true
'
)
if
(
work
===
'
true
'
)
return
true
return
true
let
ip
=
await
getIp
()
let
ip
=
await
getIp
()
console
.
log
(
ip
)
var
contents
=
await
fs
.
readFileSync
(
'
./token.txt
'
,
'
utf8
'
);
let
Hetzner
=
require
(
'
./Hetzner
'
)
let
het
=
new
Hetzner
(
contents
)
await
het
.
removeByIp
(
ip
)
await
het
.
removeByIp
(
ip
)
}
}
...
@@ -123,7 +195,7 @@ function getIp() {
...
@@ -123,7 +195,7 @@ function getIp() {
}
}
function
postRequest
()
{
function
postRequest
()
{
let
data
=
{
generation
:
generation
,
groups
:
groups
}
let
data
=
{
generation
:
generation
,
groups
:
groups
}
data
=
JSON
.
stringify
(
data
)
data
=
JSON
.
stringify
(
data
)
data
=
cryptLib
.
encryptPlainTextWithRandomIV
(
data
,
key
)
data
=
cryptLib
.
encryptPlainTextWithRandomIV
(
data
,
key
)
...
...
constant.js
0 → 100644
View file @
293fb21e
module
.
exports
=
Object
.
freeze
({
falkenstein
:
'
fsn1
'
,
nuremberg
:
'
nbg1
'
});
test.js
View file @
293fb21e
let
a
=
'
Connection to 127.0.0.1 443 port [tcp/https] succeeded!
'
let
groups
=
0
let
generation
=
0
const
express
=
require
(
'
express
'
)
const
app
=
express
()
const
port
=
4000
const
shell
=
require
(
'
shelljs
'
)
const
cron
=
require
(
'
node-cron
'
);
const
cronRemover
=
require
(
'
node-cron
'
);
const
request
=
require
(
'
request
'
);
const
fs
=
require
(
'
fs
'
)
const
cronSpeed
=
require
(
'
node-cron
'
);
const
shellExec
=
require
(
'
shell-exec
'
)
let
constant
=
require
(
'
./constant
'
)
const
cryptLib
=
require
(
'
@skavinvarnan/cryptlib
'
);
const
key
=
"
6*sN_rZxHD4!X$=T
"
;
let
Hetzner
=
require
(
'
./Hetzner
'
)
let
het
postFloatingIps
([
'
127.0.0.1
'
,
'
127.0.0.2
'
])
async
function
tesT
()
{
await
init
()
// let masterid=await het.getMasterServerId()
let
d
=
await
het
.
removeFloatingIpByIp
(
'
78.47.38.44
'
)
console
.
log
(
d
)
}
console
.
log
(
a
.
includes
(
'
succeeded
'
))
async
function
init
()
{
var
contents
=
await
fs
.
readFileSync
(
'
./token.txt
'
,
'
utf8
'
);
het
=
new
Hetzner
(
contents
)
// remover()
}
function
postFloatingIps
(
data
)
{
data
=
JSON
.
stringify
(
data
)
data
=
cryptLib
.
encryptPlainTextWithRandomIV
(
data
,
key
)
var
options
=
{
method
:
'
POST
'
,
url
:
'
http://127.0.0.1:3000/addFloatingIp
'
,
headers
:
{
'
content-type
'
:
'
application/x-www-form-urlencoded
'
},
body
:
'
data=
'
+
Buffer
.
from
(
data
).
toString
(
'
base64
'
)
};
return
new
Promise
(
function
(
resolve
,
reject
)
{
request
(
options
,
function
(
error
,
response
,
body
)
{
if
(
error
)
console
.
log
(
error
)
resolve
(
true
)
});
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment