config.go 1.3 KB

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