Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
T
tun
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
tun
Commits
a568393f
Commit
a568393f
authored
Feb 07, 2020
by
神楽坂玲奈
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
d07f5a2a
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
92 additions
and
37 deletions
+92
-37
ansible/install.yaml
ansible/install.yaml
+12
-2
ansible/roles/wg.yaml
ansible/roles/wg.yaml
+0
-0
ansible/wg.conf.j2
ansible/wg.conf.j2
+15
-0
ansible/wg.yaml
ansible/wg.yaml
+4
-0
ansible/wgfrp.yaml
ansible/wgfrp.yaml
+3
-0
src/inventory.ts
src/inventory.ts
+56
-34
update.sh
update.sh
+2
-1
No files found.
ansible/install.yaml
View file @
a568393f
---
-
hosts
:
wg
tasks
:
-
name
:
work dir
file
:
path
:
/tmp/nextgen
state
:
directory
-
name
:
'
loop
through
list
from
a
variable'
debug
:
msg
:
'
An
item:
{{
item.metric
}}
{{item.protocol}}
{{item.params}}'
include_tasks
:
'
{{item.protocol}}.yaml'
vars
:
name
:
'
{{item.name}}'
metric
:
'
{{item.metric}}'
mark
:
'
{{item.mark}}'
inbound
:
'
{{item.inbound}}'
params
:
'
{{item.params}}'
with_items
:
'
{{
connections
}}'
ansible/roles/wg.yaml
0 → 100644
View file @
a568393f
ansible/wg.conf.j2
0 → 100644
View file @
a568393f
[Interface]
Address = {{address}}/24
PrivateKey = {{key}}
{% if listen_port is defined %}
ListenPort = {{listen_port}}
{% endif %}
{% if listen_port is defined %}
ListenPort = {{listen_port}}
{% endif %}
{% if params.if is defined %}
FwMark = {{gateways[params.if].mark_gateway}}
{% endif %}
{% if inbound is not defined %}
Table = {{table}}
{% endif %}
ansible/wg.yaml
0 → 100644
View file @
a568393f
-
name
:
placeholder foo
template
:
src
:
wg.conf.j2
dest
:
'
/tmp/nextgen/{{name}}.conf'
ansible/wgfrp.yaml
0 → 100644
View file @
a568393f
-
name
:
placeholder foo
debug
:
msg
:
'
An
item:
{{params.key}}'
src/inventory.ts
View file @
a568393f
...
...
@@ -3,44 +3,66 @@ import util from 'util';
import
fs
from
'
fs
'
;
import
path
from
'
path
'
;
import
YAML
from
'
yaml
'
;
import
{
intersection
}
from
'
lodash
'
;
async
function
main
()
{
const
data
=
await
fs
.
promises
.
readFile
(
path
.
join
(
'
data
'
,
'
内网互联计划 - nextgen links.csv
'
));
// @ts-ignore
const
records
=
(
await
util
.
promisify
(
parse
)(
data
,
{
columns
:
true
})).
filter
(
h
=>
h
.
id
);
const
_hosts
=
Object
.
fromEntries
(
records
.
map
(
h
=>
[
h
.
name
,
h
]));
const
connections
=
intersection
(
Object
.
keys
(
_hosts
),
Object
.
keys
(
records
[
0
]));
const
hosts
=
Object
.
fromEntries
(
records
.
map
(
h
=>
[
h
.
host
,
host_vars
(
h
,
_hosts
,
connections
)]));
const
result
=
YAML
.
stringify
({
wg
:
{
hosts
}
});
return
fs
.
promises
.
writeFile
(
'
result/inventory.yaml
'
,
result
);
}
import
_
from
'
lodash
'
;
function
host_vars
(
host
,
hosts
,
hostNames
:
string
[])
{
const
connections
=
[]
;
for
(
const
h
of
hostNames
)
{
if
(
h
!=
host
.
name
)
{
const
peer
=
host
[
h
]
||
hosts
[
h
][
host
.
name
];
if
(
peer
)
{
connections
.
push
(
parse_connection
(
h
,
peer
));
}
}
class
InventoryBuilder
{
hosts
:
{
[
key
:
string
]:
any
}
;
gateways
:
any
;
connections
:
string
[];
async
load
(
sheetName
)
{
const
data
=
await
fs
.
promises
.
readFile
(
path
.
join
(
'
data
'
,
`内网互联计划 -
${
sheetName
}
.csv`
));
// @ts-ignore
return
(
await
util
.
promisify
(
parse
)(
data
,
{
columns
:
true
,
cast
:
true
})).
filter
(
h
=>
h
.
id
);
}
return
{
ansible_ssh_user
:
host
.
user
,
address
:
host
.
address
,
connections
};
}
async
main
()
{
this
.
hosts
=
_
.
keyBy
(
await
this
.
load
(
'
nextgen links
'
),
'
name
'
);
this
.
gateways
=
_
.
mapValues
(
_
.
groupBy
(
await
this
.
load
(
'
gateways
'
),
'
name
'
),
g
=>
_
.
keyBy
(
g
,
'
isp
'
));
this
.
connections
=
_
.
intersection
(
Object
.
keys
(
this
.
hosts
),
Object
.
keys
(
_
.
find
(
this
.
hosts
)));
console
.
log
(
Object
.
values
(
this
.
hosts
));
const
hosts
=
Object
.
fromEntries
(
Object
.
values
(
this
.
hosts
).
map
(
h
=>
[
h
.
host
,
this
.
host_vars
(
h
)]));
console
.
log
(
hosts
);
const
result
=
YAML
.
stringify
({
wg
:
{
hosts
}
});
return
fs
.
promises
.
writeFile
(
'
result/inventory.yaml
'
,
result
);
}
host_vars
(
host
)
{
const
connections
=
[];
for
(
const
h
of
this
.
connections
)
{
if
(
h
!=
host
.
name
)
{
const
to
=
host
[
h
];
const
from
=
this
.
hosts
[
h
][
host
.
name
];
if
(
from
&&
to
)
{
// 非对称连接
connections
.
push
(
this
.
parse_connection
(
h
,
from
,
true
));
connections
.
push
(
this
.
parse_connection
(
h
,
to
));
}
else
if
(
from
||
to
)
{
// 对称连接
connections
.
push
(
this
.
parse_connection
(
h
,
from
||
to
));
}
// 不连接
}
}
function
connections
()
{}
return
{
ansible_ssh_user
:
host
.
user
,
address
:
host
.
address
,
key
:
host
.
wgPrivateKey
,
gateways
:
_
.
mapValues
(
this
.
gateways
[
host
.
name
],
gw
=>
_
.
pick
(
gw
,
[
'
mark_gateway
'
])),
connections
};
}
function
parse_connection
(
name
:
string
,
str
:
string
)
{
const
[
_metric
,
protocol
,
_params
]
=
str
.
split
(
'
,
'
);
const
metric
=
parseInt
(
_metric
);
const
params
=
_params
?
_params
.
split
(
'
:
'
)
:
[];
return
{
name
,
metric
,
protocol
,
params
};
parse_connection
(
_name
:
string
,
str
:
string
,
inbound
=
false
)
{
const
[
_metric
,
protocol
,
_params
]
=
str
.
split
(
'
,
'
);
const
metric
=
parseInt
(
_metric
);
const
params
=
Object
.
fromEntries
(
new
URLSearchParams
(
_params
).
entries
());
const
mark
=
this
.
hosts
[
_name
].
mark
;
const
name
=
inbound
?
`
${
_name
}
-in`
:
_name
;
return
{
name
,
metric
,
protocol
,
params
,
mark
,
inbound
};
}
}
main
();
new
InventoryBuilder
().
main
();
update.sh
View file @
a568393f
#!/usr/bin/env bash
set
-e
mkdir
-p
result
npm run inventory
cd
ansible
||
exit
ansible-playbook
-i
../result/inventory.yaml install.yaml
ansible-playbook
-i
../result/inventory.yaml
"
$@
"
install.yaml
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