config.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package json
  2. import (
  3. "net"
  4. "github.com/v2ray/v2ray-core/proxy/common/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. HostIP string `json:"ip"`
  19. accountMap map[string]string
  20. ip net.IP
  21. }
  22. func (sc *SocksConfig) Initialize() {
  23. sc.accountMap = make(map[string]string)
  24. for _, account := range sc.Accounts {
  25. sc.accountMap[account.Username] = account.Password
  26. }
  27. if len(sc.HostIP) > 0 {
  28. sc.ip = net.ParseIP(sc.HostIP)
  29. if sc.ip == nil {
  30. sc.ip = net.IPv4(127, 0, 0, 1)
  31. }
  32. }
  33. }
  34. func (sc *SocksConfig) IsNoAuth() bool {
  35. return sc.AuthMethod == AuthMethodNoAuth
  36. }
  37. func (sc *SocksConfig) IsPassword() bool {
  38. return sc.AuthMethod == AuthMethodUserPass
  39. }
  40. func (sc *SocksConfig) HasAccount(user, pass string) bool {
  41. if actualPass, found := sc.accountMap[user]; found {
  42. return actualPass == pass
  43. }
  44. return false
  45. }
  46. func (sc *SocksConfig) IP() net.IP {
  47. return sc.ip
  48. }
  49. func init() {
  50. json.RegisterInboundConnectionConfig("socks", func() interface{} {
  51. return new(SocksConfig)
  52. })
  53. }