Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sshuttle
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
pub
sshuttle
Commits
3edeb726
Commit
3edeb726
authored
Jan 10, 2020
by
Julian Wollrath
Committed by
Brian May
Feb 06, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify nftables based method
parent
6e9c58b4
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
68 deletions
+13
-68
manpage.rst
docs/manpage.rst
+1
-1
linux.py
sshuttle/linux.py
+2
-48
nft.py
sshuttle/methods/nft.py
+10
-19
No files found.
docs/manpage.rst
View file @
3edeb726
...
@@ -44,7 +44,7 @@ Options
...
@@ -44,7 +44,7 @@ Options
to during startup will be routed over the VPN. Valid examples are
to during startup will be routed over the VPN. Valid examples are
example.com, example.com:8000 and example.com:8000-9000.
example.com, example.com:8000 and example.com:8000-9000.
.. option:: --method [auto|nat|tproxy|pf]
.. option:: --method [auto|nat|
nft|
tproxy|pf]
Which firewall method should sshuttle use? For auto, sshuttle attempts to
Which firewall method should sshuttle use? For auto, sshuttle attempts to
guess the appropriate method depending on what it can find in PATH. The
guess the appropriate method depending on what it can find in PATH. The
...
...
sshuttle/linux.py
View file @
3edeb726
import
re
import
os
import
os
import
socket
import
socket
import
subprocess
as
ssubprocess
import
subprocess
as
ssubprocess
from
sshuttle.helpers
import
log
,
debug1
,
Fatal
,
family_to_string
from
sshuttle.helpers
import
log
,
debug1
,
Fatal
,
family_to_string
...
@@ -52,10 +50,8 @@ def ipt(family, table, *args):
...
@@ -52,10 +50,8 @@ def ipt(family, table, *args):
def
nft
(
family
,
table
,
action
,
*
args
):
def
nft
(
family
,
table
,
action
,
*
args
):
if
family
==
socket
.
AF_INET
:
if
family
in
(
socket
.
AF_INET
,
socket
.
AF_INET6
):
argv
=
[
'nft'
,
action
,
'ip'
,
table
]
+
list
(
args
)
argv
=
[
'nft'
,
action
,
'inet'
,
table
]
+
list
(
args
)
elif
family
==
socket
.
AF_INET6
:
argv
=
[
'nft'
,
action
,
'ip6'
,
table
]
+
list
(
args
)
else
:
else
:
raise
Exception
(
'Unsupported family "
%
s"'
%
family_to_string
(
family
))
raise
Exception
(
'Unsupported family "
%
s"'
%
family_to_string
(
family
))
debug1
(
'>>
%
s
\n
'
%
' '
.
join
(
argv
))
debug1
(
'>>
%
s
\n
'
%
' '
.
join
(
argv
))
...
@@ -68,48 +64,6 @@ def nft(family, table, action, *args):
...
@@ -68,48 +64,6 @@ def nft(family, table, action, *args):
raise
Fatal
(
'
%
r returned
%
d'
%
(
argv
,
rv
))
raise
Fatal
(
'
%
r returned
%
d'
%
(
argv
,
rv
))
def
nft_chain_exists
(
family
,
table
,
name
):
if
family
==
socket
.
AF_INET
:
fam
=
'ip'
elif
family
==
socket
.
AF_INET6
:
fam
=
'ip6'
else
:
raise
Exception
(
'Unsupported family "
%
s"'
%
family_to_string
(
family
))
argv
=
[
'nft'
,
'list'
,
'chain'
,
fam
,
table
,
name
]
debug1
(
'>>
%
s
\n
'
%
' '
.
join
(
argv
))
env
=
{
'PATH'
:
os
.
environ
[
'PATH'
],
'LC_ALL'
:
"C"
,
}
try
:
table_exists
=
False
output
=
ssubprocess
.
check_output
(
argv
,
env
=
env
,
stderr
=
ssubprocess
.
STDOUT
)
for
line
in
output
.
decode
(
'ASCII'
)
.
split
(
'
\n
'
):
if
line
.
startswith
(
'table
%
s
%
s '
%
(
fam
,
table
)):
table_exists
=
True
if
table_exists
and
(
'chain
%
s {'
%
name
)
in
line
:
return
True
except
ssubprocess
.
CalledProcessError
:
return
False
def
nft_get_handle
(
expression
,
chain
):
cmd
=
'nft'
argv
=
[
cmd
,
'list'
,
expression
,
'-a'
]
env
=
{
'PATH'
:
os
.
environ
[
'PATH'
],
'LC_ALL'
:
"C"
,
}
try
:
output
=
ssubprocess
.
check_output
(
argv
,
env
=
env
)
for
line
in
output
.
decode
(
'utf-8'
)
.
split
(
'
\n
'
):
if
(
'jump
%
s'
%
chain
)
in
line
:
return
re
.
sub
(
'.*# '
,
''
,
line
)
except
ssubprocess
.
CalledProcessError
as
e
:
raise
Fatal
(
'
%
r returned
%
d'
%
(
argv
,
e
.
returncode
))
_no_ttl_module
=
False
_no_ttl_module
=
False
...
...
sshuttle/methods/nft.py
View file @
3edeb726
import
socket
import
socket
from
sshuttle.firewall
import
subnet_weight
from
sshuttle.firewall
import
subnet_weight
from
sshuttle.linux
import
nft
,
n
ft_get_handle
,
nft_chain_exists
,
n
onfatal
from
sshuttle.linux
import
nft
,
nonfatal
from
sshuttle.methods
import
BaseMethod
from
sshuttle.methods
import
BaseMethod
...
@@ -16,22 +16,19 @@ class Method(BaseMethod):
...
@@ -16,22 +16,19 @@ class Method(BaseMethod):
if
udp
:
if
udp
:
raise
Exception
(
"UDP not supported by nft"
)
raise
Exception
(
"UDP not supported by nft"
)
table
=
"nat"
table
=
'sshuttle-
%
s'
%
port
def
_nft
(
action
,
*
args
):
def
_nft
(
action
,
*
args
):
return
nft
(
family
,
table
,
action
,
*
args
)
return
nft
(
family
,
table
,
action
,
*
args
)
chain
=
table
# basic cleanup/setup of chains
# basic cleanup/setup of chains
_nft
(
'add table'
,
''
)
_nft
(
'add table'
,
''
)
# prerouting, postrouting, and output chains may already exist
_nft
(
'add chain'
,
'prerouting'
,
for
chain
in
[
'prerouting'
,
'postrouting'
,
'output'
]:
'{ type nat hook prerouting priority -100; policy accept; }'
)
rules
=
'{{ type nat hook {} priority -100; policy accept; }}'
\
_nft
(
'add chain'
,
'output'
,
.
format
(
chain
)
'{ type nat hook output priority -100; policy accept; }'
)
if
not
nft_chain_exists
(
family
,
table
,
chain
):
_nft
(
'add chain'
,
chain
,
rules
)
chain
=
'sshuttle-
%
s'
%
port
_nft
(
'add chain'
,
chain
)
_nft
(
'add chain'
,
chain
)
_nft
(
'flush chain'
,
chain
)
_nft
(
'flush chain'
,
chain
)
_nft
(
'add rule'
,
'output jump
%
s'
%
chain
)
_nft
(
'add rule'
,
'output jump
%
s'
%
chain
)
...
@@ -70,16 +67,10 @@ class Method(BaseMethod):
...
@@ -70,16 +67,10 @@ class Method(BaseMethod):
if
udp
:
if
udp
:
raise
Exception
(
"UDP not supported by nft method_name"
)
raise
Exception
(
"UDP not supported by nft method_name"
)
table
=
"nat"
table
=
'sshuttle-
%
s'
%
port
def
_nft
(
action
,
*
args
):
def
_nft
(
action
,
*
args
):
return
nft
(
family
,
table
,
action
,
*
args
)
return
nft
(
family
,
table
,
action
,
*
args
)
chain
=
'sshuttle-
%
s'
%
port
# basic cleanup/setup of chains
# basic cleanup/setup of chains
handle
=
nft_get_handle
(
'chain ip nat output'
,
chain
)
nonfatal
(
_nft
,
'delete table'
,
''
)
nonfatal
(
_nft
,
'delete rule'
,
'output'
,
handle
)
handle
=
nft_get_handle
(
'chain ip nat prerouting'
,
chain
)
nonfatal
(
_nft
,
'delete rule'
,
'prerouting'
,
handle
)
nonfatal
(
_nft
,
'delete chain'
,
chain
)
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