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
01c3aa51
Commit
01c3aa51
authored
Dec 13, 2020
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
query dns directly in inventory
parent
c88f17f8
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
13 deletions
+68
-13
.gitignore
.gitignore
+1
-1
ansible/protocols/wg/wg-setconf.conf.j2
ansible/protocols/wg/wg-setconf.conf.j2
+18
-0
src/inventory.ts
src/inventory.ts
+45
-9
update.sh
update.sh
+4
-3
No files found.
.gitignore
View file @
01c3aa51
...
...
@@ -113,6 +113,6 @@ dist
/build
*.retry
*
-setconf.conf.j2
wgfrp
-setconf.conf.j2
__pycache__
ansible/protocols/wg/wg-setconf.conf.j2
0 → 100644
View file @
01c3aa51
[Interface]
PrivateKey = {{key}}
ListenPort = {{conn.localPort}}
{% if conn.localGatewayMark != 0 %}
FwMark = {{conn.localGatewayMark}}
{% endif %}
[Peer]
PublicKey = {{conn.wgPublicKey}}
AllowedIPs = 0.0.0.0/0, ::/0
{% if conn.remoteAddress is defined %}
Endpoint = {{conn.remoteAddress}}:{{conn.remotePort}}
PersistentKeepalive = 1
{% endif %}
# forced change 12.12
src/inventory.ts
View file @
01c3aa51
...
...
@@ -7,12 +7,46 @@ import _ from 'lodash';
import
child_process
from
'
child_process
'
;
import
assert
from
'
assert
'
;
import
ip
from
"
ip
"
;
import
{
promises
as
dns
}
from
"
dns
"
;
class
InventoryBuilder
{
hosts
:
{
[
key
:
string
]:
any
};
gateways
:
any
;
connections
:
string
[];
routeLists
:
any
;
resolveCache
:
Map
<
string
,
string
>
;
resolver
:
dns
.
Resolver
;
async
resolve
(
domain
:
string
)
{
if
(
domain
.
match
(
/
(\d{1,3}\.){3}\d{1,3}
/
))
{
return
domain
;
}
if
(
this
.
resolveCache
.
has
(
domain
))
{
return
this
.
resolveCache
.
get
(
domain
);
}
const
rrtype
=
domain
.
includes
(
"
-v6
"
)
?
"
AAAA
"
:
"
A
"
;
let
resolvedIP
:
string
;
while
(
true
)
{
try
{
[
resolvedIP
]
=
(
await
this
.
resolver
.
resolve
(
domain
,
rrtype
))
as
string
[];
break
;
}
catch
(
e
)
{
console
.
log
(
`
${
domain
}
=> FAIL:
${
e
.
toString
()}
`
);
}
}
if
(
rrtype
===
"
AAAA
"
)
{
resolvedIP
=
`[
${
resolvedIP
}
]`
;
}
console
.
log
(
`
${
domain
}
=>
${
resolvedIP
}
`
);
this
.
resolveCache
.
set
(
domain
,
resolvedIP
);
return
resolvedIP
;
}
constructor
()
{
this
.
resolveCache
=
new
Map
();
this
.
resolver
=
new
dns
.
Resolver
();
this
.
resolver
.
setServers
([
'
114.114.114.114
'
,
'
223.5.5.5
'
]);
}
async
load
(
sheetName
)
{
const
data
=
await
fs
.
promises
.
readFile
(
path
.
join
(
'
data
'
,
`内网互联计划 -
${
sheetName
}
.csv`
));
...
...
@@ -44,7 +78,7 @@ class InventoryBuilder {
host
.
wgPublickey
=
await
this
.
wgPublickey
(
host
.
wgPrivateKey
);
}
// console.log(Object.values(this.hosts));
const
rawHosts
=
Object
.
values
(
this
.
hosts
).
map
(
h
=>
[
h
.
name
,
this
.
host_vars
(
h
)]
);
const
rawHosts
=
await
Promise
.
all
(
Object
.
values
(
this
.
hosts
).
map
(
async
(
h
)
=>
[
h
.
name
,
await
this
.
host_vars
(
h
)])
);
const
hosts
=
Object
.
fromEntries
(
rawHosts
);
// console.log(hosts);
const
vars
=
await
this
.
loadUtilities
();
...
...
@@ -84,7 +118,7 @@ class InventoryBuilder {
return
vars
;
}
host_vars
(
host
)
{
async
host_vars
(
host
)
{
const
connections
=
[];
host
.
dockerServices
=
{
version
:
'
2.4
'
,
...
...
@@ -108,17 +142,17 @@ class InventoryBuilder {
const
from
=
this
.
hosts
[
h
][
host
.
name
];
// 其他主机的这个主机的条目
if
(
from
&&
to
)
{
// 非对称连接
connections
.
push
(
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
to
,
false
,
true
,
false
));
connections
.
push
(
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
from
,
true
,
false
,
true
));
connections
.
push
(
await
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
to
,
false
,
true
,
false
));
connections
.
push
(
await
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
from
,
true
,
false
,
true
));
}
else
if
(
from
||
to
)
{
// 对称连接
const
connectionString
=
from
||
to
;
connections
.
push
(
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
connectionString
,
true
,
true
,
connectionString
===
from
));
connections
.
push
(
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
null_connection
,
false
,
false
,
false
));
connections
.
push
(
await
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
connectionString
,
true
,
true
,
connectionString
===
from
));
connections
.
push
(
await
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
null_connection
,
false
,
false
,
false
));
}
else
{
// 不连接
connections
.
push
(
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
null_connection
,
true
,
false
,
false
));
connections
.
push
(
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
null_connection
,
false
,
true
,
false
));
connections
.
push
(
await
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
null_connection
,
true
,
false
,
false
));
connections
.
push
(
await
this
.
parse_connection
(
host
,
this
.
hosts
[
h
],
null_connection
,
false
,
true
,
false
));
}
routePlans
.
push
({
name
:
h
.
replace
(
/-/g
,
"
_
"
),
...
...
@@ -146,7 +180,7 @@ class InventoryBuilder {
};
}
parse_connection
(
local
:
any
,
remote
:
any
,
connstr
:
string
,
inbound
:
boolean
,
outbound
:
boolean
,
reverse
:
boolean
)
{
async
parse_connection
(
local
:
any
,
remote
:
any
,
connstr
:
string
,
inbound
:
boolean
,
outbound
:
boolean
,
reverse
:
boolean
)
{
const
leftbottom
=
local
.
id
>
remote
.
id
;
// true 条目位于左下,false 条目位于右上
const
cis
=
!
reverse
;
// true 无需翻转,false 需要翻转。
const
primary
=
leftbottom
?
outbound
:
inbound
;
// true 使用 peerAddress、port, false 使用peerAddress2、port2
...
...
@@ -165,6 +199,7 @@ class InventoryBuilder {
//const remoteGatewayMark = remoteGatewayName ? remoteGateway.mark : undefined;
//console.log(remoteGateway.name);
const
remoteAddress
=
remoteGateway
.
address
;
const
resolvedRemoteAddress
=
await
this
.
resolve
(
remoteAddress
);
const
remoteLocalAddress
=
remote
.
address
;
const
remoteNextMark
=
remote
.
nextMark
;
const
remoteDestMark
=
remote
.
destMark
;
...
...
@@ -215,6 +250,7 @@ class InventoryBuilder {
remoteNextMark
,
remoteDestMark
,
remoteAddress
,
resolvedRemoteAddress
,
remoteLocalAddress
,
localPort
,
remotePort
,
...
...
update.sh
View file @
01c3aa51
...
...
@@ -8,11 +8,12 @@ set -e
mkdir
-p
result
npm run build
npm start
#
cd lists
#
./run.sh
#
cd ..
cd
lists
./run.sh
cd
..
cd
ansible
||
exit
...
...
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