fix: trying to let hysteria's port hopping work
This commit is contained in:
@ -58,20 +58,24 @@ type udpPacket struct {
|
||||
addr net.Addr
|
||||
}
|
||||
|
||||
func NewObfsUDPHopClientPacketConn(server string, hopInterval time.Duration, obfs obfs.Obfuscator, dialer utils.PacketDialer) (*ObfsUDPHopClientPacketConn, error) {
|
||||
host, ports, err := parseAddr(server)
|
||||
func NewObfsUDPHopClientPacketConn(server string, serverPorts string, hopInterval time.Duration, obfs obfs.Obfuscator, dialer utils.PacketDialer) (*ObfsUDPHopClientPacketConn, error) {
|
||||
ports, err := parsePorts(serverPorts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Resolve the server IP address, then attach the ports to UDP addresses
|
||||
ip, err := dialer.RemoteAddr(host)
|
||||
rAddr, err := dialer.RemoteAddr(server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ip, _, err := net.SplitHostPort(rAddr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serverAddrs := make([]net.Addr, len(ports))
|
||||
for i, port := range ports {
|
||||
serverAddrs[i] = &net.UDPAddr{
|
||||
IP: net.ParseIP(ip.String()),
|
||||
IP: net.ParseIP(ip),
|
||||
Port: int(port),
|
||||
}
|
||||
}
|
||||
@ -90,7 +94,7 @@ func NewObfsUDPHopClientPacketConn(server string, hopInterval time.Duration, obf
|
||||
},
|
||||
},
|
||||
}
|
||||
curConn, err := dialer.ListenPacket(ip)
|
||||
curConn, err := dialer.ListenPacket(rAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -100,7 +104,7 @@ func NewObfsUDPHopClientPacketConn(server string, hopInterval time.Duration, obf
|
||||
conn.currentConn = curConn
|
||||
}
|
||||
go conn.recvRoutine(conn.currentConn)
|
||||
go conn.hopRoutine(dialer, ip)
|
||||
go conn.hopRoutine(dialer, rAddr)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
@ -307,29 +311,25 @@ func trySetPacketConnWriteBuffer(pc net.PacketConn, bytes int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseAddr parses the multi-port server address and returns the host and ports.
|
||||
// parsePorts parses the multi-port server address and returns the host and ports.
|
||||
// Supports both comma-separated single ports and dash-separated port ranges.
|
||||
// Format: "host:port1,port2-port3,port4"
|
||||
func parseAddr(addr string) (host string, ports []uint16, err error) {
|
||||
host, portStr, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
portStrs := strings.Split(portStr, ",")
|
||||
func parsePorts(serverPorts string) (ports []uint16, err error) {
|
||||
portStrs := strings.Split(serverPorts, ",")
|
||||
for _, portStr := range portStrs {
|
||||
if strings.Contains(portStr, "-") {
|
||||
// Port range
|
||||
portRange := strings.Split(portStr, "-")
|
||||
if len(portRange) != 2 {
|
||||
return "", nil, net.InvalidAddrError("invalid port range")
|
||||
return nil, net.InvalidAddrError("invalid port range")
|
||||
}
|
||||
start, err := strconv.ParseUint(portRange[0], 10, 16)
|
||||
if err != nil {
|
||||
return "", nil, net.InvalidAddrError("invalid port range")
|
||||
return nil, net.InvalidAddrError("invalid port range")
|
||||
}
|
||||
end, err := strconv.ParseUint(portRange[1], 10, 16)
|
||||
if err != nil {
|
||||
return "", nil, net.InvalidAddrError("invalid port range")
|
||||
return nil, net.InvalidAddrError("invalid port range")
|
||||
}
|
||||
if start > end {
|
||||
start, end = end, start
|
||||
@ -341,10 +341,13 @@ func parseAddr(addr string) (host string, ports []uint16, err error) {
|
||||
// Single port
|
||||
port, err := strconv.ParseUint(portStr, 10, 16)
|
||||
if err != nil {
|
||||
return "", nil, net.InvalidAddrError("invalid port")
|
||||
return nil, net.InvalidAddrError("invalid port")
|
||||
}
|
||||
ports = append(ports, uint16(port))
|
||||
}
|
||||
}
|
||||
return host, ports, nil
|
||||
if len(ports) == 0 {
|
||||
return nil, net.InvalidAddrError("invalid port")
|
||||
}
|
||||
return ports, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user