mirror of
https://github.com/cedar2025/hysteria.git
synced 2025-06-13 07:59:55 +00:00
chore: move auth funcs to a separate file
This commit is contained in:
parent
9e521a7615
commit
d9d80ecbb1
59
cmd/auth/funcs.go
Normal file
59
cmd/auth/funcs.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/HyNetwork/hysteria/pkg/core"
|
||||||
|
"github.com/yosuke-furukawa/json5/encoding/json5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PasswordAuthFunc(rawMsg json5.RawMessage) (core.ConnectFunc, error) {
|
||||||
|
var pwds []string
|
||||||
|
err := json5.Unmarshal(rawMsg, &pwds)
|
||||||
|
if err != nil {
|
||||||
|
// not a string list, legacy format?
|
||||||
|
var pwdConfig map[string]string
|
||||||
|
err = json5.Unmarshal(rawMsg, &pwdConfig)
|
||||||
|
if err != nil || len(pwdConfig["password"]) == 0 {
|
||||||
|
// still no, invalid config
|
||||||
|
return nil, errors.New("invalid config")
|
||||||
|
}
|
||||||
|
// yes it is
|
||||||
|
pwds = []string{pwdConfig["password"]}
|
||||||
|
}
|
||||||
|
return func(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string) {
|
||||||
|
for _, pwd := range pwds {
|
||||||
|
if string(auth) == pwd {
|
||||||
|
return true, "Welcome"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, "Wrong password"
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExternalAuthFunc(rawMsg json5.RawMessage) (core.ConnectFunc, error) {
|
||||||
|
var extConfig map[string]string
|
||||||
|
err := json5.Unmarshal(rawMsg, &extConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("invalid config")
|
||||||
|
}
|
||||||
|
if len(extConfig["http"]) != 0 {
|
||||||
|
hp := &HTTPAuthProvider{
|
||||||
|
Client: &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
},
|
||||||
|
URL: extConfig["http"],
|
||||||
|
}
|
||||||
|
return hp.Auth, nil
|
||||||
|
} else if len(extConfig["cmd"]) != 0 {
|
||||||
|
cp := &CmdAuthProvider{
|
||||||
|
Cmd: extConfig["cmd"],
|
||||||
|
}
|
||||||
|
return cp.Auth, nil
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("invalid config")
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -116,7 +115,7 @@ func server(config *serverConfig) {
|
|||||||
return true, "Welcome"
|
return true, "Welcome"
|
||||||
}
|
}
|
||||||
case "password", "passwords":
|
case "password", "passwords":
|
||||||
authFunc, err = passwordAuthFunc(config.Auth.Config)
|
authFunc, err = auth.PasswordAuthFunc(config.Auth.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"error": err,
|
"error": err,
|
||||||
@ -125,7 +124,7 @@ func server(config *serverConfig) {
|
|||||||
logrus.Info("Password authentication enabled")
|
logrus.Info("Password authentication enabled")
|
||||||
}
|
}
|
||||||
case "external":
|
case "external":
|
||||||
authFunc, err = externalAuthFunc(config.Auth.Config)
|
authFunc, err = auth.ExternalAuthFunc(config.Auth.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"error": err,
|
"error": err,
|
||||||
@ -252,54 +251,6 @@ func server(config *serverConfig) {
|
|||||||
logrus.WithField("error", err).Fatal("Server shutdown")
|
logrus.WithField("error", err).Fatal("Server shutdown")
|
||||||
}
|
}
|
||||||
|
|
||||||
func passwordAuthFunc(rawMsg json5.RawMessage) (core.ConnectFunc, error) {
|
|
||||||
var pwds []string
|
|
||||||
err := json5.Unmarshal(rawMsg, &pwds)
|
|
||||||
if err != nil {
|
|
||||||
// not a string list, legacy format?
|
|
||||||
var pwdConfig map[string]string
|
|
||||||
err = json5.Unmarshal(rawMsg, &pwdConfig)
|
|
||||||
if err != nil || len(pwdConfig["password"]) == 0 {
|
|
||||||
// still no, invalid config
|
|
||||||
return nil, errors.New("invalid config")
|
|
||||||
}
|
|
||||||
// yes it is
|
|
||||||
pwds = []string{pwdConfig["password"]}
|
|
||||||
}
|
|
||||||
return func(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string) {
|
|
||||||
for _, pwd := range pwds {
|
|
||||||
if string(auth) == pwd {
|
|
||||||
return true, "Welcome"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, "Wrong password"
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func externalAuthFunc(rawMsg json5.RawMessage) (core.ConnectFunc, error) {
|
|
||||||
var extConfig map[string]string
|
|
||||||
err := json5.Unmarshal(rawMsg, &extConfig)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("invalid config")
|
|
||||||
}
|
|
||||||
if len(extConfig["http"]) != 0 {
|
|
||||||
hp := &auth.HTTPAuthProvider{
|
|
||||||
Client: &http.Client{
|
|
||||||
Timeout: 10 * time.Second,
|
|
||||||
},
|
|
||||||
URL: extConfig["http"],
|
|
||||||
}
|
|
||||||
return hp.Auth, nil
|
|
||||||
} else if len(extConfig["cmd"]) != 0 {
|
|
||||||
cp := &auth.CmdAuthProvider{
|
|
||||||
Cmd: extConfig["cmd"],
|
|
||||||
}
|
|
||||||
return cp.Auth, nil
|
|
||||||
} else {
|
|
||||||
return nil, errors.New("invalid config")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func disconnectFunc(addr net.Addr, auth []byte, err error) {
|
func disconnectFunc(addr net.Addr, auth []byte, err error) {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"src": defaultIPMasker.Mask(addr.String()),
|
"src": defaultIPMasker.Mask(addr.String()),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user