forked from fortuna/ss-example
-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable fwmark
(SO_MARK
) for outgoing sockets
#202
Open
sabify
wants to merge
3
commits into
Jigsaw-Code:master
Choose a base branch
from
sabify:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,16 +31,20 @@ import ( | |
|
||
"github.com/Jigsaw-Code/outline-sdk/transport" | ||
"github.com/Jigsaw-Code/outline-sdk/transport/shadowsocks" | ||
"github.com/Jigsaw-Code/outline-ss-server/ipinfo" | ||
"github.com/Jigsaw-Code/outline-ss-server/service" | ||
"github.com/lmittmann/tint" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"golang.org/x/term" | ||
|
||
"github.com/Jigsaw-Code/outline-ss-server/ipinfo" | ||
onet "github.com/Jigsaw-Code/outline-ss-server/net" | ||
"github.com/Jigsaw-Code/outline-ss-server/service" | ||
) | ||
|
||
var logLevel = new(slog.LevelVar) // Info by default | ||
var logHandler slog.Handler | ||
var ( | ||
logLevel = new(slog.LevelVar) // Info by default | ||
logHandler slog.Handler | ||
) | ||
|
||
// Set by goreleaser default ldflags. See https://goreleaser.com/customization/build/ | ||
var version = "dev" | ||
|
@@ -120,30 +124,32 @@ func newCipherListFromConfig(config ServiceConfig) (service.CipherList, error) { | |
return ciphers, nil | ||
} | ||
|
||
func (s *SSServer) NewShadowsocksStreamHandler(ciphers service.CipherList) service.StreamHandler { | ||
func (s *SSServer) NewShadowsocksStreamHandler(ciphers service.CipherList, dialer transport.StreamDialer) service.StreamHandler { | ||
authFunc := service.NewShadowsocksStreamAuthenticator(ciphers, &s.replayCache, s.m.tcpServiceMetrics) | ||
// TODO: Register initial data metrics at zero. | ||
return service.NewStreamHandler(authFunc, tcpReadTimeout) | ||
return service.NewStreamHandler(authFunc, tcpReadTimeout, dialer) | ||
} | ||
|
||
func (s *SSServer) NewShadowsocksPacketHandler(ciphers service.CipherList) service.PacketHandler { | ||
return service.NewPacketHandler(s.natTimeout, ciphers, s.m, s.m.udpServiceMetrics) | ||
func (s *SSServer) NewShadowsocksPacketHandler(ciphers service.CipherList, dialer service.UDPDialer) service.PacketHandler { | ||
return service.NewPacketHandler(s.natTimeout, ciphers, s.m, s.m.udpServiceMetrics, dialer) | ||
} | ||
|
||
func (s *SSServer) NewShadowsocksStreamHandlerFromConfig(config ServiceConfig) (service.StreamHandler, error) { | ||
ciphers, err := newCipherListFromConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s.NewShadowsocksStreamHandler(ciphers), nil | ||
dialer := service.MakeValidatingTCPStreamDialer(onet.RequirePublicIP, config.Dialer.Fwmark) | ||
return s.NewShadowsocksStreamHandler(ciphers, dialer), nil | ||
} | ||
|
||
func (s *SSServer) NewShadowsocksPacketHandlerFromConfig(config ServiceConfig) (service.PacketHandler, error) { | ||
ciphers, err := newCipherListFromConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s.NewShadowsocksPacketHandler(ciphers), nil | ||
dialer := service.MakeTargetPacketListener(config.Dialer.Fwmark) | ||
return s.NewShadowsocksPacketHandler(ciphers, dialer), nil | ||
} | ||
|
||
type listenerSet struct { | ||
|
@@ -243,7 +249,8 @@ func (s *SSServer) runConfig(config Config) (func() error, error) { | |
ciphers := service.NewCipherList() | ||
ciphers.Update(cipherList) | ||
|
||
sh := s.NewShadowsocksStreamHandler(ciphers) | ||
tcpDialer := service.MakeValidatingTCPStreamDialer(onet.RequirePublicIP, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this take serviceConfig.Dialer.Fwmark? |
||
sh := s.NewShadowsocksStreamHandler(ciphers, tcpDialer) | ||
ln, err := lnSet.ListenStream(addr) | ||
if err != nil { | ||
return err | ||
|
@@ -259,7 +266,8 @@ func (s *SSServer) runConfig(config Config) (func() error, error) { | |
return err | ||
} | ||
slog.Info("UDP service started.", "address", pc.LocalAddr().String()) | ||
ph := s.NewShadowsocksPacketHandler(ciphers) | ||
udpDialer := service.MakeTargetPacketListener(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, shouldn't it take the fwmark? |
||
ph := s.NewShadowsocksPacketHandler(ciphers, udpDialer) | ||
go ph.Handle(pc) | ||
} | ||
|
||
|
@@ -275,7 +283,12 @@ func (s *SSServer) runConfig(config Config) (func() error, error) { | |
if err != nil { | ||
return err | ||
} | ||
slog.Info("TCP service started.", "address", ln.Addr().String()) | ||
slog.Info("TCP service started.", "address", ln.Addr().String(), "fwmark", func() any { | ||
if serviceConfig.Dialer.Fwmark == 0 { | ||
return "disabled" | ||
} | ||
return serviceConfig.Dialer.Fwmark | ||
}()) | ||
if sh == nil { | ||
sh, err = s.NewShadowsocksStreamHandlerFromConfig(serviceConfig) | ||
if err != nil { | ||
|
@@ -291,7 +304,12 @@ func (s *SSServer) runConfig(config Config) (func() error, error) { | |
if err != nil { | ||
return err | ||
} | ||
slog.Info("UDP service started.", "address", pc.LocalAddr().String()) | ||
slog.Info("UDP service started.", "address", pc.LocalAddr().String(), "fwmark", func() any { | ||
if serviceConfig.Dialer.Fwmark == 0 { | ||
return "disabled" | ||
} | ||
return serviceConfig.Dialer.Fwmark | ||
}()) | ||
if ph == nil { | ||
ph, err = s.NewShadowsocksPacketHandlerFromConfig(serviceConfig) | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018 Jigsaw Operations LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux | ||
|
||
package service | ||
|
||
import ( | ||
"log/slog" | ||
"net" | ||
"os" | ||
"syscall" | ||
|
||
"github.com/Jigsaw-Code/outline-sdk/transport" | ||
|
||
onet "github.com/Jigsaw-Code/outline-ss-server/net" | ||
) | ||
|
||
// fwmark can be used in conjunction with other Linux networking features like cgroups, network namespaces, and TC (Traffic Control) for sophisticated network management. | ||
// Value of 0 disables fwmark (SO_MARK) (Linux Only) | ||
func MakeValidatingTCPStreamDialer(targetIPValidator onet.TargetIPValidator, fwmark uint) transport.StreamDialer { | ||
return &transport.TCPDialer{Dialer: net.Dialer{Control: func(network, address string, c syscall.RawConn) error { | ||
if fwmark > 0 { | ||
err := c.Control(func(fd uintptr) { | ||
err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(fwmark)) | ||
if err != nil { | ||
slog.Error("Set fwmark failed.", "err", os.NewSyscallError("failed to set mark for TCP socket", err)) | ||
} | ||
}) | ||
if err != nil { | ||
slog.Error("Set TCPDialer Control func failed.", "err", err) | ||
return err | ||
} | ||
} | ||
ip, _, _ := net.SplitHostPort(address) | ||
return targetIPValidator(net.ParseIP(ip)) | ||
}}} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.