1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00
This commit is contained in:
Darien Raymond 2018-10-18 09:25:58 +02:00
parent be56feead3
commit 4fcb39ded9
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
8 changed files with 31 additions and 16 deletions

View File

@ -9,16 +9,21 @@ option java_multiple_files = true;
import "v2ray.com/core/common/net/address.proto"; import "v2ray.com/core/common/net/address.proto";
import "v2ray.com/core/common/protocol/server_spec.proto"; import "v2ray.com/core/common/protocol/server_spec.proto";
// Account represents a Socks account.
message Account { message Account {
string username = 1; string username = 1;
string password = 2; string password = 2;
} }
// AuthType is the authentication type of Socks proxy.
enum AuthType { enum AuthType {
// NO_AUTH is for anounymous authentication.
NO_AUTH = 0; NO_AUTH = 0;
// PASSWORD is for username/password authentication.
PASSWORD = 1; PASSWORD = 1;
} }
// ServerConfig is the protobuf config for Socks server.
message ServerConfig { message ServerConfig {
AuthType auth_type = 1; AuthType auth_type = 1;
map<string, string> accounts = 2; map<string, string> accounts = 2;
@ -28,6 +33,8 @@ message ServerConfig {
uint32 user_level = 6; uint32 user_level = 6;
} }
// ClientConfig is the protobuf config for Socks client.
message ClientConfig { message ClientConfig {
// Sever is a list of Socks server addresses.
repeated v2ray.core.common.protocol.ServerEndpoint server = 1; repeated v2ray.core.common.protocol.ServerEndpoint server = 1;
} }

View File

@ -25,21 +25,22 @@ import (
// Server is a SOCKS 5 proxy server // Server is a SOCKS 5 proxy server
type Server struct { type Server struct {
config *ServerConfig config *ServerConfig
v *core.Instance policyManager policy.Manager
} }
// NewServer creates a new Server object. // NewServer creates a new Server object.
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
v := core.MustFromContext(ctx)
s := &Server{ s := &Server{
config: config, config: config,
v: core.MustFromContext(ctx), policyManager: v.PolicyManager(),
} }
return s, nil return s, nil
} }
func (s *Server) policy() policy.Session { func (s *Server) policy() policy.Session {
config := s.config config := s.config
p := s.v.PolicyManager().ForLevel(config.UserLevel) p := s.policyManager.ForLevel(config.UserLevel)
if config.Timeout > 0 { if config.Timeout > 0 {
features.PrintDeprecatedFeatureWarning("Socks timeout") features.PrintDeprecatedFeatureWarning("Socks timeout")
} }

View File

@ -6,21 +6,27 @@ import (
"v2ray.com/core/common/uuid" "v2ray.com/core/common/uuid"
) )
type InternalAccount struct { // MemoryAccount is an in-memory from of VMess account.
type MemoryAccount struct {
// ID is the main ID of the account.
ID *protocol.ID ID *protocol.ID
// AlterIDs are the alternative IDs of the account.
AlterIDs []*protocol.ID AlterIDs []*protocol.ID
// Security type of the account. Used for client connections.
Security protocol.SecurityType 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 { if len(a.AlterIDs) == 0 {
return a.ID return a.ID
} }
return a.AlterIDs[dice.Roll(len(a.AlterIDs))] return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
} }
func (a *InternalAccount) Equals(account protocol.Account) bool { // Equals implements protocol.Account.
vmessAccount, ok := account.(*InternalAccount) func (a *MemoryAccount) Equals(account protocol.Account) bool {
vmessAccount, ok := account.(*MemoryAccount)
if !ok { if !ok {
return false return false
} }
@ -28,13 +34,14 @@ func (a *InternalAccount) Equals(account protocol.Account) bool {
return a.ID.Equals(vmessAccount.ID) return a.ID.Equals(vmessAccount.ID)
} }
// AsAccount implements protocol.Account.
func (a *Account) AsAccount() (protocol.Account, error) { func (a *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(a.Id) id, err := uuid.ParseString(a.Id)
if err != nil { if err != nil {
return nil, newError("failed to parse ID").Base(err).AtError() return nil, newError("failed to parse ID").Base(err).AtError()
} }
protoID := protocol.NewID(id) protoID := protocol.NewID(id)
return &InternalAccount{ return &MemoryAccount{
ID: protoID, ID: protoID,
AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)), AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
Security: a.SecuritySettings.GetSecurityType(), Security: a.SecuritySettings.GetSecurityType(),

View File

@ -56,7 +56,7 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession {
func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error { func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error {
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)() timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
account := header.User.Account.(*vmess.InternalAccount) account := header.User.Account.(*vmess.MemoryAccount)
idHash := c.idHash(account.AnyValidID().Bytes()) idHash := c.idHash(account.AnyValidID().Bytes())
common.Must2(idHash.Write(timestamp.Bytes(nil))) common.Must2(idHash.Write(timestamp.Bytes(nil)))
common.Must2(writer.Write(idHash.Sum(nil))) common.Must2(writer.Write(idHash.Sum(nil)))

View File

@ -135,7 +135,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
} }
iv := md5.Sum(hashTimestamp(timestamp)) iv := md5.Sum(hashTimestamp(timestamp))
vmessAccount := user.Account.(*vmess.InternalAccount) vmessAccount := user.Account.(*vmess.MemoryAccount)
aesStream := crypto.NewAesDecryptionStream(vmessAccount.ID.CmdKey(), iv[:]) aesStream := crypto.NewAesDecryptionStream(vmessAccount.ID.CmdKey(), iv[:])
decryptor := crypto.NewCryptionReader(aesStream, reader) decryptor := crypto.NewCryptionReader(aesStream, reader)

View File

@ -333,7 +333,7 @@ func (h *Handler) generateCommand(ctx context.Context, request *protocol.Request
if user == nil { if user == nil {
return nil return nil
} }
account := user.Account.(*vmess.InternalAccount) account := user.Account.(*vmess.MemoryAccount)
return &protocol.CommandSwitchAccount{ return &protocol.CommandSwitchAccount{
Port: port, Port: port,
ID: account.ID.UUID(), ID: account.ID.UUID(),

View File

@ -94,7 +94,7 @@ func (v *Handler) Process(ctx context.Context, link *vio.Link, dialer proxy.Dial
Option: protocol.RequestOptionChunkStream, Option: protocol.RequestOptionChunkStream,
} }
account := request.User.Account.(*vmess.InternalAccount) account := request.User.Account.(*vmess.MemoryAccount)
request.Security = account.Security request.Security = account.Security
if request.Security == protocol.SecurityType_AES128_GCM || request.Security == protocol.SecurityType_NONE || request.Security == protocol.SecurityType_CHACHA20_POLY1305 { if request.Security == protocol.SecurityType_AES128_GCM || request.Security == protocol.SecurityType_NONE || request.Security == protocol.SecurityType_CHACHA20_POLY1305 {

View File

@ -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) genHashForID(account.ID)
for _, id := range account.AlterIDs { for _, id := range account.AlterIDs {