config.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package json
  2. import (
  3. "github.com/v2ray/v2ray-core/config"
  4. "github.com/v2ray/v2ray-core/config/json"
  5. )
  6. const (
  7. AuthMethodNoAuth = "noauth"
  8. AuthMethodUserPass = "password"
  9. )
  10. type SocksAccount struct {
  11. Username string `json:"user"`
  12. Password string `json:"pass"`
  13. }
  14. type SocksConfig struct {
  15. AuthMethod string `json:"auth"`
  16. Accounts []SocksAccount `json:"accounts"`
  17. UDPEnabled bool `json:"udp"`
  18. accountMap map[string]string
  19. }
  20. func (config *SocksConfig) Initialize() {
  21. config.accountMap = make(map[string]string)
  22. for _, account := range config.Accounts {
  23. config.accountMap[account.Username] = account.Password
  24. }
  25. }
  26. func (config *SocksConfig) IsNoAuth() bool {
  27. return config.AuthMethod == AuthMethodNoAuth
  28. }
  29. func (config *SocksConfig) IsPassword() bool {
  30. return config.AuthMethod == AuthMethodUserPass
  31. }
  32. func (config *SocksConfig) HasAccount(user, pass string) bool {
  33. if actualPass, found := config.accountMap[user]; found {
  34. return actualPass == pass
  35. }
  36. return false
  37. }
  38. func init() {
  39. json.RegisterConfigType("socks", config.TypeInbound, func() interface{} {
  40. return new(SocksConfig)
  41. })
  42. }