diff --git a/proxy/socks/config.proto b/proxy/socks/config.proto index 0d69862b4..94fee102f 100644 --- a/proxy/socks/config.proto +++ b/proxy/socks/config.proto @@ -9,16 +9,21 @@ option java_multiple_files = true; import "v2ray.com/core/common/net/address.proto"; import "v2ray.com/core/common/protocol/server_spec.proto"; +// Account represents a Socks account. message Account { string username = 1; string password = 2; } +// AuthType is the authentication type of Socks proxy. enum AuthType { + // NO_AUTH is for anounymous authentication. NO_AUTH = 0; + // PASSWORD is for username/password authentication. PASSWORD = 1; } +// ServerConfig is the protobuf config for Socks server. message ServerConfig { AuthType auth_type = 1; map accounts = 2; @@ -28,6 +33,8 @@ message ServerConfig { uint32 user_level = 6; } +// ClientConfig is the protobuf config for Socks client. message ClientConfig { + // Sever is a list of Socks server addresses. repeated v2ray.core.common.protocol.ServerEndpoint server = 1; } diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 3dd30d113..9c15d8d60 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -24,22 +24,23 @@ import ( // Server is a SOCKS 5 proxy server type Server struct { - config *ServerConfig - v *core.Instance + config *ServerConfig + policyManager policy.Manager } // NewServer creates a new Server object. func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { + v := core.MustFromContext(ctx) s := &Server{ - config: config, - v: core.MustFromContext(ctx), + config: config, + policyManager: v.PolicyManager(), } return s, nil } func (s *Server) policy() policy.Session { config := s.config - p := s.v.PolicyManager().ForLevel(config.UserLevel) + p := s.policyManager.ForLevel(config.UserLevel) if config.Timeout > 0 { features.PrintDeprecatedFeatureWarning("Socks timeout") } diff --git a/proxy/vmess/account.go b/proxy/vmess/account.go index 1efc7edaf..a57b06da6 100644 --- a/proxy/vmess/account.go +++ b/proxy/vmess/account.go @@ -6,21 +6,27 @@ import ( "v2ray.com/core/common/uuid" ) -type InternalAccount struct { - ID *protocol.ID +// MemoryAccount is an in-memory from of VMess account. +type MemoryAccount struct { + // ID is the main ID of the account. + ID *protocol.ID + // AlterIDs are the alternative IDs of the account. AlterIDs []*protocol.ID + // Security type of the account. Used for client connections. Security protocol.SecurityType } -func (a *InternalAccount) AnyValidID() *protocol.ID { +// AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any. +func (a *MemoryAccount) AnyValidID() *protocol.ID { if len(a.AlterIDs) == 0 { return a.ID } return a.AlterIDs[dice.Roll(len(a.AlterIDs))] } -func (a *InternalAccount) Equals(account protocol.Account) bool { - vmessAccount, ok := account.(*InternalAccount) +// Equals implements protocol.Account. +func (a *MemoryAccount) Equals(account protocol.Account) bool { + vmessAccount, ok := account.(*MemoryAccount) if !ok { return false } @@ -28,13 +34,14 @@ func (a *InternalAccount) Equals(account protocol.Account) bool { return a.ID.Equals(vmessAccount.ID) } +// AsAccount implements protocol.Account. func (a *Account) AsAccount() (protocol.Account, error) { id, err := uuid.ParseString(a.Id) if err != nil { return nil, newError("failed to parse ID").Base(err).AtError() } protoID := protocol.NewID(id) - return &InternalAccount{ + return &MemoryAccount{ ID: protoID, AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)), Security: a.SecuritySettings.GetSecurityType(), diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index f9a58a2ce..ce318f582 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -56,7 +56,7 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession { func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error { timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)() - account := header.User.Account.(*vmess.InternalAccount) + account := header.User.Account.(*vmess.MemoryAccount) idHash := c.idHash(account.AnyValidID().Bytes()) common.Must2(idHash.Write(timestamp.Bytes(nil))) common.Must2(writer.Write(idHash.Sum(nil))) diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index 66665adc6..5231f4cbf 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -135,7 +135,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request } iv := md5.Sum(hashTimestamp(timestamp)) - vmessAccount := user.Account.(*vmess.InternalAccount) + vmessAccount := user.Account.(*vmess.MemoryAccount) aesStream := crypto.NewAesDecryptionStream(vmessAccount.ID.CmdKey(), iv[:]) decryptor := crypto.NewCryptionReader(aesStream, reader) diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index f7aa925a4..637ac303d 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -333,7 +333,7 @@ func (h *Handler) generateCommand(ctx context.Context, request *protocol.Request if user == nil { return nil } - account := user.Account.(*vmess.InternalAccount) + account := user.Account.(*vmess.MemoryAccount) return &protocol.CommandSwitchAccount{ Port: port, ID: account.ID.UUID(), diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 2dc072b6a..896ee0d38 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -94,7 +94,7 @@ func (v *Handler) Process(ctx context.Context, link *vio.Link, dialer proxy.Dial Option: protocol.RequestOptionChunkStream, } - account := request.User.Account.(*vmess.InternalAccount) + account := request.User.Account.(*vmess.MemoryAccount) request.Security = account.Security if request.Security == protocol.SecurityType_AES128_GCM || request.Security == protocol.SecurityType_NONE || request.Security == protocol.SecurityType_CHACHA20_POLY1305 { diff --git a/proxy/vmess/vmess.go b/proxy/vmess/vmess.go index 2c40b120a..0d49fac67 100644 --- a/proxy/vmess/vmess.go +++ b/proxy/vmess/vmess.go @@ -79,7 +79,7 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user * } } - account := user.user.Account.(*InternalAccount) + account := user.user.Account.(*MemoryAccount) genHashForID(account.ID) for _, id := range account.AlterIDs {