Merge pull request #4086 from pwnnex/fix/hysteria2-protocol-aliases

hysteria: accept "hysteria2" as a protocol string (#4081)
This commit is contained in:
pwnnex 2026-04-22 16:02:05 +00:00 committed by GitHub
commit 530c1597b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 60 additions and 21 deletions

View file

@ -21,9 +21,21 @@ const (
Shadowsocks Protocol = "shadowsocks" Shadowsocks Protocol = "shadowsocks"
Mixed Protocol = "mixed" Mixed Protocol = "mixed"
WireGuard Protocol = "wireguard" WireGuard Protocol = "wireguard"
Hysteria Protocol = "hysteria" // UI stores Hysteria v1 and v2 both as "hysteria" and uses
// settings.version to discriminate. Imports from outside the panel
// can carry the literal "hysteria2" string, so IsHysteria below
// accepts both.
Hysteria Protocol = "hysteria"
Hysteria2 Protocol = "hysteria2"
) )
// IsHysteria returns true for both "hysteria" and "hysteria2".
// Use instead of a bare ==model.Hysteria check: a v2 inbound stored
// with the literal v2 string would otherwise fall through (#4081).
func IsHysteria(p Protocol) bool {
return p == Hysteria || p == Hysteria2
}
// User represents a user account in the 3x-ui panel. // User represents a user account in the 3x-ui panel.
type User struct { type User struct {
Id int `json:"id" gorm:"primaryKey;autoIncrement"` Id int `json:"id" gorm:"primaryKey;autoIncrement"`

View file

@ -0,0 +1,22 @@
package model
import "testing"
func TestIsHysteria(t *testing.T) {
cases := []struct {
in Protocol
want bool
}{
{Hysteria, true},
{Hysteria2, true},
{VLESS, false},
{Shadowsocks, false},
{Protocol(""), false},
{Protocol("hysteria3"), false},
}
for _, c := range cases {
if got := IsHysteria(c.in); got != c.want {
t.Errorf("IsHysteria(%q) = %v, want %v", c.in, got, c.want)
}
}
}

View file

@ -159,13 +159,10 @@ func (s *SubClashService) getProxies(inbound *model.Inbound, client model.Client
} }
func (s *SubClashService) buildProxy(inbound *model.Inbound, client model.Client, stream map[string]any, extraRemark string) map[string]any { func (s *SubClashService) buildProxy(inbound *model.Inbound, client model.Client, stream map[string]any, extraRemark string) map[string]any {
// Hysteria (v1 / v2) doesn't ride an xray `streamSettings.network` // Hysteria has its own transport + TLS model, applyTransport /
// transport and the TLS story is handled inside hysteria itself, so // applySecurity don't fit. IsHysteria also covers the literal
// applyTransport / applySecurity below don't model it. Build the // "hysteria2" protocol string (#4081).
// proxy directly. Without this, hysteria inbounds fell into the if model.IsHysteria(inbound.Protocol) {
// `default: return nil` branch and silently vanished from the
// generated Clash config.
if inbound.Protocol == model.Hysteria {
return s.buildHysteriaProxy(inbound, client, extraRemark) return s.buildHysteriaProxy(inbound, client, extraRemark)
} }

View file

@ -209,7 +209,7 @@ func (s *SubJsonService) getConfig(inbound *model.Inbound, client model.Client,
newOutbounds = append(newOutbounds, s.genVless(inbound, streamSettings, client)) newOutbounds = append(newOutbounds, s.genVless(inbound, streamSettings, client))
case "trojan", "shadowsocks": case "trojan", "shadowsocks":
newOutbounds = append(newOutbounds, s.genServer(inbound, streamSettings, client)) newOutbounds = append(newOutbounds, s.genServer(inbound, streamSettings, client))
case "hysteria": case "hysteria", "hysteria2":
newOutbounds = append(newOutbounds, s.genHy(inbound, newStream, client)) newOutbounds = append(newOutbounds, s.genHy(inbound, newStream, client))
} }

View file

@ -115,12 +115,14 @@ func (s *SubService) GetSubs(subId string, host string) ([]string, int64, xray.C
func (s *SubService) getInboundsBySubId(subId string) ([]*model.Inbound, error) { func (s *SubService) getInboundsBySubId(subId string) ([]*model.Inbound, error) {
db := database.GetDB() db := database.GetDB()
var inbounds []*model.Inbound var inbounds []*model.Inbound
// allow "hysteria2" so imports stored with the literal v2 protocol
// string still surface here (#4081)
err := db.Model(model.Inbound{}).Preload("ClientStats").Where(`id in ( err := db.Model(model.Inbound{}).Preload("ClientStats").Where(`id in (
SELECT DISTINCT inbounds.id SELECT DISTINCT inbounds.id
FROM inbounds, FROM inbounds,
JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client
WHERE WHERE
protocol in ('vmess','vless','trojan','shadowsocks','hysteria') protocol in ('vmess','vless','trojan','shadowsocks','hysteria','hysteria2')
AND JSON_EXTRACT(client.value, '$.subId') = ? AND enable = ? AND JSON_EXTRACT(client.value, '$.subId') = ? AND enable = ?
)`, subId, true).Find(&inbounds).Error )`, subId, true).Find(&inbounds).Error
if err != nil { if err != nil {
@ -171,7 +173,7 @@ func (s *SubService) getLink(inbound *model.Inbound, email string) string {
return s.genTrojanLink(inbound, email) return s.genTrojanLink(inbound, email)
case "shadowsocks": case "shadowsocks":
return s.genShadowsocksLink(inbound, email) return s.genShadowsocksLink(inbound, email)
case "hysteria": case "hysteria", "hysteria2":
return s.genHysteriaLink(inbound, email) return s.genHysteriaLink(inbound, email)
} }
return "" return ""
@ -906,7 +908,7 @@ func (s *SubService) genShadowsocksLink(inbound *model.Inbound, email string) st
} }
func (s *SubService) genHysteriaLink(inbound *model.Inbound, email string) string { func (s *SubService) genHysteriaLink(inbound *model.Inbound, email string) string {
if inbound.Protocol != model.Hysteria { if !model.IsHysteria(inbound.Protocol) {
return "" return ""
} }
var stream map[string]interface{} var stream map[string]interface{}
@ -985,12 +987,18 @@ func (s *SubService) genHysteriaLink(inbound *model.Inbound, email string) strin
if len(externalProxies) > 0 { if len(externalProxies) > 0 {
links := make([]string, 0, len(externalProxies)) links := make([]string, 0, len(externalProxies))
for _, externalProxy := range externalProxies { for _, externalProxy := range externalProxies {
ep, _ := externalProxy.(map[string]interface{}) ep, ok := externalProxy.(map[string]interface{})
if !ok {
continue
}
dest, _ := ep["dest"].(string) dest, _ := ep["dest"].(string)
epPort := int(ep["port"].(float64)) portF, okPort := ep["port"].(float64)
if dest == "" || !okPort {
continue
}
epRemark, _ := ep["remark"].(string) epRemark, _ := ep["remark"].(string)
link := fmt.Sprintf("%s://%s@%s:%d", protocol, auth, dest, epPort) link := fmt.Sprintf("%s://%s@%s:%d", protocol, auth, dest, int(portF))
u, _ := url.Parse(link) u, _ := url.Parse(link)
q := u.Query() q := u.Query()
for k, v := range params { for k, v := range params {

View file

@ -270,7 +270,7 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, boo
if client.Email == "" { if client.Email == "" {
return inbound, false, common.NewError("empty client ID") return inbound, false, common.NewError("empty client ID")
} }
case "hysteria": case "hysteria", "hysteria2":
if client.Auth == "" { if client.Auth == "" {
return inbound, false, common.NewError("empty client ID") return inbound, false, common.NewError("empty client ID")
} }
@ -675,7 +675,7 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) (bool, error) {
if client.Email == "" { if client.Email == "" {
return false, common.NewError("empty client ID") return false, common.NewError("empty client ID")
} }
case "hysteria": case "hysteria", "hysteria2":
if client.Auth == "" { if client.Auth == "" {
return false, common.NewError("empty client ID") return false, common.NewError("empty client ID")
} }
@ -769,7 +769,7 @@ func (s *InboundService) DelInboundClient(inboundId int, clientId string) (bool,
client_key = "password" client_key = "password"
case "shadowsocks": case "shadowsocks":
client_key = "email" client_key = "email"
case "hysteria": case "hysteria", "hysteria2":
client_key = "auth" client_key = "auth"
} }
@ -877,7 +877,7 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId strin
case "shadowsocks": case "shadowsocks":
oldClientId = oldClient.Email oldClientId = oldClient.Email
newClientId = clients[0].Email newClientId = clients[0].Email
case "hysteria": case "hysteria", "hysteria2":
oldClientId = oldClient.Auth oldClientId = oldClient.Auth
newClientId = clients[0].Auth newClientId = clients[0].Auth
default: default:

View file

@ -231,7 +231,7 @@ func (x *XrayAPI) AddUser(Protocol string, inboundTag string, user map[string]an
Email: userEmail, Email: userEmail,
}) })
} }
case "hysteria": case "hysteria", "hysteria2":
auth, err := getRequiredUserString(user, "auth") auth, err := getRequiredUserString(user, "auth")
if err != nil { if err != nil {
return err return err