feat: remove separate udp-hop protocol, auto detect by address format

This commit is contained in:
Toby 2022-11-02 23:55:07 -07:00
parent a5985c5b6f
commit e329e8a2e9
3 changed files with 20 additions and 28 deletions

View file

@ -32,7 +32,6 @@ import (
var clientPacketConnFuncFactoryMap = map[string]pktconns.ClientPacketConnFuncFactory{
"": pktconns.NewClientUDPConnFunc,
"udp": pktconns.NewClientUDPConnFunc,
"udp-hop": pktconns.NewClientUDPHopConnFunc,
"wechat": pktconns.NewClientWeChatConnFunc,
"wechat-video": pktconns.NewClientWeChatConnFunc,
"faketcp": pktconns.NewClientFakeTCPConnFunc,

View file

@ -26,7 +26,6 @@ import (
var serverPacketConnFuncFactoryMap = map[string]pktconns.ServerPacketConnFuncFactory{
"": pktconns.NewServerUDPConnFunc,
"udp": pktconns.NewServerUDPConnFunc,
"udp-hop": pktconns.NewServerUDPHopConnFunc,
"wechat": pktconns.NewServerWeChatConnFunc,
"wechat-video": pktconns.NewServerWeChatConnFunc,
"faketcp": pktconns.NewServerFakeTCPConnFunc,

View file

@ -2,6 +2,7 @@ package pktconns
import (
"net"
"strings"
"github.com/HyNetwork/hysteria/pkg/transport/pktconns/udphop"
@ -24,6 +25,9 @@ type (
func NewClientUDPConnFunc(obfsPassword string) ClientPacketConnFunc {
if obfsPassword == "" {
return func(server string) (net.PacketConn, net.Addr, error) {
if isAddrPortHopping(server) {
return udphop.NewObfsUDPHopClientPacketConn(server, nil)
}
sAddr, err := net.ResolveUDPAddr("udp", server)
if err != nil {
return nil, nil, err
@ -33,6 +37,10 @@ func NewClientUDPConnFunc(obfsPassword string) ClientPacketConnFunc {
}
} else {
return func(server string) (net.PacketConn, net.Addr, error) {
if isAddrPortHopping(server) {
ob := obfs.NewXPlusObfuscator([]byte(obfsPassword))
return udphop.NewObfsUDPHopClientPacketConn(server, ob)
}
sAddr, err := net.ResolveUDPAddr("udp", server)
if err != nil {
return nil, nil, err
@ -47,19 +55,6 @@ func NewClientUDPConnFunc(obfsPassword string) ClientPacketConnFunc {
}
}
func NewClientUDPHopConnFunc(obfsPassword string) ClientPacketConnFunc {
if obfsPassword == "" {
return func(server string) (net.PacketConn, net.Addr, error) {
return udphop.NewObfsUDPHopClientPacketConn(server, nil)
}
} else {
return func(server string) (net.PacketConn, net.Addr, error) {
ob := obfs.NewXPlusObfuscator([]byte(obfsPassword))
return udphop.NewObfsUDPHopClientPacketConn(server, ob)
}
}
}
func NewClientWeChatConnFunc(obfsPassword string) ClientPacketConnFunc {
if obfsPassword == "" {
return func(server string) (net.PacketConn, net.Addr, error) {
@ -118,6 +113,9 @@ func NewClientFakeTCPConnFunc(obfsPassword string) ClientPacketConnFunc {
func NewServerUDPConnFunc(obfsPassword string) ServerPacketConnFunc {
if obfsPassword == "" {
return func(listen string) (net.PacketConn, error) {
if isAddrPortHopping(listen) {
return udphop.NewObfsUDPHopServerPacketConn(listen, nil)
}
laddrU, err := net.ResolveUDPAddr("udp", listen)
if err != nil {
return nil, err
@ -126,6 +124,10 @@ func NewServerUDPConnFunc(obfsPassword string) ServerPacketConnFunc {
}
} else {
return func(listen string) (net.PacketConn, error) {
if isAddrPortHopping(listen) {
ob := obfs.NewXPlusObfuscator([]byte(obfsPassword))
return udphop.NewObfsUDPHopServerPacketConn(listen, ob)
}
ob := obfs.NewXPlusObfuscator([]byte(obfsPassword))
laddrU, err := net.ResolveUDPAddr("udp", listen)
if err != nil {
@ -140,19 +142,6 @@ func NewServerUDPConnFunc(obfsPassword string) ServerPacketConnFunc {
}
}
func NewServerUDPHopConnFunc(obfsPassword string) ServerPacketConnFunc {
if obfsPassword == "" {
return func(listen string) (net.PacketConn, error) {
return udphop.NewObfsUDPHopServerPacketConn(listen, nil)
}
} else {
return func(listen string) (net.PacketConn, error) {
ob := obfs.NewXPlusObfuscator([]byte(obfsPassword))
return udphop.NewObfsUDPHopServerPacketConn(listen, ob)
}
}
}
func NewServerWeChatConnFunc(obfsPassword string) ServerPacketConnFunc {
if obfsPassword == "" {
return func(listen string) (net.PacketConn, error) {
@ -198,3 +187,8 @@ func NewServerFakeTCPConnFunc(obfsPassword string) ServerPacketConnFunc {
}
}
}
func isAddrPortHopping(addr string) bool {
_, portStr, err := net.SplitHostPort(addr)
return err == nil && strings.Contains(portStr, ",")
}