-
Notifications
You must be signed in to change notification settings - Fork 188
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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 ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func SetFwmark(fd uintptr, fwmark uint) error { | ||
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. Take the |
||
err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(fwmark)) | ||
if err != nil { | ||
os.NewSyscallError("failed to set fwmark for socket", err) | ||
} | ||
return nil | ||
} |
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.