mirror of
https://github.com/cmz0228/hysteria-dev.git
synced 2025-08-24 02:01:47 +00:00
chore: code optimizations
This commit is contained in:
@@ -63,10 +63,10 @@ type serverConfig struct {
|
|||||||
User string `json:"user"`
|
User string `json:"user"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
} `json:"socks5_outbound"`
|
} `json:"socks5_outbound"`
|
||||||
SocketConfig struct {
|
BindOutbound struct {
|
||||||
BindAddress string `json:"bind_address"`
|
Address string `json:"address"`
|
||||||
BindToDevice string `json:"bind_to_device"`
|
Device string `json:"device"`
|
||||||
} `json:"socket_config"`
|
} `json:"bind_outbound"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *serverConfig) Speed() (uint64, uint64, error) {
|
func (c *serverConfig) Speed() (uint64, uint64, error) {
|
||||||
|
@@ -164,32 +164,26 @@ func server(config *serverConfig) {
|
|||||||
}
|
}
|
||||||
transport.DefaultServerTransport.SOCKS5Client = ob
|
transport.DefaultServerTransport.SOCKS5Client = ob
|
||||||
}
|
}
|
||||||
// socket settings
|
// Bind outbound
|
||||||
if config.SocketConfig.BindToDevice != "" {
|
if config.BindOutbound.Device != "" {
|
||||||
iface, err := net.InterfaceByName(config.SocketConfig.BindToDevice)
|
iface, err := net.InterfaceByName(config.BindOutbound.Device)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"error": err,
|
"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)
|
sockopt.BindDialer(transport.DefaultServerTransport.Dialer, iface)
|
||||||
}
|
}
|
||||||
if config.SocketConfig.BindAddress != "" {
|
if config.BindOutbound.Address != "" {
|
||||||
ip := net.ParseIP(config.SocketConfig.BindAddress)
|
ip := net.ParseIP(config.BindOutbound.Address)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"error": err,
|
"error": err,
|
||||||
}).Fatal("Failed to parse bind_address")
|
}).Fatal("Failed to parse the address")
|
||||||
}
|
|
||||||
transport.DefaultServerTransport.Dialer.LocalAddr = &net.TCPAddr{
|
|
||||||
IP: ip,
|
|
||||||
Port: 0,
|
|
||||||
}
|
|
||||||
transport.DefaultServerTransport.LocalAddrUDP = &net.UDPAddr{
|
|
||||||
IP: ip,
|
|
||||||
Port: 0,
|
|
||||||
}
|
}
|
||||||
|
transport.DefaultServerTransport.Dialer.LocalAddr = &net.TCPAddr{IP: ip}
|
||||||
|
transport.DefaultServerTransport.LocalUDPAddr = &net.UDPAddr{IP: ip}
|
||||||
}
|
}
|
||||||
// ACL
|
// ACL
|
||||||
var aclEngine *acl.Engine
|
var aclEngine *acl.Engine
|
||||||
|
@@ -5,7 +5,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
//https://github.com/v2fly/v2ray-core/blob/4e247840821f3dd326722d4db02ee3c237074fc2/transport/internet/config.pb.go#L420-L426
|
// https://github.com/v2fly/v2ray-core/blob/4e247840821f3dd326722d4db02ee3c237074fc2/transport/internet/config.pb.go#L420-L426
|
||||||
|
|
||||||
func BindDialer(d *net.Dialer, intf *net.Interface) {
|
func BindDialer(d *net.Dialer, intf *net.Interface) {
|
||||||
d.Control = func(network, address string, c syscall.RawConn) error {
|
d.Control = func(network, address string, c syscall.RawConn) error {
|
||||||
|
@@ -8,9 +8,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func bindRawConn(network string, c syscall.RawConn, bindIface *net.Interface) error {
|
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 {
|
if bindIface != nil {
|
||||||
unix.BindToDevice(int(fd), bindIface.Name)
|
err2 = unix.BindToDevice(int(fd), bindIface.Name)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
if err1 != nil {
|
||||||
|
return err1
|
||||||
|
} else {
|
||||||
|
return err2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,8 +3,11 @@
|
|||||||
package sockopt
|
package sockopt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"syscall"
|
"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")
|
||||||
|
}
|
||||||
|
@@ -21,8 +21,8 @@ type ServerTransport struct {
|
|||||||
PrefEnabled bool
|
PrefEnabled bool
|
||||||
PrefIPv6 bool
|
PrefIPv6 bool
|
||||||
PrefExclusive bool
|
PrefExclusive bool
|
||||||
LocalAddrUDP *net.UDPAddr
|
LocalUDPAddr *net.UDPAddr
|
||||||
Intf *net.Interface
|
LocalUDPIntf *net.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddrEx is like net.TCPAddr or net.UDPAddr, but with additional domain information for SOCKS5.
|
// 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 {
|
if st.SOCKS5Client != nil {
|
||||||
return st.SOCKS5Client.ListenUDP()
|
return st.SOCKS5Client.ListenUDP()
|
||||||
} else {
|
} else {
|
||||||
conn, err := net.ListenUDP("udp", st.LocalAddrUDP)
|
conn, err := net.ListenUDP("udp", st.LocalUDPAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if st.Intf != nil {
|
if st.LocalUDPIntf != nil {
|
||||||
err = sockopt.BindUDPConn("udp", conn, st.Intf)
|
err = sockopt.BindUDPConn("udp", conn, st.LocalUDPIntf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Reference in New Issue
Block a user