mirror of
https://github.com/cedar2025/hysteria.git
synced 2025-06-09 05:59:54 +00:00
feat: command auth
This commit is contained in:
parent
d3db1e4a1d
commit
25b8eef959
@ -98,6 +98,7 @@ type serverConfigAuth struct {
|
|||||||
Password string `mapstructure:"password"`
|
Password string `mapstructure:"password"`
|
||||||
UserPass map[string]string `mapstructure:"userpass"`
|
UserPass map[string]string `mapstructure:"userpass"`
|
||||||
HTTP serverConfigAuthHTTP `mapstructure:"http"`
|
HTTP serverConfigAuthHTTP `mapstructure:"http"`
|
||||||
|
Command string `mapstructure:"command"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverConfigResolverTCP struct {
|
type serverConfigResolverTCP struct {
|
||||||
@ -405,6 +406,12 @@ func (c *serverConfig) fillAuthenticator(hyConfig *server.Config) error {
|
|||||||
}
|
}
|
||||||
hyConfig.Authenticator = auth.NewHTTPAuthenticator(c.Auth.HTTP.URL, c.Auth.HTTP.Insecure)
|
hyConfig.Authenticator = auth.NewHTTPAuthenticator(c.Auth.HTTP.URL, c.Auth.HTTP.Insecure)
|
||||||
return nil
|
return nil
|
||||||
|
case "command", "cmd":
|
||||||
|
if c.Auth.Command == "" {
|
||||||
|
return configError{Field: "auth.command", Err: errors.New("empty auth command")}
|
||||||
|
}
|
||||||
|
hyConfig.Authenticator = &auth.CommandAuthenticator{Cmd: c.Auth.Command}
|
||||||
|
return nil
|
||||||
default:
|
default:
|
||||||
return configError{Field: "auth.type", Err: errors.New("unsupported auth type")}
|
return configError{Field: "auth.type", Err: errors.New("unsupported auth type")}
|
||||||
}
|
}
|
||||||
|
@ -70,6 +70,7 @@ func TestServerConfig(t *testing.T) {
|
|||||||
URL: "http://127.0.0.1:5000/auth",
|
URL: "http://127.0.0.1:5000/auth",
|
||||||
Insecure: true,
|
Insecure: true,
|
||||||
},
|
},
|
||||||
|
Command: "/etc/some_command",
|
||||||
},
|
},
|
||||||
Resolver: serverConfigResolver{
|
Resolver: serverConfigResolver{
|
||||||
Type: "udp",
|
Type: "udp",
|
||||||
|
@ -49,6 +49,7 @@ auth:
|
|||||||
http:
|
http:
|
||||||
url: http://127.0.0.1:5000/auth
|
url: http://127.0.0.1:5000/auth
|
||||||
insecure: true
|
insecure: true
|
||||||
|
command: /etc/some_command
|
||||||
|
|
||||||
resolver:
|
resolver:
|
||||||
type: udp
|
type: udp
|
||||||
|
28
extras/auth/command.go
Normal file
28
extras/auth/command.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/apernet/hysteria/core/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ server.Authenticator = &CommandAuthenticator{}
|
||||||
|
|
||||||
|
type CommandAuthenticator struct {
|
||||||
|
Cmd string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *CommandAuthenticator) Authenticate(addr net.Addr, auth string, tx uint64) (ok bool, id string) {
|
||||||
|
cmd := exec.Command(a.Cmd, addr.String(), auth, strconv.Itoa(int(tx)))
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
// This includes failing to execute the command,
|
||||||
|
// or the command exiting with a non-zero exit code.
|
||||||
|
return false, ""
|
||||||
|
} else {
|
||||||
|
return true, strings.TrimSpace(string(out))
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user