socks5_check_access_handle.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package socks5
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/cnlh/nps/core"
  6. "net"
  7. )
  8. type CheckAccess struct {
  9. core.NpsPlugin
  10. clientConn net.Conn
  11. clientUsername string
  12. clientPassword string
  13. configUsername string
  14. configPassword string
  15. }
  16. func (check *CheckAccess) GetConfigName() *core.NpsConfigs {
  17. c := core.NewNpsConfigs("socks5_simple_access_check", "need check the permission simply")
  18. c.Add("socks5_simple_access_username", "simple auth username")
  19. c.Add("socks5_simple_access_password", "simple auth password")
  20. return c
  21. }
  22. func (check *CheckAccess) Run(ctx context.Context, config map[string]string) (context.Context, error) {
  23. check.clientConn = check.GetClientConn(ctx)
  24. check.configUsername = config["socks5_access_username"]
  25. check.configPassword = config["socks5_access_password"]
  26. return ctx, nil
  27. }
  28. func (check *CheckAccess) checkAuth(configUserName, configPassword string) error {
  29. if check.clientUsername == configUserName && check.clientPassword == configPassword {
  30. _, err := check.clientConn.Write([]byte{userAuthVersion, authSuccess})
  31. return err
  32. } else {
  33. _, err := check.clientConn.Write([]byte{userAuthVersion, authFailure})
  34. if err != nil {
  35. return err
  36. }
  37. return errors.New("auth check error,username or password does not match")
  38. }
  39. }