chore: code optimizations

This commit is contained in:
Toby
2022-06-06 16:06:30 -07:00
parent 32f35894cc
commit 6d8e79170e
6 changed files with 32 additions and 29 deletions

View File

@@ -63,10 +63,10 @@ type serverConfig struct {
User string `json:"user"`
Password string `json:"password"`
} `json:"socks5_outbound"`
SocketConfig struct {
BindAddress string `json:"bind_address"`
BindToDevice string `json:"bind_to_device"`
} `json:"socket_config"`
BindOutbound struct {
Address string `json:"address"`
Device string `json:"device"`
} `json:"bind_outbound"`
}
func (c *serverConfig) Speed() (uint64, uint64, error) {

View File

@@ -164,32 +164,26 @@ func server(config *serverConfig) {
}
transport.DefaultServerTransport.SOCKS5Client = ob
}
// socket settings
if config.SocketConfig.BindToDevice != "" {
iface, err := net.InterfaceByName(config.SocketConfig.BindToDevice)
// Bind outbound
if config.BindOutbound.Device != "" {
iface, err := net.InterfaceByName(config.BindOutbound.Device)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Fatal("Failed to get bind_to_device")
}).Fatal("Failed to find the interface")
}
transport.DefaultServerTransport.Intf = iface
transport.DefaultServerTransport.LocalUDPIntf = iface
sockopt.BindDialer(transport.DefaultServerTransport.Dialer, iface)
}
if config.SocketConfig.BindAddress != "" {
ip := net.ParseIP(config.SocketConfig.BindAddress)
if config.BindOutbound.Address != "" {
ip := net.ParseIP(config.BindOutbound.Address)
if ip == nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Fatal("Failed to parse bind_address")
}
transport.DefaultServerTransport.Dialer.LocalAddr = &net.TCPAddr{
IP: ip,
Port: 0,
}
transport.DefaultServerTransport.LocalAddrUDP = &net.UDPAddr{
IP: ip,
Port: 0,
}).Fatal("Failed to parse the address")
}
transport.DefaultServerTransport.Dialer.LocalAddr = &net.TCPAddr{IP: ip}
transport.DefaultServerTransport.LocalUDPAddr = &net.UDPAddr{IP: ip}
}
// ACL
var aclEngine *acl.Engine

View File

@@ -8,9 +8,15 @@ import (
)
func bindRawConn(network string, c syscall.RawConn, bindIface *net.Interface) error {
return c.Control(func(fd uintptr) {
var err1, err2 error
err1 = c.Control(func(fd uintptr) {
if bindIface != nil {
unix.BindToDevice(int(fd), bindIface.Name)
err2 = unix.BindToDevice(int(fd), bindIface.Name)
}
})
if err1 != nil {
return err1
} else {
return err2
}
}

View File

@@ -3,8 +3,11 @@
package sockopt
import (
"errors"
"net"
"syscall"
)
func bindRawConn(network string, c syscall.RawConn, bindIface *net.Interface) error { return nil }
func bindRawConn(network string, c syscall.RawConn, bindIface *net.Interface) error {
return errors.New("binding interface is not supported on the current system")
}

View File

@@ -21,8 +21,8 @@ type ServerTransport struct {
PrefEnabled bool
PrefIPv6 bool
PrefExclusive bool
LocalAddrUDP *net.UDPAddr
Intf *net.Interface
LocalUDPAddr *net.UDPAddr
LocalUDPIntf *net.Interface
}
// AddrEx is like net.TCPAddr or net.UDPAddr, but with additional domain information for SOCKS5.
@@ -167,12 +167,12 @@ func (st *ServerTransport) ListenUDP() (PUDPConn, error) {
if st.SOCKS5Client != nil {
return st.SOCKS5Client.ListenUDP()
} else {
conn, err := net.ListenUDP("udp", st.LocalAddrUDP)
conn, err := net.ListenUDP("udp", st.LocalUDPAddr)
if err != nil {
return nil, err
}
if st.Intf != nil {
err = sockopt.BindUDPConn("udp", conn, st.Intf)
if st.LocalUDPIntf != nil {
err = sockopt.BindUDPConn("udp", conn, st.LocalUDPIntf)
if err != nil {
conn.Close()
return nil, err