Skip to content

Commit

Permalink
feat: command auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyxdd committed Aug 12, 2023
1 parent d3db1e4 commit 25b8eef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type serverConfigAuth struct {
Password string `mapstructure:"password"`
UserPass map[string]string `mapstructure:"userpass"`
HTTP serverConfigAuthHTTP `mapstructure:"http"`
Command string `mapstructure:"command"`
}

type serverConfigResolverTCP struct {
Expand Down Expand Up @@ -405,6 +406,12 @@ func (c *serverConfig) fillAuthenticator(hyConfig *server.Config) error {
}
hyConfig.Authenticator = auth.NewHTTPAuthenticator(c.Auth.HTTP.URL, c.Auth.HTTP.Insecure)
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:
return configError{Field: "auth.type", Err: errors.New("unsupported auth type")}
}
Expand Down
1 change: 1 addition & 0 deletions app/cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestServerConfig(t *testing.T) {
URL: "http://127.0.0.1:5000/auth",
Insecure: true,
},
Command: "/etc/some_command",
},
Resolver: serverConfigResolver{
Type: "udp",
Expand Down
1 change: 1 addition & 0 deletions app/cmd/server_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ auth:
http:
url: http://127.0.0.1:5000/auth
insecure: true
command: /etc/some_command

resolver:
type: udp
Expand Down
28 changes: 28 additions & 0 deletions extras/auth/command.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 25b8eef

Please sign in to comment.